SanJinService.php 4.7 KB

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