Skip to content

Commit c9d9d7c

Browse files
committed
fix retry modal
1 parent 1c63575 commit c9d9d7c

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/routes/(console)/organization-[organization]/billing/retryPaymentModal.svelte

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
import { FakeModal } from '$lib/components';
44
import { Button } from '$lib/elements/forms';
55
import { Dependencies } from '$lib/constants';
6-
import type { Invoice } from '$lib/sdk/billing';
6+
import type { Invoice, PaymentMethodData } from '$lib/sdk/billing';
77
import { addNotification } from '$lib/stores/notifications';
88
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
99
import { page } from '$app/state';
10-
import { confirmPayment, isStripeInitialized, submitStripeCard } from '$lib/stores/stripe';
10+
import {
11+
confirmPayment,
12+
isStripeInitialized,
13+
setPaymentMethod,
14+
submitStripeCard
15+
} from '$lib/stores/stripe';
1116
import { organization } from '$lib/stores/organization';
1217
import { toLocaleDate } from '$lib/helpers/date';
1318
import { PaymentBoxes } from '$lib/components/billing';
@@ -16,6 +21,7 @@
1621
import { getApiEndpoint, sdk } from '$lib/stores/sdk';
1722
import { formatCurrency } from '$lib/helpers/numbers';
1823
import { base } from '$app/paths';
24+
import type { PaymentMethod } from '@stripe/stripe-js';
1925
2026
export let show = false;
2127
export let invoice: Invoice;
@@ -24,6 +30,9 @@
2430
let name: string;
2531
let paymentMethodId: string;
2632
let setAsDefault = false;
33+
let showState: boolean = false;
34+
let state: string = '';
35+
let paymentMethod: PaymentMethod | null = null;
2736
const endpoint = getApiEndpoint();
2837
2938
onMount(async () => {
@@ -48,7 +57,24 @@
4857
try {
4958
if (paymentMethodId === null) {
5059
try {
51-
const method = await submitStripeCard(name, $organization.$id);
60+
if (showState && !state) {
61+
throw Error('Please select a state');
62+
}
63+
let method: PaymentMethodData;
64+
if (showState) {
65+
method = await setPaymentMethod(paymentMethod.id, name, state);
66+
} else {
67+
const card = await submitStripeCard(name, $organization.$id);
68+
if (card && Object.hasOwn(card, 'id')) {
69+
if ((card as PaymentMethod).card.country === 'US') {
70+
paymentMethod = card as PaymentMethod;
71+
showState = true;
72+
return;
73+
}
74+
} else if (card && Object.hasOwn(card, '$id')) {
75+
method = card as PaymentMethodData;
76+
}
77+
}
5278
const card = await sdk.forConsole.billing.getPaymentMethod(method.$id);
5379
if (card?.last4) {
5480
paymentMethodId = card.$id;
@@ -131,6 +157,9 @@
131157
</Button>
132158

133159
<PaymentBoxes
160+
bind:paymentMethod
161+
bind:showState
162+
bind:state
134163
methods={filteredMethods}
135164
defaultMethod={$organization?.paymentMethodId}
136165
backupMethod={$organization?.backupPaymentMethodId}

0 commit comments

Comments
 (0)