Paymentdo.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use app\common\model\Wallet;
  5. /*
  6. *处理支付结果 接受服务器和服务器之间的通知
  7. */
  8. class Paymentdo
  9. {
  10. //接受服务器的通知
  11. public function receive($orderId,$payamount)
  12. {
  13. filePut('[payment][receive]异步回调开始');
  14. //$orderId = Pay::getOrderId();
  15. if (!$orderId)
  16. {
  17. filePut('[payment][receive]支付号未匹配');
  18. return false;
  19. }
  20. $where = array();
  21. $where['pay_no'] = $orderId;
  22. $info = Db::name('pay_order')->where($where)->find();
  23. if ($info)
  24. {
  25. if($info['status'])
  26. {
  27. filePut('[payment][receive]本支付号已处理过 '.$orderId);
  28. return true;
  29. }
  30. else
  31. {
  32. filePut('[payment][receive]开始支付 '.$orderId);
  33. }
  34. //获取到它使用的支付接口
  35. //$pay = new Pay($info['payment_class']);
  36. //if ($rs = $pay->receive())
  37. if (1 === 1)
  38. {
  39. $this->touchEvent('success', $orderId);
  40. filePut('[payment][receive]支付完成,结果 success, '.$orderId);
  41. return true;
  42. }
  43. else
  44. {
  45. $this->touchEvent('failed', $orderId);
  46. filePut('[payment][receive]支付完成,结果 failed, '.$orderId);
  47. }
  48. //filePut('[payment][receive]支付完成,结果 '.json_encode($rs).', '.$orderId);
  49. }
  50. else
  51. {
  52. filePut('[payment][receive]无效支付号 '.$orderId);
  53. return false;
  54. }
  55. return false;
  56. //1.服务器发回处理结果
  57. //2.这里交给核心类处理
  58. //3.核心类交给支付接口判断支付成功还是失败
  59. //4.根据支付接口的返回结果,核心类调用之前绑定好的事件.
  60. }
  61. /**
  62. * 执行绑定事件
  63. * @param string $event 事件名称
  64. * @param string $order_id 订单号
  65. */
  66. public function touchEvent($event, $orderId)
  67. {
  68. //检查该订单是否已绑定事件
  69. $pay_event = Db::name('pay_event');
  70. $pay_order = Db::name('pay_order');
  71. $where = [];
  72. $where['pay_no'] = $orderId;
  73. $where['event'] = $event;
  74. $info = $pay_event->where($where)->find();
  75. if(!$info||!isset($info['class']))
  76. {return;}
  77. $class = new $info['class'];
  78. db()->startTrans();
  79. $orderInfo = $pay_order->where(['pay_no' => $orderId])->lock(true)->find();
  80. filePut("[PAY][touchEvent] STRAT" . $orderId);
  81. //如果找到了绑定的事件
  82. if ($info) {
  83. $args = json_decode($info['args'], true); //强制转换为数组格式
  84. //$args['moneynumber'] = $orderInfo['money'];//兼容以前的设计,追加money
  85. if ($event === 'success') {
  86. if($orderInfo['status'] === 0)//成功事件只触发1次
  87. {
  88. $pay_order->where(['pay_no' => $orderId])->update(['status'=>2]);
  89. db()->commit();
  90. filePut('[PAY][touchEvent] success start'.$orderId);//.$info['args']
  91. call_user_func_array([$class, $info['method']], [$orderId, $args]);
  92. //得到事件模型类
  93. filePut('[PAY][touchEvent] success end '.$orderId);
  94. }
  95. }
  96. else
  97. {
  98. call_user_func_array([$class, $info['method']], [$orderId, $args]);
  99. filePut('[PAY][touchEvent] '.$event.' fail'.$orderId);
  100. }
  101. }
  102. }
  103. //接受浏览器的同步通知
  104. public function callback()
  105. {
  106. filePut('[payment][callback]同步回调开始');
  107. $orderId = Pay::getOrderId();
  108. if (!$orderId)
  109. {
  110. filePut('[payment][callback]无法分析到支付号未匹配');
  111. $this->error('无效支付!');
  112. }
  113. $where = array();
  114. $where['pay_no'] = $orderId;
  115. $info = Db::name('pay_order')->where($where)->find();
  116. if ($info)
  117. {
  118. //获取到它使用的支付接口
  119. $pay = new Pay($info['payment_class']);
  120. $rs = $pay->callback();
  121. if (!$info['status'])
  122. {
  123. filePut('[payment][callback]开始支付 '.$orderId);
  124. //Db::startTrans(); 修改为:充值+扣款,防止扣款失败时充值也回滚
  125. if ($rs)
  126. {
  127. $pay->touchEvent('success', $orderId);
  128. $pay->printMessage('success', 'callback结果 success, '.$orderId);
  129. filePut('[payment][callback]支付完成,结果 success, '.$orderId);
  130. filePut('[payment][callback]支付完成,结果 '.json_encode($rs).', '.$orderId);
  131. }
  132. else
  133. {
  134. $pay->touchEvent('failed', $orderId);
  135. $pay->printMessage('failed', 'callback结果 failed, '.$orderId);
  136. filePut('[payment][callback]支付完成,结果 failed, '.$orderId);
  137. filePut('[payment][callback]支付完成,结果 '.json_encode($rs).', '.$orderId);
  138. /*if (IS_MOBILE)
  139. {
  140. $url = session('redirect_fail_uri', '','user')?:'/wap/';
  141. session('redirect_fail_uri', null,'user');
  142. $this->redirect($url);
  143. }
  144. else
  145. {*/
  146. $this->error('支付失败!');
  147. /*}*/
  148. }
  149. }
  150. }
  151. else
  152. {
  153. filePut('[payment][callback]无效支付号 '.$orderId);
  154. /*if (IS_MOBILE)
  155. {
  156. $url = session('redirect_fail_uri', '','user')?:'/wap/';
  157. session('redirect_fail_uri', null,'user');
  158. $this->redirect($url);
  159. }
  160. else
  161. {*/
  162. $this->error('无效支付订单号!');
  163. /* }*/
  164. }
  165. //以下代码用于同步支付成功后回跳,请定制
  166. /*$pays = Pay::getInstalled();
  167. $pinfo = $pays[Pay::$payname];*/
  168. $pinfo = ['type'=>'pc'];
  169. filePut('[payment][callback]回调地址 '.json_encode(session('redirect_uri','','user')));
  170. //同步通知处理完进行回跳转
  171. if ('app'==$pinfo['type']||request()->isAjax())
  172. {
  173. //app同步请求
  174. if($rs)
  175. $pay->printMessage('success', 'app callback结果 success, '.$orderId);
  176. else
  177. $pay->printMessage('failed', 'app callback结果 failed, '.$orderId);
  178. }
  179. else
  180. {
  181. if (session('redirect_uri','','user'))
  182. {
  183. $url = str_replace('.html', '', session('redirect_uri','','user'));
  184. session('redirect_uri', null, 'user');
  185. $this->redirect($url);
  186. }
  187. elseif('wap'==$pinfo['type'])
  188. $this->redirect('/wap/');
  189. else
  190. $this->redirect('/index/user/walletcenter');
  191. }
  192. exit();
  193. //1.服务器发回处理结果
  194. //2.这里交给核心类处理
  195. //3.核心类交给支付接口判断支付成功还是失败
  196. //4.根据支付接口的返回结果,核心类调用之前绑定好的事件.
  197. }
  198. }
  199. ?>