6
0
Fork 0
jiqu-library-server/app/Console/Commands/OrderRefundCommand.php

187 lines
4.6 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\AlipayService;
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()
{
while (true) {
$page = 0;
OrderRefundLog::pending()->chunkById(200, function ($logs) use (&$page) {
foreach ($logs as $log) {
try {
$method = '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 refundByWxpayApp(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 refundByAlipayApp(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,
]);
}
}