CheckOrderStatus.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Jobs\SendTexts;
  4. use App\Modules\SendOrder\Services\SendOrderService;
  5. use App\Modules\Subscribe\Services\YearOrderService;
  6. use App\Modules\User\Models\UserMonthSign;
  7. use App\Modules\User\Services\UserMonthService;
  8. use App\Modules\User\Services\UserService;
  9. use Illuminate\Console\Command;
  10. use App\Modules\Subscribe\Services\OrderService;
  11. use Redis;
  12. use Log;
  13. use DB;
  14. class CheckOrderStatus extends Command
  15. {
  16. /**
  17. * The name and signature of the console command.
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'checkOrderStatus {--uid= : the uid}';
  22. /**
  23. * The console command description.
  24. *
  25. * @var string
  26. */
  27. protected $description = 'Command description';
  28. /**
  29. * Create a new command instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct()
  34. {
  35. parent::__construct();
  36. }
  37. /**
  38. * Execute the console command.
  39. *
  40. * @return mixed
  41. */
  42. public function handle()
  43. {
  44. //$this->checkOne(157286149);
  45. $uid = $this->option('uid');
  46. if($uid) {
  47. $this->checkOne($uid);
  48. }else{
  49. $result = UserMonthSign::where('created_at','>=',date('Y-m-d H:i:s',time()-1800))->select('uid')->get();
  50. if($result->isNotEmpty()){
  51. foreach ($result as $item){
  52. $this->checkOne($item->uid);
  53. }
  54. }
  55. }
  56. }
  57. public function checkOne($uid){
  58. //int $user_id,int $plan_id,string $app_id,string $key,string $app_secret
  59. $plan_id = env('MONTH_ORDER_PLAN_ID');
  60. $app_id = env('MONTH_ORDER_APPID');
  61. $key = env('MONTH_ORDER_KEY');
  62. $app_secret = env('MONTH_ORDER_APP_SECRET');
  63. $result = UserMonthService::checkOrderStatus($uid,$plan_id,$app_id,$key,$app_secret);
  64. if(isset($result['code']) && !$result['code']){
  65. echo sprintf('sign fail ,uid is :%s',$uid).PHP_EOL;
  66. return '';
  67. }
  68. //print_r($result);
  69. $trade_no = $result['trade_no'];
  70. $out_trade_no = $result['out_trade_no'];
  71. $money = $result['money'];
  72. if(empty($trade_no) || empty($out_trade_no) || empty($money)){
  73. Log::info(sprintf('uid is : %s,order is not exist',$uid));
  74. echo sprintf('uid is : %s,order is not exist',$uid).PHP_EOL;
  75. return '';
  76. }
  77. Log::info($result);
  78. if (!UserMonthService::getOrderByOrder($trade_no, $out_trade_no)) {
  79. Log::info('deal uid is :'.$uid);
  80. UserMonthService::createLOrder($uid, $plan_id, $money, $trade_no, $out_trade_no);
  81. $this->addOrderAndVip($uid, $out_trade_no, $trade_no, $money);
  82. }
  83. }
  84. private function addOrderAndVip($uid, $out_trade_no, $trade_no, $price)
  85. {
  86. $order_info = OrderService::getByTradeNo($out_trade_no);
  87. //订单已经存在
  88. if ($order_info) {
  89. return;
  90. }
  91. $user_info = UserService::getById($uid);
  92. if (!$user_info) return;
  93. $this->createOrder($uid, $user_info->distribution_channel_id, 0, $price, $out_trade_no, $trade_no);
  94. YearOrderService::save_month_order([
  95. 'uid' => $uid,
  96. 'distribution_channel_id' => $user_info->distribution_channel_id,
  97. 'fee' => $price / 100,
  98. 'send_order_id' => 0
  99. ]);
  100. }
  101. private function createOrder($uid, $distribution_channel_id, $product_id, $price, $trade_no, $transaction_id)
  102. {
  103. $count = OrderService::getUserChargeTimes($uid);
  104. $count = $count + 1;
  105. $send_order_id = 0;
  106. try {
  107. $send_order_id = (int)Redis::hget('book_read:' . $uid, 'send_order_id');
  108. } catch (\Exception $e) {
  109. }
  110. $send_order_name = '';
  111. if ($send_order_id) {
  112. $send_order_info = SendOrderService::getById($send_order_id);
  113. if ($send_order_info && isset($send_order_info->name) && !empty($send_order_info->name)) {
  114. $send_order_name = $send_order_info->name;
  115. }
  116. }
  117. $init_order = [
  118. 'distribution_channel_id' => $distribution_channel_id,
  119. 'uid' => $uid,
  120. 'product_id' => 6826,
  121. 'price' => $price / 100,
  122. 'pay_type' => $count,
  123. 'trade_no' => $trade_no,
  124. 'pay_merchant_source' => 'MONTH',
  125. 'pay_merchant_id' => 99,
  126. 'create_ip' => '',
  127. 'send_order_id' => $send_order_id,
  128. 'send_order_name' => $send_order_name,
  129. 'order_type' => 'RECHARGE',
  130. 'from_bid' => '0',
  131. 'from_type' => 'auto_month',
  132. 'activity_id' => 0,
  133. 'inner_send_order_id' => '',
  134. 'status' => 'PAID',
  135. 'transaction_id' => $transaction_id,
  136. 'pay_end_at' => date('Y-m-d H:i:s')
  137. ];
  138. OrderService::save_order($init_order);
  139. }
  140. protected function successPayPushMsg($uid)
  141. {
  142. try {
  143. $force_sub_info = DB::table('force_subscribe_users')->where('uid', $uid)->where('is_subscribed', 1)->first();
  144. $data = UserService::getById($uid);
  145. if (!$force_sub_info) {
  146. return;
  147. }
  148. $content_format = "包月购买成功通知:\r\n购买类型: 30元包月\r\n包月特权:\r\n 💝 全站所有作品免费阅读\r\n 💝 全站无广告\r\n 💝 阅读管家1对1服务\r\n为保障包月权益,请<a href='%s'>置顶公众号</a> ";
  149. $delay = 0;
  150. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($data->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/continue';;
  151. $top_url = 'https://help.zhuishuyun.com/top.html';
  152. $content = sprintf($content_format, $url, $top_url);
  153. $res['openid'] = $force_sub_info->openid;
  154. $res['appid'] = $force_sub_info->appid;
  155. $res['content'] = $content;
  156. $res['type'] = 'one_task';
  157. $res['send_time'] = date("Y-m-d H:i:s");
  158. $res['task_id'] = md5('month_pay_success_push');
  159. $send_data = array(
  160. 'send_time' => date("Y-m-d H:i:s"),
  161. 'data' => $res
  162. );
  163. dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'));
  164. } catch (\Exception $e) {
  165. Log::error('pay_success_push error');
  166. Log::error($e);
  167. }
  168. return;
  169. }
  170. }