feat: confimation payment stripe

This commit is contained in:
Arman
2023-11-24 14:00:01 +01:00
parent 6804376a28
commit 7bd20f2417
3 changed files with 56 additions and 19 deletions
+3 -2
View File
@@ -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 = {
@@ -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'
});
}
}
});
@@ -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