151 lines
3.7 KiB
PHP
151 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Exceptions\BizException;
|
|
use App\Models\BalanceLog;
|
|
use App\Models\OrderRefundLog;
|
|
use App\Models\WalletLog;
|
|
use App\Services\BalanceService;
|
|
use App\Services\WalletService;
|
|
use App\Services\WeChatPayService;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Str;
|
|
use Throwable;
|
|
|
|
class OrderRefundCommand extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'order:refund';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = '订单退款';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
OrderRefundLog::pending()->chunkById(200, function ($logs) {
|
|
foreach ($logs as $log) {
|
|
try {
|
|
$method = 'refundBy'.Str::studly($log->order->pay_way);
|
|
|
|
if (! method_exists($this, $method)) {
|
|
throw new BizException('退款方式暂不支持');
|
|
}
|
|
|
|
$this->{$method}($log);
|
|
} catch (Throwable $e) {
|
|
report($e);
|
|
|
|
$log->update([
|
|
'status' => OrderRefundLog::STATUS_FAILED,
|
|
'failed_reason' => $e->getMessage(),
|
|
]);
|
|
}
|
|
}
|
|
});
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* 微信支付退款
|
|
*
|
|
* @param \App\Models\OrderRefundLog $log
|
|
* @return void
|
|
*/
|
|
protected function refundByWxpay(OrderRefundLog $log)
|
|
{
|
|
$order = $log->order;
|
|
|
|
(new WeChatPayService())->refundByOutTradeNumber(
|
|
$order->pay_sn,
|
|
$log->sn,
|
|
$order->total_amount,
|
|
$log->amount,
|
|
[
|
|
'refund_desc' => $log->reason,
|
|
'notify_url' => url(route('wxpay.order_refund_notify', [], false), [], true),
|
|
]
|
|
);
|
|
|
|
$log->update([
|
|
'status' => OrderRefundLog::STATUS_SUCCESS,
|
|
'failed_reason' => null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 可提支付退款
|
|
*
|
|
* @param \App\Models\OrderRefundLog $log
|
|
* @return void
|
|
*/
|
|
protected function refundByWallet(OrderRefundLog $log)
|
|
{
|
|
$order = $log->order;
|
|
|
|
(new WalletService())->changeBalance(
|
|
$order->user,
|
|
$order->total_amount,
|
|
$log->after_sale_id ? WalletLog::ACTION_ORDER_AFTER_SALE : WalletLog::ACTION_ORDER_CANCELLED,
|
|
$log->after_sale_id ? '订单-售后退款' : $log->reason,
|
|
$order
|
|
);
|
|
|
|
$log->update([
|
|
'status' => OrderRefundLog::STATUS_SUCCESS,
|
|
'failed_reason' => null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 余额支付退款
|
|
*
|
|
* @param \App\Models\OrderRefundLog $log
|
|
* @return void
|
|
*/
|
|
protected function refundByBalance(OrderRefundLog $log)
|
|
{
|
|
$order = $log->order;
|
|
|
|
(new BalanceService())->changeBalance(
|
|
$order->user,
|
|
$order->total_amount,
|
|
$log->after_sale_id ? BalanceLog::ACTION_ORDER_AFTER_SALE : BalanceLog::ACTION_ORDER_CANCELLED,
|
|
$log->after_sale_id ? '订单-售后退款' : $log->reason,
|
|
$order
|
|
);
|
|
|
|
$log->update([
|
|
'status' => OrderRefundLog::STATUS_SUCCESS,
|
|
'failed_reason' => null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 现金支付退款
|
|
*
|
|
* @param \App\Models\OrderRefundLog $log
|
|
* @return void
|
|
*/
|
|
protected function refundByOffline(OrderRefundLog $log)
|
|
{
|
|
$log->update([
|
|
'status' => OrderRefundLog::STATUS_SUCCESS,
|
|
]);
|
|
}
|
|
}
|