CheckOrderStatus.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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,`type` 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,$item->type);
  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,$type){
  68. //int $user_id,int $plan_id,string $app_id,string $key,string $app_secret
  69. $app_id = env('MONTH_ORDER_APPID');
  70. if($type == 'MONTH'){
  71. $plan_id = env('MONTH_ORDER_PLAN_ID');
  72. $product_id = 6826;
  73. }elseif ($type == 'WEEK'){
  74. $product_id =7394;
  75. $plan_id = env('MONTH_WEEK_ORDER_PLAN_ID');
  76. }else{
  77. $product_id =0;
  78. $plan_id ='';
  79. }
  80. if(empty($plan_id)) {
  81. Log::info(sprintf('plan is not exist,uid is : %s,order is not exist',$uid));
  82. return '';
  83. }
  84. $key = env('MONTH_ORDER_KEY');
  85. $app_secret = env('MONTH_ORDER_APP_SECRET');
  86. $result = UserMonthService::checkOrderStatus($uid,$plan_id,$app_id,$key,$app_secret);
  87. if(isset($result['code']) && !$result['code']){
  88. echo sprintf('sign fail ,uid is :%s',$uid).PHP_EOL;
  89. return '';
  90. }
  91. //print_r($result);
  92. $trade_no = $result['trade_no'];
  93. $out_trade_no = $result['out_trade_no'];
  94. $money = $result['money'];
  95. if(empty($trade_no) || empty($out_trade_no) || empty($money)){
  96. Log::info(sprintf('uid is : %s,order is not exist',$uid));
  97. echo sprintf('uid is : %s,order is not exist',$uid).PHP_EOL;
  98. return '';
  99. }
  100. Log::info($result);
  101. if (!UserMonthService::getOrderByOrder($trade_no, $out_trade_no)) {
  102. Log::info('deal uid is :'.$uid);
  103. UserMonthService::createLOrder($uid, $plan_id, $money, $trade_no, $out_trade_no,$type);
  104. $this->addOrderAndVip($uid, $out_trade_no, $trade_no, $money,$type,$product_id);
  105. }
  106. }
  107. private function addOrderAndVip($uid, $out_trade_no, $trade_no, $price,$type,$product_id)
  108. {
  109. $order_info = OrderService::getByTradeNo($out_trade_no);
  110. //订单已经存在
  111. if ($order_info) {
  112. return;
  113. }
  114. $user_info = UserService::getById($uid);
  115. if (!$user_info) return;
  116. $this->createOrder($uid, $user_info->distribution_channel_id, $product_id, $price, $out_trade_no, $trade_no);
  117. /*YearOrderService::save_month_order([
  118. 'uid' => $uid,
  119. 'distribution_channel_id' => $user_info->distribution_channel_id,
  120. 'fee' => $price / 100,
  121. 'send_order_id' => 0
  122. ]);*/
  123. if($type == 'MONTH'){
  124. YearOrderService::save_month_order([
  125. 'uid' => $uid,
  126. 'distribution_channel_id' => $user_info->distribution_channel_id,
  127. 'fee' => $price / 100,
  128. 'send_order_id' => 0
  129. ]);
  130. }
  131. if($type == 'WEEK'){
  132. YearOrderService::save_vip_order_daynum([
  133. 'uid' => $uid,
  134. 'distribution_channel_id' => $user_info->distribution_channel_id,
  135. 'fee' => $price / 100,
  136. 'send_order_id' => 0,
  137. 'day_num'=>7
  138. ]);
  139. }
  140. }
  141. private function createOrder($uid, $distribution_channel_id, $product_id, $price, $trade_no, $transaction_id)
  142. {
  143. $count = OrderService::getUserChargeTimes($uid);
  144. $count = $count + 1;
  145. $send_order_id = 0;
  146. try {
  147. $send_order_id = (int)Redis::hget('book_read:' . $uid, 'send_order_id');
  148. } catch (\Exception $e) {
  149. }
  150. $send_order_name = '';
  151. if ($send_order_id) {
  152. $send_order_info = SendOrderService::getById($send_order_id);
  153. if ($send_order_info && isset($send_order_info->name) && !empty($send_order_info->name)) {
  154. $send_order_name = $send_order_info->name;
  155. }
  156. }
  157. $from_bid = ReadRecordService::getSimpleFirstReadRecord($uid);
  158. $init_order = [
  159. 'distribution_channel_id' => $distribution_channel_id,
  160. 'uid' => $uid,
  161. 'product_id' => 6826,
  162. 'price' => $price / 100,
  163. 'pay_type' => $count,
  164. 'trade_no' => $trade_no,
  165. 'pay_merchant_source' => 'MONTH',
  166. 'pay_merchant_id' => 99,
  167. 'create_ip' => '',
  168. 'send_order_id' => $send_order_id,
  169. 'send_order_name' => $send_order_name,
  170. 'order_type' => 'RECHARGE',
  171. 'from_bid' => $from_bid,
  172. 'from_type' => 'auto_month',
  173. 'activity_id' => 0,
  174. 'inner_send_order_id' => '',
  175. 'status' => 'PAID',
  176. 'transaction_id' => $transaction_id,
  177. 'pay_end_at' => date('Y-m-d H:i:s')
  178. ];
  179. OrderService::save_order($init_order);
  180. }
  181. protected function successPayPushMsg($uid)
  182. {
  183. try {
  184. $force_sub_info = DB::table('force_subscribe_users')->where('uid', $uid)->where('is_subscribed', 1)->first();
  185. $data = UserService::getById($uid);
  186. if (!$force_sub_info) {
  187. return;
  188. }
  189. $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> ";
  190. $delay = 0;
  191. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($data->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/continue';;
  192. $top_url = 'https://help.zhuishuyun.com/top.html';
  193. $content = sprintf($content_format, $url, $top_url);
  194. $res['openid'] = $force_sub_info->openid;
  195. $res['appid'] = $force_sub_info->appid;
  196. $res['content'] = $content;
  197. $res['type'] = 'one_task';
  198. $res['send_time'] = date("Y-m-d H:i:s");
  199. $res['task_id'] = md5('month_pay_success_push');
  200. $send_data = array(
  201. 'send_time' => date("Y-m-d H:i:s"),
  202. 'data' => $res
  203. );
  204. dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'));
  205. } catch (\Exception $e) {
  206. Log::error('pay_success_push error');
  207. Log::error($e);
  208. }
  209. return;
  210. }
  211. }