SanJinService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. // 获取异步的通知地址
  14. public static function getNotifyUrl()
  15. {
  16. return 'https://botpc28.testx2.cc/api/pay/notify';
  17. }
  18. /**
  19. * @description: 获取请求客户端
  20. * @return {*}
  21. */
  22. public static function getClient(): Client
  23. {
  24. return new Client([
  25. 'base_uri' => self::REQUEST_URL,
  26. 'timeout' => 5.0,
  27. ]);
  28. }
  29. /**
  30. * @description: 查询商户余额
  31. * @return {array}
  32. * {array.success - 是否成功 true|false}
  33. * {array.msg - 返回错误消息}
  34. * {array.code - 返回状态码:200成功,其他失败}
  35. * {array.timestamp - 时间戳 13位}
  36. * {array.data - 商户余额}
  37. */
  38. public static function findBalance()
  39. {
  40. $merchant_id = config('app.tree_payment_merchant_id');
  41. $secret = config('app.tree_payment_secret');
  42. $sign = md5($merchant_id . $secret);
  43. $data = [
  44. 'merchantNum' => $merchant_id,
  45. 'sign' => $sign,
  46. ];
  47. $client = self::getClient();
  48. $response = $client->get('api/findBalance', [
  49. 'query' => $data,
  50. ]);
  51. $body = $response->getBody();
  52. return json_decode($body->getContents(), true);
  53. }
  54. // 代收
  55. public static function pay($amount, $order_no, $payType = self::ALIPAY_TO_ALIPAY)
  56. {
  57. $data = [];
  58. $merchant_id = config('app.tree_payment_merchant_id');
  59. $secret = config('app.tree_payment_secret');
  60. $notify_url = self::getNotifyUrl();
  61. $data['merchantNum'] = $merchant_id;
  62. $data['amount'] = $amount;
  63. $data['orderNo'] = $order_no;
  64. $data['notifyUrl'] = $notify_url;
  65. $data['payType'] = $payType;
  66. // $data['returnUrl'] = $return_url;
  67. $signStr = $merchant_id . $order_no . $amount . $secret;
  68. $sign = md5($signStr);
  69. $data['sign'] = $sign;
  70. $client = self::getClient();
  71. $response = $client->post('api/pay', [
  72. 'form_params' => $data,
  73. 'headers' => [
  74. 'Content-Type' => 'application/x-www-form-urlencoded',
  75. ]
  76. ]);
  77. $body = $response->getBody();
  78. $result = json_decode($body->getContents(), true);
  79. return $result;
  80. }
  81. // 代付
  82. public static function payout($amount, $order_no, $bank_name, $account, $card_no, $payType = self::ALIPAY_TO_ALIPAY)
  83. {
  84. $data = [];
  85. $merchant_id = config('app.tree_payment_merchant_id');
  86. $secret = config('app.tree_payment_secret');
  87. $notify_url = self::getNotifyUrl();
  88. $data['merchantNum'] = $merchant_id;
  89. $data['amount'] = $amount;
  90. $data['orderNo'] = $order_no;
  91. $data['notifyUrl'] = $notify_url;
  92. $data['payType'] = $payType;
  93. $signStr = $merchant_id . $order_no . $amount . $secret;
  94. $sign = md5($signStr);
  95. $data['sign'] = $sign;
  96. $data['bankName'] = $bank_name;
  97. $data['account'] = $account;
  98. $data['cardNumber'] = $card_no;
  99. $client = self::getClient();
  100. $response = $client->post('api/payout', [
  101. 'form_params' => $data,
  102. 'headers' => [
  103. 'Content-Type' => 'application/x-www-form-urlencoded',
  104. ]
  105. ]);
  106. $body = $response->getBody();
  107. $result = json_decode($body->getContents(), true);
  108. return $result;
  109. }
  110. }