UnionPay.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace common\components\payment;
  3. use Yii;
  4. use Omnipay\Omnipay;
  5. /**
  6. * 银联支付类
  7. *
  8. * Class UnionPay
  9. * @package common\components\payment
  10. */
  11. class UnionPay
  12. {
  13. protected $config;
  14. const DEFAULT = 'UnionPay_Express';
  15. /**
  16. * UnionPay constructor.
  17. */
  18. public function __construct($config)
  19. {
  20. $this->config = $config;
  21. }
  22. /**
  23. * 实例化类
  24. *
  25. * @param string $type
  26. * @return \Omnipay\UnionPay\ExpressGateway
  27. */
  28. private function create($type = self::DEFAULT)
  29. {
  30. /* @var $gateway \Omnipay\UnionPay\ExpressGateway */
  31. $gateway = Omnipay::create($type);
  32. $gateway->setMerId($this->config['mch_id']);
  33. $gateway->setCertId($this->config['cert_id']);
  34. $gateway->setPublicKey(Yii::getAlias($this->config['public_key'])); // path or content
  35. $gateway->setPrivateKey(Yii::getAlias($this->config['private_key'])); // path or content
  36. $gateway->setReturnUrl($this->config['return_url']);
  37. $gateway->setNotifyUrl($this->config['notify_url']);
  38. return $gateway;
  39. }
  40. /**
  41. * 回调
  42. *
  43. * @return mixed
  44. */
  45. public function notify()
  46. {
  47. $gateway = $this->create();
  48. return $gateway->completePurchase([
  49. 'request_params' => $_REQUEST
  50. ])->send();
  51. }
  52. /**
  53. * APP
  54. *
  55. * @param $order
  56. * @param bool $debug
  57. * @return mixed
  58. */
  59. public function app($order, $debug = false)
  60. {
  61. $gateway = $this->create();
  62. /* @var $response \Omnipay\UnionPay\Message\CreateOrderResponse */
  63. $response = $gateway->createOrder($order)->send();
  64. return $debug ? $response->getData() : $response->getTradeNo();
  65. }
  66. /**
  67. * PC/Wap
  68. *
  69. * @param $order
  70. * @param bool $debug
  71. * @return mixed
  72. */
  73. public function html($order, $debug = false)
  74. {
  75. $gateway = $this->create();
  76. /* @var $response \Omnipay\UnionPay\Message\LegacyQuickPayPurchaseResponse */
  77. $response = $gateway->purchase($order)->send();
  78. return $debug ? $response->getData() : $response->getRedirectHtml();
  79. }
  80. /**
  81. * 查询订单
  82. *
  83. * @param int $orderId 订单id
  84. * @param int $txnTime 订单交易时间
  85. * @param int $txnAmt 订单总费用
  86. * @return mixed
  87. */
  88. public function query($orderId, $txnTime, $txnAmt)
  89. {
  90. $gateway = $this->create();
  91. $response = $gateway->query([
  92. 'orderId' => $orderId, //Your site trade no, not union tn.
  93. 'txnTime' => $txnTime, //Order trade time
  94. 'txnAmt' => $txnAmt, //Order total fee
  95. ])->send();
  96. return $response->getData();
  97. }
  98. /**
  99. * 查询订单
  100. *
  101. * @param int $orderId 订单id
  102. * @param int $txnTime 订单交易时间
  103. * @param int $txnAmt 订单总费用
  104. * @return mixed
  105. */
  106. public function close($orderId, $txnTime, $txnAmt, $queryId)
  107. {
  108. $gateway = $this->create();
  109. $response = $gateway->query([
  110. 'orderId' => $orderId, //Your site trade no, not union tn.
  111. 'txnTime' => $txnTime, //Order trade time
  112. 'txnAmt' => $txnAmt, //Order total fee
  113. 'queryId' => $queryId, //Order total fee
  114. ])->send();
  115. return $response->getData();
  116. }
  117. /**
  118. * 退款
  119. *
  120. * @param int $orderId 订单id
  121. * @param int $txnTime 订单交易时间
  122. * @param int $txnAmt 订单总费用
  123. * @return mixed
  124. */
  125. public function refund($orderId, $txnTime, $txnAmt, $queryId)
  126. {
  127. $gateway = $this->create();
  128. $response = $gateway->refund([
  129. 'orderId' => $orderId, //Your site trade no, not union tn.
  130. 'txnTime' => $txnTime, //Order trade time
  131. 'txnAmt' => $txnAmt, //Order total fee
  132. 'queryId' => $queryId, //Order total fee
  133. ])->send();
  134. return $response->getData();
  135. }
  136. }