UnionPay.php 4.8 KB

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