SanJinService.php 7.7 KB

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