| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- /**
- * 支付回调
- * @author wesmiler
- */
- namespace app\api\controller;
- use app\weixin\model\Books;
- use app\weixin\model\Member;
- use app\weixin\model\Wechat;
- use app\weixin\model\Payment;
- use app\weixin\service\PRedis;
- use think\Controller;
- use think\Request;
- class NotifyController extends Controller
- {
- public $scene = 'books';
- /**
- * 订单JSAPI支付回调
- * @author wesmiler
- */
- public function index()
- {
- $this->scene = 'books';
- $postStr = file_get_contents('php://input');
- $postData =$postStr? json_decode($postStr, true) : [];
- $outTradeNo = isset($postData['out_trade_no']) ? $postData['out_trade_no'] : '';
- if(empty($postData) || empty($outTradeNo)){
- //禁止引用外部xml实体
- libxml_disable_entity_loader(true);
- $postData = (array)(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA));
- }
- $outTradeNo = isset($postData['out_trade_no']) ? $postData['out_trade_no'] : '';
- $taskNo = $outTradeNo ? $outTradeNo : date('YmdHis');
- PRedis::set('payments:'.$this->scene.':result_'.$taskNo, ['result'=> $postData], 7200);
- if (Wechat::checkJsapiNotify($postData)) {
- // 验证订单是否存在
- $orderInfo = Books::getInfo(['order_sn'=> $outTradeNo]);
- // 验证参数
- PRedis::set('payments:'.$this->scene.':order_'.$taskNo, ['result'=> $postData, 'order'=> $orderInfo], 7200);
- $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : 0;
- if (empty($orderInfo)) {
- Payment::rebackOk();
- return false;
- }
- // 验证订单状态是否可处理
- if ($orderStatus != 1) {
- Books::saveData(['order_sn' => $outTradeNo],['remark'=> '订单已处理']);
- Payment::rebackOk();
- return false;
- }
- // 验证订单金额是否正确
- $payDebug = config('weixin.payDebug');
- $payMoney = isset($postData['total_fee']) ? moneyFormat($postData['total_fee']) : 0;
- $orderMoney = isset($orderInfo['money']) ? moneyFormat($orderInfo['money']) : 0.00;
- $credit = isset($orderInfo['credit']) ? moneyFormat($orderInfo['credit']) : 0.00;
- $orderAmount = moneyFormat($orderMoney + $credit);
- if (!$payDebug && intval($orderAmount * 100) != intval($payMoney)) {
- PRedis::set('payments:'.$this->scene.':money_'.$taskNo, ['remark'=> "订单金额错误:\n支付金额:" . $payMoney . "\n订单金额:" . $orderMoney.'|'.$orderAmount], 7200);
- return false;
- }
- // 处理订单逻辑
- if (Payment::catchOrder($outTradeNo, $postData)) {
- PRedis::set('payments:'.$this->scene.':success_'.$taskNo, ['result'=> $postData, 'order'=> $orderInfo], 7200);
- Payment::rebackOk();
- return true;
- } else {
- PRedis::set('payments:'.$this->scene.':fail_'.$taskNo, ['result'=> $postData, 'order'=> $orderInfo], 7200);
- return false;
- }
- }
- }
- /**
- * 充值订单回调
- * @return bool
- * @throws \think\Exception
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- * @throws \think\exception\PDOException
- */
- public function recharge()
- {
- $this->scene = 'redheart';
- $postStr = file_get_contents('php://input');
- //禁止引用外部xml实体
- libxml_disable_entity_loader(true);
- $postData = (array)(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA));
- $outTradeNo = isset($postData['out_trade_no']) ? $postData['out_trade_no'] : '';
- $taskNo = $outTradeNo ? $outTradeNo : date('YmdHis');
- PRedis::set('payments:'.$this->scene.':result_'.$taskNo, ['result'=> $postData], 600);
- if (Wechat::checkJsapiNotify($postData)) {
- // 验证订单是否存在
- $orderInfo = db('user_recharge_log')
- ->where(['order_sn' => $outTradeNo])
- ->find();
- // 订单已处理
- PRedis::set('payments:'.$this->scene.':order_'.$taskNo, ['result'=> $postData, 'order'=> $orderInfo], 600);
- $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : 0;
- // 验证订单状态是否可处理
- if ($orderStatus != 1) {
- db('user_recharge_log')->where(['order_sn' => $outTradeNo])->update(['remark'=> '订单已处理']);
- Payment::rebackOk();
- return false;
- }
- // 验证订单金额是否正确
- $payDebug = config('weixin.payDebug');
- $payMoney = isset($postData['total_fee']) ? moneyFormat($postData['total_fee']) : 0;
- $orderMoney = isset($orderInfo['pay_money']) ? moneyFormat($orderInfo['pay_money']) : 0.00;
- if (!$payDebug && $orderMoney * 100 != $payMoney) {
- $error = ['remark'=> "订单金额错误:\n支付金额:" . $payMoney . "\n订单金额:" . $orderMoney];
- PRedis::set('payments:'.$this->scene.':errorMoney_'.$taskNo, ['result'=> $postData, 'order'=> $orderInfo,'error'=> $error], 600);
- db('user_recharge_log')->where(['order_sn' => $outTradeNo])->update($error);
- return false;
- }
- // 处理订单逻辑
- if (Payment::catchRechargeOrder($outTradeNo, $postData)) {
- PRedis::set('payments:'.$this->scene.':success_'.$taskNo, ['result'=> $postData, 'order'=> $orderInfo], 600);
- Payment::rebackOk();
- return true;
- } else {
- PRedis::set('payments:'.$this->scene.':fail_'.$taskNo, ['result'=> $postData, 'order'=> $orderInfo], 600);
- return false;
- }
- }
- }
- /**
- * 公共回调处理
- */
- public function pay(){
- $this->scene = input('scene','');
- PRedis::set('payments:t'.$this->scene, ['result'=> input()], 600);
- if(empty($this->scene)){
- return 'fail';
- }
- $postStr = file_get_contents('php://input');
- //禁止引用外部xml实体
- libxml_disable_entity_loader(true);
- $postData = (array)(simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA));
- $outTradeNo = isset($postData['out_trade_no']) ? $postData['out_trade_no'] : '';
- $taskNo = $outTradeNo ? $outTradeNo : date('YmdHis');
- PRedis::set('payments:'.$this->scene.':result_'.$taskNo, ['result'=> $postData], 600);
- if (Wechat::checkJsapiNotify($postData)) {
- $catchService = "catch".ucwords($this->scene);
- Payment::$catchService($outTradeNo, $postData, $taskNo);
- }
- return 'fail';
- }
- /**
- * 人脸识别回调
- */
- public function face(){
- PRedis::set("caches:face:notify:post", input(), 3600);
- }
- }
|