|
1 | 1 | <script lang="ts">
|
2 | 2 | import { WizardStep } from '$lib/layout';
|
3 | 3 | import { onMount } from 'svelte';
|
4 |
| - import type { PaymentList } from '$lib/sdk/billing'; |
| 4 | + import type { PaymentList, PaymentMethodData } from '$lib/sdk/billing'; |
5 | 5 | import { invalidate } from '$app/navigation';
|
6 | 6 | import { Dependencies } from '$lib/constants';
|
7 |
| - import { isStripeInitialized, submitStripeCard } from '$lib/stores/stripe'; |
| 7 | + import { isStripeInitialized, setPaymentMethod, submitStripeCard } from '$lib/stores/stripe'; |
8 | 8 | import { sdk } from '$lib/stores/sdk';
|
9 | 9 | import { PaymentBoxes } from '$lib/components/billing';
|
10 | 10 | import { addCreditWizardStore } from '../store';
|
11 | 11 | import { organization } from '$lib/stores/organization';
|
| 12 | + import type { PaymentMethod } from '@stripe/stripe-js'; |
12 | 13 |
|
13 | 14 | let methods: PaymentList;
|
14 | 15 | let name: string;
|
| 16 | + let showState: boolean = false; |
| 17 | + let state: string = ''; |
| 18 | + let paymentMethod: PaymentMethod | null = null; |
15 | 19 |
|
16 | 20 | onMount(async () => {
|
17 | 21 | methods = await sdk.forConsole.billing.listPaymentMethods();
|
|
21 | 25 |
|
22 | 26 | async function handleSubmit() {
|
23 | 27 | try {
|
24 |
| - const method = await submitStripeCard(name, $organization.$id); |
| 28 | + if (showState && !state) { |
| 29 | + throw Error('Please select a state'); |
| 30 | + } |
| 31 | + let method: PaymentMethodData; |
| 32 | + if (showState) { |
| 33 | + method = await setPaymentMethod(paymentMethod.id, name, state); |
| 34 | + } else { |
| 35 | + const card = await submitStripeCard(name, $organization.$id); |
| 36 | + if (card && Object.hasOwn(card, 'id')) { |
| 37 | + if ((card as PaymentMethod).card.country === 'US') { |
| 38 | + paymentMethod = card as PaymentMethod; |
| 39 | + showState = true; |
| 40 | + return; |
| 41 | + } |
| 42 | + } else if (card && Object.hasOwn(card, '$id')) { |
| 43 | + method = card as PaymentMethodData; |
| 44 | + } |
| 45 | + } |
25 | 46 | $addCreditWizardStore.paymentMethodId = method.$id;
|
26 | 47 | invalidate(Dependencies.PAYMENT_METHODS);
|
27 | 48 | } catch (e) {
|
|
45 | 66 |
|
46 | 67 | <p class="text"><b>Payment method</b></p>
|
47 | 68 | <PaymentBoxes
|
| 69 | + bind:paymentMethod |
| 70 | + bind:showState |
| 71 | + bind:state |
48 | 72 | methods={filteredMethods}
|
49 | 73 | bind:name
|
50 | 74 | bind:group={$addCreditWizardStore.paymentMethodId} />
|
|
0 commit comments