57 行
2.4 KiB
PHP
57 行
2.4 KiB
PHP
<?php
|
||
namespace app\admin\command;
|
||
|
||
use app\common\library\OrderStatus;
|
||
use app\common\library\PaymentService;
|
||
use think\console\Command;
|
||
use think\console\Input;
|
||
use think\console\Output;
|
||
use think\Db;
|
||
|
||
/** 定期执行:php think order:refund-retry */
|
||
class OrderRefundRetry extends Command
|
||
{
|
||
protected function configure()
|
||
{
|
||
$this->setName('order:refund-retry')->setDescription('核对并重试已取消订单的原路退款');
|
||
}
|
||
|
||
protected function execute(Input $input, Output $output)
|
||
{
|
||
// 支付通知晚到或丢失时,先向微信确认取消订单下的待支付流水。
|
||
$pending = Db::name('pay_order')->alias('p')
|
||
->join('order o', 'o.id = p.order_id')
|
||
->where('o.status', OrderStatus::CANCELLED)
|
||
->where('p.status', 'pending')
|
||
->where('p.openid', '<>', 'BALANCE')
|
||
->where(function ($query) {
|
||
$query->whereNull('p.last_sync_time')->whereOr('p.last_sync_time', '<', time() - 60);
|
||
})
|
||
->field('p.out_trade_no')->order('p.id asc')->limit(100)->select();
|
||
foreach ($pending as $payment) {
|
||
try {
|
||
PaymentService::confirmOrderPay((string)$payment['out_trade_no']);
|
||
} catch (\Throwable $e) {
|
||
\think\Log::error('[OrderRefundRetry payment] ' . $payment['out_trade_no'] . ' ' . $e->getMessage());
|
||
}
|
||
}
|
||
$orders = Db::name('pay_order')->alias('p')->join('order o', 'o.id = p.order_id')
|
||
->where('o.status', OrderStatus::CANCELLED)
|
||
->whereIn('p.status', ['paid', 'refunding', 'refund_failed'])
|
||
->where('p.updatetime', '<', time() - 60)
|
||
->field('o.id')->group('o.id')->order('o.id asc')->limit(100)->select();
|
||
foreach ($orders as $order) {
|
||
$id = (int)$order['id'];
|
||
try {
|
||
PaymentService::refundOrder($id, '取消订单原路退款恢复');
|
||
PaymentService::syncCancelledOrderRefund($id);
|
||
$output->writeln('订单 #' . $id . ' 已核对退款');
|
||
} catch (\Throwable $e) {
|
||
PaymentService::syncCancelledOrderRefund($id);
|
||
\think\Log::error('[OrderRefundRetry] order_id=' . $id . ' ' . $e->getMessage());
|
||
$output->writeln('订单 #' . $id . ' 退款异常:' . $e->getMessage());
|
||
}
|
||
}
|
||
}
|
||
}
|