UnionPay.php 4.7 KB

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