CheckOrderStatus.php 6.3 KB

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