From b09fc5e01e00ea61fd9895085312268e9914f7f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Tue, 21 Dec 2021 13:41:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E5=8D=95=E6=94=AF=E4=BB=98=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Order/OrderController.php | 3 ++ .../Http/Controllers/WeChatPayController.php | 7 +++-- app/Events/OrderPaid.php | 21 ++++++++++++++ app/Listeners/OrderPaidNotify.php | 29 +++++++++++++++++++ app/Providers/EventServiceProvider.php | 3 ++ app/Services/OrderService.php | 6 ++-- 6 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 app/Events/OrderPaid.php create mode 100644 app/Listeners/OrderPaidNotify.php diff --git a/app/Endpoint/Api/Http/Controllers/Order/OrderController.php b/app/Endpoint/Api/Http/Controllers/Order/OrderController.php index 2b86c985..4669388f 100644 --- a/app/Endpoint/Api/Http/Controllers/Order/OrderController.php +++ b/app/Endpoint/Api/Http/Controllers/Order/OrderController.php @@ -6,6 +6,7 @@ use App\Endpoint\Api\Http\Controllers\Controller; use App\Endpoint\Api\Http\Resources\OrderPackageResource; use App\Endpoint\Api\Http\Resources\OrderResource; use App\Endpoint\Api\Http\Resources\OrderResourceCollection; +use App\Events\OrderPaid; use App\Exceptions\BizException; use App\Helpers\Paginator as PaginatorHelper; use App\Models\KuaidiLog; @@ -102,6 +103,8 @@ class OrderController extends Controller throw $e; } + OrderPaid::dispatchIf($order->isPaid(), $order); + return OrderResource::make($order); } diff --git a/app/Endpoint/Callback/Http/Controllers/WeChatPayController.php b/app/Endpoint/Callback/Http/Controllers/WeChatPayController.php index c903169b..6d7791c9 100644 --- a/app/Endpoint/Callback/Http/Controllers/WeChatPayController.php +++ b/app/Endpoint/Callback/Http/Controllers/WeChatPayController.php @@ -2,6 +2,7 @@ namespace App\Endpoint\Callback\Http\Controllers; +use App\Events\OrderPaid; use App\Exceptions\BizException; use App\Models\Order; use App\Models\OrderRefundTask; @@ -34,13 +35,15 @@ class WeChatPayController extends Controller } try { - DB::transaction(function () use ($message) { - (new OrderService())->paySuccess($message['out_trade_no'], [ + $order = DB::transaction(function () use ($message) { + return (new OrderService())->paySuccess($message['out_trade_no'], [ 'pay_sn' => $message['transaction_id'], 'pay_way' => Order::PAY_WAY_WXPAY, 'pay_at' => Carbon::parse($message['time_end']), ]); }); + + OrderPaid::dispatchIf($order->isPaid(), $order); } catch (ModelNotFoundException | BizException $e) { } catch (Throwable $e) { throw $e; diff --git a/app/Events/OrderPaid.php b/app/Events/OrderPaid.php new file mode 100644 index 00000000..38117e79 --- /dev/null +++ b/app/Events/OrderPaid.php @@ -0,0 +1,21 @@ +order); + } catch (Throwable $e) { + report($e); + } + } +} diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 95870462..bc2c05a2 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -16,6 +16,9 @@ class EventServiceProvider extends ServiceProvider \App\Events\ProductSku\Viewed::class => [ \App\Listeners\CreateProductViewLog::class, ], + \App\Events\OrderPaid::class => [ + \App\Listeners\OrderPaidNotify::class, + ], ]; /** diff --git a/app/Services/OrderService.php b/app/Services/OrderService.php index d28086bd..c2ac0df2 100644 --- a/app/Services/OrderService.php +++ b/app/Services/OrderService.php @@ -725,9 +725,9 @@ class OrderService * * @param string $sn * @param array $params - * @return void + * @return \App\Models\Order */ - public function paySuccess(string $sn, array $params = []): void + public function paySuccess(string $sn, array $params = []): Order { $order = Order::where('sn', $sn)->firstOrFail(); @@ -743,6 +743,8 @@ class OrderService ]); // todo 处理预收益 + + return $order; } /**