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

99 lines
2.3 KiB
PHP

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