diff --git a/src/lib/components/billing/paymentBoxes.svelte b/src/lib/components/billing/paymentBoxes.svelte index 27719cba5..5883b4460 100644 --- a/src/lib/components/billing/paymentBoxes.svelte +++ b/src/lib/components/billing/paymentBoxes.svelte @@ -4,11 +4,11 @@ import { CreditCardBrandImage } from '..'; import { initializeStripe, unmountPaymentElement } from '$lib/stores/stripe'; import { Badge, Card, Layout } from '@appwrite.io/pink-svelte'; - import type { PaymentMethodData } from '$lib/sdk/billing'; import type { PaymentMethod } from '@stripe/stripe-js'; import StatePicker from './statePicker.svelte'; + import type { Models } from '@appwrite.io/console'; - export let methods: PaymentMethodData[]; + export let methods: Array; export let group: string; export let name: string; export let defaultMethod: string = null; diff --git a/src/lib/components/billing/paymentModal.svelte b/src/lib/components/billing/paymentModal.svelte index e881cd93e..bdda244fd 100644 --- a/src/lib/components/billing/paymentModal.svelte +++ b/src/lib/components/billing/paymentModal.svelte @@ -13,19 +13,19 @@ import { addNotification } from '$lib/stores/notifications'; import { page } from '$app/state'; import { Spinner } from '@appwrite.io/pink-svelte'; - import type { PaymentMethod } from '@stripe/stripe-js'; + import type { PaymentMethod as StripePaymentMethod } from '@stripe/stripe-js'; import StatePicker from './statePicker.svelte'; - import type { PaymentMethodData } from '$lib/sdk/billing'; + import type { Models } from '@appwrite.io/console'; export let show = false; - export let onCardSubmit: ((card: PaymentMethodData) => void) | null = null; + export let onCardSubmit: ((card: Models.PaymentMethod) => void) | null = null; let modal: FakeModal; let name: string; let state: string = ''; let error: string = null; let showState: boolean = false; - let paymentMethod: PaymentMethod | null = null; + let paymentMethod: StripePaymentMethod | null = null; async function handleSubmit() { try { @@ -47,15 +47,15 @@ const card = await submitStripeCard(name, page?.params?.organization ?? null); if (card && Object.hasOwn(card, 'id')) { - if ((card as PaymentMethod).card?.country === 'US') { - paymentMethod = card as PaymentMethod; + if ((card as StripePaymentMethod).card?.country === 'US') { + paymentMethod = card as StripePaymentMethod; showState = true; return; } } modal.closeModal(); await invalidate(Dependencies.PAYMENT_METHODS); - onCardSubmit?.(card as PaymentMethodData); + onCardSubmit?.(card as Models.PaymentMethod); addNotification({ type: 'success', message: 'A new payment method has been added to your account' diff --git a/src/lib/components/billing/selectPaymentMethod.svelte b/src/lib/components/billing/selectPaymentMethod.svelte index c80aacacf..9682b9995 100644 --- a/src/lib/components/billing/selectPaymentMethod.svelte +++ b/src/lib/components/billing/selectPaymentMethod.svelte @@ -1,6 +1,5 @@ diff --git a/src/lib/sdk/billing.ts b/src/lib/sdk/billing.ts index d19589875..c52e2ed18 100644 --- a/src/lib/sdk/billing.ts +++ b/src/lib/sdk/billing.ts @@ -4,31 +4,6 @@ import type { Client, Models } from '@appwrite.io/console'; import type { PaymentMethod } from '@stripe/stripe-js'; import type { Organization, OrganizationError, OrganizationList } from '../stores/organization'; -export type PaymentMethodData = { - $id: string; - $createdAt: string; - $updatedAt: string; - providerMethodId: string; - providerUserId: string; - userId: string; - expiryMonth: number; - expiryYear: number; - expired: boolean; - last4: string; - country: string; - brand: string; - clientSecret: string; - failed: boolean; - name: string; - mandateId?: string; - lastError?: string; -}; - -export type PaymentList = { - paymentMethods: PaymentMethodData[]; - total: number; -}; - export type Invoice = { $id: string; $createdAt: Date; @@ -1002,7 +977,7 @@ export class Billing { async getOrganizationPaymentMethod( organizationId: string, paymentMethodId: string - ): Promise { + ): Promise { const path = `/organizations/${organizationId}/payment-methods/${paymentMethodId}`; const params = { organizationId, @@ -1041,7 +1016,7 @@ export class Billing { //ACCOUNT - async listPaymentMethods(queries: [] = []): Promise { + async listPaymentMethods(queries: [] = []): Promise { const path = `/account/payment-methods`; const params = { queries @@ -1057,7 +1032,7 @@ export class Billing { ); } - async getPaymentMethod(paymentMethodId: string): Promise { + async getPaymentMethod(paymentMethodId: string): Promise { const path = `/account/payment-methods/${paymentMethodId}`; const params = { paymentMethodId @@ -1073,7 +1048,7 @@ export class Billing { ); } - async createPaymentMethod(): Promise { + async createPaymentMethod(): Promise { const path = `/account/payment-methods`; const params = {}; const uri = new URL(this.client.config.endpoint + path); @@ -1092,7 +1067,7 @@ export class Billing { providerMethodId: string | PaymentMethod, name: string, state: string | undefined = undefined - ): Promise { + ): Promise { const path = `/account/payment-methods/${paymentMethodId}/provider`; const params = { paymentMethodId, @@ -1118,7 +1093,7 @@ export class Billing { paymentMethodId: string, expiryMonth: string, expiryYear: string - ): Promise { + ): Promise { const path = `/account/payment-methods/${paymentMethodId}`; const params = { paymentMethodId, @@ -1136,7 +1111,7 @@ export class Billing { ); } - async deletePaymentMethod(paymentMethodId: string): Promise { + async deletePaymentMethod(paymentMethodId: string): Promise { const path = `/account/payment-methods/${paymentMethodId}`; const params = { paymentMethodId @@ -1151,7 +1126,7 @@ export class Billing { params ); } - async setDefaultPaymentMethod(paymentMethodId: string): Promise { + async setDefaultPaymentMethod(paymentMethodId: string): Promise { const path = `/account/payment-methods/${paymentMethodId}/default`; const params = { paymentMethodId @@ -1170,7 +1145,7 @@ export class Billing { async setupPaymentMandate( organizationId: string, paymentMethodId: string - ): Promise { + ): Promise { const path = `/account/payment-methods/${paymentMethodId}/setup`; const params = { organizationId, diff --git a/src/lib/stores/billing.ts b/src/lib/stores/billing.ts index 5c7305125..60746241d 100644 --- a/src/lib/stores/billing.ts +++ b/src/lib/stores/billing.ts @@ -17,8 +17,6 @@ import type { AggregationTeam, Invoice, InvoiceList, - PaymentList, - PaymentMethodData, BillingPlansMap } from '$lib/sdk/billing'; import { isCloud } from '$lib/system'; @@ -69,7 +67,10 @@ export const roles = [ export const teamStatusReadonly = 'readonly'; export const billingLimitOutstandingInvoice = 'outstanding_invoice'; -export const paymentMethods = derived(page, ($page) => $page.data.paymentMethods as PaymentList); +export const paymentMethods = derived( + page, + ($page) => $page.data.paymentMethods as Models.PaymentMethodList +); export const addressList = derived(page, ($page) => $page.data.addressList as AddressesList); export const plansInfo = derived(page, ($page) => $page.data.plansInfo as BillingPlansMap); export const daysLeftInTrial = writable(0); @@ -538,7 +539,7 @@ export function checkForMarkedForDeletion(org: Organization) { } } -export const paymentMissingMandate = writable(null); +export const paymentMissingMandate = writable(null); export async function checkForMandate(org: Organization) { const paymentId = org.paymentMethodId ?? org.backupPaymentMethodId; diff --git a/src/lib/stores/stripe.ts b/src/lib/stores/stripe.ts index 9f4bc727b..cda0bf776 100644 --- a/src/lib/stores/stripe.ts +++ b/src/lib/stores/stripe.ts @@ -1,6 +1,6 @@ import type { Appearance, - PaymentMethod, + PaymentMethod as StripePaymentMethod, Stripe, StripeElement, StripeElements @@ -8,21 +8,21 @@ import type { import { sdk } from './sdk'; import { app } from './app'; import { get, writable } from 'svelte/store'; -import type { PaymentMethodData } from '$lib/sdk/billing'; import { Submit, trackError, trackEvent } from '$lib/actions/analytics'; import { addNotification } from './notifications'; import { organization } from './organization'; import { base } from '$app/paths'; import { ThemeDarkCloud, ThemeLightCloud } from '$themes'; import Color from 'color'; +import type { Models } from '@appwrite.io/console'; export const stripe = writable(); -let paymentMethod: PaymentMethodData; +export const isStripeInitialized = writable(false); + let clientSecret: string; let elements: StripeElements; let paymentElement: StripeElement; - -export const isStripeInitialized = writable(false); +let paymentMethod: Models.PaymentMethod; export async function initializeStripe(node: HTMLElement) { if (!get(stripe)) return; @@ -116,12 +116,12 @@ export async function submitStripeCard(name: string, organizationId?: string) { } if (setupIntent && setupIntent.status === 'succeeded') { - const pm = setupIntent.payment_method as PaymentMethod | string | undefined; + const pm = setupIntent.payment_method as StripePaymentMethod | string | undefined; // If Stripe returned an expanded PaymentMethod object, check the card country. // If it returned a string id (common), `typeof pm === 'string'` and we skip this. if (typeof pm !== 'string' && pm?.card?.country === 'US') { // need to get state - return pm as PaymentMethod; + return pm as StripePaymentMethod; } // The backend expects a provider method ID (string). Extract the id @@ -130,7 +130,7 @@ export async function submitStripeCard(name: string, organizationId?: string) { if (typeof pm === 'string') { providerId = pm; } else { - providerId = (pm as PaymentMethod)?.id; + providerId = (pm as StripePaymentMethod)?.id; } if (!providerId) { diff --git a/src/routes/(console)/account/payments/editPaymentModal.svelte b/src/routes/(console)/account/payments/editPaymentModal.svelte index 7850cdea9..5b1145f68 100644 --- a/src/routes/(console)/account/payments/editPaymentModal.svelte +++ b/src/routes/(console)/account/payments/editPaymentModal.svelte @@ -5,12 +5,12 @@ import { Dependencies } from '$lib/constants'; import { addNotification } from '$lib/stores/notifications'; import { sdk } from '$lib/stores/sdk'; - import type { PaymentMethodData } from '$lib/sdk/billing'; import { Submit, trackError, trackEvent } from '$lib/actions/analytics'; import { Alert } from '@appwrite.io/pink-svelte'; + import type { Models } from '@appwrite.io/console'; export let show = false; - export let selectedPaymentMethod: PaymentMethodData; + export let selectedPaymentMethod: Models.PaymentMethod; export let isLinked = false; const currentYear = new Date().getFullYear(); let error: string; diff --git a/src/routes/(console)/account/payments/paymentMethods.svelte b/src/routes/(console)/account/payments/paymentMethods.svelte index 8e53a8d3d..53d3b270a 100644 --- a/src/routes/(console)/account/payments/paymentMethods.svelte +++ b/src/routes/(console)/account/payments/paymentMethods.svelte @@ -2,7 +2,6 @@ import { CardGrid, CreditCardInfo, Empty } from '$lib/components'; import { Button } from '$lib/elements/forms'; import { paymentMethods } from '$lib/stores/billing'; - import type { PaymentMethodData } from '$lib/sdk/billing'; import { organizationList, type Organization } from '$lib/stores/organization'; import { base } from '$app/paths'; import EditPaymentModal from './editPaymentModal.svelte'; @@ -26,10 +25,11 @@ Tag, Typography } from '@appwrite.io/pink-svelte'; + import type { Models } from '@appwrite.io/console'; export let showPayment = false; let showDropdown = []; - let selectedMethod: PaymentMethodData; + let selectedMethod: Models.PaymentMethod; let selectedLinkedOrgs: Organization[] = []; let showDelete = false; let showEdit = false; @@ -37,9 +37,7 @@ $: orgList = $organizationList.teams as unknown as Organization[]; - $: filteredMethods = $paymentMethods?.paymentMethods.filter( - (method: PaymentMethodData) => !!method?.last4 - ); + $: filteredMethods = $paymentMethods?.paymentMethods.filter((method) => !!method?.last4); const isMethodLinkedToOrg = (methodId: string, org: Organization) => methodId === org.paymentMethodId || methodId === org.backupPaymentMethodId; diff --git a/src/routes/(console)/apply-credit/+page.svelte b/src/routes/(console)/apply-credit/+page.svelte index b7226b3e7..29b8c85df 100644 --- a/src/routes/(console)/apply-credit/+page.svelte +++ b/src/routes/(console)/apply-credit/+page.svelte @@ -8,7 +8,6 @@ import { Button, Form, InputSelect, InputTags, InputText } from '$lib/elements/forms'; import { toLocaleDate } from '$lib/helpers/date'; import { Wizard } from '$lib/layout'; - import type { PaymentList } from '$lib/sdk/billing'; import { addNotification } from '$lib/stores/notifications'; import { organizationList, @@ -48,7 +47,7 @@ let formComponent: Form; let couponForm: Form; let isSubmitting = writable(false); - let methods: PaymentList; + let methods: Models.PaymentMethodList; let paymentMethodId: string; let collaborators: string[]; let taxId: string; diff --git a/src/routes/(console)/organization-[organization]/billing/+page.svelte b/src/routes/(console)/organization-[organization]/billing/+page.svelte index 99729b8e9..271912fd0 100644 --- a/src/routes/(console)/organization-[organization]/billing/+page.svelte +++ b/src/routes/(console)/organization-[organization]/billing/+page.svelte @@ -9,7 +9,6 @@ import PaymentHistory from './paymentHistory.svelte'; import TaxId from './taxId.svelte'; import { failedInvoice, tierToPlan, upgradeURL, useNewPricingModal } from '$lib/stores/billing'; - import type { PaymentMethodData } from '$lib/sdk/billing'; import { onMount } from 'svelte'; import { page } from '$app/state'; import { confirmPayment } from '$lib/stores/stripe'; @@ -31,11 +30,11 @@ // why are these reactive? $: defaultPaymentMethod = data?.paymentMethods?.paymentMethods?.find( - (method: PaymentMethodData) => method.$id === organization?.paymentMethodId + (method) => method.$id === organization?.paymentMethodId ); $: backupPaymentMethod = data?.paymentMethods?.paymentMethods?.find( - (method: PaymentMethodData) => method.$id === organization?.backupPaymentMethodId + (method) => method.$id === organization?.backupPaymentMethodId ); onMount(async () => { diff --git a/src/routes/(console)/organization-[organization]/billing/paymentMethods.svelte b/src/routes/(console)/organization-[organization]/billing/paymentMethods.svelte index 124518f1e..6f45b4b94 100644 --- a/src/routes/(console)/organization-[organization]/billing/paymentMethods.svelte +++ b/src/routes/(console)/organization-[organization]/billing/paymentMethods.svelte @@ -8,7 +8,6 @@ import { type Organization } from '$lib/stores/organization'; import { Button } from '$lib/elements/forms'; import { hasStripePublicKey, isCloud } from '$lib/system'; - import type { PaymentList, PaymentMethodData } from '$lib/sdk/billing'; import DeleteOrgPayment from './deleteOrgPayment.svelte'; import ReplaceCard from './replaceCard.svelte'; import EditPaymentModal from '$routes/(console)/account/payments/editPaymentModal.svelte'; @@ -33,17 +32,18 @@ IconSwitchHorizontal, IconTrash } from '@appwrite.io/pink-icons-svelte'; + import type { Models } from '@appwrite.io/console'; export let organization: Organization; - export let methods: PaymentList; + export let methods: Models.PaymentMethodList; - let showPayment = false; let showEdit = false; let showDelete = false; + let showPayment = false; let showReplace = false; let isSelectedBackup = false; - let backupPaymentMethod: PaymentMethodData; - let defaultPaymentMethod: PaymentMethodData; + let backupPaymentMethod: Models.PaymentMethod; + let defaultPaymentMethod: Models.PaymentMethod; async function addPaymentMethod(paymentMethodId: string) { try { diff --git a/src/routes/(console)/organization-[organization]/billing/replaceCard.svelte b/src/routes/(console)/organization-[organization]/billing/replaceCard.svelte index ea4cc7d26..05551918f 100644 --- a/src/routes/(console)/organization-[organization]/billing/replaceCard.svelte +++ b/src/routes/(console)/organization-[organization]/billing/replaceCard.svelte @@ -7,23 +7,23 @@ import { Dependencies } from '$lib/constants'; import { setPaymentMethod, submitStripeCard } from '$lib/stores/stripe'; import { onMount } from 'svelte'; - import type { PaymentList, PaymentMethodData } from '$lib/sdk/billing'; import { addNotification } from '$lib/stores/notifications'; import { Submit, trackError, trackEvent } from '$lib/actions/analytics'; import { PaymentBoxes } from '$lib/components/billing'; - import type { PaymentMethod } from '@stripe/stripe-js'; + import type { PaymentMethod as StripePaymentMethod } from '@stripe/stripe-js'; + import type { Models } from '@appwrite.io/console'; export let organization: Organization; export let show = false; export let isBackup = false; - export let methods: PaymentList; + export let methods: Models.PaymentMethodList; let name: string; let error: string = null; let selectedPaymentMethodId: string; let showState: boolean = false; let state: string = ''; - let paymentMethod: PaymentMethod | null = null; + let paymentMethod: StripePaymentMethod | null = null; onMount(async () => { if (!organization.paymentMethodId && !organization.backupPaymentMethodId) { @@ -48,19 +48,19 @@ if (showState && !state) { throw Error('Please select a state'); } - let method: PaymentMethodData; + let method: Models.PaymentMethod; if (showState) { method = await setPaymentMethod(paymentMethod.id, name, state); } else { const card = await submitStripeCard(name, organization.$id); if (card && Object.hasOwn(card, 'id')) { - if ((card as PaymentMethod).card?.country === 'US') { - paymentMethod = card as PaymentMethod; + if ((card as StripePaymentMethod).card?.country === 'US') { + paymentMethod = card as StripePaymentMethod; showState = true; return; } } else if (card && Object.hasOwn(card, '$id')) { - method = card as PaymentMethodData; + method = card as Models.PaymentMethod; } } selectedPaymentMethodId = method.$id; diff --git a/src/routes/(console)/organization-[organization]/billing/retryPaymentModal.svelte b/src/routes/(console)/organization-[organization]/billing/retryPaymentModal.svelte index 65e217d2c..ab7384d56 100644 --- a/src/routes/(console)/organization-[organization]/billing/retryPaymentModal.svelte +++ b/src/routes/(console)/organization-[organization]/billing/retryPaymentModal.svelte @@ -3,7 +3,7 @@ import { FakeModal } from '$lib/components'; import { Button } from '$lib/elements/forms'; import { Dependencies } from '$lib/constants'; - import type { Invoice, PaymentMethodData } from '$lib/sdk/billing'; + import type { Invoice } from '$lib/sdk/billing'; import { addNotification } from '$lib/stores/notifications'; import { Submit, trackError, trackEvent } from '$lib/actions/analytics'; import { page } from '$app/state'; @@ -21,18 +21,21 @@ import { getApiEndpoint, sdk } from '$lib/stores/sdk'; import { formatCurrency } from '$lib/helpers/numbers'; import { base } from '$app/paths'; - import type { PaymentMethod } from '@stripe/stripe-js'; + import type { PaymentMethod as StripePaymentMethod } from '@stripe/stripe-js'; + import type { Models } from '@appwrite.io/console'; export let show = false; export let invoice: Invoice; - let error: string = null; - let isButtonDisabled = false; + let name: string; - let paymentMethodId: string; - let setAsDefault = false; - let showState: boolean = false; let state: string = ''; - let paymentMethod: PaymentMethod | null = null; + let error: string = null; + let setAsDefault = false; + let isButtonDisabled = false; + let paymentMethodId: string; + let showState: boolean = false; + let paymentMethod: StripePaymentMethod | null = null; + const endpoint = getApiEndpoint(); onMount(async () => { @@ -60,15 +63,15 @@ if (showState && !state) { throw Error('Please select a state'); } - let method: PaymentMethodData; + let method: Models.PaymentMethod; if (showState) { method = await setPaymentMethod(paymentMethod.id, name, state); } else { const card = await submitStripeCard(name, $organization.$id); // When Stripe returns an expanded PaymentMethod for US cards, we need state. - if (Object.hasOwn(card, 'id') && (card as PaymentMethod)?.card) { - if ((card as PaymentMethod).card?.country === 'US') { - paymentMethod = card as PaymentMethod; + if (Object.hasOwn(card, 'id') && (card as StripePaymentMethod)?.card) { + if ((card as StripePaymentMethod).card?.country === 'US') { + paymentMethod = card as StripePaymentMethod; showState = true; return; } @@ -76,7 +79,7 @@ // Otherwise, we expect an Appwrite PaymentMethodData with `$id`. if (Object.hasOwn(card, '$id')) { - method = card as PaymentMethodData; + method = card as Models.PaymentMethod; } } const card = await sdk.forConsole.billing.getPaymentMethod(method.$id); diff --git a/src/routes/(console)/organization-[organization]/billing/wizard/paymentDetails.svelte b/src/routes/(console)/organization-[organization]/billing/wizard/paymentDetails.svelte index fd27def7e..7ee8a49b6 100644 --- a/src/routes/(console)/organization-[organization]/billing/wizard/paymentDetails.svelte +++ b/src/routes/(console)/organization-[organization]/billing/wizard/paymentDetails.svelte @@ -1,7 +1,6 @@