SanJinService.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. // 获取商户ID
  12. public static function getMerchantId()
  13. {
  14. return config('app.tree_pay_mch_id');
  15. }
  16. // 获取商户秘钥
  17. public static function getSecret()
  18. {
  19. return config('app.tree_pay_key');
  20. }
  21. // 获取异步的通知地址
  22. public static function getNotifyUrl()
  23. {
  24. return 'https://botpc28.testx2.cc/api/pay/harvest';
  25. }
  26. /**
  27. * @description: 获取请求客户端
  28. * @return {*}
  29. */
  30. public static function getClient(): Client
  31. {
  32. return new Client([
  33. 'base_uri' => self::REQUEST_URL,
  34. 'timeout' => 5.0,
  35. ]);
  36. }
  37. // 签名
  38. public static function signature($params = [],$must = [])
  39. {
  40. if($must){
  41. foreach($params as $k => $v){
  42. if(!in_array($k,$must)){
  43. unset($params[$k]);
  44. }
  45. }
  46. }
  47. ksort($params, SORT_STRING);
  48. var_dump($params);
  49. $parts = [];
  50. foreach($params as $k => $v){
  51. array_push($parts,$k.'='.$v);
  52. }
  53. $mch_key = self::getSecret();
  54. $parts[] = "key=".$mch_key;
  55. var_dump(implode('&',$parts));
  56. $sign = md5(implode('&',$parts));
  57. return $sign;
  58. }
  59. /**
  60. * @description: 发起支付订单
  61. * @param {*} $amount 金额单位分
  62. * @param {*} $orderNo 订单号
  63. * @param {*} $type 支付通道
  64. * @return {*}
  65. */
  66. public static function pay($amount,$orderNo,$type = self::PRODUCT_TEST)
  67. {
  68. $must = ['mchId','productId','outTradeNo','amount','reqTime','notifyUrl'];
  69. $mch_id = self::getMerchantId();
  70. $data = [];
  71. $data['mchId'] = $mch_id;
  72. $data['amount'] = $amount;
  73. $data['outTradeNo'] = $orderNo;
  74. $data['notifyUrl'] = self::getNotifyUrl();
  75. $data['reqTime'] = time() * 1000;
  76. $data['productId'] = $type;
  77. $data['sign'] = self::signature($data,$must);
  78. $client = self::getClient();
  79. $response = $client->post('api/v1/pay/unifiedOrder', [
  80. 'json' => $data,
  81. 'headers' => [
  82. 'Content-Type' => 'application/json',
  83. ]
  84. ]);
  85. $body = $response->getBody();
  86. return json_decode($body->getContents(), true);
  87. }
  88. }