123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- namespace App\Components;
- use App\Http\Models\Goods;
- use App\Http\Models\GoodsLabel;
- use App\Http\Models\Order;
- use App\Http\Models\Payment;
- use App\Http\Models\ReferralLog;
- use App\Http\Models\User;
- use App\Http\Models\UserLabel;
- use DB;
- use Exception;
- use Log;
- trait Callback
- {
- protected static $systemConfig;
- function __construct()
- {
- self::$systemConfig = Helpers::systemConfig();
- }
- public function show()
- {
- exit('show');
- }
-
- private function tradePaid($msg, $pay_type)
- {
- $pay_type_name = $pay_type == 1? '余额支付' : ($pay_type == 4? '支付宝国际' : ($pay_type == 5? '支付宝当面付' : ''));
- if($pay_type != 1){
-
- $payment = Payment::query()->with(['order', 'order.goods'])->where('status', 0)->where('order_sn', $msg['out_trade_no'])->first();
- if(!$payment){
- Log::info('【'.$pay_type_name.'】回调订单【'.$msg['out_trade_no'].'】不存在');
- return FALSE;
- }
- Log::info('【'.$pay_type_name.'】支付成功,开始处理回调订单');
- }else{
- Log::info('【'.$pay_type_name.'】订单处理');
- }
-
- try{
- DB::beginTransaction();
- if($pay_type != 1){
-
- $payment->pay_way = $pay_type == 4 || $pay_type == 5? 2 : 1;
- $payment->status = 1;
- $payment->save();
- }
-
- $order = Order::query()->where('order_sn', $msg['out_trade_no'])->first();
- if($order->goods_id == -1){
- $order->status = 2;
- $order->save();
- User::query()->where('id', $order->user_id)->increment('balance', $order->amount*100);
-
- $this->addUserBalanceLog($order->user_id, $order->oid, $order->user->balance, $order->user->balance+$order->amount, $order->amount, '用户在线充值');
- }else{
-
- $goods = Goods::query()->where('id', $order->goods_id)->first();
-
- $user = User::query()->where('id', $order->user_id)->first();
-
- switch($goods->type){
- case 1:
- $order->status = 2;
- $order->save();
- Helpers::addUserTrafficModifyLog($order->user_id, $order->oid, $user->transfer_enable, $user->transfer_enable+$goods->traffic*1048576, '[在线支付]加上用户购买的套餐流量');
- User::query()->where('id', $order->user_id)->increment('transfer_enable', $goods->traffic*1048576);
- break;
- case 2:
- $activePlan = Order::query()
- ->where('user_id', $order->user_id)
- ->with(['goods'])
- ->whereHas('goods', function($q){
- $q->where('type', 2);
- })
- ->where('is_expire', 0)
- ->where('status', 2)
- ->exists();
-
- $order->status = $activePlan? 3 : 2;
- $order->save();
-
- if(!$activePlan){
-
- Order::query()
- ->where('user_id', $order->user_id)
- ->with(['goods'])
- ->whereHas('goods', function($q){
- $q->where('type', '<=', 2);
- })
- ->where('is_expire', 0)
- ->where('status', 2)
- ->where('oid', '<>', $order->oid)
- ->update(['expire_at' => date('Y-m-d H:i:s'), 'is_expire' => 1]);
- Helpers::addUserTrafficModifyLog($order->user_id, $order->oid, $user->transfer_enable, 0, '[在线支付]用户购买新套餐,先清空流量');
- User::query()->where('id', $order->user_id)->update(['u' => 0, 'd' => 0, 'transfer_enable' => 0]);
- $userTraffic = $goods->traffic*1048576;
-
- $expireTime = date('Y-m-d', strtotime("+".$goods->days." days"));
-
- $nextResetTime = date('Y-m-d', strtotime("+".$goods->period." days"));
- if($nextResetTime >= $expireTime){
- $nextResetTime = NULL;
- }
-
- if($goods->label){
-
- UserLabel::query()->where('user_id', $order->user_id)->delete();
-
- $newUserLabels = array_values(array_unique(array_merge(GoodsLabel::query()->where('goods_id', $order->goods_id)->pluck('label_id')->toArray(), self::$systemConfig['initial_labels_for_user']? explode(',', self::$systemConfig['initial_labels_for_user']) : [])));
-
- foreach($newUserLabels as $vo){
- $obj = new UserLabel();
- $obj->user_id = $order->user_id;
- $obj->label_id = $vo;
- $obj->save();
- }
- }
- Helpers::addUserTrafficModifyLog($order->user_id, $order->oid, $user->transfer_enable, $userTraffic, '[在线支付]加上用户购买的套餐流量');
- User::query()->where('id', $order->user_id)->increment('invite_num', $goods->invite_num? : 0, ['transfer_enable' => $userTraffic, 'reset_time' => $nextResetTime, 'expire_time' => $expireTime, 'enable' => 1]);
- }else{
-
- User::query()->where('id', $order->user_id)->update(['expire_time' => date('Y-m-d', strtotime("+".$goods->days." days", strtotime($user->expire_time)))]);
- }
-
- if(Helpers::systemConfig()['referral_type'] && $order->user->referral_uid){
-
- $referral = ReferralLog::where('user_id', $order->user_id)->get();
-
- if(!$referral && self::$systemConfig['is_invite_register']){
-
- User::query()->where('id', $order->user->referral_uid)->increment('invite_num', 1);
- }
-
- if(Helpers::systemConfig()['referral_type'] == 2 || (Helpers::systemConfig()['referral_type'] == 1 && !$referral)){
- $this->addReferralLog($order->user_id, $order->user->referral_uid, $order->oid, $order->amount, $order->amount*self::$systemConfig['referral_percent']);
- }
- }
- break;
- default:
- Log::info('【处理订单】出现错误-未知套餐类型');
- }
- }
- DB::commit();
- Log::info('【'.$pay_type_name.'】处理成功');
- } catch(Exception $e){
- DB::rollBack();
- Log::info('【'.$pay_type_name.'】回调更新支付单和订单异常:'.$e->getMessage());
- }
- return FALSE;
- }
- private function activePrepaidOrder($oid)
- {
-
- $prepaidOrder = Order::query()->where('oid', $oid)->first();
-
- Order::query()->where('user_id', $prepaidOrder->user_id)->where('status', 2)->where('is_expire', 0)->update(['expire_at' => date('Y-m-d H:i:s'), 'is_expire' => 1]);
-
- $prepaidGood = Goods::query()->where('id', $prepaidOrder->goods_id)->first();
-
- Order::query()->where('oid', $prepaidOrder->oid)->update(['expire_at' => date("Y-m-d H:i:s", strtotime("+".$prepaidGood->days." days")), 'status' => 2]);
-
- $user = User::query()->where('id', $prepaidOrder->user_id)->first();
- $userTraffic = $prepaidGood->traffic*1048576;
-
- $expire_time = date('Y-m-d', strtotime("+".$prepaidGood->days." days"));
- $prepaidOrders = Order::query()->where('user_id', $prepaidOrder->user_id)->where('status', 3)->get();
- foreach($prepaidOrders as $paidOrder){
-
- $goods = Goods::query()->where('id', $paidOrder->goods_id)->first();
- $expire_time = date('Y-m-d', strtotime("+".$goods->days." days", strtotime($expire_time)));
- }
-
- $nextResetTime = date('Y-m-d', strtotime("+".$prepaidGood->period." days"));
- if($nextResetTime >= $expire_time){
- $nextResetTime = NULL;
- }
-
- $defaultLabels = self::$systemConfig['initial_labels_for_user']? explode(',', self::$systemConfig['initial_labels_for_user']) : [];
-
- $newUserLabels = array_values(array_unique(array_merge(GoodsLabel::query()->where('goods_id', $prepaidOrder->goods_id)->pluck('label_id')->toArray(), $defaultLabels)));
-
- foreach($newUserLabels as $vo){
- $obj = new UserLabel();
- $obj->user_id = $prepaidOrder->user_id;
- $obj->label_id = $vo;
- $obj->save();
- }
- Helpers::addUserTrafficModifyLog($prepaidOrder->user_id, $prepaidOrder->oid, $user->transfer_enable, $userTraffic, '[预支付订单激活]加上用户购买的套餐流量');
- User::query()->where('id', $prepaidOrder->user_id)->increment('invite_num', $prepaidOrder->invite_num? : 0, ['u' => 0, 'd' => 0, 'transfer_enable' => $userTraffic, 'expire_time' => $expire_time, 'reset_time' => $nextResetTime]);
- }
- }
|