SanJinService.php 6.7 KB

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