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