OrderPaySuccess.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Modules\Trade\Pay;
  3. use App\Modules\Book\Services\BookConfigService;
  4. use App\Modules\Subscribe\Models\Order;
  5. use DB;
  6. use App\Jobs\QappOrder;
  7. /**
  8. *
  9. */
  10. class OrderPaySuccess
  11. {
  12. /**
  13. * 支付成功回调处理订单
  14. * @param string $trade_no 订单号
  15. * @param string $transaction_id 微信商户号
  16. * @return bool
  17. */
  18. public static function handle(string $trade_no, string $transaction_id)
  19. {
  20. $order = Order::where('trade_no', $trade_no)->first();
  21. \Log::info('OrderPaySuccess_handle:trade_no:'.$trade_no.' transaction_id:'.$transaction_id);
  22. if(is_empty($order)){
  23. \Log::info('OrderPaySuccess_handle_error:not find by read node:trade_no:'.$trade_no.' transaction_id:'.$transaction_id);
  24. $appEnv = env('APP_ENV', 'production');
  25. $date = date("Y-m-d H:i:s");
  26. $msg = <<<EOF
  27. 项目:quick_app [$appEnv]
  28. 报错时间:$date
  29. 报错信息:- 支付回调在读库查找失败-trade_no: $trade_no;transaction_id:$transaction_id
  30. EOF;
  31. sendNotice($msg);
  32. $order= Order::onWriteConnection()->where('trade_no', $trade_no)->first();
  33. }
  34. if ($order) {
  35. $is_change = self::orderAcrossDay($order);
  36. //订单跨天
  37. $order->transaction_id = $transaction_id;
  38. try {
  39. //添加异步任务队列
  40. $job = new QappOrder($trade_no);
  41. dispatch($job->onConnection('redis_queue')->delay(5)->onQueue('{qapp_order_pay_success}'));
  42. } catch (Exception $e) {
  43. sendNotice($e->getMessage());
  44. }
  45. if ($order->status == 'PAID') {
  46. myLog('pay_notify')->info('has_pay:' . $trade_no);
  47. return true;
  48. }
  49. if ($order->order_type == 'YEAR') {
  50. $app = new YearOrderPaySuccess($order);
  51. } elseif ($order->order_type == 'BOOK') {
  52. $app = new BookOrderPaySuccess($order);
  53. } elseif ($order->order_type == 'QUARTER') {
  54. $app = new QuarterOrderPaySuccess($order);
  55. } elseif ($order->order_type == 'RECHARGE') {
  56. $app = new RechargeOrderPaySuccess($order);
  57. }
  58. $status = $app->success();
  59. if ($is_change) {
  60. DB::table('orders')->where('id', $order->id)->update([
  61. 'pay_end_at' => date('Y-m-d H:i:s', time() + 5),
  62. 'updated_at' => date('Y-m-d H:i:s', time() + 5),
  63. 'created_at' => date('Y-m-d H:i:s')
  64. ]);
  65. }
  66. self::freeBookStats($order->from_bid,$order->price,$order->uid);
  67. return $status;
  68. } else {
  69. return false;
  70. }
  71. }
  72. private static function freeBookStats($bid,$price,$uid){
  73. if(!$bid) return ;
  74. $free_book = BookConfigService::getByBidNoFilter($bid);
  75. if(!$free_book) return ;
  76. if(strtotime($free_book->end_time)+7*86400 < time()) return ;
  77. BookConfigService::chargeStats($free_book->id,$price,$uid);
  78. }
  79. private static function orderAcrossDay($order_info)
  80. {
  81. if (date('Y-m-d', strtotime($order_info->created_at)) == date('Y-m-d')) return false;
  82. $created_at = date('Y-m-d H:i:s', strtotime($order_info->created_at));
  83. $trade_no = $order_info->trade_no;
  84. $init_order = [
  85. 'distribution_channel_id' => $order_info->distribution_channel_id,
  86. 'uid' => $order_info->uid,
  87. 'product_id' => $order_info->product_id,
  88. 'price' => $order_info->price,
  89. 'pay_type' => $order_info->pay_type,
  90. 'trade_no' => 'cd-' . $trade_no,
  91. 'pay_merchant_source' => $order_info->pay_merchant_source,
  92. 'pay_merchant_id' => $order_info->pay_merchant_id,
  93. 'create_ip' => $order_info->create_ip,
  94. 'send_order_id' => $order_info->send_order_id,
  95. 'send_order_name' => $order_info->send_order_name,
  96. 'order_type' => $order_info->order_type,
  97. 'from_bid' => $order_info->from_bid,
  98. 'from_type' => $order_info->from_type,
  99. 'activity_id' => $order_info->activity_id,
  100. 'inner_send_order_id' => $order_info->inner_send_order_id,
  101. 'status' => 'UNPAID',
  102. 'transaction_id' => '',
  103. 'pay_end_at' => '0000-00-00 00:00:00',
  104. 'created_at' => $created_at,
  105. 'updated_at' => $created_at
  106. ];
  107. try {
  108. DB::table('orders')->insert($init_order);
  109. } catch (\Exception $e) {
  110. return false;
  111. }
  112. return true;
  113. }
  114. }