| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- // +----------------------------------------------------------------------
- // | Laravel框架 [ Laravel ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 Laravel研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: wesmiler <12345678@qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- use App\Models\GongdengOrderModel;
- use App\Models\MemberModel;
- use App\Models\SignsModel;
- use App\Models\TradeModel;
- use Illuminate\Support\Facades\DB;
- use Psr\Http\Message\MessageInterface;
- /**
- * 支付回调管理-服务类
- * @author wesmiler
- * @since 2020/11/11
- * Class NotifyService
- * @package App\Services
- */
- class NotifyService extends BaseService
- {
- protected static $instance = null;
- /**
- * 构造函数
- * @author wesmiler
- * @since 2020/11/11
- * NotifyService constructor.
- */
- public function __construct()
- {
- $this->model = new GongdengOrderModel();
- }
- /**
- * 静态入口
- * @return NotifyService|null
- */
- public static function make(){
- if(!self::$instance){
- self::$instance = new NotifyService();
- }
- return self::$instance;
- }
- /**
- * 供灯订单回调处理
- * @param $notifyData
- * @param $outTradeNo
- * @return false
- */
- public function notifyGongdeng($notifyData, $outTradeNo){
- $errorKey = "caches:orders:gongdeng:{$outTradeNo}";
- // 验证订单是否存在
- $orderInfo = $this->model::where(['order_sn'=> $outTradeNo])
- ->select(['id','source_id','buy_type','user_id','num','total','status'])
- ->first();
- // 验证参数
- RedisService::set($errorKey.':order',['order'=> $orderInfo,'notify'=> $notifyData], 3600);
- $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : 0;
- $userId = isset($orderInfo['user_id']) ? intval($orderInfo['user_id']) : 0;
- if (empty($orderInfo) || $userId<=0) {
- return NotifyService::rebackMsg('订单数据不存在', 'success');
- }
- // 订单用户
- $memberInfo = MemberModel::where(['id'=> $userId])->select(['id','openid','nickname','balance','coupon','status'])->first();
- if(!$memberInfo){
- return NotifyService::rebackMsg('订单用户不存在', 'success');
- }
- // 验证订单状态是否可处理
- if ($orderStatus != 1) {
- return NotifyService::rebackMsg('订单已处理', 'success');
- }
- // 验证订单金额是否正确
- $payDebug = config('weixin.payDebug');
- $amount = isset($notifyData['amount'])? $notifyData['amount'] : [];
- $payMoney = isset($amount['total']) ? moneyFormat($amount['total']) : 0;
- $orderMoney = isset($orderInfo['total']) ? moneyFormat($orderInfo['total']) : 0.00;
- $orderAmount = moneyFormat($orderMoney);
- if (!$payDebug && intval($orderAmount * 100) != intval($payMoney)) {
- RedisService::set($errorKey.':error_money',['notify'=> $notifyData, 'error'=> '实付金额与订单金额不一致','order'=> $orderInfo
- ], 3600);
- return NotifyService::rebackMsg('实付金额与订单金额不一致', 'error');
- }
- // 更新订单数据
- DB::beginTransaction();
- $tradeNo = isset($notifyData['transaction_id'])? $notifyData['transaction_id'] : '';
- if(!$this->model::where(['order_sn'=> $outTradeNo])->update(['status'=> 2,'trade_no'=> $tradeNo,'pay_at'=> date('Y-m-d H:i:s'),'pay_money'=> moneyFormat($payMoney/100),'remark'=> '已支付'])){
- RedisService::set($errorKey.':error_update',['notify'=> $notifyData, 'error'=> '更新订单信息失败','order'=> $orderInfo
- ], 3600);
- DB::rollBack();
- return NotifyService::rebackMsg('更新订单数据失败', 'error');
- }
- // 处理支付明细
- $data = [
- 'user_id'=> $userId,
- 'type'=> 1,
- 'coin_type'=> 2,
- 'pay_type'=> 2,
- 'money'=> $payMoney,
- 'change_type'=> 2,
- 'balance'=> $memberInfo->balance? $memberInfo->balance : 0,
- 'create_time'=> time(),
- 'remark'=> '供灯订单支付',
- 'status'=> 1
- ];
- if(!TradeModel::insertGetId($data)){
- RedisService::set($errorKey.':error_account',['notify'=> $notifyData, 'error'=> '处理交易明细失败','order'=> $orderInfo
- ], 3600);
- DB::rollBack();
- return NotifyService::rebackMsg('处理交易明细失败', 'error');
- }
- DB::commit();
- // 开灯处理,续费不处理
- $buyType = isset($orderInfo['buy_type'])? $orderInfo['buy_type'] : 0;
- if($buyType == 1){
- DevicesService::make()->catchLamp($outTradeNo);
- }
- // 消息处理
- return NotifyService::rebackMsg('支付处理成功','success');
- }
- /**
- * 回去回调报文内容
- * @param $message
- * @return string
- */
- public static function getBody($message){
- $body = '';
- $bodyStream = $message->getBody();
- if ($bodyStream->isSeekable()) {
- $body = (string)$bodyStream;
- $bodyStream->rewind();
- }
- return $body;
- }
- /**
- * 返回成功
- */
- public static function rebackOk(){
- echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
- exit;
- }
- /**
- * V3支付回调应答
- * @param $code
- * @param string $msg
- * @return false|string
- */
- public static function rebackMsg($msg='失败', $code='error'){
- return ['code'=> $code, 'message'=> $msg];
- }
- }
|