SanJinService.php 3.4 KB

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