UnionPay.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace App\Libs\Pay\Merchants;
  3. use Exception;
  4. use GuzzleHttp\Client;
  5. /**
  6. * 银联支付
  7. */
  8. class UnionPay implements PayMerchantInterface
  9. {
  10. private $make_order_url = 'https://qr.chinaums.com/netpay-portal/webpay/pay.do';
  11. private $query_order_url = 'https://qr.chinaums.com/netpay-route-server/api/';
  12. private $msgType = 'WXPay.jsPay';
  13. private $signType = 'MD5';
  14. private $mid;
  15. private $tid;
  16. private $instMid;
  17. private $msgSrc;
  18. private $msgSrcId;
  19. private $key;
  20. public function __construct(array $config)
  21. {
  22. $this->mid = $config['mid'];
  23. $this->tid = $config['tid'];
  24. $this->instMid = $config['instMid'];
  25. $this->msgSrc = $config['msgSrc'];
  26. $this->msgSrcId = $config['msgSrcId'];
  27. $this->key = $config['key'];
  28. }
  29. public function send(array $data)
  30. {
  31. $result = [
  32. 'mid' => $this->mid,
  33. 'tid' => $this->tid,
  34. 'instMid' => $this->instMid,
  35. 'msgSrc' => $this->msgSrc,
  36. 'msgSrcId' => $this->msgSrcId,
  37. 'msgType' => $this->msgType,
  38. 'signType' => $this->signType,
  39. 'requestTimestamp' => date('Y-m-d H:i:s'),
  40. 'notifyUrl' => env('UNIONPAY_NOFITY_URL'),
  41. 'returnUrl' => $data['pay_wait_url'],
  42. 'totalAmount' => $data['price'],
  43. 'merOrderId' => $data['trade_no'],
  44. ];
  45. $result['sign'] = $this->makeSign($result);
  46. $query_params = http_build_query($result);
  47. return $this->make_order_url . '?' . $query_params;
  48. }
  49. public function query(string $trade_no)
  50. {
  51. $query_params = [
  52. 'mid' => $this->mid,
  53. 'tid' => $this->tid,
  54. 'instMid' => $this->instMid,
  55. 'msgSrc' => $this->msgSrc,
  56. 'msgSrcId' => $this->msgSrcId,
  57. 'signType' => $this->signType,
  58. 'msgType' => 'query',
  59. 'requestTimestamp' => date('Y-m-d H:i:s'),
  60. 'merOrderId' => $trade_no,
  61. ];
  62. $result = $this->request($query_params);
  63. if (
  64. isset($result->errCode) && $result->errCode === 'SUCCESS' &&
  65. isset($result->status) && $result->status === 'TRADE_SUCCESS'
  66. ) {
  67. return true;
  68. } else {
  69. \Log::info('union_pay.query: ');
  70. \Log::info(object_to_array($result));
  71. }
  72. return false;
  73. }
  74. public function notify(array $data)
  75. {
  76. if (isset($data['status']) && $data['status'] === 'TRADE_SUCCESS') {
  77. $sign = $this->makeSign($data);
  78. if ($sign === $data['sign']) {
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. public function refund(array $data)
  85. {
  86. $refund_params = [
  87. 'mid' => $this->mid,
  88. 'tid' => $this->tid,
  89. 'instMid' => $this->instMid,
  90. 'msgSrc' => $this->msgSrc,
  91. 'msgSrcId' => $this->msgSrcId,
  92. 'signType' => $this->signType,
  93. 'msgType' => 'refund',
  94. 'requestTimestamp' => date('Y-m-d H:i:s'),
  95. 'merOrderId' => $data['trade_no'],
  96. 'refundAmount' => $data['price'],
  97. ];
  98. $result = $this->request($refund_params);
  99. if (
  100. isset($result->errCode) && $result->errCode === 'SUCCESS' &&
  101. isset($result->refundStatus) && $result->refundStatus === 'SUCCESS' &&
  102. isset($result->status) && $result->status === 'TRADE_SUCCESS'
  103. ) {
  104. return true;
  105. } else {
  106. \Log::info('union_pay.refund: ');
  107. \Log::info(object_to_array($result));
  108. }
  109. return false;
  110. }
  111. /**
  112. * 生成订单号
  113. */
  114. public function generateTradeNo()
  115. {
  116. return substr($this->msgSrcId . date("YmdHis") . hexdec(uniqid()), 0, 30);
  117. }
  118. public function makeSign(array $data)
  119. {
  120. //签名步骤一:按字典序排序参数
  121. ksort($data);
  122. $buff = "";
  123. foreach ($data as $k => $v) {
  124. if ($k != "sign" && $v != "" && !is_array($v)) {
  125. $buff .= $k . "=" . $v . "&";
  126. }
  127. }
  128. $buff = trim($buff, "&");
  129. //签名步骤二:在string后加入KEY
  130. $string = $buff . $this->key;
  131. //签名步骤三:MD5加密
  132. $string = md5($string);
  133. //签名步骤四:所有字符转为大写
  134. $result = strtoupper($string);
  135. return $result;
  136. }
  137. private function request(array $data)
  138. {
  139. try {
  140. $data['sign'] = $this->makeSign($data);
  141. $client = new Client(['timeout' => 3]);
  142. $result = $client->request('post', $this->query_order_url, ['json' => $data])->getBody()->getContents();
  143. if ($result) {
  144. $result = json_decode($result);
  145. }
  146. return $result;
  147. } catch (Exception $e) {
  148. \Log::error('union_pay.request: ' . $e->getMessage());
  149. }
  150. }
  151. }