|
3 | 3 | import { FakeModal } from '$lib/components';
|
4 | 4 | import { Button } from '$lib/elements/forms';
|
5 | 5 | import { Dependencies } from '$lib/constants';
|
6 |
| - import type { Invoice } from '$lib/sdk/billing'; |
| 6 | + import type { Invoice, PaymentMethodData } from '$lib/sdk/billing'; |
7 | 7 | import { addNotification } from '$lib/stores/notifications';
|
8 | 8 | import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
|
9 | 9 | 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'; |
11 | 16 | import { organization } from '$lib/stores/organization';
|
12 | 17 | import { toLocaleDate } from '$lib/helpers/date';
|
13 | 18 | import { PaymentBoxes } from '$lib/components/billing';
|
|
16 | 21 | import { getApiEndpoint, sdk } from '$lib/stores/sdk';
|
17 | 22 | import { formatCurrency } from '$lib/helpers/numbers';
|
18 | 23 | import { base } from '$app/paths';
|
| 24 | + import type { PaymentMethod } from '@stripe/stripe-js'; |
19 | 25 |
|
20 | 26 | export let show = false;
|
21 | 27 | export let invoice: Invoice;
|
|
24 | 30 | let name: string;
|
25 | 31 | let paymentMethodId: string;
|
26 | 32 | let setAsDefault = false;
|
| 33 | + let showState: boolean = false; |
| 34 | + let state: string = ''; |
| 35 | + let paymentMethod: PaymentMethod | null = null; |
27 | 36 | const endpoint = getApiEndpoint();
|
28 | 37 |
|
29 | 38 | onMount(async () => {
|
|
48 | 57 | try {
|
49 | 58 | if (paymentMethodId === null) {
|
50 | 59 | 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 | + } |
52 | 78 | const card = await sdk.forConsole.billing.getPaymentMethod(method.$id);
|
53 | 79 | if (card?.last4) {
|
54 | 80 | paymentMethodId = card.$id;
|
|
131 | 157 | </Button>
|
132 | 158 |
|
133 | 159 | <PaymentBoxes
|
| 160 | + bind:paymentMethod |
| 161 | + bind:showState |
| 162 | + bind:state |
134 | 163 | methods={filteredMethods}
|
135 | 164 | defaultMethod={$organization?.paymentMethodId}
|
136 | 165 | backupMethod={$organization?.backupPaymentMethodId}
|
|
0 commit comments