WxpayController.php 6.6 KB

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