SanJinService.php 5.1 KB

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