Paymentdo.php 7.4 KB

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