first(); if ($paymentIntentModel && ! $paymentIntentModel->isActive()) { throw new PaymentNotConfirmedException('Payment intent already processed.'); } if (! $paymentIntentModel) { $paymentIntentModel = StripePaymentIntent::create([ 'intent_id' => $paymentIntentId, 'cart_id' => $cart->id, ]); } $paymentIntentModel->update(['processing_at' => now()]); $stripe = Stripe::getClient(); $paymentIntent = $stripe->paymentIntents->retrieve($paymentIntentId); if (! $paymentIntent) { throw new PaymentNotConfirmedException('Unable to locate payment intent.'); } $policy = config('lunar.stripe.policy', 'automatic'); if ($paymentIntent->status === PaymentIntent::STATUS_REQUIRES_CAPTURE && $policy === 'automatic') { $paymentIntent = $stripe->paymentIntents->capture($paymentIntentId); } if ($paymentIntent->status !== PaymentIntent::STATUS_SUCCEEDED) { $paymentIntentModel->update(['status' => $paymentIntent->status]); throw new PaymentNotConfirmedException( $paymentIntent->last_payment_error->message ?? "Payment intent status: {$paymentIntent->status}." ); } try { $order = $this->checkout->placeOrder($fingerprint); } catch (DisallowMultipleCartOrdersException|CartException $e) { throw new PaymentNotConfirmedException($e->getMessage(), previous: $e); } $paymentIntentModel->order_id = $order->id; $paymentIntentModel->status = $paymentIntent->status; $paymentIntentModel->processed_at = now(); $paymentIntentModel->save(); UpdateOrderFromIntent::execute($order, $paymentIntent); return $order->refresh(); } }