فهرست منبع

month pay command and success push

zz 5 سال پیش
والد
کامیت
eafb843f38

+ 184 - 0
app/Console/Commands/CheckOrderStatus.php

@@ -0,0 +1,184 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Modules\SendOrder\Services\SendOrderService;
+use App\Modules\Subscribe\Services\YearOrderService;
+use App\Modules\User\Models\UserMonthSign;
+use App\Modules\User\Services\UserMonthService;
+use App\Modules\User\Services\UserService;
+use Illuminate\Console\Command;
+use App\Modules\Subscribe\Services\OrderService;
+use Redis;
+use Log;
+use DB;
+
+class CheckOrderStatus extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'checkOrderStatus {--uid= : the uid}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        //$this->checkOne(157286149);
+        $uid = $this->option('uid');
+        if($uid) {
+            $this->checkOne($uid);
+        }else{
+            $result = UserMonthSign::where('created_at','>=',date('Y-m-d H:i:s',time()-300))->select('uid')->get();
+            if($result->isNotEmpty()){
+                foreach ($result as $item){
+                    $this->checkOne($item->uid);
+                }
+            }
+        }
+    }
+    public function checkOne($uid){
+        //int $user_id,int $plan_id,string $app_id,string $key,string $app_secret
+        $plan_id = env('MONTH_ORDER_PLAN_ID');
+        $app_id = env('MONTH_ORDER_APPID');
+        $key = env('MONTH_ORDER_KEY');
+        $app_secret = env('MONTH_ORDER_APP_SECRET');
+        $result = UserMonthService::checkOrderStatus($uid,$plan_id,$app_id,$key,$app_secret);
+
+        if(isset($result['code']) && !$result['code']){
+            echo sprintf('sign fail ,uid is :%s',$uid).PHP_EOL;
+            return '';
+        }
+        //print_r($result);
+        $trade_no = $result['trade_no'];
+        $out_trade_no = $result['out_trade_no'];
+        $money = $result['money'];
+        if(empty($trade_no) || empty($out_trade_no) || empty($money)){
+            echo sprintf('uid is : %s,order is not exist',$uid).PHP_EOL;
+            return '';
+        }
+
+        if (!UserMonthService::getOrderByOrder($trade_no, $out_trade_no)) {
+            UserMonthService::createLOrder($uid, $plan_id, $money, $trade_no, $out_trade_no);
+            $this->addOrderAndVip($uid, $out_trade_no, $trade_no, $money);
+        }
+    }
+
+    private function addOrderAndVip($uid, $out_trade_no, $trade_no, $price)
+    {
+
+        $order_info = OrderService::getByTradeNo($out_trade_no);
+        //订单已经存在
+        if ($order_info) {
+            return;
+        }
+        $user_info = UserService::getById($uid);
+        if (!$user_info) return;
+        $this->createOrder($uid, $user_info->distribution_channel_id, 0, $price, $out_trade_no, $trade_no);
+        YearOrderService::save_month_order([
+            'uid' => $uid,
+            'distribution_channel_id' => $user_info->distribution_channel_id,
+            'fee' => $price / 100,
+            'send_order_id' => 0
+        ]);
+    }
+
+    private function createOrder($uid, $distribution_channel_id, $product_id, $price, $trade_no, $transaction_id)
+    {
+        $count = OrderService::getUserChargeTimes($uid);
+        $count = $count + 1;
+        $send_order_id = 0;
+        try {
+            $send_order_id = (int)Redis::hget('book_read:' . $uid, 'send_order_id');
+        } catch (\Exception $e) {
+        }
+        $send_order_name = '';
+        if ($send_order_id) {
+            $send_order_info = SendOrderService::getById($send_order_id);
+            if ($send_order_info && isset($send_order_info->name) && !empty($send_order_info->name)) {
+                $send_order_name = $send_order_info->name;
+            }
+        }
+        $init_order = [
+            'distribution_channel_id' => $distribution_channel_id,
+            'uid' => $uid,
+            'product_id' => 6826,
+            'price' => $price / 100,
+            'pay_type' => $count,
+            'trade_no' => $trade_no,
+            'pay_merchant_source' => 'MONTH',
+            'pay_merchant_id' => 99,
+            'create_ip' => '',
+            'send_order_id' => $send_order_id,
+            'send_order_name' => $send_order_name,
+            'order_type' => 'RECHARGE',
+            'from_bid' => '0',
+            'from_type' => 'auto_month',
+            'activity_id' => 0,
+            'inner_send_order_id' => '',
+            'status' => 'PAID',
+            'transaction_id' => $transaction_id,
+            'pay_end_at' => date('Y-m-d H:i:s')
+        ];
+        OrderService::save_order($init_order);
+    }
+
+    protected function successPayPushMsg($uid)
+    {
+        try {
+            $force_sub_info = DB::table('force_subscribe_users')->where('uid', $uid)->where('is_subscribed', 1)->first();
+            $data = UserService::getById($uid);
+            if (!$force_sub_info) {
+                return;
+            }
+
+            $content_format = "包月购买成功通知:\r\n\r\n购买类型: 30元包月\r\n\r\n><a href='%s'>点击继续上次阅读</a>\r\n\r\n为了方便阅读,请<a href='%s'>置顶公众号</a>\r\n\r\n为了方便阅读,请<a href='%s'>置顶公众号</a>\r\n\r\n为了方便阅读,请<a href='%s'>置顶公众号</a>";
+
+            $delay = 0;
+            $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($data->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/continue';;
+            $top_url = 'https://help.zhuishuyun.com/top.html';
+
+            $content = sprintf($content_format, $url, $top_url, $top_url, $top_url);
+
+            $res['openid'] = $force_sub_info->openid;
+            $res['appid'] = $force_sub_info->appid;
+            $res['content'] = $content;
+            $res['type'] = 'one_task';
+            $res['send_time'] = date("Y-m-d H:i:s");
+            $res['task_id'] = md5('month_pay_success_push');
+            $send_data = array(
+                'send_time' => date("Y-m-d H:i:s"),
+                'data' => $res
+            );
+            dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'));
+
+
+        } catch (\Exception $e) {
+            Log::error('pay_success_push error');
+            Log::error($e);
+        }
+        return;
+    }
+}

+ 4 - 1
app/Console/Kernel.php

@@ -24,7 +24,8 @@ class Kernel extends ConsoleKernel
         Commands\YqBook::class,
         Commands\channelCpcCode::class,
         Commands\updateFromNewYunqi::class,
-        Commands\InnerOuterWeixinStats::class
+        Commands\InnerOuterWeixinStats::class,
+        Commands\CheckOrderStatus::class,
     ];
 
     /**
@@ -38,5 +39,7 @@ class Kernel extends ConsoleKernel
         $schedule->command('book:update')->dailyAt('06:00');
 
         $schedule->command('book:update')->dailyAt('18:00');
+
+        $schedule->command('checkOrderStatus')->everyFiveMinutes();
     }
 }

+ 112 - 62
app/Http/Controllers/Wap/Pay/MonthOrderController.php

@@ -8,6 +8,8 @@
 
 namespace App\Http\Controllers\Wap\Pay;
 
+use App\Jobs\SendTexts;
+use App\Modules\OfficialAccount\Services\OfficialAccountService;
 use App\Modules\SendOrder\Services\SendOrderService;
 use App\Modules\Subscribe\Models\Order;
 use App\Modules\Subscribe\Services\OrderService;
@@ -23,7 +25,8 @@ use Redis;
 class MonthOrderController extends Controller
 {
 
-    public function getH5RedirectUrl(Request $request){
+    public function getH5RedirectUrl(Request $request)
+    {
         $app_id = 'Aekgrv87yo5wxnl715';
         $app_secret = '57gwsnd158ib2sn7wluq5716brp8fzm';
         $key = 'sn7wluq5716brp8fzm';
@@ -31,16 +34,17 @@ class MonthOrderController extends Controller
         $plan_id = 39603;
         $user_id = 10008;
         $ip = _getIp();
-        $sign = _sign(compact('app_id','app_secret','plan_id','user_id','ip'),$key.$key);
+        $sign = _sign(compact('app_id', 'app_secret', 'plan_id', 'user_id', 'ip'), $key . $key);
         $sign = strtoupper($sign);
         $client = new  Client();
-        Log::info(compact('app_id','app_secret','plan_id','user_id','ip','sign'));
-        $url = 'http://pap.manyuedu.org/h5.php?'.http_build_query(compact('app_id','app_secret','plan_id','user_id','ip','sign'));
+        Log::info(compact('app_id', 'app_secret', 'plan_id', 'user_id', 'ip', 'sign'));
+        $url = 'http://pap.manyuedu.org/h5.php?' . http_build_query(compact('app_id', 'app_secret', 'plan_id', 'user_id', 'ip', 'sign'));
         //$url = 'http://pap.manyuedu.org/?'.http_build_query(compact('app_id','app_secret','plan_id','user_id','ip','sign'));
         return $client->get($url)->getBody()->getContents();
     }
 
-    public function getWechatRedirectUrl(Request $request){
+    public function getWechatRedirectUrl(Request $request)
+    {
         $app_id = 'Aekgrv87yo5wxnl715';
         $app_secret = '57gwsnd158ib2sn7wluq5716brp8fzm';
         $key = 'sn7wluq5716brp8fzm';
@@ -48,17 +52,18 @@ class MonthOrderController extends Controller
         $plan_id = 39603;
         $user_id = 10008;
         $ip = _getIp();
-        $sign = _sign(compact('app_id','app_secret','plan_id','user_id','ip'),$key.$key);
+        $sign = _sign(compact('app_id', 'app_secret', 'plan_id', 'user_id', 'ip'), $key . $key);
         $sign = strtoupper($sign);
         $client = new  Client();
-        Log::info(compact('app_id','app_secret','plan_id','user_id','ip','sign'));
+        Log::info(compact('app_id', 'app_secret', 'plan_id', 'user_id', 'ip', 'sign'));
         //$url = 'http://pap.manyuedu.org/h5.php?'.http_build_query(compact('app_id','app_secret','plan_id','user_id','ip','sign'));
-        $url = 'http://pap.manyuedu.org/?'.http_build_query(compact('app_id','app_secret','plan_id','user_id','ip','sign'));
+        $url = 'http://pap.manyuedu.org/?' . http_build_query(compact('app_id', 'app_secret', 'plan_id', 'user_id', 'ip', 'sign'));
         return $client->get($url)->getBody()->getContents();
     }
 
 
-    public function signCallBack(Request $request){
+    public function signCallBack(Request $request)
+    {
         Log::info('signCallBack------------------------------------------signCallBack');
         Log::info($request->all());
         Log::info($request->getMethod());
@@ -68,59 +73,60 @@ class MonthOrderController extends Controller
         $plan_id = $request->post('plan_id');
         $user_id = $request->post('user_id');
         $change_type = $request->post('change_type');
-        if($check = checkParam($request->except('_url'),['app_id','plan_id','user_id','change_type'])){
-            return response()->error('PARAM_EMPTY',['msg'=>$check]);
+        if ($check = checkParam($request->except('_url'), ['app_id', 'plan_id', 'user_id', 'change_type'])) {
+            return response()->error('PARAM_EMPTY', ['msg' => $check]);
         }
-        $sign = _sign(compact('app_id','plan_id','user_id','change_type'),$key.$key);
+        $sign = _sign(compact('app_id', 'plan_id', 'user_id', 'change_type'), $key . $key);
         $sign = strtoupper($sign);
         $user_info = UserService::getById($user_id);
-        if(!$user_info){
-            Log::error('signCallBack user is not exist,uid is :'.$user_id);
+        if (!$user_info) {
+            Log::error('signCallBack user is not exist,uid is :' . $user_id);
             return response('fail');
         }
-        if($sign == $request->post('sign')){
-            UserMonthService::createSign($user_id,$plan_id,$change_type,$user_info->openid);
+        if ($sign == $request->post('sign')) {
+            UserMonthService::createSign($user_id, $plan_id, $change_type, $user_info->openid);
         }
         Log::info('signCallBack-------------------end-----------------------signCallBack');
         return response('success');
     }
 
 
-    public function orderCallBackV2(Request $request){
+    public function orderCallBackV2(Request $request)
+    {
         Log::info('orderCallBack------------------------------------------orderCallBack');
         Log::info($request->all());
         Log::info($request->getMethod());
 
         $key = env('MONTH_ORDER_KEY');//'sn7wluq5716brp8fzm';
         $app_id = $request->post('app_id');
-        $plan_id = $request->post('plan_id',0);
+        $plan_id = $request->post('plan_id', 0);
         $user_id = $request->post('user_id');
         $total_fee = $request->post('total_fee');
         $trade_no = $request->post('trade_no');
         $out_trade_no = $request->post('out_trade_no');
-        if($check = checkParam($request->except('_url'),['app_id','user_id','total_fee','trade_no','out_trade_no'])){
-            return response()->error('PARAM_EMPTY',['msg'=>$check]);
+        if ($check = checkParam($request->except('_url'), ['app_id', 'user_id', 'total_fee', 'trade_no', 'out_trade_no'])) {
+            return response()->error('PARAM_EMPTY', ['msg' => $check]);
         }
-        $sign = _sign(compact('app_id','user_id','total_fee','trade_no','out_trade_no'),$key.$key);
+        $sign = _sign(compact('app_id', 'user_id', 'total_fee', 'trade_no', 'out_trade_no'), $key . $key);
         $sign = strtoupper($sign);
-        Log::info('self sign is: '.$sign);
+        Log::info('self sign is: ' . $sign);
         $order_info = Order::find($user_id);
-        if(!$order_info){
-            Log::error('month order not exist !!!order is :'.$trade_no);
+        if (!$order_info) {
+            Log::error('month order not exist !!!order is :' . $trade_no);
             return response('fail');
         }
-        if($order_info->status == 'PAID'){
-            Log::error('month order had deal !!!, order is :'.$trade_no);
+        if ($order_info->status == 'PAID') {
+            Log::error('month order had deal !!!, order is :' . $trade_no);
             return response('fail');
         }
         //if($sign == $request->post('sign')){
-        if(!UserMonthService::getOrderByOrder($trade_no,$out_trade_no)){
-            UserMonthService::createLOrder($order_info->uid,$plan_id,$total_fee,$trade_no,$out_trade_no);
+        if (!UserMonthService::getOrderByOrder($trade_no, $out_trade_no)) {
+            UserMonthService::createLOrder($order_info->uid, $plan_id, $total_fee, $trade_no, $out_trade_no);
             $order_info->transaction_id = $trade_no;
             $order_info->status = 'PAID';
             $order_info->pay_end_at = date('Y-m-d H:i:s');
             $order_info->save();
-            self::addOrderAndVipV2($order_info->uid,$total_fee);
+            self::addOrderAndVipV2($order_info->uid, $total_fee);
         }
         //}
 
@@ -128,27 +134,29 @@ class MonthOrderController extends Controller
         return response('success');
     }
 
-    public function orderCallBack(Request $request){
+    public function orderCallBack(Request $request)
+    {
         Log::info('orderCallBack------------------------------------------orderCallBack');
         Log::info($request->all());
         Log::info($request->getMethod());
 
         $key = env('MONTH_ORDER_KEY');//'sn7wluq5716brp8fzm';
         $app_id = $request->post('app_id');
-        $plan_id = $request->post('plan_id',0);
+        $plan_id = $request->post('plan_id', 0);
         $user_id = $request->post('user_id');
         $total_fee = $request->post('total_fee');
         $trade_no = $request->post('trade_no');
         $out_trade_no = $request->post('out_trade_no');
-        if($check = checkParam($request->except('_url'),['app_id','user_id','total_fee','trade_no','out_trade_no'])){
-            return response()->error('PARAM_EMPTY',['msg'=>$check]);
+        if ($check = checkParam($request->except('_url'), ['app_id', 'user_id', 'total_fee', 'trade_no', 'out_trade_no'])) {
+            return response()->error('PARAM_EMPTY', ['msg' => $check]);
         }
-        $sign = _sign(compact('app_id','user_id','total_fee','trade_no','out_trade_no'),$key.$key);
+        $sign = _sign(compact('app_id', 'user_id', 'total_fee', 'trade_no', 'out_trade_no'), $key . $key);
         $sign = strtoupper($sign);
         //if($sign == $request->post('sign')){
-        if(!UserMonthService::getOrderByOrder($trade_no,$out_trade_no)){
-            UserMonthService::createLOrder($user_id,$plan_id,$total_fee,$trade_no,$out_trade_no);
-            self::addOrderAndVip($user_id,$out_trade_no,$trade_no,$total_fee);
+        if (!UserMonthService::getOrderByOrder($trade_no, $out_trade_no)) {
+            UserMonthService::createLOrder($user_id, $plan_id, $total_fee, $trade_no, $out_trade_no);
+            $this->addOrderAndVip($user_id, $out_trade_no, $trade_no, $total_fee);
+            $this->successPayPushMsg($user_id);
         }
         //}
 
@@ -157,39 +165,42 @@ class MonthOrderController extends Controller
     }
 
 
-    private function addOrderAndVip($uid,$out_trade_no,$trade_no,$price){
+    private function addOrderAndVip($uid, $out_trade_no, $trade_no, $price)
+    {
 
         $order_info = OrderService::getByTradeNo($out_trade_no);
         //订单已经存在
-        if($order_info){
-            return ;
+        if ($order_info) {
+            return;
         }
         $user_info = UserService::getById($uid);
-        if(!$user_info) return ;
-        $this->createOrder($uid,$user_info->distribution_channel_id,0,$price,$out_trade_no,$trade_no);
+        if (!$user_info) return;
+        $this->createOrder($uid, $user_info->distribution_channel_id, 0, $price, $out_trade_no, $trade_no);
         YearOrderService::save_month_order([
-            'uid'=>$uid,
-            'distribution_channel_id'=>$user_info->distribution_channel_id,
-            'fee'=>$price / 100,
-            'send_order_id'=>0
+            'uid' => $uid,
+            'distribution_channel_id' => $user_info->distribution_channel_id,
+            'fee' => $price / 100,
+            'send_order_id' => 0
         ]);
     }
 
-    private function addOrderAndVipV2($uid,$price){
+    private function addOrderAndVipV2($uid, $price)
+    {
 
 
         $user_info = UserService::getById($uid);
-        if(!$user_info) return ;
+        if (!$user_info) return;
 
         YearOrderService::save_month_order([
-            'uid'=>$uid,
-            'distribution_channel_id'=>$user_info->distribution_channel_id,
-            'fee'=>$price / 100,
-            'send_order_id'=>0
+            'uid' => $uid,
+            'distribution_channel_id' => $user_info->distribution_channel_id,
+            'fee' => $price / 100,
+            'send_order_id' => 0
         ]);
     }
 
-    private function createOrder($uid,$distribution_channel_id,$product_id,$price,$trade_no,$transaction_id){
+    private function createOrder($uid, $distribution_channel_id, $product_id, $price, $trade_no, $transaction_id)
+    {
         $count = OrderService::getUserChargeTimes($uid);
         $count = $count + 1;
         $send_order_id = 0;
@@ -221,34 +232,73 @@ class MonthOrderController extends Controller
             'from_type' => 'auto_month',
             'activity_id' => 0,
             'inner_send_order_id' => '',
-            'status'=>'PAID',
-            'transaction_id'=>$transaction_id,
-            'pay_end_at'=>date('Y-m-d H:i:s')
+            'status' => 'PAID',
+            'transaction_id' => $transaction_id,
+            'pay_end_at' => date('Y-m-d H:i:s')
         ];
         OrderService::save_order($init_order);
     }
 
-    public function wait(Request $request){
+    public function wait(Request $request)
+    {
         //$user_cookie = \Cookie::get(env('COOKIE_AUTH_WEB_WECHAT'));
         //$uid = $user_cookie ? decrypt($user_cookie) : null;
         $r = $request->get('r');
         $r = urldecode($r);
-        return view('pay.order.monthpaywait',['url'=>$r]);
+        return view('pay.order.monthpaywait', ['url' => $r]);
     }
 
-    public function issuccess(Request $request){
+    public function issuccess(Request $request)
+    {
         $uid = \Cookie::get(env('COOKIE_AUTH_WEB_WECHAT'));
         //$uid = $user_cookie ? decrypt($user_cookie) : null;
-        if(!$uid){
+        if (!$uid) {
             return response()->success();
         }
         $last = UserMonthService::getLastOrder($uid);
-        if(!$last){
+        if (!$last) {
             return response()->error('WAP_SYS_ERROR');
         }
-        if(time()-strtotime($last->created_at) <20){
+        if (time() - strtotime($last->created_at) < 20) {
             return response()->success();
         }
         return response()->error('WAP_SYS_ERROR');
     }
+
+    protected function successPayPushMsg($uid)
+    {
+        try {
+            $force_sub_info = DB::table('force_subscribe_users')->where('uid', $uid)->where('is_subscribed', 1)->first();
+            $data = UserService::getById($uid);
+            if (!$force_sub_info) {
+                return;
+            }
+
+            $content_format = "包月购买成功通知:\r\n\r\n购买类型: 30元包月\r\n\r\n><a href='%s'>点击继续上次阅读</a>\r\n\r\n为了方便阅读,请<a href='%s'>置顶公众号</a>\r\n\r\n为了方便阅读,请<a href='%s'>置顶公众号</a>\r\n\r\n为了方便阅读,请<a href='%s'>置顶公众号</a>";
+
+            $delay = 0;
+            $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($data->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/continue';;
+            $top_url = 'https://help.zhuishuyun.com/top.html';
+
+            $content = sprintf($content_format, $url, $top_url, $top_url, $top_url);
+
+            $res['openid'] = $force_sub_info->openid;
+            $res['appid'] = $force_sub_info->appid;
+            $res['content'] = $content;
+            $res['type'] = 'one_task';
+            $res['send_time'] = date("Y-m-d H:i:s");
+            $res['task_id'] = md5('month_pay_success_push');
+            $send_data = array(
+                'send_time' => date("Y-m-d H:i:s"),
+                'data' => $res
+            );
+            dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'));
+
+
+        } catch (\Exception $e) {
+            Log::error('pay_success_push error');
+            Log::error($e);
+        }
+        return;
+    }
 }

+ 5 - 7
app/Modules/User/Services/UserMonthService.php

@@ -59,16 +59,14 @@ class UserMonthService
         return $info;
     }
 
-
     public static function checkOrderStatus(int $user_id,int $plan_id,string $app_id,string $key,string $app_secret){
         $pay_year = date('Y');
         $pay_month = date('m');
-        $sign = _sign(compact('app_id','app_secret','user_id','plan_id','pay_year','pay_month'),$key.$key);
+        $sign = strtoupper(_sign(compact('app_id','app_secret','user_id','plan_id','pay_year','pay_month'),$key.$key));
         $client = new Client();
-        $url = 'http://pap.manyuedu.org/checkPay.php';
-        $client->request('post',$url,['form_params'=>
-            compact('user_id','app_secret','app_id','plan_id','pay_year','pay_month','sign')])
-            ->getBody()->getContents();
-
+        $url = 'http://pap.manyuedu.org/checkpay.php?';
+        $url .= http_build_query(compact('user_id','app_secret','app_id','plan_id','pay_year','pay_month','sign'));
+        $result = $client->request('post',$url)->getBody()->getContents();
+        return json_decode($result,true);
     }
 }