Skip to content

Commit 16b0fe3

Browse files
committed
Fix replace card
1 parent b7e2e44 commit 16b0fe3

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

src/lib/components/billing/paymentBoxes.svelte

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import { initializeStripe, unmountPaymentElement } from '$lib/stores/stripe';
66
import { Badge, Card, Layout } from '@appwrite.io/pink-svelte';
77
import type { PaymentMethodData } from '$lib/sdk/billing';
8+
import type { PaymentMethod } from '@stripe/stripe-js';
9+
import StatePicker from './statePicker.svelte';
810
911
export let methods: PaymentMethodData[];
1012
export let group: string;
@@ -14,6 +16,9 @@
1416
export let disabledCondition: string = null;
1517
export let setAsDefault = false;
1618
export let showSetAsDefault = false;
19+
export let showState = false;
20+
export let paymentMethod: PaymentMethod | null = null;
21+
export let state: string = '';
1722
1823
let element: HTMLDivElement;
1924
let loader: HTMLDivElement;
@@ -79,25 +84,29 @@
7984
{/each}
8085
<Card.Selector title="Add new payment method" name="$new" bind:group value="$new" />
8186
{#if group === '$new'}
82-
<InputText
83-
id="name"
84-
label="Cardholder name"
85-
placeholder="Cardholder name"
86-
bind:value={name}
87-
required
88-
autofocus={true} />
87+
{#if showState}
88+
<StatePicker card={paymentMethod} bind:state />
89+
{:else}
90+
<InputText
91+
id="name"
92+
label="Cardholder name"
93+
placeholder="Cardholder name"
94+
bind:value={name}
95+
required
96+
autofocus={true} />
8997

90-
<div class="aw-stripe-container" data-private>
91-
<div class="loader-container" bind:this={loader}>
92-
<div class="loader"></div>
98+
<div class="aw-stripe-container" data-private>
99+
<div class="loader-container" bind:this={loader}>
100+
<div class="loader"></div>
101+
</div>
102+
<div bind:this={element}></div>
93103
</div>
94-
<div bind:this={element}></div>
95-
</div>
96-
{#if showSetAsDefault}
97-
<InputChoice
98-
bind:value={setAsDefault}
99-
id="default"
100-
label="Set as default payment method for this organization" />
104+
{#if showSetAsDefault}
105+
<InputChoice
106+
bind:value={setAsDefault}
107+
id="default"
108+
label="Set as default payment method for this organization" />
109+
{/if}
101110
{/if}
102111
{/if}
103112
</Layout.Stack>

src/lib/components/billing/statePicker.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { states } from './state.ts';
2+
import { states } from './state';
33
import { InputSelect } from '$lib/elements/forms';
44
import type { PaymentMethod } from '@stripe/stripe-js';
55
import { Alert, Card, Layout, Typography } from '@appwrite.io/pink-svelte';

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import { sdk } from '$lib/stores/sdk';
66
import type { Organization } from '$lib/stores/organization';
77
import { Dependencies } from '$lib/constants';
8-
import { submitStripeCard } from '$lib/stores/stripe';
8+
import { setPaymentMethod, submitStripeCard } from '$lib/stores/stripe';
99
import { onMount } from 'svelte';
10-
import type { PaymentList } from '$lib/sdk/billing';
10+
import type { PaymentList, PaymentMethodData } from '$lib/sdk/billing';
1111
import { addNotification } from '$lib/stores/notifications';
1212
import { Submit, trackError, trackEvent } from '$lib/actions/analytics';
1313
import { PaymentBoxes } from '$lib/components/billing';
14+
import type { PaymentMethod } from '@stripe/stripe-js';
1415
1516
export let organization: Organization;
1617
export let show = false;
@@ -20,6 +21,9 @@
2021
let name: string;
2122
let error: string;
2223
let selectedPaymentMethodId: string;
24+
let showState: boolean = false;
25+
let state: string = '';
26+
let paymentMethod: PaymentMethod | null = null;
2327
2428
onMount(async () => {
2529
if (!organization.paymentMethodId && !organization.backupPaymentMethodId) {
@@ -41,7 +45,24 @@
4145
async function handleSubmit() {
4246
try {
4347
if (selectedPaymentMethodId === '$new') {
44-
const method = await submitStripeCard(name, organization.$id);
48+
if (showState && !state) {
49+
throw Error('Please select a state');
50+
}
51+
let method: PaymentMethodData;
52+
if (showState) {
53+
method = await setPaymentMethod(paymentMethod.id, name, state);
54+
} else {
55+
const card = await submitStripeCard(name, organization.$id);
56+
if (card && Object.hasOwn(card, 'id')) {
57+
if ((card as PaymentMethod).card.country === 'US') {
58+
paymentMethod = card as PaymentMethod;
59+
showState = true;
60+
return;
61+
}
62+
} else if (card && Object.hasOwn(card, '$id')) {
63+
method = card as PaymentMethodData;
64+
}
65+
}
4566
selectedPaymentMethodId = method.$id;
4667
}
4768
isBackup
@@ -97,6 +118,9 @@
97118
<PaymentBoxes
98119
methods={filteredMethods}
99120
bind:name
121+
bind:paymentMethod
122+
bind:showState
123+
bind:state
100124
bind:group={selectedPaymentMethodId}
101125
defaultMethod={organization?.paymentMethodId}
102126
backupMethod={organization?.backupPaymentMethodId}

0 commit comments

Comments
 (0)