PayService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Tool\MayouTool\Pay;
  3. use Tool\MayouTool\Bean\Pay\GetCodePayUrlParamBean;
  4. use Tool\MayouTool\Exception\PayException;
  5. use Tool\MayouTool\HttpCurl;
  6. /**
  7. * 支付服务类
  8. * Class PayService
  9. * @package Tool\MayouTool\Pay
  10. */
  11. class PayService
  12. {
  13. /**
  14. * 获取支付二维码接口
  15. * @param GetCodePayUrlParamBean $bean
  16. * @return array ["qrcode_url"=>"支付二维码","qr"=>"支付二维码","order_number"=>"订单编号"]
  17. * @throws PayException
  18. */
  19. public static function getCodePayUrlService(GetCodePayUrlParamBean $bean)
  20. {
  21. $url = env("OFFICIAL_PAY_URL");
  22. if(!$url){
  23. throw new PayException("官方支付地址不能为空");
  24. }
  25. if(!$bean->getOrderPrice()){
  26. throw new PayException("支付金额不能为空");
  27. }
  28. if(!$bean->getRelationOrderNumber()){
  29. throw new PayException("支付关联订单号不能为空");
  30. }
  31. if(!$bean->getPayType()){
  32. throw new PayException("支付类型不能为空");
  33. }
  34. if(!$bean->getAppKey()){
  35. throw new PayException("支付appkey不能为空");
  36. }
  37. if(!$bean->getAppSecret()){
  38. throw new PayException("支付appsecret不能为空");
  39. }
  40. $url = $url."v1/pay/codePayUrl";
  41. $params = [
  42. "orderPrice"=>$bean->getOrderPrice(),
  43. "relationOrderNumber"=>$bean->getRelationOrderNumber(),
  44. "payType"=>$bean->getPayType()
  45. ];
  46. $header = [
  47. "appKey"=>$bean->getAppKey(),
  48. "appSecret"=>$bean->getAppSecret()
  49. ];
  50. $res = HttpCurl::postCurl($url,$params,$header);
  51. if(!$res["status"]){
  52. throw new PayException("请换成其他方式充值");
  53. }
  54. return $res["data"];
  55. }
  56. /**
  57. * 根据官方订单编号获取订单
  58. * @param $token string token
  59. * @param $trade_sn string 官方订单编号
  60. * @param $amount int 金额(单位为分)
  61. * @return ["order_sn"=>"淘象订单编号","price"=>"支付金额(单位为分)"]
  62. */
  63. public static function getOfficialOrderByOrderNumberService($token, $trade_sn, $amount)
  64. {
  65. $url = env("TAOXIANG_PAY_URL");
  66. if(!$url){
  67. throw new PayException("淘象支付地址不能为空");
  68. }
  69. if(!$token){
  70. throw new PayException("token不能为空");
  71. }
  72. if(!$trade_sn){
  73. throw new PayException("官方支付单号不能为空");
  74. }
  75. if(!$amount){
  76. throw new PayException("订单金额不能为空");
  77. }
  78. $params = [
  79. "token"=>$token,
  80. "trade_sn"=>$trade_sn,
  81. "amount"=>$amount
  82. ];
  83. $url = $url."merchant/alipay_apply_quick/forget";
  84. $res = HttpCurl::postCurl($url,$params);
  85. if($res["status"]!=0){
  86. throw new PayException("订单获取失败");
  87. }
  88. return $res["data"];
  89. }
  90. /**
  91. * 获取支付方式
  92. * @param $appKey string appkey
  93. * @param $appSecret string secret
  94. */
  95. public static function getPayMethodService($appKey,$appSecret)
  96. {
  97. $url = env("OFFICIAL_PAY_URL");
  98. if(!$url){
  99. throw new PayException("官方支付地址不能为空");
  100. }
  101. if(!$appSecret){
  102. throw new PayException("用户不能为空");
  103. }
  104. if(!$appKey){
  105. throw new PayException("用户不能为空");
  106. }
  107. $url = $url."v1/pay/method";
  108. $header = [
  109. "appKey"=>$appKey,
  110. "appSecret"=>$appSecret
  111. ];
  112. $res = HttpCurl::getCurl($url,[],$header);
  113. if(!$res["status"]){
  114. throw new PayException("支付方式失败");
  115. }
  116. return $res["data"];
  117. }
  118. }