OrderPaySuccess.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. //订单跨天
  23. $is_change = self::orderAcrossDay($order);
  24. $order->transaction_id = $transaction_id;
  25. if ($order) {
  26. try {
  27. //添加异步任务队列
  28. $job = new QappOrder($trade_no);
  29. myLog('pay_notify')->info('start:' . $trade_no);
  30. dispatch($job->onConnection('redis')->onQueue('{qapp_order_pay_success}'));
  31. myLog('pay_notify')->info('end:' . $trade_no);
  32. } catch (Exception $e) {
  33. sendNotice($e->getMessage());
  34. }
  35. if ($order->status == 'PAID') {
  36. myLog('pay_notify')->info('has_pay:' . $trade_no);
  37. return true;
  38. }
  39. if ($order->order_type == 'YEAR') {
  40. $app = new YearOrderPaySuccess($order);
  41. } elseif ($order->order_type == 'BOOK') {
  42. $app = new BookOrderPaySuccess($order);
  43. } elseif ($order->order_type == 'QUARTER') {
  44. $app = new QuarterOrderPaySuccess($order);
  45. } elseif ($order->order_type == 'RECHARGE') {
  46. $app = new RechargeOrderPaySuccess($order);
  47. }
  48. $status = $app->success();
  49. if ($is_change) {
  50. DB::table('orders')->where('id', $order->id)->update([
  51. 'pay_end_at' => date('Y-m-d H:i:s', time() + 5),
  52. 'updated_at' => date('Y-m-d H:i:s', time() + 5),
  53. 'created_at' => date('Y-m-d H:i:s')
  54. ]);
  55. }
  56. self::freeBookStats($order->from_bid,$order->price,$order->uid);
  57. return $status;
  58. } else {
  59. return false;
  60. }
  61. }
  62. private static function freeBookStats($bid,$price,$uid){
  63. if(!$bid) return ;
  64. $free_book = BookConfigService::getByBidNoFilter($bid);
  65. if(!$free_book) return ;
  66. if(strtotime($free_book->end_time)+7*86400 < time()) return ;
  67. BookConfigService::chargeStats($free_book->id,$price,$uid);
  68. }
  69. private static function orderAcrossDay($order_info)
  70. {
  71. if (date('Y-m-d', strtotime($order_info->created_at)) == date('Y-m-d')) return false;
  72. $created_at = date('Y-m-d H:i:s', strtotime($order_info->created_at));
  73. $trade_no = $order_info->trade_no;
  74. $init_order = [
  75. 'distribution_channel_id' => $order_info->distribution_channel_id,
  76. 'uid' => $order_info->uid,
  77. 'product_id' => $order_info->product_id,
  78. 'price' => $order_info->price,
  79. 'pay_type' => $order_info->pay_type,
  80. 'trade_no' => 'cd-' . $trade_no,
  81. 'pay_merchant_source' => $order_info->pay_merchant_source,
  82. 'pay_merchant_id' => $order_info->pay_merchant_id,
  83. 'create_ip' => $order_info->create_ip,
  84. 'send_order_id' => $order_info->send_order_id,
  85. 'send_order_name' => $order_info->send_order_name,
  86. 'order_type' => $order_info->order_type,
  87. 'from_bid' => $order_info->from_bid,
  88. 'from_type' => $order_info->from_type,
  89. 'activity_id' => $order_info->activity_id,
  90. 'inner_send_order_id' => $order_info->inner_send_order_id,
  91. 'status' => 'UNPAID',
  92. 'transaction_id' => '',
  93. 'pay_end_at' => '0000-00-00 00:00:00',
  94. 'created_at' => $created_at,
  95. 'updated_at' => $created_at
  96. ];
  97. try {
  98. DB::table('orders')->insert($init_order);
  99. } catch (\Exception $e) {
  100. return false;
  101. }
  102. return true;
  103. }
  104. }