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

99 lines
2.4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Exceptions\BizException;
use App\Models\OrderRefundTask;
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()
{
OrderRefundTask::pending()->chunkById(200, function ($tasks) {
foreach ($tasks as $task) {
try {
$method = 'refundBy'.Str::studly($task->order->pay_way);
if (! method_exists($this, $method)) {
throw new BizException('退款方式暂不支持');
}
$this->{$method}($task);
} catch (Throwable $e) {
report($e);
$task->update([
'status' => OrderRefundTask::STATUS_FAILED,
'failed_reason' => $e->getMessage(),
]);
}
}
});
return 0;
}
/**
* 微信支付退款
*
* @param \App\Models\OrderRefundTask $orderRefundTask
* @return void
*/
protected function refundByWxpay(OrderRefundTask $orderRefundTask)
{
$order = $orderRefundTask->order;
(new WeChatPayService())->refundByOutTradeNumber(
$order->sn,
$orderRefundTask->sn,
$order->total_amount,
$orderRefundTask->amount,
[
'refund_desc' => $orderRefundTask->reason,
'notify_url' => url(route('wxpay.order_refund_notify', [], false), [], true),
]
);
$orderRefundTask->update([
'status' => OrderRefundTask::STATUS_SUCCESS,
'failed_reason' => null,
]);
}
/**
* 现金支付退款
*
* @param \App\Models\OrderRefundTask $orderRefundTask
* @return void
*/
protected function refundByOffline(OrderRefundTask $orderRefundTask)
{
$orderRefundTask->update([
'status' => OrderRefundTask::STATUS_SUCCESS,
]);
}
}