From 708517a0d075ee90e7706770e388a455b5635ae5 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 28 Aug 2024 11:08:18 +0545 Subject: [PATCH] authorize payment --- .../commandCenter/searchers/organizations.ts | 2 +- src/lib/sdk/billing.ts | 4 ++-- src/lib/stores/organization.ts | 9 ++++++++ src/routes/+layout.ts | 2 +- .../console/account/organizations/+page.ts | 2 +- .../console/create-organization/+page.svelte | 21 ++++++++++++++++--- 6 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/lib/commandCenter/searchers/organizations.ts b/src/lib/commandCenter/searchers/organizations.ts index df72d77b2..e5e8d7694 100644 --- a/src/lib/commandCenter/searchers/organizations.ts +++ b/src/lib/commandCenter/searchers/organizations.ts @@ -3,7 +3,7 @@ import { sdk } from '$lib/stores/sdk'; import type { Searcher } from '../commands'; export const orgSearcher = (async (query: string) => { - const { teams } = await sdk.forConsole.teams.list(); + const { teams } = await sdk.forConsole.billing.listOrganization(); return teams .filter((organization) => organization.name.toLowerCase().includes(query.toLowerCase())) .map((organization) => { diff --git a/src/lib/sdk/billing.ts b/src/lib/sdk/billing.ts index ebe2a5557..ea278c5ba 100644 --- a/src/lib/sdk/billing.ts +++ b/src/lib/sdk/billing.ts @@ -1,5 +1,5 @@ import type { Client, Models } from '@appwrite.io/console'; -import type { Organization, OrganizationList } from '../stores/organization'; +import type { CreateOrgAuth, Organization, OrganizationList } from '../stores/organization'; import type { PaymentMethod } from '@stripe/stripe-js'; import type { Tier } from '$lib/stores/billing'; @@ -310,7 +310,7 @@ export class Billing { billingAddressId: string = undefined, couponId: string = undefined, invites: Array = [] - ): Promise { + ): Promise { const path = `/organizations`; const params = { organizationId, diff --git a/src/lib/stores/organization.ts b/src/lib/stores/organization.ts index 2beb13b03..d41cc3f54 100644 --- a/src/lib/stores/organization.ts +++ b/src/lib/stores/organization.ts @@ -3,6 +3,15 @@ import { derived, writable } from 'svelte/store'; import type { Models } from '@appwrite.io/console'; import type { Tier } from './billing'; +export type CreateOrgAuth = { + status: number; + message: string; + teamId: string; + invoiceId: string; + clientSecret: string; + type: string; +}; + export type Organization = Models.Team> & { billingBudget: number; billingPlan: Tier; diff --git a/src/routes/+layout.ts b/src/routes/+layout.ts index 85ad4e83d..5f0472229 100644 --- a/src/routes/+layout.ts +++ b/src/routes/+layout.ts @@ -22,7 +22,7 @@ export const load: LayoutLoad = async ({ depends, url }) => { return { account, - organizations: await sdk.forConsole.teams.list() + organizations: await sdk.forConsole.billing.listOrganization() }; } catch (error) { const acceptedRoutes = [ diff --git a/src/routes/console/account/organizations/+page.ts b/src/routes/console/account/organizations/+page.ts index 0b89b1d30..3f21115b8 100644 --- a/src/routes/console/account/organizations/+page.ts +++ b/src/routes/console/account/organizations/+page.ts @@ -12,7 +12,7 @@ export const load: PageLoad = async ({ url, route }) => { return { offset, limit, - organizations: await sdk.forConsole.teams.list([ + organizations: await sdk.forConsole.billing.listOrganization([ Query.offset(offset), Query.limit(limit), Query.orderDesc('') diff --git a/src/routes/console/create-organization/+page.svelte b/src/routes/console/create-organization/+page.svelte index 1b9dd8b45..3bcbae041 100644 --- a/src/routes/console/create-organization/+page.svelte +++ b/src/routes/console/create-organization/+page.svelte @@ -22,8 +22,13 @@ import type { Coupon, PaymentList } from '$lib/sdk/billing'; import { plansInfo, tierFree, tierPro, tierToPlan } from '$lib/stores/billing'; import { addNotification } from '$lib/stores/notifications'; - import { organizationList, type Organization } from '$lib/stores/organization'; + import { + organizationList, + type CreateOrgAuth, + type Organization + } from '$lib/stores/organization'; import { sdk } from '$lib/stores/sdk'; + import { confirmPayment } from '$lib/stores/stripe'; import { ID } from '@appwrite.io/console'; import { onMount } from 'svelte'; import { writable } from 'svelte/store'; @@ -90,9 +95,13 @@ paymentMethodId = methods.paymentMethods.find((method) => !!method?.last4)?.$id ?? null; } + function isOrganization(org: Organization | CreateOrgAuth): org is Organization { + return (org as Organization).$id !== undefined; + } + async function create() { try { - let org: Organization; + let org: Organization | CreateOrgAuth; if (billingPlan === BillingPlan.FREE) { org = await sdk.forConsole.billing.createOrganization( @@ -113,8 +122,14 @@ collaborators ); + if (!isOrganization(org) && org.status == 402) { + let clientSecret = org.clientSecret; + await confirmPayment('', clientSecret, paymentMethodId); + // throw new Error('Payment requires authentication'); + } + //Add budget - if (billingBudget) { + if (billingBudget && isOrganization(org)) { await sdk.forConsole.billing.updateBudget(org.$id, billingBudget, [75]); }