123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace App\Console\Commands;
- use App\Models\Order;
- use App\Models\User;
- use App\Models\WalletLog;
- use App\Notifications\OrderRefundNotification;
- use Carbon\Carbon;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Notification;
- class RefundTestPlatformCommand extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'test_platform:refund';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '测试平台自动退款脚本';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- //
- $start_at = Carbon::yesterday();
- $end_at = Carbon::today();
- $orders = Order::query()
- ->where('status', Order::STATUS_COMPLETE_ORDER)
- ->where('pay_status', Order::PAY_STATUS_OK)
- ->where('pay_money', '>', 0)
- ->where('pay_time', '>', $start_at)
- ->where('pay_time', '<', $end_at)
- ->get();
- foreach ($orders as $order) {
- $this->refundMoney($order);
- }
- }
- public function refundMoney($order)
- {
- $pay_money = $order->pay_money;
- DB::beginTransaction();
- try {
- $old_pay_money = $order->pay_money;
- //1.更新订单
- $order->pay_money = 0.00;
- $order->total_money = 0.00;
- $order->order_money = 0.00;
- $order->is_refund_money = Order::REFUND_MONEY_OK;
- $order->save();
- //2.判断返还类别 更新用户余额 或微信退款
- $return_type_name = '原路退回';
- //退微信
- if ($order->pay_type == Order::PAY_TYPE_ACCOUNT) {
- DB::rollBack();
- return $this->error('此订单为余额支付 ');
- }
- //插入钱包记录 增加
- $wallet_log_add = new WalletLog();
- $wallet_log_add->name = WalletLog::$typeMaps[WalletLog::TYPE_ADD_WECHAT_PAY_ORDER_MONEY];
- $wallet_log_add->type = WalletLog::TYPE_ADD_WECHAT_PAY_ORDER_MONEY;
- $wallet_log_add->operate_type = WalletLog::OPERATE_TYPE_ADD;
- $wallet_log_add->money = (float)$pay_money;
- $wallet_log_add->user_id = $order->user_id;
- $wallet_log_add->area_id = $order->area_id;
- $wallet_log_add->log_id = $order->id;
- $wallet_log_add->log_type = Order::class;
- $wallet_log_add->save();
- //3.插入钱包记录 减少
- $wallet_log = new WalletLog();
- $wallet_log->name = WalletLog::$typeMaps[WalletLog::TYPE_SUB_ORDER_MONEY_PAY_WECHAT];
- $wallet_log->type = WalletLog::TYPE_SUB_ORDER_MONEY_PAY_WECHAT;
- $wallet_log->operate_type = WalletLog::OPERATE_TYPE_SUB;
- $wallet_log_add->money = -(float)$pay_money;
- $wallet_log_add->user_id = $order->user_id;
- $wallet_log_add->area_id = $order->area_id;
- $wallet_log_add->log_id = $order->id;
- $wallet_log->status = WalletLog::STATUS_PAUSE;
- $wallet_log->log_type = Order::class;
- $wallet_log->save();
- //退微信
- $payment = app('wechat.payment'); // 微信支付
- $refund_no = $order->no;
- $result = $payment->refund->byOutTradeNumber($order->no, $refund_no, wechat_fee($old_pay_money), wechat_fee($pay_money), [
- // 可在此处传入其他参数,详细参数见微信支付文档
- 'refund_desc' => '退订单支付费用',
- ]);
- if ($result['return_code'] === 'SUCCESS') {
- $user = User::find($order->user_id);
- // 返还通知
- Notification::send($user, new OrderRefundNotification($user, $order, $pay_money, $return_type_name));
- DB::commit();
- Log::info('已自动退款订单号:' . $order->no);
- // return $this->ok('退还成功');
- } else {
- Log::error('微信退款接口失败');
- DB::rollBack();
- // return $this->error('退还失败,请联系管理员');
- }
- $user = User::find($order->user_id);
- // 返还通知
- Notification::send($user, new OrderRefundNotification($user, $order, $pay_money, $return_type_name));
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- Log::info($e->getMessage());
- // return $this->error('操作失败请联系管理员');
- }
- }
- }
|