CheckOrderStatus.php 7.1 KB

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