SanJinService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 $PRODUCT = [
  12. 'T888' => [
  13. 'type' => 'test',
  14. 'rate' => 0.02,
  15. 'max' => 5000,
  16. 'min' => 10
  17. ],
  18. 'ZPB001' => [
  19. 'type' => 'zfb',
  20. 'rate' => 0.085,
  21. 'max' => 200,
  22. 'min' => 100
  23. ],
  24. 'ZPB002' => [
  25. 'type' => 'zfb',
  26. 'rate' => 0.057,
  27. 'max' => 200,
  28. 'min' => 1000
  29. ],
  30. 'ZPB003' => [
  31. 'type' => 'zfb',
  32. 'rate' => 0.052,
  33. 'max' => 1000,
  34. 'min' => 3000
  35. ],
  36. 'ZPB004' => [
  37. 'type' => 'zfb',
  38. 'rate' => 0.042,
  39. 'max' => 3000,
  40. 'min' => 20000
  41. ],
  42. ];
  43. // 获取商户ID
  44. public static function getMerchantId()
  45. {
  46. return config('app.tree_pay_mch_id');
  47. }
  48. // 获取商户秘钥
  49. public static function getSecret()
  50. {
  51. return config('app.tree_pay_key');
  52. }
  53. // 获取异步的通知地址
  54. public static function getNotifyUrl()
  55. {
  56. $host = config('app.url');
  57. return $host.'/api/pay/harvest';
  58. }
  59. /**
  60. * @description: 获取请求客户端
  61. * @return {*}
  62. */
  63. public static function getClient(): Client
  64. {
  65. return new Client([
  66. 'base_uri' => self::REQUEST_URL,
  67. 'timeout' => 5.0,
  68. ]);
  69. }
  70. // 签名
  71. public static function signature($params = [],$must = [])
  72. {
  73. if($must){
  74. foreach($params as $k => $v){
  75. if(!in_array($k,$must)){
  76. unset($params[$k]);
  77. }
  78. }
  79. }
  80. ksort($params, SORT_STRING);
  81. $parts = [];
  82. foreach($params as $k => $v){
  83. array_push($parts,$k.'='.$v);
  84. }
  85. $mch_key = self::getSecret();
  86. $parts[] = "key=".$mch_key;
  87. $sign = md5(implode('&',$parts));
  88. return $sign;
  89. }
  90. /**
  91. * @description: 发起支付订单
  92. * @param {*} $amount 金额单位分
  93. * @param {*} $orderNo 订单号
  94. * @param {*} $type 支付通道
  95. * @return {*}
  96. */
  97. public static function pay($amount,$orderNo,$type = self::PRODUCT_TEST)
  98. {
  99. $must = ['mchId','productId','outTradeNo','amount','reqTime','notifyUrl'];
  100. $mch_id = self::getMerchantId();
  101. $data = [];
  102. $data['mchId'] = $mch_id;
  103. $data['amount'] = $amount;
  104. $data['outTradeNo'] = $orderNo;
  105. $data['notifyUrl'] = self::getNotifyUrl();
  106. $data['reqTime'] = time() * 1000;
  107. $data['productId'] = $type;
  108. $data['sign'] = self::signature($data,$must);
  109. $client = self::getClient();
  110. $response = $client->post('api/v1/pay/unifiedOrder', [
  111. 'json' => $data,
  112. 'headers' => [
  113. 'Content-Type' => 'application/json',
  114. ]
  115. ]);
  116. $body = $response->getBody();
  117. return json_decode($body->getContents(), true);
  118. }
  119. }