|
2 | 2 | import { FakeModal } from '$lib/components';
|
3 | 3 | import { InputText, Button } from '$lib/elements/forms';
|
4 | 4 | import { createEventDispatcher, onMount } from 'svelte';
|
5 |
| - import { initializeStripe, submitStripeCard } from '$lib/stores/stripe'; |
| 5 | + import { initializeStripe, setPaymentMethod, submitStripeCard } from '$lib/stores/stripe'; |
6 | 6 | import { invalidate } from '$app/navigation';
|
7 | 7 | import { Dependencies } from '$lib/constants';
|
8 | 8 | import { addNotification } from '$lib/stores/notifications';
|
9 | 9 | import { page } from '$app/state';
|
10 | 10 | import { Spinner } from '@appwrite.io/pink-svelte';
|
| 11 | + import type { PaymentMethod } from '@stripe/stripe-js'; |
| 12 | + import StatePicker from './statePicker.svelte'; |
11 | 13 |
|
12 | 14 | export let show = false;
|
13 | 15 |
|
|
16 | 18 | let name: string;
|
17 | 19 | let error: string;
|
18 | 20 | let modal: FakeModal;
|
| 21 | + let showState: boolean = false; |
| 22 | + let state: string = ''; |
| 23 | + let paymentMethod: PaymentMethod | null = null; |
19 | 24 |
|
20 | 25 | async function handleSubmit() {
|
21 | 26 | try {
|
| 27 | + if (showState && !state) { |
| 28 | + throw Error('Please select a state'); |
| 29 | + } |
| 30 | +
|
| 31 | + if (showState) { |
| 32 | + const card = await setPaymentMethod(paymentMethod.id, name, state); |
| 33 | + modal.closeModal(); |
| 34 | + await invalidate(Dependencies.PAYMENT_METHODS); |
| 35 | + dispatch('submit', card); |
| 36 | + addNotification({ |
| 37 | + type: 'success', |
| 38 | + message: 'A new payment method has been added to your account' |
| 39 | + }); |
| 40 | + return; |
| 41 | + } |
| 42 | +
|
22 | 43 | const card = await submitStripeCard(name, page?.params?.organization ?? null);
|
| 44 | + if (card && Object.hasOwn(card, 'id')) { |
| 45 | + if ((card as PaymentMethod).card.country === 'US') { |
| 46 | + paymentMethod = card as PaymentMethod; |
| 47 | + showState = true; |
| 48 | + return; |
| 49 | + } |
| 50 | + } |
23 | 51 | modal.closeModal();
|
24 | 52 | await invalidate(Dependencies.PAYMENT_METHODS);
|
25 | 53 | dispatch('submit', card);
|
|
73 | 101 | bind:error
|
74 | 102 | onSubmit={handleSubmit}>
|
75 | 103 | <slot />
|
76 |
| - <InputText |
77 |
| - id="name" |
78 |
| - required |
79 |
| - autofocus={true} |
80 |
| - bind:value={name} |
81 |
| - label="Cardholder name" |
82 |
| - placeholder="Cardholder name" /> |
83 |
| - |
84 |
| - <div class="aw-stripe-container" data-private> |
85 |
| - {#if isLoading} |
86 |
| - <div class="loader-element"> |
87 |
| - <Spinner /> |
| 104 | + {#if showState} |
| 105 | + <StatePicker card={paymentMethod} bind:state /> |
| 106 | + {:else} |
| 107 | + <InputText |
| 108 | + id="name" |
| 109 | + required |
| 110 | + autofocus={true} |
| 111 | + bind:value={name} |
| 112 | + label="Cardholder name" |
| 113 | + placeholder="Cardholder name" /> |
| 114 | + <div class="aw-stripe-container" data-private> |
| 115 | + {#if isLoading} |
| 116 | + <div class="loader-element"> |
| 117 | + <Spinner /> |
| 118 | + </div> |
| 119 | + {/if} |
| 120 | + <div |
| 121 | + style:display={isLoading ? 'none' : 'initial'} |
| 122 | + class="stripe-element" |
| 123 | + bind:this={element}> |
| 124 | + <!-- Stripe will create form elements here --> |
88 | 125 | </div>
|
89 |
| - {/if} |
90 |
| - |
91 |
| - <div |
92 |
| - style:display={isLoading ? 'none' : 'initial'} |
93 |
| - class="stripe-element" |
94 |
| - bind:this={element}> |
95 |
| - <!-- Stripe will create form elements here --> |
96 | 126 | </div>
|
97 |
| - </div> |
| 127 | + {/if} |
98 | 128 | <slot name="end"></slot>
|
99 | 129 | <svelte:fragment slot="footer">
|
100 | 130 | <Button secondary on:click={() => (show = false)}>Cancel</Button>
|
|
0 commit comments