feat: more stripe work -.-

This commit is contained in:
Arman committed 2023-07-25 18:54:26 +02:00
1 parent e629a2b654
commit 2d44aa9044
7 files changed
+145 -39

No files matched your search

+44 -5
View File
@@ -49,10 +49,13 @@ export async function initializeStripe() {
paymentElement.mount('#payment-element');
} catch (e) {
console.log(e);
throw e;
}
}
export async function submitStripeCard(name: string) {
// TODO: fix redirect
export async function submitStripeCard(name?: string) {
try {
// If a payment method was during initialization, use it, otherwise create a new one
if (!paymentMethod) {
@@ -77,7 +80,41 @@ export async function submitStripeCard(name: string) {
redirect: 'if_required'
});
// console.log(setupIntent);
if (setupIntent && setupIntent.status === 'succeeded') {
const method = await sdk.forConsole.billing.updatePaymentMethod(
paymentMethod.$id,
setupIntent.payment_method
);
paymentElement.destroy();
return method;
} else {
console.log('something went wrong');
throw new Error('Something went wrong');
}
} catch (e) {
console.log(e);
throw e;
}
}
// TODO: fix update
export async function updateStripeCard(selectedPaymentMethod?: string) {
try {
paymentMethod = await sdk.forConsole.billing.getPaymentMethod(selectedPaymentMethod);
clientSecret = paymentMethod.clientSecret;
// Element needs to be submitted before confirming the setup intent
elements.submit();
const { setupIntent } = await stripe.confirmSetup({
elements,
clientSecret,
confirmParams: {
return_url: 'https://example.com'
},
redirect: 'if_required'
});
if (setupIntent && setupIntent.status === 'succeeded') {
const method = await sdk.forConsole.billing.updatePaymentMethod(
@@ -86,10 +123,12 @@ export async function submitStripeCard(name: string) {
);
paymentElement.destroy();
return method;
} else console.log('something went wrong');
} else {
console.log('something went wrong');
throw new Error('Something went wrong');
}
} catch (e) {
console.log(e);
console.log(e.message);
// trackError(StripeError, Submit.ProjectCreate);
throw e;
}
}
@@ -1,10 +1,8 @@
<script lang="ts">
import { Heading } from '$lib/components';
import { Container } from '$lib/layout';
import { hasStripePublicKey, isCloud } from '$lib/system';
import BillingAddress from './billingAddress.svelte';
import PaymentMethods from './paymentMethods.svelte';
import PaymentModal from './paymentModal.svelte';
// export let data;
let showPayment = false;
@@ -17,7 +15,3 @@
<PaymentMethods bind:showPayment />
<BillingAddress />
</Container>
{#if showPayment && isCloud && hasStripePublicKey}
<PaymentModal bind:show={showPayment} />
{/if}
@@ -5,8 +5,10 @@
import { Dependencies } from '$lib/constants';
import { Button } from '$lib/elements/forms';
import { addNotification } from '$lib/stores/notifications';
import type { Organization } from '$lib/stores/organization';
import { sdk } from '$lib/stores/sdk';
export let linkedOrgs: Organization[] = [];
export let showDelete = false;
export let method: string;
@@ -36,11 +38,28 @@
icon="exclamation"
state="warning"
headerDivider={false}>
<svelte:fragment slot="header">Delete payment method</svelte:fragment>
<p class="text">Are you sure you want to delete this payment method from your account?</p>
<svelte:fragment slot="header"
>{linkedOrgs.length
? 'Unable to delete payment method'
: 'Delete payment method'}</svelte:fragment>
{#if linkedOrgs.length === 1}
<p class="text">
This payment method is set as the default for the <span class="u-bold"
>{linkedOrgs[0].name}</span
>. As it has upcoming invoices it cannot be deleted from your account.
</p>
{:else if linkedOrgs.length > 1}
<p class="text">
This payment method is set as the default for the following organisations. As they have
upcoming invoices it cannot be deleted from your account.
</p>
<ul class="list" />
{:else}
<p class="text">Are you sure you want to delete this payment method from your account?</p>
{/if}
<svelte:fragment slot="footer">
<Button text on:click={() => (showDelete = false)}>Cancel</Button>
<Button secondary submit>Delete</Button>
<Button secondary submit disabled={!!linkedOrgs.length}>Delete</Button>
</svelte:fragment>
</Modal>
@@ -0,0 +1,45 @@
<script lang="ts">
// import { Submit, trackEvent, trackError } from '$lib/actions/analytics';
import { Modal } from '$lib/components';
import { Button, FormList } from '$lib/elements/forms';
import { onMount } from 'svelte';
import { initializeStripe, updateStripeCard } from '$lib/stores/stripe';
import { invalidate } from '$app/navigation';
import { Dependencies } from '$lib/constants';
import { addNotification } from '$lib/stores/notifications';
export let show = false;
export let selectedPaymentMethod: string;
let error: string;
onMount(async () => {
await initializeStripe();
});
async function handleSubmit() {
try {
await updateStripeCard(selectedPaymentMethod);
show = false;
addNotification({
type: 'success',
message: 'Your payment method has been updated'
});
invalidate(Dependencies.PAYMENT_METHODS);
} catch (e) {
error = e.message;
}
}
</script>
<Modal bind:error onSubmit={handleSubmit} size="big" bind:show>
<svelte:fragment slot="header">Update payment method</svelte:fragment>
<FormList>
<div id="payment-element">
<!-- Elements will create form elements here -->
</div>
</FormList>
<svelte:fragment slot="footer">
<Button secondary on:click={() => (show = false)}>Cancel</Button>
<Button submit>Save</Button>
</svelte:fragment>
</Modal>
@@ -17,19 +17,22 @@
TableHeader,
TableRow
} from '$lib/elements/table';
import DeletePayment from './deletePayment.svelte';
import { paymentMethods } from './store';
import type { PaymentMethodData } from '$lib/sdk/billing';
import { organizationList, type Organization } from '$lib/stores/organization';
import { tooltip } from '$lib/actions/tooltip';
import { base } from '$app/paths';
import EditPaymentModal from './editPaymentModal.svelte';
import DeletePaymentModal from './deletePaymentModal.svelte';
import { hasStripePublicKey, isCloud } from '$lib/system';
import PaymentModal from './paymentModal.svelte';
export let showPayment = false;
let showDropdown = [];
let selectedMethod: string;
let selectedLinkedOrgs: Organization[] = [];
let showDelete = false;
console.log($paymentMethods);
let showEdit = false;
$: orgList = $organizationList.teams as unknown as Organization[];
@@ -79,22 +82,6 @@
)
.join('')}
</div>`
// onShow(instance) {
// if (isFetching || data) {
// return;
// }
// getData(role)
// .then((n) => {
// data = n;
// })
// .finally(() => {
// tick().then(() => {
// instance.setContent(content);
// });
// });
// }
// }
}}>
<Pill button>
<span class="icon-info" /> linked to organization
@@ -117,7 +104,9 @@
<DropListItem
icon="pencil"
on:click={() => {
console.log('test');
showEdit = true;
showDropdown[i] = false;
selectedMethod = paymentMethod.$id;
}}>
Edit
</DropListItem>
@@ -125,6 +114,7 @@
icon="trash"
on:click={() => {
selectedMethod = paymentMethod.$id;
selectedLinkedOrgs = linkedOrgs;
showDelete = true;
showDropdown[i] = false;
}}>
@@ -151,6 +141,13 @@
</svelte:fragment>
</CardGrid>
{#if showPayment && isCloud && hasStripePublicKey}
<PaymentModal bind:show={showPayment} />
{/if}
{#if showEdit && isCloud && hasStripePublicKey}
<EditPaymentModal selectedPaymentMethod={selectedMethod} bind:show={showEdit} />
{/if}
{#if selectedMethod}
<DeletePayment method={selectedMethod} bind:showDelete />
<DeletePaymentModal method={selectedMethod} bind:showDelete linkedOrgs={selectedLinkedOrgs} />
{/if}
@@ -6,6 +6,7 @@
import { initializeStripe, submitStripeCard } from '$lib/stores/stripe';
import { invalidate } from '$app/navigation';
import { Dependencies } from '$lib/constants';
import { addNotification } from '$lib/stores/notifications';
export let show = false;
@@ -21,6 +22,10 @@
await submitStripeCard(name);
show = false;
invalidate(Dependencies.PAYMENT_METHODS);
addNotification({
type: 'success',
message: 'A new payment method has been added to your account'
});
} catch (e) {
error = e.message;
console.log(e.message);
@@ -29,7 +34,7 @@
</script>
<Modal bind:error onSubmit={handleSubmit} size="big" bind:show>
<svelte:fragment slot="header">Add Payment Method</svelte:fragment>
<svelte:fragment slot="header">Add payment method</svelte:fragment>
<FormList>
<InputText
id="name"
@@ -85,11 +85,18 @@
</TableHeader>
<TableBody>
{#each $invoiceList?.invoices as invoice, i}
{@const status = invoice.status}
<TableRow>
<TableCellText title="date"
>{toLocaleDate(invoice.dueAt)}</TableCellText>
<TableCellText title="date">
{toLocaleDate(invoice.dueAt)}
</TableCellText>
<TableCell title="status">
<Pill>{invoice.status}</Pill>
<Pill
danger={status === 'overdue'}
success={status === 'paid'}
warning={status === 'pending'}>
{status}
</Pill>
</TableCell>
<TableCellText title="due">{invoice.amount}</TableCellText>
<TableCellText title="invoice number">