NotifyController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * 支付回调
  4. * @author wesmiler
  5. */
  6. namespace app\api\controller;
  7. use app\weixin\model\Books;
  8. use app\weixin\model\Member;
  9. use app\weixin\model\Wechat;
  10. use app\weixin\model\Payment;
  11. use app\weixin\service\PRedis;
  12. use think\Controller;
  13. use think\Request;
  14. class NotifyController extends Controller
  15. {
  16. public $scene = 'books';
  17. /**
  18. * 订单JSAPI支付回调
  19. * @author wesmiler
  20. */
  21. public function index()
  22. {
  23. $this->scene = 'books';
  24. $postStr = file_get_contents('php://input');
  25. $postData =$postStr? json_decode($postStr, true) : [];
  26. $outTradeNo = isset($postData['out_trade_no']) ? $postData['out_trade_no'] : '';
  27. if(empty($postData) || empty($outTradeNo)){
  28. //禁止引用外部xml实体
  29. libxml_disable_entity_loader(true);
  30. $postData = (array)(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA));
  31. }
  32. $outTradeNo = isset($postData['out_trade_no']) ? $postData['out_trade_no'] : '';
  33. $taskNo = $outTradeNo ? $outTradeNo : date('YmdHis');
  34. PRedis::set('payments:'.$this->scene.':result_'.$taskNo, ['result'=> $postData], 7200);
  35. if (Wechat::checkJsapiNotify($postData)) {
  36. // 验证订单是否存在
  37. $orderInfo = Books::getInfo(['order_sn'=> $outTradeNo]);
  38. // 验证参数
  39. PRedis::set('payments:'.$this->scene.':order_'.$taskNo, ['result'=> $postData, 'order'=> $orderInfo], 7200);
  40. $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : 0;
  41. if (empty($orderInfo)) {
  42. Payment::rebackOk();
  43. return false;
  44. }
  45. // 验证订单状态是否可处理
  46. if ($orderStatus != 1) {
  47. Books::saveData(['order_sn' => $outTradeNo],['remark'=> '订单已处理']);
  48. Payment::rebackOk();
  49. return false;
  50. }
  51. // 验证订单金额是否正确
  52. $payDebug = config('weixin.payDebug');
  53. $payMoney = isset($postData['total_fee']) ? moneyFormat($postData['total_fee']) : 0;
  54. $orderMoney = isset($orderInfo['money']) ? moneyFormat($orderInfo['money']) : 0.00;
  55. $credit = isset($orderInfo['credit']) ? moneyFormat($orderInfo['credit']) : 0.00;
  56. $orderAmount = moneyFormat($orderMoney + $credit);
  57. if (!$payDebug && intval($orderAmount * 100) != intval($payMoney)) {
  58. PRedis::set('payments:'.$this->scene.':money_'.$taskNo, ['remark'=> "订单金额错误:\n支付金额:" . $payMoney . "\n订单金额:" . $orderMoney.'|'.$orderAmount], 7200);
  59. return false;
  60. }
  61. // 处理订单逻辑
  62. if (Payment::catchOrder($outTradeNo, $postData)) {
  63. PRedis::set('payments:'.$this->scene.':success_'.$taskNo, ['result'=> $postData, 'order'=> $orderInfo], 7200);
  64. Payment::rebackOk();
  65. return true;
  66. } else {
  67. PRedis::set('payments:'.$this->scene.':fail_'.$taskNo, ['result'=> $postData, 'order'=> $orderInfo], 7200);
  68. return false;
  69. }
  70. }
  71. }
  72. /**
  73. * 充值订单回调
  74. * @return bool
  75. * @throws \think\Exception
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. * @throws \think\exception\DbException
  79. * @throws \think\exception\PDOException
  80. */
  81. public function recharge()
  82. {
  83. $this->scene = 'redheart';
  84. $postStr = file_get_contents('php://input');
  85. //禁止引用外部xml实体
  86. libxml_disable_entity_loader(true);
  87. $postData = (array)(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA));
  88. $outTradeNo = isset($postData['out_trade_no']) ? $postData['out_trade_no'] : '';
  89. $taskNo = $outTradeNo ? $outTradeNo : date('YmdHis');
  90. PRedis::set('payments:'.$this->scene.':result_'.$taskNo, ['result'=> $postData], 600);
  91. if (Wechat::checkJsapiNotify($postData)) {
  92. // 验证订单是否存在
  93. $orderInfo = db('user_recharge_log')
  94. ->where(['order_sn' => $outTradeNo])
  95. ->find();
  96. // 订单已处理
  97. PRedis::set('payments:'.$this->scene.':order_'.$taskNo, ['result'=> $postData, 'order'=> $orderInfo], 600);
  98. $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : 0;
  99. // 验证订单状态是否可处理
  100. if ($orderStatus != 1) {
  101. db('user_recharge_log')->where(['order_sn' => $outTradeNo])->update(['remark'=> '订单已处理']);
  102. Payment::rebackOk();
  103. return false;
  104. }
  105. // 验证订单金额是否正确
  106. $payDebug = config('weixin.payDebug');
  107. $payMoney = isset($postData['total_fee']) ? moneyFormat($postData['total_fee']) : 0;
  108. $orderMoney = isset($orderInfo['pay_money']) ? moneyFormat($orderInfo['pay_money']) : 0.00;
  109. if (!$payDebug && $orderMoney * 100 != $payMoney) {
  110. $error = ['remark'=> "订单金额错误:\n支付金额:" . $payMoney . "\n订单金额:" . $orderMoney];
  111. PRedis::set('payments:'.$this->scene.':errorMoney_'.$taskNo, ['result'=> $postData, 'order'=> $orderInfo,'error'=> $error], 600);
  112. db('user_recharge_log')->where(['order_sn' => $outTradeNo])->update($error);
  113. return false;
  114. }
  115. // 处理订单逻辑
  116. if (Payment::catchRechargeOrder($outTradeNo, $postData)) {
  117. PRedis::set('payments:'.$this->scene.':success_'.$taskNo, ['result'=> $postData, 'order'=> $orderInfo], 600);
  118. Payment::rebackOk();
  119. return true;
  120. } else {
  121. PRedis::set('payments:'.$this->scene.':fail_'.$taskNo, ['result'=> $postData, 'order'=> $orderInfo], 600);
  122. return false;
  123. }
  124. }
  125. }
  126. /**
  127. * 公共回调处理
  128. */
  129. public function pay(){
  130. $this->scene = input('scene','');
  131. PRedis::set('payments:t'.$this->scene, ['result'=> input()], 600);
  132. if(empty($this->scene)){
  133. return 'fail';
  134. }
  135. $postStr = file_get_contents('php://input');
  136. //禁止引用外部xml实体
  137. libxml_disable_entity_loader(true);
  138. $postData = (array)(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA));
  139. $outTradeNo = isset($postData['out_trade_no']) ? $postData['out_trade_no'] : '';
  140. $taskNo = $outTradeNo ? $outTradeNo : date('YmdHis');
  141. PRedis::set('payments:'.$this->scene.':result_'.$taskNo, ['result'=> $postData], 600);
  142. if (Wechat::checkJsapiNotify($postData)) {
  143. $catchService = "catch".ucwords($this->scene);
  144. Payment::$catchService($outTradeNo, $postData, $taskNo);
  145. }
  146. return 'fail';
  147. }
  148. /**
  149. * 人脸识别回调
  150. */
  151. public function face(){
  152. PRedis::set("caches:face:notify:post", input(), 3600);
  153. }
  154. }