OrderPaySuccess.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /**
  7. *
  8. */
  9. class OrderPaySuccess
  10. {
  11. /**
  12. * 支付成功回调处理订单
  13. * @param string $trade_no 订单号
  14. * @param string $transaction_id 微信商户号
  15. * @return bool
  16. */
  17. public static function handle(string $trade_no, string $transaction_id)
  18. {
  19. $order = Order::where('trade_no', $trade_no)->first();
  20. \Log::info('OrderPaySuccess_handle:trade_no:'.$trade_no.' transaction_id:'.$transaction_id);
  21. //订单跨天
  22. $is_change = self::orderAcrossDay($order);
  23. $order->transaction_id = $transaction_id;
  24. if ($order) {
  25. if ($order->status == 'PAID') {
  26. myLog('pay_notify')->info('has_pay:' . $trade_no);
  27. return true;
  28. }
  29. if ($order->order_type == 'YEAR') {
  30. $app = new YearOrderPaySuccess($order);
  31. } elseif ($order->order_type == 'BOOK') {
  32. $app = new BookOrderPaySuccess($order);
  33. } elseif ($order->order_type == 'QUARTER') {
  34. $app = new QuarterOrderPaySuccess($order);
  35. } elseif ($order->order_type == 'RECHARGE') {
  36. $app = new RechargeOrderPaySuccess($order);
  37. }
  38. $status = $app->success();
  39. if ($is_change) {
  40. DB::table('orders')->where('id', $order->id)->update([
  41. 'pay_end_at' => date('Y-m-d H:i:s', time() + 5),
  42. 'updated_at' => date('Y-m-d H:i:s', time() + 5),
  43. 'created_at' => date('Y-m-d H:i:s')
  44. ]);
  45. }
  46. self::freeBookStats($order->from_bid,$order->price,$order->uid);
  47. return $status;
  48. } else {
  49. return false;
  50. }
  51. }
  52. private static function freeBookStats($bid,$price,$uid){
  53. if(!$bid) return ;
  54. $free_book = BookConfigService::getByBidNoFilter($bid);
  55. if(!$free_book) return ;
  56. if(strtotime($free_book->end_time)+7*86400 < time()) return ;
  57. BookConfigService::chargeStats($free_book->id,$price,$uid);
  58. }
  59. private static function orderAcrossDay($order_info)
  60. {
  61. if (date('Y-m-d', strtotime($order_info->created_at)) == date('Y-m-d')) return false;
  62. $created_at = date('Y-m-d H:i:s', strtotime($order_info->created_at));
  63. $trade_no = $order_info->trade_no;
  64. $init_order = [
  65. 'distribution_channel_id' => $order_info->distribution_channel_id,
  66. 'uid' => $order_info->uid,
  67. 'product_id' => $order_info->product_id,
  68. 'price' => $order_info->price,
  69. 'pay_type' => $order_info->pay_type,
  70. 'trade_no' => 'cd-' . $trade_no,
  71. 'pay_merchant_source' => $order_info->pay_merchant_source,
  72. 'pay_merchant_id' => $order_info->pay_merchant_id,
  73. 'create_ip' => $order_info->create_ip,
  74. 'send_order_id' => $order_info->send_order_id,
  75. 'send_order_name' => $order_info->send_order_name,
  76. 'order_type' => $order_info->order_type,
  77. 'from_bid' => $order_info->from_bid,
  78. 'from_type' => $order_info->from_type,
  79. 'activity_id' => $order_info->activity_id,
  80. 'inner_send_order_id' => $order_info->inner_send_order_id,
  81. 'status' => 'UNPAID',
  82. 'transaction_id' => '',
  83. 'pay_end_at' => '0000-00-00 00:00:00',
  84. 'created_at' => $created_at,
  85. 'updated_at' => $created_at
  86. ];
  87. try {
  88. DB::table('orders')->insert($init_order);
  89. } catch (\Exception $e) {
  90. return false;
  91. }
  92. return true;
  93. }
  94. }