CheckOrderStatus.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. $sql_format = 'SELECT uid FROM user_month_sign WHERE created_at >= DATE_ADD(NOW(),INTERVAL -1 hour) and
  50. NOT EXISTS (SELECT id FROM user_month_order WHERE user_month_order.uid =user_month_sign.uid and created_at BETWEEN "%s" and "%s" )';
  51. $sql = sprintf($sql_format,date('Y-m-01'),date('Y-m-01',strtotime('+1 month')));
  52. $result = DB::select($sql);
  53. if($result){
  54. foreach ($result as $item){
  55. $this->checkOne($item->uid);
  56. }
  57. }
  58. /*$result = UserMonthSign::where('created_at','>=',date('Y-m-d H:i:s',time()-1800))->select('uid')->get();
  59. if($result->isNotEmpty()){
  60. foreach ($result as $item){
  61. $this->checkOne($item->uid);
  62. }
  63. }*/
  64. }
  65. }
  66. public function checkOne($uid){
  67. //int $user_id,int $plan_id,string $app_id,string $key,string $app_secret
  68. $plan_id = env('MONTH_ORDER_PLAN_ID');
  69. $app_id = env('MONTH_ORDER_APPID');
  70. $key = env('MONTH_ORDER_KEY');
  71. $app_secret = env('MONTH_ORDER_APP_SECRET');
  72. $result = UserMonthService::checkOrderStatus($uid,$plan_id,$app_id,$key,$app_secret);
  73. if(isset($result['code']) && !$result['code']){
  74. echo sprintf('sign fail ,uid is :%s',$uid).PHP_EOL;
  75. return '';
  76. }
  77. //print_r($result);
  78. $trade_no = $result['trade_no'];
  79. $out_trade_no = $result['out_trade_no'];
  80. $money = $result['money'];
  81. if(empty($trade_no) || empty($out_trade_no) || empty($money)){
  82. Log::info(sprintf('uid is : %s,order is not exist',$uid));
  83. echo sprintf('uid is : %s,order is not exist',$uid).PHP_EOL;
  84. return '';
  85. }
  86. Log::info($result);
  87. if (!UserMonthService::getOrderByOrder($trade_no, $out_trade_no)) {
  88. Log::info('deal uid is :'.$uid);
  89. UserMonthService::createLOrder($uid, $plan_id, $money, $trade_no, $out_trade_no);
  90. //$this->addOrderAndVip($uid, $out_trade_no, $trade_no, $money);
  91. }
  92. }
  93. private function addOrderAndVip($uid, $out_trade_no, $trade_no, $price)
  94. {
  95. $order_info = OrderService::getByTradeNo($out_trade_no);
  96. //订单已经存在
  97. if ($order_info) {
  98. return;
  99. }
  100. $user_info = UserService::getById($uid);
  101. if (!$user_info) return;
  102. $this->createOrder($uid, $user_info->distribution_channel_id, 0, $price, $out_trade_no, $trade_no);
  103. YearOrderService::save_month_order([
  104. 'uid' => $uid,
  105. 'distribution_channel_id' => $user_info->distribution_channel_id,
  106. 'fee' => $price / 100,
  107. 'send_order_id' => 0
  108. ]);
  109. }
  110. private function createOrder($uid, $distribution_channel_id, $product_id, $price, $trade_no, $transaction_id)
  111. {
  112. $count = OrderService::getUserChargeTimes($uid);
  113. $count = $count + 1;
  114. $send_order_id = 0;
  115. try {
  116. $send_order_id = (int)Redis::hget('book_read:' . $uid, 'send_order_id');
  117. } catch (\Exception $e) {
  118. }
  119. $send_order_name = '';
  120. if ($send_order_id) {
  121. $send_order_info = SendOrderService::getById($send_order_id);
  122. if ($send_order_info && isset($send_order_info->name) && !empty($send_order_info->name)) {
  123. $send_order_name = $send_order_info->name;
  124. }
  125. }
  126. $init_order = [
  127. 'distribution_channel_id' => $distribution_channel_id,
  128. 'uid' => $uid,
  129. 'product_id' => 6826,
  130. 'price' => $price / 100,
  131. 'pay_type' => $count,
  132. 'trade_no' => $trade_no,
  133. 'pay_merchant_source' => 'MONTH',
  134. 'pay_merchant_id' => 99,
  135. 'create_ip' => '',
  136. 'send_order_id' => $send_order_id,
  137. 'send_order_name' => $send_order_name,
  138. 'order_type' => 'RECHARGE',
  139. 'from_bid' => '0',
  140. 'from_type' => 'auto_month',
  141. 'activity_id' => 0,
  142. 'inner_send_order_id' => '',
  143. 'status' => 'PAID',
  144. 'transaction_id' => $transaction_id,
  145. 'pay_end_at' => date('Y-m-d H:i:s')
  146. ];
  147. OrderService::save_order($init_order);
  148. }
  149. protected function successPayPushMsg($uid)
  150. {
  151. try {
  152. $force_sub_info = DB::table('force_subscribe_users')->where('uid', $uid)->where('is_subscribed', 1)->first();
  153. $data = UserService::getById($uid);
  154. if (!$force_sub_info) {
  155. return;
  156. }
  157. $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> ";
  158. $delay = 0;
  159. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($data->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/continue';;
  160. $top_url = 'https://help.zhuishuyun.com/top.html';
  161. $content = sprintf($content_format, $url, $top_url);
  162. $res['openid'] = $force_sub_info->openid;
  163. $res['appid'] = $force_sub_info->appid;
  164. $res['content'] = $content;
  165. $res['type'] = 'one_task';
  166. $res['send_time'] = date("Y-m-d H:i:s");
  167. $res['task_id'] = md5('month_pay_success_push');
  168. $send_data = array(
  169. 'send_time' => date("Y-m-d H:i:s"),
  170. 'data' => $res
  171. );
  172. dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'));
  173. } catch (\Exception $e) {
  174. Log::error('pay_success_push error');
  175. Log::error($e);
  176. }
  177. return;
  178. }
  179. }