UnionPay.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. myLog("union_pay_query")->info(object_to_array($result));
  70. }
  71. return false;
  72. }
  73. public function notify(array $data)
  74. {
  75. if (isset($data['status']) && $data['status'] === 'TRADE_SUCCESS') {
  76. $sign = $this->makeSign($data);
  77. if ($sign === $data['sign']) {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. public function refund(array $data)
  84. {
  85. $refund_params = [
  86. 'mid' => $this->mid,
  87. 'tid' => $this->tid,
  88. 'instMid' => $this->instMid,
  89. 'msgSrc' => $this->msgSrc,
  90. 'msgSrcId' => $this->msgSrcId,
  91. 'signType' => $this->signType,
  92. 'msgType' => 'refund',
  93. 'requestTimestamp' => date('Y-m-d H:i:s'),
  94. 'merOrderId' => $data['trade_no'],
  95. 'refundAmount' => $data['price'],
  96. ];
  97. $result = $this->request($refund_params);
  98. if (
  99. isset($result->errCode) && $result->errCode === 'SUCCESS' &&
  100. isset($result->refundStatus) && $result->refundStatus === 'SUCCESS' &&
  101. isset($result->status) && $result->status === 'TRADE_SUCCESS'
  102. ) {
  103. return true;
  104. } else {
  105. myLog("union_pay_refund")->info(object_to_array($result));
  106. }
  107. return false;
  108. }
  109. /**
  110. * 生成订单号
  111. */
  112. public function generateTradeNo()
  113. {
  114. return substr($this->msgSrcId . date("YmdHis") . hexdec(uniqid()), 0, 30);
  115. }
  116. public function makeSign(array $data)
  117. {
  118. //签名步骤一:按字典序排序参数
  119. ksort($data);
  120. $buff = "";
  121. foreach ($data as $k => $v) {
  122. if ($k != "sign" && $v != "" && !is_array($v)) {
  123. $buff .= $k . "=" . $v . "&";
  124. }
  125. }
  126. $buff = trim($buff, "&");
  127. //签名步骤二:在string后加入KEY
  128. $string = $buff . $this->key;
  129. //签名步骤三:MD5加密
  130. $string = md5($string);
  131. //签名步骤四:所有字符转为大写
  132. $result = strtoupper($string);
  133. return $result;
  134. }
  135. private function request(array $data)
  136. {
  137. try {
  138. $data['sign'] = $this->makeSign($data);
  139. $client = new Client(['timeout' => 3]);
  140. $result = $client->request('post', $this->query_order_url, ['json' => $data])->getBody()->getContents();
  141. if ($result) {
  142. $result = json_decode($result);
  143. }
  144. return $result;
  145. } catch (Exception $e) {
  146. myLog("union_pay_request")->info(object_to_array($result));
  147. }
  148. }
  149. }