OrderPaySuccess.php 3.0 KB

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