SanJinService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. const REQUEST_URL = 'https://jkapi-sanjin.jkcbb.com/';
  10. const PRODUCT_TEST = 'T888'; // 测试支付通道
  11. public static $PRODUCT = [
  12. 'T888' => [
  13. 'type' => 'test',
  14. 'rate' => 0.02,
  15. 'max' => 5000,
  16. 'min' => 10
  17. ],
  18. 'ZPB001' => [
  19. 'type' => 'zfb',
  20. 'rate' => 0.085,
  21. 'max' => 200,
  22. 'min' => 100
  23. ],
  24. 'ZPB002' => [
  25. 'type' => 'zfb',
  26. 'rate' => 0.057,
  27. 'max' => 200,
  28. 'min' => 1000
  29. ],
  30. 'ZPB003' => [
  31. 'type' => 'zfb',
  32. 'rate' => 0.052,
  33. 'max' => 1000,
  34. 'min' => 3000
  35. ],
  36. 'ZPB004' => [
  37. 'type' => 'zfb',
  38. 'rate' => 0.042,
  39. 'max' => 3000,
  40. 'min' => 20000
  41. ],
  42. ];
  43. // 获取商户ID
  44. public static function getMerchantId()
  45. {
  46. return config('app.tree_pay_mch_id');
  47. }
  48. // 获取商户秘钥
  49. public static function getSecret()
  50. {
  51. return config('app.tree_pay_key');
  52. }
  53. // 获取异步的通知地址
  54. public static function getNotifyUrl()
  55. {
  56. return 'https://botpc28.testx2.cc/api/pay/harvest';
  57. }
  58. /**
  59. * @description: 获取请求客户端
  60. * @return {*}
  61. */
  62. public static function getClient(): Client
  63. {
  64. return new Client([
  65. 'base_uri' => self::REQUEST_URL,
  66. 'timeout' => 5.0,
  67. ]);
  68. }
  69. // 签名
  70. public static function signature($params = [],$must = [])
  71. {
  72. if($must){
  73. foreach($params as $k => $v){
  74. if(!in_array($k,$must)){
  75. unset($params[$k]);
  76. }
  77. }
  78. }
  79. ksort($params, SORT_STRING);
  80. $parts = [];
  81. foreach($params as $k => $v){
  82. array_push($parts,$k.'='.$v);
  83. }
  84. $mch_key = self::getSecret();
  85. $parts[] = "key=".$mch_key;
  86. $sign = md5(implode('&',$parts));
  87. return $sign;
  88. }
  89. /**
  90. * @description: 发起支付订单
  91. * @param {*} $amount 金额单位分
  92. * @param {*} $orderNo 订单号
  93. * @param {*} $type 支付通道
  94. * @return {*}
  95. */
  96. public static function pay($amount,$orderNo,$type = self::PRODUCT_TEST)
  97. {
  98. $must = ['mchId','productId','outTradeNo','amount','reqTime','notifyUrl'];
  99. $mch_id = self::getMerchantId();
  100. $data = [];
  101. $data['mchId'] = $mch_id;
  102. $data['amount'] = $amount;
  103. $data['outTradeNo'] = $orderNo;
  104. $data['notifyUrl'] = self::getNotifyUrl();
  105. $data['reqTime'] = time() * 1000;
  106. $data['productId'] = $type;
  107. $data['sign'] = self::signature($data,$must);
  108. $client = self::getClient();
  109. $response = $client->post('api/v1/pay/unifiedOrder', [
  110. 'json' => $data,
  111. 'headers' => [
  112. 'Content-Type' => 'application/json',
  113. ]
  114. ]);
  115. $body = $response->getBody();
  116. return json_decode($body->getContents(), true);
  117. }
  118. }