SanJinService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. public static function pay($amount, $order_no, $payType = self::ALIPAY_TO_ALIPAY)
  55. {
  56. $data = [];
  57. $merchant_id = config('app.tree_payment_merchant_id');
  58. $secret = config('app.tree_payment_secret');
  59. $notify_url = self::getNotifyUrl();
  60. $data['merchantNum'] = $merchant_id;
  61. $data['amount'] = $amount;
  62. $data['orderNo'] = $order_no;
  63. $data['notifyUrl'] = $notify_url;
  64. $data['payType'] = self::ALIPAY_TO_ALIPAY;
  65. // $data['returnUrl'] = $return_url;
  66. $signStr = $merchant_id . $order_no . $amount . $secret;
  67. $sign = md5($signStr);
  68. $data['sign'] = $sign;
  69. $client = self::getClient();
  70. $response = $client->post('api/pay', [
  71. 'form_params' => $data,
  72. 'headers' => [
  73. 'Content-Type' => 'application/x-www-form-urlencoded',
  74. ]
  75. ]);
  76. $body = $response->getBody();
  77. $result = json_decode($body->getContents(), true);
  78. return $result;
  79. }
  80. }