SanJinService.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Services\Payment;
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\Exception\RequestException;
  5. use GuzzleHttp\Psr7\Response;
  6. use App\Services\BaseService;
  7. class SanJinService extends BaseService
  8. {
  9. // SanJin payment service methods would go here
  10. const REQUEST_URL = 'https://api.qbdf13.com/';
  11. const ALIPAY_TO_CARD = 'DF001'; // 支付宝转卡
  12. const ALIPAY_TO_ALIPAY = 'DF002'; // 支付宝转支付宝
  13. const PROVISION_TO_CARD = 'DF003'; // 备付金转卡
  14. const PROVISION_TO_ALIPAY = 'DF004'; // 备付金转支付宝
  15. const NUMBER_RMB = 'DF005'; // 数字人民币
  16. // 获取异步的通知地址
  17. public static function getNotifyUrl()
  18. {
  19. return 'https://botpc28.testx2.cc/api/pay/notify';
  20. }
  21. /**
  22. * @description: 获取请求客户端
  23. * @return {*}
  24. */
  25. public static function getClient(): Client
  26. {
  27. return new Client([
  28. 'base_uri' => self::REQUEST_URL,
  29. 'timeout' => 5.0,
  30. ]);
  31. }
  32. /**
  33. * @description: 查询商户余额
  34. * @return {array}
  35. * {array.success - 是否成功 true|false}
  36. * {array.msg - 返回错误消息}
  37. * {array.code - 返回状态码:200成功,其他失败}
  38. * {array.timestamp - 时间戳 13位}
  39. * {array.data - 商户余额}
  40. */
  41. public static function findBalance()
  42. {
  43. $merchant_id = config('app.tree_payment_merchant_id');
  44. $secret = config('app.tree_payment_secret');
  45. $sign = md5($merchant_id . $secret);
  46. $data = [
  47. 'merchantNum' => $merchant_id,
  48. 'sign' => $sign,
  49. ];
  50. $client = self::getClient();
  51. $response = $client->get('api/findBalance', [
  52. 'query' => $data,
  53. ]);
  54. $body = $response->getBody();
  55. return json_decode($body->getContents(), true);
  56. }
  57. /**
  58. * @description: 查询代付订单信息
  59. * @param {*} $order_no 代付订单
  60. * @return {array} $result
  61. */
  62. public static function findPayoutOrderInfo($order_no)
  63. {
  64. $merchant_id = config('app.tree_payment_merchant_id');
  65. $secret = config('app.tree_payment_secret');
  66. $data = [];
  67. $data['merchantNum'] = $merchant_id;
  68. $data['orderNo'] = $order_no;
  69. $sign = md5($merchant_id.$order_no.$secret);
  70. $data['sign'] = $sign;
  71. $client = self::getClient();
  72. $response = $client->get('api/findPayoutOrderInfo', [
  73. 'query' => $data,
  74. ]);
  75. $body = $response->getBody();
  76. return json_decode($body->getContents(), true);
  77. }
  78. /**
  79. * @description: 查询代收订单信息
  80. * @param {*} $order_no 代收订单
  81. * @return {array} $result
  82. */
  83. public static function findPayOrderInfo($order_no)
  84. {
  85. $merchant_id = config('app.tree_payment_merchant_id');
  86. $secret = config('app.tree_payment_secret');
  87. $data = [];
  88. $data['merchantNum'] = $merchant_id;
  89. $data['orderNo'] = $order_no;
  90. $sign = md5($merchant_id.$order_no.$secret);
  91. $data['sign'] = $sign;
  92. $client = self::getClient();
  93. $response = $client->get('api/findPayOrderInfo', [
  94. 'query' => $data,
  95. ]);
  96. $body = $response->getBody();
  97. return json_decode($body->getContents(), true);
  98. }
  99. // 代收
  100. public static function pay($amount, $order_no, $payType = self::ALIPAY_TO_ALIPAY)
  101. {
  102. $data = [];
  103. $merchant_id = config('app.tree_payment_merchant_id');
  104. $secret = config('app.tree_payment_secret');
  105. $notify_url = self::getNotifyUrl();
  106. $data['merchantNum'] = $merchant_id;
  107. $data['amount'] = $amount;
  108. $data['orderNo'] = $order_no;
  109. $data['notifyUrl'] = $notify_url;
  110. $data['payType'] = $payType;
  111. // $data['returnUrl'] = $return_url;
  112. $signStr = $merchant_id . $order_no . $amount . $secret;
  113. $sign = md5($signStr);
  114. $data['sign'] = $sign;
  115. $client = self::getClient();
  116. $response = $client->post('api/pay', [
  117. 'form_params' => $data,
  118. 'headers' => [
  119. 'Content-Type' => 'application/x-www-form-urlencoded',
  120. ]
  121. ]);
  122. $body = $response->getBody();
  123. $result = json_decode($body->getContents(), true);
  124. return $result;
  125. }
  126. // 代付
  127. public static function payout($amount, $order_no, $bank_name, $account, $card_no, $payType = self::ALIPAY_TO_ALIPAY)
  128. {
  129. $data = [];
  130. $merchant_id = config('app.tree_payment_merchant_id');
  131. $secret = config('app.tree_payment_secret');
  132. $notify_url = self::getNotifyUrl();
  133. $data['merchantNum'] = $merchant_id;
  134. $data['amount'] = $amount;
  135. $data['orderNo'] = $order_no;
  136. $data['notifyUrl'] = $notify_url;
  137. $data['payType'] = $payType;
  138. switch($payType){
  139. case self::ALIPAY_TO_ALIPAY:
  140. $bankName = '支付宝';
  141. break;
  142. case self::NUMBER_RMB:
  143. $bankName = '数字人民币';
  144. break;
  145. }
  146. $signStr = $merchant_id . $order_no . $amount . $secret;
  147. $sign = md5($signStr);
  148. $data['sign'] = $sign;
  149. $data['bankName'] = $bank_name;
  150. $data['account'] = $account;
  151. $data['cardNumber'] = $card_no;
  152. $client = self::getClient();
  153. $response = $client->post('api/payout', [
  154. 'form_params' => $data,
  155. 'headers' => [
  156. 'Content-Type' => 'application/x-www-form-urlencoded',
  157. ]
  158. ]);
  159. $body = $response->getBody();
  160. $result = json_decode($body->getContents(), true);
  161. return $result;
  162. }
  163. }