remove: paymentMethodList & paymentMethodData manual types.

This commit is contained in:
Darshan committed 2025-12-18 13:23:27 +05:30
1 parent c462360681
commit c3a5a3a4b2
16 files changed
+88 -112

No files matched your search

@@ -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<Models.PaymentMethod>;
export let group: string;
export let name: string;
export let defaultMethod: string = null;
@@ -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'
@@ -1,6 +1,5 @@
<script lang="ts">
import { Button, InputText } from '$lib/elements/forms';
import type { PaymentList, PaymentMethodData } from '$lib/sdk/billing';
import { hasStripePublicKey, isCloud } from '$lib/system';
import { onMount } from 'svelte';
import PaymentModal from './paymentModal.svelte';
@@ -10,15 +9,16 @@
import InputSelect from '$lib/elements/forms/inputSelect.svelte';
import { invalidate } from '$app/navigation';
import { Dependencies } from '$lib/constants';
import type { Models } from '@appwrite.io/console';
export let value: string;
export let taxId = '';
export let methods: PaymentList;
export let value: string;
export let methods: Models.PaymentMethodList;
let showTaxId = false;
let showPaymentModal = false;
async function cardSaved(card: PaymentMethodData) {
async function cardSaved(card: Models.PaymentMethod) {
value = card.$id;
if (value) {
+4 -4
View File
@@ -1,12 +1,12 @@
<script lang="ts">
import type { PaymentMethodData } from '$lib/sdk/billing';
import { Badge, Layout, Link, Popover, Table } from '@appwrite.io/pink-svelte';
import CreditCardBrandImage from './creditCardBrandImage.svelte';
import type { Models } from '@appwrite.io/console';
import type { TableRootProp } from '$lib/helpers/types';
import CreditCardBrandImage from './creditCardBrandImage.svelte';
import { Badge, Layout, Link, Popover, Table } from '@appwrite.io/pink-svelte';
export let root: TableRootProp;
export let paymentMethod: PaymentMethodData;
export let isBackup: boolean = false;
export let paymentMethod: Models.PaymentMethod;
</script>
<Table.Cell column="cc" {root}>
+9 -34
View File
@@ -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<PaymentMethodData> {
): Promise<Models.PaymentMethod> {
const path = `/organizations/${organizationId}/payment-methods/${paymentMethodId}`;
const params = {
organizationId,
@@ -1041,7 +1016,7 @@ export class Billing {
//ACCOUNT
async listPaymentMethods(queries: [] = []): Promise<PaymentList> {
async listPaymentMethods(queries: [] = []): Promise<Models.PaymentMethodList> {
const path = `/account/payment-methods`;
const params = {
queries
@@ -1057,7 +1032,7 @@ export class Billing {
);
}
async getPaymentMethod(paymentMethodId: string): Promise<PaymentMethodData> {
async getPaymentMethod(paymentMethodId: string): Promise<Models.PaymentMethod> {
const path = `/account/payment-methods/${paymentMethodId}`;
const params = {
paymentMethodId
@@ -1073,7 +1048,7 @@ export class Billing {
);
}
async createPaymentMethod(): Promise<PaymentMethodData> {
async createPaymentMethod(): Promise<Models.PaymentMethod> {
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<PaymentMethodData> {
): Promise<Models.PaymentMethod> {
const path = `/account/payment-methods/${paymentMethodId}/provider`;
const params = {
paymentMethodId,
@@ -1118,7 +1093,7 @@ export class Billing {
paymentMethodId: string,
expiryMonth: string,
expiryYear: string
): Promise<PaymentMethodData> {
): Promise<Models.PaymentMethod> {
const path = `/account/payment-methods/${paymentMethodId}`;
const params = {
paymentMethodId,
@@ -1136,7 +1111,7 @@ export class Billing {
);
}
async deletePaymentMethod(paymentMethodId: string): Promise<PaymentMethodData> {
async deletePaymentMethod(paymentMethodId: string): Promise<Models.PaymentMethod> {
const path = `/account/payment-methods/${paymentMethodId}`;
const params = {
paymentMethodId
@@ -1151,7 +1126,7 @@ export class Billing {
params
);
}
async setDefaultPaymentMethod(paymentMethodId: string): Promise<PaymentMethodData> {
async setDefaultPaymentMethod(paymentMethodId: string): Promise<Models.PaymentMethod> {
const path = `/account/payment-methods/${paymentMethodId}/default`;
const params = {
paymentMethodId
@@ -1170,7 +1145,7 @@ export class Billing {
async setupPaymentMandate(
organizationId: string,
paymentMethodId: string
): Promise<PaymentMethodData> {
): Promise<Models.PaymentMethod> {
const path = `/account/payment-methods/${paymentMethodId}/setup`;
const params = {
organizationId,
+5 -4
View File
@@ -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<number>(0);
@@ -538,7 +539,7 @@ export function checkForMarkedForDeletion(org: Organization) {
}
}
export const paymentMissingMandate = writable<PaymentMethodData>(null);
export const paymentMissingMandate = writable<Models.PaymentMethod>(null);
export async function checkForMandate(org: Organization) {
const paymentId = org.paymentMethodId ?? org.backupPaymentMethodId;
+8 -8
View File
@@ -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<Stripe>();
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) {
@@ -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;
@@ -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;
@@ -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;
@@ -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 () => {
@@ -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 {
@@ -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;
@@ -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);
@@ -1,7 +1,6 @@
<script lang="ts">
import { WizardStep } from '$lib/layout';
import { onMount } from 'svelte';
import type { PaymentList, PaymentMethodData } from '$lib/sdk/billing';
import { invalidate } from '$app/navigation';
import { Dependencies } from '$lib/constants';
import { isStripeInitialized, setPaymentMethod, submitStripeCard } from '$lib/stores/stripe';
@@ -9,13 +8,14 @@
import { PaymentBoxes } from '$lib/components/billing';
import { addCreditWizardStore } from '../store';
import { organization } from '$lib/stores/organization';
import type { PaymentMethod } from '@stripe/stripe-js';
import type { Models } from '@appwrite.io/console';
import type { PaymentMethod as StripePaymentMethod } from '@stripe/stripe-js';
let methods: PaymentList;
let name: string;
let showState: boolean = false;
let state: string = '';
let paymentMethod: PaymentMethod | null = null;
let showState: boolean = false;
let methods: Models.PaymentMethodList;
let paymentMethod: StripePaymentMethod | null = null;
onMount(async () => {
methods = await sdk.forConsole.billing.listPaymentMethods();
@@ -28,19 +28,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;
}
}
$addCreditWizardStore.paymentMethodId = method.$id;
@@ -9,7 +9,7 @@
import { Button, Form, InputSelect, InputTags, InputTextarea } from '$lib/elements/forms';
import { formatCurrency } from '$lib/helpers/numbers.js';
import { Wizard } from '$lib/layout';
import { type Coupon, type PaymentMethodData } from '$lib/sdk/billing';
import { type Coupon } from '$lib/sdk/billing';
import { isOrganization, plansInfo, tierToPlan } from '$lib/stores/billing';
import { addNotification } from '$lib/stores/notifications';
import { currentPlan, organization } from '$lib/stores/organization';
@@ -67,7 +67,8 @@
$: paymentMethodId =
data.organization.paymentMethodId ??
paymentMethods?.paymentMethods?.find((method: PaymentMethodData) => !!method?.last4)?.$id;
paymentMethods?.paymentMethods?.find((method: Models.PaymentMethod) => !!method?.last4)
?.$id;
afterNavigate(({ from }) => {
previousPage = from?.url?.pathname || previousPage;