JoinPay.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Libs\Pay\Merchants;
  3. use Exception;
  4. use GuzzleHttp\Client;
  5. /**
  6. * 汇聚支付
  7. */
  8. class JoinPay implements PayMerchantInterface
  9. {
  10. private $make_order_url = 'https://www.joinpay.com/trade/uniPayApi.action';
  11. private $query_order_url = 'https://www.joinpay.com/trade/queryOrder.action';
  12. private $key = "0c855107fa394d62bd4b9803795f16ae";
  13. private $merchantNo = "888108800000947";
  14. private $version = "1.0";
  15. private $trade_type = "WEIXIN_GZH";
  16. private $return_url;
  17. private $notify_url;
  18. private $appId;
  19. public function __construct(array $config)
  20. {
  21. $this->appId = $config['appId'];
  22. $this->return_url = $config['return_url'];
  23. $this->notify_url = $config['notify_url'];
  24. }
  25. public function send(array $data)
  26. {
  27. $options = [
  28. 'p0_Version' => $this->version,
  29. 'p1_MerchantNo' => $this->merchantNo,
  30. 'p2_OrderNo' => $data['trade_no'],
  31. 'p3_Amount' => $data['price'],
  32. 'p4_Cur' => '1',
  33. 'p5_ProductName' => $data['body'],
  34. 'p7_Mp' => $data['remark'],
  35. 'p8_ReturnUrl' => $this->return_url,
  36. 'p9_NotifyUrl' => $this->notify_url,
  37. 'q1_FrpCode' => $this->trade_type,
  38. ];
  39. if ($this->trade_type == "WEIXIN_GZH" || $this->trade_type == "WEIXIN_XCX") {
  40. $options['q5_OpenId'] = $data['openId'];
  41. }
  42. $result = $this->request($this->make_order_url, $options);
  43. if ($result->ra_Code == 100) {
  44. return $result->rc_Result;
  45. } else {
  46. return false;
  47. }
  48. }
  49. public function query(string $trade_no)
  50. {
  51. $options = [
  52. 'p1_MerchantNo' => $this->merchantNo,
  53. 'p2_OrderNo' => $trade_no,
  54. ];
  55. $result = $this->request($this->query_order_url, $options);
  56. if ($result->ra_Code == 100 && $result->ra_Status == 100) {
  57. return true;
  58. } else {
  59. return false;
  60. }
  61. }
  62. public function notify(array $data)
  63. {
  64. if (isset($data['r6_Status']) && $data['r6_Status'] == 100) {
  65. if (isset($data['hmac']) && $data['hmac'] == $this->makeSign($data)) {
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. public function refund(array $data)
  72. { }
  73. public function makeSign(array $data)
  74. {
  75. //签名步骤一:按字典序排序参数
  76. ksort($data);
  77. $buff = "";
  78. foreach ($data as $k => $v) {
  79. if ($k != "hmac" && $v != "" && !is_array($v)) {
  80. $buff .= $k . "=" . $v . "&";
  81. }
  82. }
  83. $buff = trim($buff, "&");
  84. //签名步骤二:在string后加入KEY
  85. $string = $buff . $this->key;
  86. //签名步骤三:MD5加密
  87. $string = md5($string);
  88. //签名步骤四:所有字符转为大写
  89. $result = strtoupper($string);
  90. return $result;
  91. }
  92. private function request(string $url, array $data)
  93. {
  94. try {
  95. $data['hmac'] = $this->makeSign($data);
  96. $client = new Client(['timeout' => 3]);
  97. $result = $client->request('post', $url, ['json' => $data])->getBody()->getContents();
  98. if ($result) {
  99. $result = json_decode($result);
  100. }
  101. return $result;
  102. } catch (Exception $e) { }
  103. }
  104. }