Fix wrapAction and checkout usage to prevent saving into session the uncommited transaction model
This commit is contained in:
parent
9bc60dbaa0
commit
f5f00e685a
3 changed files with 47 additions and 51 deletions
|
|
@ -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;
|
||||
|
||||
class InShopPaymentAdapter extends PaymentAdapter
|
||||
{
|
||||
|
|
@ -31,19 +30,14 @@ class InShopPaymentAdapter extends PaymentAdapter
|
|||
\Closure $failed
|
||||
): RedirectResponse {
|
||||
// phpcs:enable
|
||||
return $this->wrapAction(function () use ($customer, $order, $transaction, $succeed) {
|
||||
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);
|
||||
}
|
||||
|
||||
return \response()->redirectTo($this->successRoute($transaction));
|
||||
});
|
||||
return $this->wrapAction(function () use ($order, $transaction) {
|
||||
// * Enregistrement commande.
|
||||
if (!$order->save()) {
|
||||
throw new \RuntimeException("Order saving error happened.");
|
||||
}
|
||||
return \response()->redirectTo($this->successRoute($transaction));
|
||||
}, function () use ($succeed, $customer, $order, $transaction) {
|
||||
$succeed($customer, $order, $transaction);
|
||||
}, $failed);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -62,43 +62,39 @@ class StripePaymentAdapter extends PaymentAdapter
|
|||
\Closure $failed
|
||||
): RedirectResponse {
|
||||
// phpcs:enable
|
||||
return $this->wrapAction(function () use ($succeed, $customer, $order, $transaction) {
|
||||
return DB::transaction(function () use ($succeed, $customer, $order, $transaction) {
|
||||
// * Associate transaction to order
|
||||
$transaction->order()->associate($order);
|
||||
return $this->wrapAction(function () use ($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');
|
||||
}
|
||||
return $checkout->toResponse(\request());
|
||||
}, function () use ($succeed, $customer, $order, $transaction) {
|
||||
$succeed($customer, $order, $transaction);
|
||||
}, $failed);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
|
||||
abstract class PaymentAdapter
|
||||
|
|
@ -27,15 +28,20 @@ abstract class PaymentAdapter
|
|||
/**
|
||||
*
|
||||
* @param \Closure $action
|
||||
* @param \Closure(): void $success
|
||||
* @param \Closure(\Exception): void $failure
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* @phpcs:disable Squiz.Commenting.FunctionComment.IncorrectTypeHint
|
||||
*/
|
||||
protected function wrapAction(\Closure $action, \Closure $failure): RedirectResponse
|
||||
protected function wrapAction(\Closure $action, \Closure $success, \Closure $failure): RedirectResponse
|
||||
{
|
||||
// phpcs:enable
|
||||
try {
|
||||
return $action();
|
||||
$out = DB::transaction(function () use ($action) {
|
||||
return $action();
|
||||
});
|
||||
$success();
|
||||
return $out;
|
||||
} catch (\Exception $e) {
|
||||
// Sentry report failed action.
|
||||
\report($e);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue