UnionPay.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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' => $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. \Log::info('union_pay.notify: ');
  91. \Log::info($data);
  92. if ($data['status'] === 'TRADE_SUCCESS') {
  93. $trade_no = $data['merOrderId'];
  94. if ($trade_no) {
  95. $order = OrderService::getByTradeNo($trade_no);
  96. $order->transaction_id = $data['targetOrderId'];
  97. if ($order) {
  98. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')
  99. ->where('id', $order->pay_merchant_id)->where('is_enabled', 1)->first();
  100. $instance = new UnionPay(json_decode($pay_merchant->config_info, true));
  101. $sign = $instance->makeSign($data);
  102. if ($sign === $data['sign']) {
  103. if (call_user_func_array($callback, [$order])) {
  104. return 'SUCCESS';
  105. }
  106. }
  107. }
  108. }
  109. }
  110. return 'FAILED';
  111. }
  112. /**
  113. * 生成订单号
  114. */
  115. public function generateTradeNo()
  116. {
  117. return substr($this->msgSrcId . date("YmdHis") . hexdec(uniqid()), 0, 30);
  118. }
  119. public function makeSign(array $data)
  120. {
  121. //签名步骤一:按字典序排序参数
  122. ksort($data);
  123. $buff = "";
  124. foreach ($data as $k => $v) {
  125. if ($k != "sign" && $v != "" && !is_array($v)) {
  126. $buff .= $k . "=" . $v . "&";
  127. }
  128. }
  129. $buff = trim($buff, "&");
  130. //签名步骤二:在string后加入KEY
  131. $string = $buff . $this->key;
  132. //签名步骤三:MD5加密
  133. $string = md5($string);
  134. //签名步骤四:所有字符转为大写
  135. $result = strtoupper($string);
  136. return $result;
  137. }
  138. }