diff --git a/src/lib/sdk/billing.ts b/src/lib/sdk/billing.ts index 08821c7af..fd0f37c4c 100644 --- a/src/lib/sdk/billing.ts +++ b/src/lib/sdk/billing.ts @@ -37,6 +37,7 @@ export type Invoice = { to: string; status: string; dueAt: string; + clientSecret: string; }; export type InvoiceList = { @@ -145,10 +146,10 @@ export type Aggregation = { }; export type OrganizationUsage = { - bandwidth: []; + bandwidth: { date: string; value: number }[]; executions: number; storage: number; - users: []; + users: { date: string; value: number }[]; }; export type AggregationList = { diff --git a/src/routes/console/organization-[organization]/billing/+page.svelte b/src/routes/console/organization-[organization]/billing/+page.svelte index 02ea49441..0699966b5 100644 --- a/src/routes/console/organization-[organization]/billing/+page.svelte +++ b/src/routes/console/organization-[organization]/billing/+page.svelte @@ -14,8 +14,10 @@ import type { PaymentMethodData } from '$lib/sdk/billing'; import { onMount } from 'svelte'; import { page } from '$app/stores'; - import { isStripeInitialized, stripe } from '$lib/stores/stripe'; + import { initializeStripe, isStripeInitialized, stripe } from '$lib/stores/stripe'; import { base } from '$app/paths'; + import { sdk } from '$lib/stores/sdk'; + import { addNotification } from '$lib/stores/notifications'; $: defaultPaymentMethod = $paymentMethods?.paymentMethods?.find( (method: PaymentMethodData) => method.$id === $organization?.paymentMethodId @@ -27,25 +29,58 @@ onMount(async () => { if ( - $isStripeInitialized && - $page.url.searchParams.has('invoice') && - $page.url.searchParams.has('type') && - $page.url.searchParams.get('type') === 'confirmation' + ($page.url.searchParams.has('invoice') && + $page.url.searchParams.has('type') && + $page.url.searchParams.get('type') === 'confirmation') || + $page.url.searchParams.has('clientSecret') ) { + if (!$isStripeInitialized) { + await initializeStripe(); + } console.log('test'); console.log($page.url.searchParams.get('invoice')); - - const { setupIntent, error } = await $stripe.confirmCardSetup( - '{SETUP_INTENT_CLIENT_SECRET}', - { - payment_method: { - id: $organization.paymentMethodId, + try { + const invoiceId = $page.url.searchParams.get('invoice'); + const invoice = await sdk.forConsole.billing.getInvoice( + $page.params.organization, + invoiceId + ); + const { setupIntent, error } = await $stripe.confirmCardSetup( + invoice.clientSecret, + { + payment_method: $organization.paymentMethodId, return_url: `${base}/console/organization-${$organization.$id}/billing` } + ); + console.log(setupIntent); + if (error) { + console.log('Something went wrong'); } - ); - if (error) { - console.log('Something went wrong'); + if (setupIntent.status === 'succeeded') { + if (typeof setupIntent.payment_method === 'string') { + await sdk.forConsole.billing.setOrganizationPaymentMethod( + $page.params.organization, + setupIntent.payment_method + ); + } else { + await sdk.forConsole.billing.setOrganizationPaymentMethod( + $page.params.organization, + setupIntent.payment_method.id + ); + } + addNotification({ + title: 'Success', + message: 'Your payment method has been updated', + type: 'success' + }); + } + } catch (error) { + addNotification({ + title: 'Error', + message: + 'There was an error processing your payment, try again later. If the problem persists, please contact support.', + type: 'error' + }); } } }); diff --git a/src/routes/console/organization-[organization]/excesLimitModal.svelte b/src/routes/console/organization-[organization]/excesLimitModal.svelte index 90c06db66..102fa4258 100644 --- a/src/routes/console/organization-[organization]/excesLimitModal.svelte +++ b/src/routes/console/organization-[organization]/excesLimitModal.svelte @@ -30,14 +30,15 @@ }); function calculateExcess() { + const totBandwidth = usage?.bandwidth?.length > 0 ? usage.bandwidth[0].value : 0; + const totUsers = usage?.users?.length > 0 ? usage.users[0].value : 0; excess = { - bandwidth: - usage?.bandwidth?.[0] > plan.bandwidth ? usage?.bandwidth?.[0] - plan.bandwidth : 0, + bandwidth: totBandwidth > plan.bandwidth ? totBandwidth - plan.bandwidth : 0, storage: usage?.storage[0] > sizeToBytes(plan.storage, 'GB') ? usage.storage[0] - plan.storage : 0, - users: usage?.users?.[0] > plan.users ? usage?.users?.[0] - plan.users : 0, + users: totUsers > plan.users ? totUsers - plan.users : 0, executions: usage?.executions[0] > plan.executions ? usage.executions[0] - plan.executions : 0, members: members.total > plan.members ? members.total - (plan.members || Infinity) : 0