diff --git a/app/Livewire/CheckoutPage.php b/app/Livewire/CheckoutPage.php index 7fded1b9..ad084d3e 100644 --- a/app/Livewire/CheckoutPage.php +++ b/app/Livewire/CheckoutPage.php @@ -99,19 +99,6 @@ public function mount(): void return; } - if ($this->payment_intent) { - $payment = Payments::driver($this->paymentType)->cart($this->cart)->withData([ - 'payment_intent_client_secret' => $this->payment_intent_client_secret, - 'payment_intent' => $this->payment_intent, - ])->authorize(); - - if ($payment->success) { - redirect()->route('checkout-success.view'); - - return; - } - } - // Do we have a shipping address? $this->shipping = $this->cart->shippingAddress ?: new CartAddress; @@ -252,12 +239,16 @@ public function checkout() ])->authorize(); if ($payment->success) { - redirect()->route('checkout-success.view'); + redirect()->route('checkout-success.view', [ + 'cartId' => $this->cart->id, + ]); return; } - return redirect()->route('checkout-success.view'); + return redirect()->route('checkout-success.view', [ + 'cartId' => $this->cart->id, + ]); } /** diff --git a/app/Livewire/CheckoutProcessing.php b/app/Livewire/CheckoutProcessing.php new file mode 100644 index 00000000..771601cc --- /dev/null +++ b/app/Livewire/CheckoutProcessing.php @@ -0,0 +1,69 @@ +paymentIntent = StripePaymentIntent::where('intent_id', $request->get('payment_intent'))->firstOrFail(); + $this->paymentIntentClientSecret = $request->get('payment_intent_client_secret'); + } + + public function checkStatus(): void + { + if (! $this->cart?->completedOrder()->exists()) { + $this->tries++; + } + + if ($this->tries >= 5) { + $this->manuallyProcessOrder(); + } + + if ($this->cart?->completedOrder()->exists()) { + to_route('checkout-success.view', [ + 'cartId' => $this->cart->id, + ]); + } + } + + protected function manuallyProcessOrder(): void + { + $this->paymentIntent->update([ + 'processing_at' => now(), + ]); + + Payments::driver('stripe')->cart($this->cart)->withData([ + 'payment_intent_client_secret' => $this->paymentIntentClientSecret, + 'payment_intent' => $this->paymentIntent->intent_id, + ])->authorize(); + } + + public function hydrate() + { + $this->checkStatus(); + } + + public function getCartProperty() + { + return $this->paymentIntent->cart; + } + + public function render() + { + return view('livewire.checkout-processing'); + } +} diff --git a/app/Livewire/CheckoutSuccessPage.php b/app/Livewire/CheckoutSuccessPage.php index 5436a418..480fe1ca 100644 --- a/app/Livewire/CheckoutSuccessPage.php +++ b/app/Livewire/CheckoutSuccessPage.php @@ -2,6 +2,7 @@ namespace App\Livewire; +use Illuminate\Http\Request; use Illuminate\View\View; use Livewire\Component; use Lunar\Facades\CartSession; @@ -14,9 +15,10 @@ class CheckoutSuccessPage extends Component public Order $order; - public function mount(): void + public function mount(Request $request): void { - $this->cart = CartSession::current(); + $this->cart = Cart::find($request->get('cartId')); + if (! $this->cart || ! $this->cart->completedOrder) { $this->redirect('/'); diff --git a/config/services.php b/config/services.php index 23045b20..7a183ad2 100644 --- a/config/services.php +++ b/config/services.php @@ -34,6 +34,9 @@ 'stripe' => [ 'public_key' => env('STRIPE_KEY'), 'key' => env('STRIPE_SECRET'), + 'webhooks' => [ + 'lunar' => env('STRIPE_SIGNING_SECRET'), + ] ], ]; diff --git a/resources/views/livewire/checkout-processing.blade.php b/resources/views/livewire/checkout-processing.blade.php new file mode 100644 index 00000000..c6f6d920 --- /dev/null +++ b/resources/views/livewire/checkout-processing.blade.php @@ -0,0 +1,19 @@ +
+
+
+

+ + ⏳ + + + + Processing your order... + + +

+

Behind the scenes we are checking to see if Lunar has processed the Stripe webhook...

+ +
+
+
diff --git a/resources/views/partials/checkout/payment.blade.php b/resources/views/partials/checkout/payment.blade.php index babe8a59..6fe6f366 100644 --- a/resources/views/partials/checkout/payment.blade.php +++ b/resources/views/partials/checkout/payment.blade.php @@ -31,7 +31,7 @@ @if ($paymentType == 'card') + :returnUrl="route('checkout.processing')" /> @endif @if ($paymentType == 'cash-in-hand') diff --git a/routes/web.php b/routes/web.php index 0082a394..c4b2528e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -29,4 +29,6 @@ Route::get('checkout', CheckoutPage::class)->name('checkout.view'); +Route::get('checkout/processing', \App\Livewire\CheckoutProcessing::class)->name('checkout.processing'); + Route::get('checkout/success', CheckoutSuccessPage::class)->name('checkout-success.view');