WxpayController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * WxpayController.php UTF-8
  4. * 微信支付处理
  5. *
  6. * @date : 2018/2/27 22:18
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace api\apple\controller;
  13. use api\common\controller\AppleApiBaseController;
  14. use huo\controller\pay\Notify;
  15. use huo\controller\pay\SdkPayCache;
  16. use huolib\constant\OrderConst;
  17. use huolib\pay\Pay;
  18. use think\Config;
  19. class WxpayController extends AppleApiBaseController {
  20. function _initialize() {
  21. parent::_initialize();
  22. \think\Log::write($this->request->getContent(), 'error');
  23. Config::set('default_return_type', 'html');
  24. }
  25. /**
  26. * 微信支付回调地址
  27. * wxpay/notify
  28. */
  29. public function notifyUrl() {
  30. $_xml = file_get_contents('php://input');
  31. $_REQUEST = $this->xmlToArray($_xml);
  32. $_class = new Notify();
  33. $_product_id = $_REQUEST['attach'];
  34. $_class->notifyUrl('wxpay', $_product_id);
  35. }
  36. /**
  37. * 微信支付通知地址
  38. * wxpay/return
  39. *
  40. * @return mixed
  41. * @throws \think\Exception
  42. */
  43. public function returnUrl() {
  44. $_pay_class = Pay::ins()->get('wxpay');
  45. $_pay_class->setPayway('Wxpay');
  46. $_order_id = $this->request->param('order_id/s', '');
  47. $_apple_id = $this->request->param('apple_id/s', '');
  48. $_return_token = '';
  49. if (empty($_order_id)) {
  50. $_msg = "参数错误!";
  51. $this->assign('return_url', $_return_token);
  52. $this->assign('msg', $_msg);
  53. $this->assign('failed', 1);
  54. return $this->fetch('order/success');
  55. }
  56. $_pay_rs = $_pay_class->orderQuery($_order_id, '');
  57. $_failed = 1;
  58. $_st = 1;
  59. $_bt = 'CHECK_SAVEFAILED';
  60. if (false == $_pay_rs) {
  61. $_msg = "支付失败!";
  62. } else {
  63. switch ($_pay_rs) {
  64. case OrderConst::WXPAY_STATUS_SUCCESS:
  65. $_msg = '支付成功';
  66. $_failed = 0;
  67. $_bt = 'CHECK_SAVESUCCESS';
  68. $_st = 2;
  69. break;
  70. case OrderConst::WXPAY_STATUS_REFUND:
  71. $_msg = '转入退款';
  72. break;
  73. case OrderConst::WXPAY_STATUS_NOTPAY:
  74. $_msg = '未支付';
  75. break;
  76. case OrderConst::WXPAY_STATUS_CLOSED:
  77. $_msg = '已关闭';
  78. break;
  79. case OrderConst::WXPAY_STATUS_REVOKED:
  80. $_msg = '已撤销';
  81. break;
  82. case OrderConst::WXPAY_STATUS_USERPAYING:
  83. $_msg = '支付中';
  84. break;
  85. default:
  86. $_msg = '支付失败';
  87. }
  88. }
  89. if (!empty($_apple_id)) {
  90. //$_return_token = 'htsdk.com633403350://qo?oi='.$_order_id.'&bt=1&st='.$_st;
  91. $_return_token = 'h'.DOCDOMAIN.$_apple_id.'://qo?oi='.$_order_id.'&bt='.$_bt.'&st='.$_st;
  92. }
  93. $this->assign('msg', $_msg);
  94. $this->assign('return_url', $_return_token);
  95. if ($_failed) {
  96. return $this->fetch('order/failed');
  97. }
  98. return $this->fetch('order/success');
  99. }
  100. /**
  101. * 微信支付显示地址
  102. * wxpay/show
  103. *
  104. * @return mixed
  105. */
  106. public function showUrl() {
  107. $_order_id = $this->request->param('order_id/s', '');
  108. $_apple_id = $this->request->param('apple_id/s', '');
  109. $_return_token = '';
  110. if (empty($_apple_id)) {
  111. $_return_token = 'h'.DOCDOMAIN.$_apple_id.'://qo?oi='.$_order_id.'&bt=CHECK_SAVEFAILED&st=1';
  112. }
  113. $this->assign('return_url', $_return_token);
  114. if (empty($_order_id)) {
  115. $_msg = '未支付';
  116. $this->assign('msg', $_msg);
  117. return $this->fetch('order/failed');
  118. }
  119. $_msg = '玩家取消支付,请重新下单';
  120. $this->assign('msg', $_msg);
  121. return $this->fetch('order/failed');
  122. }
  123. /**
  124. * 将xml转为array
  125. */
  126. public function xmlToArray($xml) {
  127. $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  128. return $array_data;
  129. }
  130. /**
  131. * IOS支付切换 支付url
  132. */
  133. public function submit() {
  134. $_order_id = $this->request->param('order_id/s', '');
  135. $_apple_id = $this->request->param('apple_id/s', '');
  136. $_pay_token = $this->request->param('pay_token/s', '');
  137. $_pay_data = SdkPayCache::ins()->getInfoByOrderId($_order_id);
  138. $_return_token = '';
  139. if (empty($_apple_id)) {
  140. $_return_token = 'h'.DOCDOMAIN.$_apple_id.'://qo?oi='.$_order_id.'&bt=CHECK_SAVEFAILED&st=1';
  141. }
  142. if (empty($_order_id) || empty($_pay_token)) {
  143. $_msg = "亲,您支付失败了,请点击关闭按钮重试!";
  144. $this->assign('return_url', $_return_token);
  145. $this->assign('msg', $_msg);
  146. return $this->fetch('order/submit');
  147. }
  148. if ($_pay_token != $_pay_data['pay_token']) {
  149. $this->assign('return_url', $_return_token);
  150. $_msg = "亲,您支付失败了,请点击关闭按钮重试!";
  151. $this->assign('msg', $_msg);
  152. return $this->fetch('order/submit');
  153. }
  154. $this->assign('token', $_pay_data['token']);
  155. $this->assign('return_url', $_return_token);
  156. $this->assign(
  157. 'query_url',
  158. SDKSITE.url('Pay/Wxpay/checkurl', array('order_id' => $_order_id))
  159. );
  160. return $this->fetch('order/wxsubmit');
  161. }
  162. }