SanJinService.php 3.4 KB

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