194 lines
5.0 KiB
PHP
194 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Enums\PayWay;
|
|
use App\Exceptions\BizException;
|
|
use App\Models\BalanceLog;
|
|
use App\Models\OrderRefundLog;
|
|
use App\Models\WalletLog;
|
|
use App\Services\AlipayService;
|
|
use App\Services\BalanceService;
|
|
use App\Services\Payment\WxpayService;
|
|
use App\Services\WalletService;
|
|
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()
|
|
{
|
|
while (true) {
|
|
$page = 0;
|
|
|
|
OrderRefundLog::pending()->chunkById(200, function ($logs) use (&$page) {
|
|
foreach ($logs as $log) {
|
|
try {
|
|
$method = match ($log->order->pay_way) {
|
|
PayWay::WxpayApp, PayWay::WxpayH5, PayWay::WxpayJsApi, PayWay::WxpayMiniProgram => 'refundByWxpay',
|
|
PayWay::AlipayApp => 'refundByAlipay',
|
|
default => 'refundBy'.Str::studly($log->order->pay_way->value),
|
|
};
|
|
|
|
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(),
|
|
]);
|
|
}
|
|
}
|
|
|
|
$page++;
|
|
});
|
|
|
|
if ($page === 0) {
|
|
sleep(60);
|
|
} elseif ($page === 1) {
|
|
sleep(30);
|
|
} else {
|
|
sleep(15);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 微信支付退款
|
|
*
|
|
* @param \App\Models\OrderRefundLog $log
|
|
* @return void
|
|
*/
|
|
protected function refundByWxpay(OrderRefundLog $log)
|
|
{
|
|
$order = $log->order;
|
|
|
|
(new WxpayService())->refundByOutTradeNumber([
|
|
'number' => $order->pay_sn,
|
|
'refund_number' => $log->sn,
|
|
'total_fee' => $order->total_amount,
|
|
'refund_fee' => $log->amount,
|
|
'optional' => [
|
|
'refund_desc' => $log->reason,
|
|
],
|
|
], match ($order->pay_way) {
|
|
PayWay::WxpayMiniProgram => 'mini_program',
|
|
default => 'default',
|
|
});
|
|
|
|
$log->update([
|
|
'status' => OrderRefundLog::STATUS_SUCCESS,
|
|
'failed_reason' => null,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 支付宝退款
|
|
*
|
|
* @param \App\Models\OrderRefundLog $log
|
|
* @return void
|
|
*/
|
|
protected function refundByAlipay(OrderRefundLog $log)
|
|
{
|
|
$order = $log->order;
|
|
|
|
(new AlipayService())->refund(
|
|
$order->pay_sn,
|
|
$log->sn,
|
|
bcdiv($log->amount, 100, 2),
|
|
$log->reason,
|
|
);
|
|
|
|
$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,
|
|
$log->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,
|
|
$log->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,
|
|
]);
|
|
}
|
|
}
|