WxpayController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 mini\sdk\controller;
  13. use huo\controller\pay\Notify;
  14. use huo\controller\pay\SdkPayCache;
  15. use huolib\constant\OrderConst;
  16. use huolib\pay\Pay;
  17. use mini\common\controller\V2ApiBaseController;
  18. class WxpayController extends V2ApiBaseController {
  19. function _initialize() {
  20. parent::_initialize();
  21. }
  22. /**
  23. * 微信支付回调地址
  24. * wxpay/notify
  25. */
  26. public function notifyUrl() {
  27. $_xml = file_get_contents('php://input');
  28. $_data = $this->xmlToArray($_xml);
  29. $_class = new Notify();
  30. $_product_id = get_val($_data, 'attach', '');
  31. $_app_id = get_val($_data, 'appid', '');
  32. $_class->notifyUrl('wxpay', $_product_id, $_app_id);
  33. }
  34. /**
  35. * 微信支付通知地址
  36. * wxpay/return
  37. *
  38. * @return mixed
  39. * @throws \think\Exception
  40. */
  41. public function returnUrl() {
  42. $_pay_class = Pay::ins()->get('wxpay');
  43. $_pay_class->setPayway('Wxpay');
  44. $_order_id = $this->request->param('order_id/s', '');
  45. $_apple_id = $this->request->param('apple_id/s', '');
  46. $_return_token = '';
  47. if (empty($_order_id)) {
  48. $_msg = "参数错误!";
  49. $this->assign('return_url', $_return_token);
  50. $this->assign('msg', $_msg);
  51. $this->assign('failed', 1);
  52. return $this->fetch('order/success');
  53. }
  54. $_pay_rs = $_pay_class->orderQuery($_order_id, '');
  55. $_failed = 1;
  56. $_st = 1;
  57. $_bt = 'CHECK_SAVEFAILED';
  58. if (false == $_pay_rs) {
  59. $_msg = "支付失败!";
  60. } else {
  61. switch ($_pay_rs) {
  62. case OrderConst::WXPAY_STATUS_SUCCESS:
  63. $_msg = '支付成功';
  64. $_failed = 0;
  65. $_bt = 'CHECK_SAVESUCCESS';
  66. $_st = 2;
  67. break;
  68. case OrderConst::WXPAY_STATUS_REFUND:
  69. $_msg = '转入退款';
  70. break;
  71. case OrderConst::WXPAY_STATUS_NOTPAY:
  72. $_msg = '未支付';
  73. break;
  74. case OrderConst::WXPAY_STATUS_CLOSED:
  75. $_msg = '已关闭';
  76. break;
  77. case OrderConst::WXPAY_STATUS_REVOKED:
  78. $_msg = '已撤销';
  79. break;
  80. case OrderConst::WXPAY_STATUS_USERPAYING:
  81. $_msg = '支付中';
  82. break;
  83. default:
  84. $_msg = '支付失败';
  85. }
  86. }
  87. if (!empty($_apple_id)) {
  88. //$_return_token = 'htsdk.com633403350://qo?oi='.$_order_id.'&bt=1&st='.$_st;
  89. $_return_token = 'h'.DOCDOMAIN.$_apple_id.'://qo?oi='.$_order_id.'&bt='.$_bt.'&st='.$_st;
  90. }
  91. $this->assign('msg', $_msg);
  92. $this->assign('return_url', $_return_token);
  93. if ($_failed) {
  94. return $this->fetch('order/failed');
  95. }
  96. return $this->fetch('order/success');
  97. }
  98. /**
  99. * 微信支付显示地址
  100. * wxpay/show
  101. *
  102. * @return mixed
  103. */
  104. public function showUrl() {
  105. $_order_id = $this->request->param('order_id/s', '');
  106. $_apple_id = $this->request->param('apple_id/s', '');
  107. $_return_token = '';
  108. if (empty($_apple_id)) {
  109. $_return_token = 'h'.DOCDOMAIN.$_apple_id.'://qo?oi='.$_order_id.'&bt=CHECK_SAVEFAILED&st=1';
  110. }
  111. $this->assign('return_url', $_return_token);
  112. if (empty($_order_id)) {
  113. $_msg = '未支付';
  114. $this->assign('msg', $_msg);
  115. return $this->fetch('order/failed');
  116. }
  117. $_msg = '玩家取消支付,请重新下单';
  118. $this->assign('msg', $_msg);
  119. return $this->fetch('order/failed');
  120. }
  121. /**
  122. * 将xml转为array
  123. */
  124. public function xmlToArray($xml) {
  125. $array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  126. return $array_data;
  127. }
  128. /**
  129. * IOS支付切换 支付url
  130. */
  131. public function submit() {
  132. $_order_id = $this->request->param('order_id/s', '');
  133. $_apple_id = $this->request->param('apple_id/s', '');
  134. $_pay_token = $this->request->param('pay_token/s', '');
  135. $_pay_data = SdkPayCache::ins()->getInfoByOrderId($_order_id);
  136. $_return_token = '';
  137. if (empty($_apple_id)) {
  138. $_return_token = 'h'.DOCDOMAIN.$_apple_id.'://qo?oi='.$_order_id.'&bt=CHECK_SAVEFAILED&st=1';
  139. }
  140. if (empty($_order_id) || empty($_pay_token)) {
  141. $_msg = "亲,您支付失败了,请点击关闭按钮重试!";
  142. $this->assign('return_url', $_return_token);
  143. $this->assign('msg', $_msg);
  144. return $this->fetch('order/submit');
  145. }
  146. if ($_pay_token != $_pay_data['pay_token']) {
  147. $this->assign('return_url', $_return_token);
  148. $_msg = "亲,您支付失败了,请点击关闭按钮重试!";
  149. $this->assign('msg', $_msg);
  150. return $this->fetch('order/submit');
  151. }
  152. $this->assign('token', $_pay_data['token']);
  153. $this->assign('return_url', $_return_token);
  154. $this->assign(
  155. 'query_url',
  156. SDKSITE.url('Pay/Wxpay/checkurl', array('order_id' => $_order_id))
  157. );
  158. return $this->fetch('order/wxsubmit');
  159. }
  160. /**
  161. * 自动支付
  162. */
  163. public function payUrl() {
  164. $_order_id = $this->request->param('order_id/s', '');
  165. if (empty($_order_id)) {
  166. $this->error('订单号错误');
  167. }
  168. $_pay_data = SdkPayCache::ins()->getInfoByOrderId($_order_id);
  169. if (empty($_pay_data) || empty($_pay_data['token'])) {
  170. $this->error('支付参数错误');
  171. }
  172. echo "<script> window.location.href='".$_pay_data['token']."'</script>";
  173. }
  174. }