SanJinService.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.jkccb.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. $parts = [];
  49. foreach($params as $k => $v){
  50. array_push($parts,$k.'='.$v);
  51. }
  52. $mch_key = self::getSecret();
  53. $parts[] = "key=".$mch_key;
  54. $sign = md5(implode('&',$parts));
  55. return $sign;
  56. }
  57. public static function pay($amount,$orderNo,$type = self::PRODUCT_TEST)
  58. {
  59. $must = ['mchId','productId','outTradeNo','amount','reqTime','notifyUrl'];
  60. $mch_id = self::getMerchantId();
  61. $data = [];
  62. $data['mchId'] = $mch_id;
  63. $data['amount'] = $amount;
  64. $data['outTradeNo'] = $orderNo;
  65. $data['notifyUrl'] = self::getNotifyUrl();
  66. $data['reqTime'] = microtime(true) * 1000;
  67. $data['productId'] = $type;
  68. $data['sign'] = self::signature($data,$must);
  69. $client = self::getClient();
  70. $response = $client->post('api/pay', [
  71. 'json' => $data,
  72. 'headers' => [
  73. 'Content-Type' => 'application/json',
  74. ]
  75. ]);
  76. $body = $response->getBody();
  77. return json_decode($body->getContents(), true);
  78. }
  79. }