RefundTestPlatformCommand.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Order;
  4. use App\Models\User;
  5. use App\Models\WalletLog;
  6. use App\Notifications\OrderRefundNotification;
  7. use Carbon\Carbon;
  8. use Illuminate\Console\Command;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. use Illuminate\Support\Facades\Notification;
  12. class RefundTestPlatformCommand extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'test_platform:refund';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = '测试平台自动退款脚本';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return mixed
  39. */
  40. public function handle()
  41. {
  42. //
  43. $start_at = Carbon::yesterday();
  44. $end_at = Carbon::today();
  45. $orders = Order::query()
  46. ->where('status', Order::STATUS_COMPLETE_ORDER)
  47. ->where('pay_status', Order::PAY_STATUS_OK)
  48. ->where('pay_money', '>', 0)
  49. ->where('pay_time', '>', $start_at)
  50. ->where('pay_time', '<', $end_at)
  51. ->get();
  52. foreach ($orders as $order) {
  53. $this->refundMoney($order);
  54. }
  55. }
  56. public function refundMoney($order)
  57. {
  58. $pay_money = $order->pay_money;
  59. DB::beginTransaction();
  60. try {
  61. $old_pay_money = $order->pay_money;
  62. //1.更新订单
  63. $order->pay_money = 0.00;
  64. $order->total_money = 0.00;
  65. $order->order_money = 0.00;
  66. $order->is_refund_money = Order::REFUND_MONEY_OK;
  67. $order->save();
  68. //2.判断返还类别 更新用户余额 或微信退款
  69. $return_type_name = '原路退回';
  70. //退微信
  71. if ($order->pay_type == Order::PAY_TYPE_ACCOUNT) {
  72. DB::rollBack();
  73. return $this->error('此订单为余额支付 ');
  74. }
  75. //插入钱包记录 增加
  76. $wallet_log_add = new WalletLog();
  77. $wallet_log_add->name = WalletLog::$typeMaps[WalletLog::TYPE_ADD_WECHAT_PAY_ORDER_MONEY];
  78. $wallet_log_add->type = WalletLog::TYPE_ADD_WECHAT_PAY_ORDER_MONEY;
  79. $wallet_log_add->operate_type = WalletLog::OPERATE_TYPE_ADD;
  80. $wallet_log_add->money = (float)$pay_money;
  81. $wallet_log_add->user_id = $order->user_id;
  82. $wallet_log_add->area_id = $order->area_id;
  83. $wallet_log_add->log_id = $order->id;
  84. $wallet_log_add->log_type = Order::class;
  85. $wallet_log_add->save();
  86. //3.插入钱包记录 减少
  87. $wallet_log = new WalletLog();
  88. $wallet_log->name = WalletLog::$typeMaps[WalletLog::TYPE_SUB_ORDER_MONEY_PAY_WECHAT];
  89. $wallet_log->type = WalletLog::TYPE_SUB_ORDER_MONEY_PAY_WECHAT;
  90. $wallet_log->operate_type = WalletLog::OPERATE_TYPE_SUB;
  91. $wallet_log_add->money = -(float)$pay_money;
  92. $wallet_log_add->user_id = $order->user_id;
  93. $wallet_log_add->area_id = $order->area_id;
  94. $wallet_log_add->log_id = $order->id;
  95. $wallet_log->status = WalletLog::STATUS_PAUSE;
  96. $wallet_log->log_type = Order::class;
  97. $wallet_log->save();
  98. //退微信
  99. $payment = app('wechat.payment'); // 微信支付
  100. $refund_no = $order->no;
  101. $result = $payment->refund->byOutTradeNumber($order->no, $refund_no, wechat_fee($old_pay_money), wechat_fee($pay_money), [
  102. // 可在此处传入其他参数,详细参数见微信支付文档
  103. 'refund_desc' => '退订单支付费用',
  104. ]);
  105. if ($result['return_code'] === 'SUCCESS') {
  106. $user = User::find($order->user_id);
  107. // 返还通知
  108. Notification::send($user, new OrderRefundNotification($user, $order, $pay_money, $return_type_name));
  109. DB::commit();
  110. Log::info('已自动退款订单号:' . $order->no);
  111. // return $this->ok('退还成功');
  112. } else {
  113. Log::error('微信退款接口失败');
  114. DB::rollBack();
  115. // return $this->error('退还失败,请联系管理员');
  116. }
  117. $user = User::find($order->user_id);
  118. // 返还通知
  119. Notification::send($user, new OrderRefundNotification($user, $order, $pay_money, $return_type_name));
  120. DB::commit();
  121. } catch (\Exception $e) {
  122. DB::rollBack();
  123. Log::info($e->getMessage());
  124. // return $this->error('操作失败请联系管理员');
  125. }
  126. }
  127. }