stripeCustomerId(); $this->card = $stripe->defaultPaymentMethod($customerId); if (StripePublishableKey::current() === '') { return; } $this->clientSecret = $stripe->createSetupIntent($customerId)['client_secret']; } /** * Vom Browser gerufen, nachdem Elements den SetupIntent bestätigt hat. * * Die ID kommt damit aus dem Browser und wird geprüft, bevor sie an Stripe * weitergereicht wird — sonst wäre das ein Weg, einen beliebigen Bezeichner * an das eigene Stripe-Konto zu hängen. */ public function confirmed(string $paymentMethodId): void { Validator::make( ['paymentMethodId' => $paymentMethodId], ['paymentMethodId' => ['required', 'string', 'max:255', 'regex:/^pm_[A-Za-z0-9]+$/']], )->validate(); $stripe = app(StripeClient::class); $customerId = $this->stripeCustomerId(); // Hinterlegen genügt nicht — ohne diesen Schritt bucht Stripe weiter // mit der alten Karte ab. $stripe->setDefaultPaymentMethod($customerId, $paymentMethodId); $this->card = $stripe->defaultPaymentMethod($customerId); $this->dispatch('notify', message: __('billing.card_saved')); } private function stripeCustomerId(): string { $customer = Customer::forUser(Auth::user()); abort_if($customer === null || blank($customer->stripe_customer_id), 404); return (string) $customer->stripe_customer_id; } public function render() { return view('livewire.payment-method', [ 'publishableKey' => StripePublishableKey::current(), ]); } }