订单退款
parent
42bdfafd4c
commit
07fd87fcd1
|
|
@ -0,0 +1,84 @@
|
||||||
|
<?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);
|
||||||
|
|
||||||
|
$task->update([
|
||||||
|
'status' => OrderRefundTask::STATUS_SUCCESS,
|
||||||
|
'failed_reason' => null,
|
||||||
|
]);
|
||||||
|
} 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,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -158,6 +158,7 @@ class OrderController extends Controller
|
||||||
$order->refundTasks()->create([
|
$order->refundTasks()->create([
|
||||||
'sn' => OrderHelper::serialNumber(),
|
'sn' => OrderHelper::serialNumber(),
|
||||||
'amount' => $order->total_amount,
|
'amount' => $order->total_amount,
|
||||||
|
'reason' => '取消订单',
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@ use Illuminate\Database\Eloquent\Model;
|
||||||
class OrderRefundTask extends Model
|
class OrderRefundTask extends Model
|
||||||
{
|
{
|
||||||
public const STATUS_PENDING = 0;
|
public const STATUS_PENDING = 0;
|
||||||
|
public const STATUS_SUCCESS = 5;
|
||||||
|
public const STATUS_FAILED = 6;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
|
|
@ -24,6 +26,7 @@ class OrderRefundTask extends Model
|
||||||
'after_sale_id',
|
'after_sale_id',
|
||||||
'amount',
|
'amount',
|
||||||
'status',
|
'status',
|
||||||
|
'reason',
|
||||||
'failed_reason',
|
'failed_reason',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -101,4 +101,70 @@ class WeChatPayService
|
||||||
{
|
{
|
||||||
return $this->app->handlePaidNotify($closure);
|
return $this->app->handlePaidNotify($closure);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据微信订单号退款
|
||||||
|
*
|
||||||
|
* @param string $transactionId
|
||||||
|
* @param string $refundNumber
|
||||||
|
* @param int $totalFee
|
||||||
|
* @param int $refundFee
|
||||||
|
* @param array $optional
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function refundByTransactionId(string $transactionId, string $refundNumber, int $totalFee, int $refundFee, array $optional = [])
|
||||||
|
{
|
||||||
|
$result = $this->app->refund->byTransactionId($transactionId, $refundNumber, $totalFee, $refundFee, $optional);
|
||||||
|
|
||||||
|
if (data_get($result, 'return_code') !== 'SUCCESS') {
|
||||||
|
throw new WeChatPayException(
|
||||||
|
data_get($result, 'return_msg', '请求失败')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data_get($result, 'result_code') !== 'SUCCESS') {
|
||||||
|
throw new WeChatPayException(
|
||||||
|
sprintf(
|
||||||
|
'[%s] %s',
|
||||||
|
data_get($result, 'err_code', '-1'),
|
||||||
|
data_get($result, 'err_code_des', '退款失败')
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据商户订单号退款
|
||||||
|
*
|
||||||
|
* @param string $number
|
||||||
|
* @param string $refundNumber
|
||||||
|
* @param int $totalFee
|
||||||
|
* @param int $refundFee
|
||||||
|
* @param array $optional
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function refundByOutTradeNumber(string $number, string $refundNumber, int $totalFee, int $refundFee, array $optional = [])
|
||||||
|
{
|
||||||
|
$result = $this->app->refund->byOutTradeNumber($number, $refundNumber, $totalFee, $refundFee, $optional);
|
||||||
|
|
||||||
|
if (data_get($result, 'return_code') !== 'SUCCESS') {
|
||||||
|
throw new WeChatPayException(
|
||||||
|
data_get($result, 'return_msg', '请求失败')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data_get($result, 'result_code') !== 'SUCCESS') {
|
||||||
|
throw new WeChatPayException(
|
||||||
|
sprintf(
|
||||||
|
'[%s] %s',
|
||||||
|
data_get($result, 'err_code', '-1'),
|
||||||
|
data_get($result, 'err_code_des', '退款失败')
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ class CreateOrderRefundTasksTable extends Migration
|
||||||
$table->unsignedBigInteger('after_sale_id')->nullable()->comment('售后ID');
|
$table->unsignedBigInteger('after_sale_id')->nullable()->comment('售后ID');
|
||||||
$table->unsignedBigInteger('amount')->comment('退款金额');
|
$table->unsignedBigInteger('amount')->comment('退款金额');
|
||||||
$table->tinyInteger('status')->default(0)->comment('状态');
|
$table->tinyInteger('status')->default(0)->comment('状态');
|
||||||
|
$table->string('reason')->nullable()->comment('退款原因');
|
||||||
$table->string('failed_reason')->nullable()->comment('失败原因');
|
$table->string('failed_reason')->nullable()->comment('失败原因');
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue