6
0
Fork 0

订单退款

release
李静 2021-12-22 19:07:27 +08:00
parent f2c77c457b
commit 97ffed1d49
7 changed files with 47 additions and 39 deletions

View File

@ -3,7 +3,7 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Exceptions\BizException; use App\Exceptions\BizException;
use App\Models\OrderRefundJob; use App\Models\OrderRefundLog;
use App\Services\WeChatPayService; use App\Services\WeChatPayService;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@ -32,21 +32,21 @@ class OrderRefundCommand extends Command
*/ */
public function handle() public function handle()
{ {
OrderRefundJob::pending()->chunkById(200, function ($jobs) { OrderRefundLog::pending()->chunkById(200, function ($logs) {
foreach ($jobs as $job) { foreach ($logs as $log) {
try { try {
$method = 'refundBy'.Str::studly($job->order->pay_way); $method = 'refundBy'.Str::studly($log->order->pay_way);
if (! method_exists($this, $method)) { if (! method_exists($this, $method)) {
throw new BizException('退款方式暂不支持'); throw new BizException('退款方式暂不支持');
} }
$this->{$method}($job); $this->{$method}($log);
} catch (Throwable $e) { } catch (Throwable $e) {
report($e); report($e);
$job->update([ $log->update([
'status' => OrderRefundJob::STATUS_FAILED, 'status' => OrderRefundLog::STATUS_FAILED,
'failed_reason' => $e->getMessage(), 'failed_reason' => $e->getMessage(),
]); ]);
} }
@ -59,26 +59,26 @@ class OrderRefundCommand extends Command
/** /**
* 微信支付退款 * 微信支付退款
* *
* @param \App\Models\OrderRefundJob $job * @param \App\Models\OrderRefundLog $log
* @return void * @return void
*/ */
protected function refundByWxpay(OrderRefundJob $job) protected function refundByWxpay(OrderRefundLog $log)
{ {
$order = $job->order; $order = $log->order;
(new WeChatPayService())->refundByOutTradeNumber( (new WeChatPayService())->refundByOutTradeNumber(
$order->sn, $order->sn,
$job->sn, $log->sn,
$order->total_amount, $order->total_amount,
$job->amount, $log->amount,
[ [
'refund_desc' => $job->reason, 'refund_desc' => $log->reason,
'notify_url' => url(route('wxpay.order_refund_notify', [], false), [], true), 'notify_url' => url(route('wxpay.order_refund_notify', [], false), [], true),
] ]
); );
$job->update([ $log->update([
'status' => OrderRefundJob::STATUS_SUCCESS, 'status' => OrderRefundLog::STATUS_SUCCESS,
'failed_reason' => null, 'failed_reason' => null,
]); ]);
} }
@ -86,13 +86,13 @@ class OrderRefundCommand extends Command
/** /**
* 现金支付退款 * 现金支付退款
* *
* @param \App\Models\OrderRefundJob $job * @param \App\Models\OrderRefundLog $log
* @return void * @return void
*/ */
protected function refundByOffline(OrderRefundJob $job) protected function refundByOffline(OrderRefundLog $log)
{ {
$job->update([ $log->update([
'status' => OrderRefundJob::STATUS_SUCCESS, 'status' => OrderRefundLog::STATUS_SUCCESS,
]); ]);
} }
} }

View File

@ -5,7 +5,7 @@ namespace App\Endpoint\Callback\Http\Controllers;
use App\Events\OrderPaid; use App\Events\OrderPaid;
use App\Exceptions\BizException; use App\Exceptions\BizException;
use App\Models\Order; use App\Models\Order;
use App\Models\OrderRefundJob; use App\Models\OrderRefundLog;
use App\Services\OrderService; use App\Services\OrderService;
use App\Services\WeChatPayService; use App\Services\WeChatPayService;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
@ -74,10 +74,10 @@ class WeChatPayController extends Controller
// 退款失败 // 退款失败
if ($reqInfo['refund_status'] !== 'SUCCESS') { if ($reqInfo['refund_status'] !== 'SUCCESS') {
$orderRefundJob = OrderRefundJob::where('sn', $reqInfo['out_refund_no'])->first(); $log = OrderRefundLog::where('sn', $reqInfo['out_refund_no'])->first();
$orderRefundJob?->update([ $log?->update([
'status' => OrderRefundJob::STATUS_FAILED, 'status' => OrderRefundLog::STATUS_FAILED,
'failed_reason' => $reqInfo['refund_status'] === 'CHANGE' ? '退款异常' : '退款关闭', 'failed_reason' => $reqInfo['refund_status'] === 'CHANGE' ? '退款异常' : '退款关闭',
]); ]);
} }

View File

@ -136,9 +136,9 @@ class Order extends Model
/** /**
* 属于此订单的退款任务 * 属于此订单的退款任务
*/ */
public function refundJobs() public function refundLogs()
{ {
return $this->hasMany(OrderRefundJob::class); return $this->hasMany(OrderRefundLog::class);
} }
/** /**

View File

@ -4,11 +4,11 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
class OrderRefundJob extends Model class OrderRefundLog extends Model
{ {
public const STATUS_PENDING = 0; // 待处理 public const STATUS_PENDING = 0;
public const STATUS_SUCCESS = 5; // 退款成功 public const STATUS_SUCCESS = 4;
public const STATUS_FAILED = 6; // 退款失败 public const STATUS_FAILED = 5;
/** /**
* @var array * @var array
@ -31,7 +31,16 @@ class OrderRefundJob extends Model
]; ];
/** /**
* 只查询待退款的任务 * @var array
*/
protected $statusTexts = [
self::STATUS_PENDING => '待处理',
self::STATUS_SUCCESS => '成功',
self::STATUS_FAILED => '失败',
];
/**
* 只查询待退款的记录
*/ */
public function scopePending($query) public function scopePending($query)
{ {
@ -39,7 +48,7 @@ class OrderRefundJob extends Model
} }
/** /**
* 此退款任务所属的订单 * 此退款记录所属的订单
*/ */
public function order() public function order()
{ {

View File

@ -330,7 +330,7 @@ class AfterSaleService
$order = $afterSale->order; $order = $afterSale->order;
if (in_array($afterSale->type, [AfterSale::TYPE_REFUND_AND_RETURN, AfterSale::TYPE_REFUND])) { if (in_array($afterSale->type, [AfterSale::TYPE_REFUND_AND_RETURN, AfterSale::TYPE_REFUND])) {
//todo-执行实际退款操作; //todo-执行实际退款操作;
$order->refundJobs()->create([ $order->refundLogs()->create([
'sn' => OrderHelper::serialNumber(), 'sn' => OrderHelper::serialNumber(),
'amount' => $afterSale->amount, 'amount' => $afterSale->amount,
'reason' => '取消订单', 'reason' => '取消订单',

View File

@ -784,7 +784,7 @@ class OrderService
} }
if ($order->isWaitShipping()) { if ($order->isWaitShipping()) {
$order->refundJobs()->create([ $order->refundLogs()->create([
'sn' => OrderHelper::serialNumber(), 'sn' => OrderHelper::serialNumber(),
'amount' => $order->total_amount, 'amount' => $order->total_amount,
'reason' => '取消订单', 'reason' => '取消订单',

View File

@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
class CreateOrderRefundJobsTable extends Migration class CreateOrderRefundLogsTable extends Migration
{ {
/** /**
* Run the migrations. * Run the migrations.
@ -13,18 +13,17 @@ class CreateOrderRefundJobsTable extends Migration
*/ */
public function up() public function up()
{ {
Schema::create('order_refund_jobs', function (Blueprint $table) { Schema::create('order_refund_logs', function (Blueprint $table) {
$table->id(); $table->id();
$table->string('sn')->comment('退款单号'); $table->string('sn')->unique()->comment('退款流水号');
$table->unsignedBigInteger('order_id')->comment('订单ID'); $table->unsignedBigInteger('order_id')->comment('订单ID');
$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('reason')->nullable()->comment('退款原因');
$table->string('failed_reason')->nullable()->comment('失败原因'); $table->string('failed_reason')->nullable()->comment('失败原因');
$table->timestamps(); $table->timestamps();
$table->unique('sn');
$table->unique(['order_id', 'after_sale_id']); $table->unique(['order_id', 'after_sale_id']);
}); });
} }
@ -36,6 +35,6 @@ class CreateOrderRefundJobsTable extends Migration
*/ */
public function down() public function down()
{ {
Schema::dropIfExists('order_refund_jobs'); Schema::dropIfExists('order_refund_logs');
} }
} }