SanJinService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 $CHANNEL = [
  12. // 'test' => '测试',
  13. 'zfbsm' => '支付宝扫码',
  14. 'zfbge' => '支付宝固额',
  15. 'zfbzk' => '支付宝转卡',
  16. 'ylsm' => '银联扫码',
  17. 'szrmb' => '数字人民币',
  18. ];
  19. public static $PRODUCT = [
  20. // 'T888' => [
  21. // 'type' => 'test',
  22. // 'rate' => 0.02,
  23. // 'max' => 5000,
  24. // 'min' => 10
  25. // ],
  26. 'YL001' => [
  27. 'type' => 'ylsm',
  28. 'rate' => 0.05,
  29. 'max' => 500,
  30. 'min' => 50
  31. ],
  32. 'SZ002' => [
  33. 'type' => 'szrmb',
  34. 'rate' => 0.05,
  35. 'max' => 100,
  36. 'min' => 10
  37. ],
  38. 'SZ001' => [
  39. 'type' => 'szrmb',
  40. 'rate' => 0.048,
  41. 'max' => 5000,
  42. 'min' => 100
  43. ],
  44. 'ZFB001' => [
  45. 'type' => 'zfbsm',
  46. 'rate' => 0.085,
  47. 'max' => 200,
  48. 'min' => 100
  49. ],
  50. 'ZFB002' => [
  51. 'type' => 'zfbsm',
  52. 'rate' => 0.057,
  53. 'max' => 1000,
  54. 'min' => 200
  55. ],
  56. 'ZFB003' => [
  57. 'type' => 'zfbsm',
  58. 'rate' => 0.052,
  59. 'max' => 3000,
  60. 'min' => 1000
  61. ],
  62. 'ZFB004' => [
  63. 'type' => 'zfbsm',
  64. 'rate' => 0.042,
  65. 'max' => 20000,
  66. 'min' => 3000
  67. ],
  68. 'ZFB005' => [
  69. 'type' => 'zfbge',
  70. 'rate' => 0.027,
  71. 'fixed' => [945 ,988 ,990]
  72. ],
  73. 'ZFBZK001' => [
  74. 'type' => 'zfbzk',
  75. 'rate' => 0.05,
  76. 'max' => 2000,
  77. 'min' => 200
  78. ]
  79. ];
  80. // 获取商户ID
  81. public static function getMerchantId()
  82. {
  83. return config('app.tree_pay_mch_id');
  84. }
  85. // 获取商户秘钥
  86. public static function getSecret()
  87. {
  88. return config('app.tree_pay_key');
  89. }
  90. // 获取异步的通知地址
  91. public static function getNotifyUrl()
  92. {
  93. $host = config('app.url');
  94. return $host.'/api/pay/harvest';
  95. }
  96. /**
  97. * @description: 获取请求客户端
  98. * @return {*}
  99. */
  100. public static function getClient(): Client
  101. {
  102. return new Client([
  103. 'base_uri' => self::REQUEST_URL,
  104. 'timeout' => 5.0,
  105. ]);
  106. }
  107. // 签名
  108. public static function signature($params = [],$must = [])
  109. {
  110. if($must){
  111. foreach($params as $k => $v){
  112. if(!in_array($k,$must)){
  113. unset($params[$k]);
  114. }
  115. }
  116. }
  117. ksort($params, SORT_STRING);
  118. $parts = [];
  119. foreach($params as $k => $v){
  120. array_push($parts,$k.'='.$v);
  121. }
  122. $mch_key = self::getSecret();
  123. $parts[] = "key=".$mch_key;
  124. $sign = md5(implode('&',$parts));
  125. return $sign;
  126. }
  127. /**
  128. * @description: 发起支付订单
  129. * @param {*} $amount 金额单位分
  130. * @param {*} $orderNo 订单号
  131. * @param {*} $type 支付通道
  132. * @return {*}
  133. */
  134. public static function pay($amount,$orderNo,$type = self::PRODUCT_TEST)
  135. {
  136. $must = ['mchId','productId','outTradeNo','amount','reqTime','notifyUrl'];
  137. $mch_id = self::getMerchantId();
  138. $data = [];
  139. $data['mchId'] = $mch_id;
  140. $data['amount'] = $amount;
  141. $data['outTradeNo'] = $orderNo;
  142. $data['notifyUrl'] = self::getNotifyUrl();
  143. $data['reqTime'] = time() * 1000;
  144. $data['productId'] = $type;
  145. $data['sign'] = self::signature($data,$must);
  146. $client = self::getClient();
  147. $response = $client->post('api/v1/pay/unifiedOrder', [
  148. 'json' => $data,
  149. 'headers' => [
  150. 'Content-Type' => 'application/json',
  151. ]
  152. ]);
  153. $body = $response->getBody();
  154. return json_decode($body->getContents(), true);
  155. }
  156. }