From 9bc60dbaa053bc64db0fca7d11793b25117c1906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Fri, 31 Mar 2023 13:07:02 +0200 Subject: [PATCH] Undo wrapper in transaction as it would somehow not work --- .../Adapters/InShopPaymentAdapter.php | 19 +++--- .../Adapters/StripePaymentAdapter.php | 63 ++++++++++--------- app/Lib/Payments/PaymentAdapter.php | 5 +- 3 files changed, 46 insertions(+), 41 deletions(-) diff --git a/app/Lib/Payments/Adapters/InShopPaymentAdapter.php b/app/Lib/Payments/Adapters/InShopPaymentAdapter.php index faee2b9..3c10411 100644 --- a/app/Lib/Payments/Adapters/InShopPaymentAdapter.php +++ b/app/Lib/Payments/Adapters/InShopPaymentAdapter.php @@ -7,6 +7,7 @@ use App\Models\Customer\Customer; use App\Models\Payment\Transaction; use App\Models\Shop\Order; use Illuminate\Http\RedirectResponse; +use Illuminate\Support\Facades\DB; class InShopPaymentAdapter extends PaymentAdapter { @@ -31,16 +32,18 @@ class InShopPaymentAdapter extends PaymentAdapter ): RedirectResponse { // phpcs:enable return $this->wrapAction(function () use ($customer, $order, $transaction, $succeed) { - // * Enregistrement commande. - if (!$order->save()) { - throw new \RuntimeException("Order saving error happened."); - } + return DB::transaction(function () use ($succeed, $customer, $order, $transaction) { + // * Enregistrement commande. + if (!$order->save()) { + throw new \RuntimeException("Order saving error happened."); + } - if ($succeed) { - $succeed($customer, $order, $transaction); - } + if ($succeed) { + $succeed($customer, $order, $transaction); + } - return \response()->redirectTo($this->successRoute($transaction)); + return \response()->redirectTo($this->successRoute($transaction)); + }); }, $failed); } diff --git a/app/Lib/Payments/Adapters/StripePaymentAdapter.php b/app/Lib/Payments/Adapters/StripePaymentAdapter.php index 29da5f2..b4f4bbe 100644 --- a/app/Lib/Payments/Adapters/StripePaymentAdapter.php +++ b/app/Lib/Payments/Adapters/StripePaymentAdapter.php @@ -63,37 +63,42 @@ class StripePaymentAdapter extends PaymentAdapter ): RedirectResponse { // phpcs:enable return $this->wrapAction(function () use ($succeed, $customer, $order, $transaction) { - // * Associate transaction to order - $transaction->order()->associate($order); + return DB::transaction(function () use ($succeed, $customer, $order, $transaction) { + // * Associate transaction to order + $transaction->order()->associate($order); - // * Enregistrement commande avant checkout. - if (!$order->save() or !$transaction->save()) { - report(new \RuntimeException("Order saving error happened.")); - throw ValidationException::withMessages([ - 'error' => trans('Une erreur est survenue, veuillez réssayer') - ]); - } + // * Enregistrement commande avant checkout. + if (!$order->save() or !$transaction->save()) { + report(new \RuntimeException("Order saving error happened.")); + throw ValidationException::withMessages([ + 'error' => trans('Une erreur est survenue, veuillez réssayer') + ]); + } - $checkout = $customer->checkout( - [[ - 'price_data' => [ - 'currency' => Str::lower($transaction->currency->getCode()), - 'unit_amount' => $order->total_ttc, - 'product_data' => ['name' => trans('Commande :ref', ['ref' => $order->ref])] - ], - 'quantity' => 1, - ]], - [ - 'success_url' => $this->successRoute($transaction, ['session_id' => '{CHECKOUT_SESSION_ID}']), - 'cancel_url' => $this->cancelRoute($order), - ] - ); - $transaction->assignPaymentId($checkout->asStripeCheckoutSession()->payment_intent); - if (!$transaction->save()) { - throw new \RuntimeException('Failed to save transaction id'); - } - $succeed($customer, $order, $transaction); - return $checkout->toResponse(\request()); + $checkout = $customer + ->checkout( + [[ + 'price_data' => [ + 'currency' => Str::lower($transaction->currency->getCode()), + 'unit_amount' => $order->total_ttc, + 'product_data' => ['name' => trans('Commande :ref', ['ref' => $order->ref])] + ], + 'quantity' => 1, + ]], + [ + 'success_url' => $this->successRoute($transaction, [ + 'session_id' => '{CHECKOUT_SESSION_ID}' + ]), + 'cancel_url' => $this->cancelRoute($order), + ] + ); + $transaction->assignPaymentId($checkout->asStripeCheckoutSession()->payment_intent); + if (!$transaction->save()) { + throw new \RuntimeException('Failed to save transaction id'); + } + $succeed($customer, $order, $transaction); + return $checkout->toResponse(\request()); + }); }, $failed); } diff --git a/app/Lib/Payments/PaymentAdapter.php b/app/Lib/Payments/PaymentAdapter.php index 07bc015..d985aa5 100644 --- a/app/Lib/Payments/PaymentAdapter.php +++ b/app/Lib/Payments/PaymentAdapter.php @@ -7,7 +7,6 @@ use App\Models\Customer\Customer; use App\Models\Payment\Transaction; use App\Models\Shop\Order; use Illuminate\Http\RedirectResponse; -use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\URL; abstract class PaymentAdapter @@ -36,9 +35,7 @@ abstract class PaymentAdapter { // phpcs:enable try { - return DB::transaction(function () use ($action) { - return $action(); - }); + return $action(); } catch (\Exception $e) { // Sentry report failed action. \report($e);