From 5ec52553a22e632fd06e1d30d611bf5d7686029a Mon Sep 17 00:00:00 2001 From: Arman Date: Tue, 9 Apr 2024 09:46:44 +0200 Subject: [PATCH 1/2] fix: replace card flow --- .../billing/replaceCard.svelte | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/routes/console/organization-[organization]/billing/replaceCard.svelte b/src/routes/console/organization-[organization]/billing/replaceCard.svelte index 639964119..92e1bd949 100644 --- a/src/routes/console/organization-[organization]/billing/replaceCard.svelte +++ b/src/routes/console/organization-[organization]/billing/replaceCard.svelte @@ -5,7 +5,7 @@ import { sdk } from '$lib/stores/sdk'; import { organization } from '$lib/stores/organization'; import { Dependencies } from '$lib/constants'; - import { submitStripeCard } from '$lib/stores/stripe'; + import { initializeStripe, isStripeInitialized, submitStripeCard } from '$lib/stores/stripe'; import { onMount } from 'svelte'; import type { PaymentList } from '$lib/sdk/billing'; import { addNotification } from '$lib/stores/notifications'; @@ -21,13 +21,19 @@ onMount(async () => { methods = await sdk.forConsole.billing.listPaymentMethods(); - - if (!$organization.paymentMethodId && !$organization.backupPaymentMethodId) { - selectedPaymentMethodId = methods?.total ? methods.paymentMethods[0].$id : null; - } selectedPaymentMethodId = isBackup ? $organization.backupPaymentMethodId : $organization.paymentMethodId; + if (!$organization.paymentMethodId && !$organization.backupPaymentMethodId) { + selectedPaymentMethodId = methods?.total ? methods.paymentMethods[0].$id : null; + } + // If the selected payment method does not belong to the current user, reset it + if ( + methods?.total && + !methods.paymentMethods.some((method) => method.$id === selectedPaymentMethodId) + ) { + selectedPaymentMethodId = null; + } }); async function handleSubmit() { @@ -82,6 +88,14 @@ $: filteredMethods = methods?.paymentMethods.filter((method) => !!method?.last4); + $: if (selectedPaymentMethodId === null && !$isStripeInitialized) { + initializeStripe(); + } + + $: if (selectedPaymentMethodId) { + isStripeInitialized.set(false); + } + $: if (!show) { selectedPaymentMethodId = null; } From 1e19bd505f12adb9de9efcab226accab3796f1d2 Mon Sep 17 00:00:00 2001 From: Arman Date: Tue, 9 Apr 2024 09:53:08 +0200 Subject: [PATCH 2/2] fix: logic --- .../billing/replaceCard.svelte | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/routes/console/organization-[organization]/billing/replaceCard.svelte b/src/routes/console/organization-[organization]/billing/replaceCard.svelte index 92e1bd949..bf0f62916 100644 --- a/src/routes/console/organization-[organization]/billing/replaceCard.svelte +++ b/src/routes/console/organization-[organization]/billing/replaceCard.svelte @@ -21,18 +21,20 @@ onMount(async () => { methods = await sdk.forConsole.billing.listPaymentMethods(); - selectedPaymentMethodId = isBackup - ? $organization.backupPaymentMethodId - : $organization.paymentMethodId; + if (!$organization.paymentMethodId && !$organization.backupPaymentMethodId) { selectedPaymentMethodId = methods?.total ? methods.paymentMethods[0].$id : null; - } - // If the selected payment method does not belong to the current user, reset it - if ( - methods?.total && - !methods.paymentMethods.some((method) => method.$id === selectedPaymentMethodId) - ) { - selectedPaymentMethodId = null; + } else { + selectedPaymentMethodId = isBackup + ? $organization.backupPaymentMethodId + : $organization.paymentMethodId; + // If the selected payment method does not belong to the current user, select the first one. + if ( + methods?.total && + !methods.paymentMethods.some((method) => method.$id === selectedPaymentMethodId) + ) { + selectedPaymentMethodId = methods.paymentMethods[0].$id; + } } });