diff --git a/src/lib/sdk/billing.ts b/src/lib/sdk/billing.ts index 60165d4536..21173b191b 100644 --- a/src/lib/sdk/billing.ts +++ b/src/lib/sdk/billing.ts @@ -743,6 +743,46 @@ export class Billing { ); } + async getOrganizationPaymentMethod( + organizationId: string, + paymentMethodId: string + ): Promise { + const path = `/organizations/${organizationId}/payment-methods/${paymentMethodId}`; + const params = { + organizationId, + paymentMethodId + }; + const uri = new URL(this.client.config.endpoint + path); + return await this.client.call( + 'GET', + uri, + { + 'content-type': 'application/json' + }, + params + ); + } + + async getOrganizationBillingAddress( + organizationId: string, + billingAddressId: string + ): Promise
{ + const path = `/organizations/${organizationId}/billing-addresses/${billingAddressId}`; + const params = { + organizationId, + billingAddressId + }; + const uri = new URL(this.client.config.endpoint + path); + return await this.client.call( + 'GET', + uri, + { + 'content-type': 'application/json' + }, + params + ); + } + //ACCOUNT async listPaymentMethods(queries: [] = []): Promise { diff --git a/src/lib/stores/billing.ts b/src/lib/stores/billing.ts index c213a7147f..760f5af76c 100644 --- a/src/lib/stores/billing.ts +++ b/src/lib/stores/billing.ts @@ -223,7 +223,10 @@ export async function checkPaymentAuthorizationRequired(org: Organization) { export async function paymentExpired(org: Organization) { if (!org?.paymentMethodId) return; - const payment = await sdk.forConsole.billing.getPaymentMethod(org.paymentMethodId); + const payment = await sdk.forConsole.billing.getOrganizationPaymentMethod( + org.$id, + org.paymentMethodId + ); if (!payment?.expiryYear) return; const year = new Date().getFullYear(); const month = new Date().getMonth(); diff --git a/src/routes/console/organization-[organization]/billing/+page.svelte b/src/routes/console/organization-[organization]/billing/+page.svelte index 379814dd17..cee7c3a1df 100644 --- a/src/routes/console/organization-[organization]/billing/+page.svelte +++ b/src/routes/console/organization-[organization]/billing/+page.svelte @@ -23,6 +23,8 @@ import RetryPaymentModal from './retryPaymentModal.svelte'; import { selectedInvoice, showRetryModal } from './store'; + export let data; + $: defaultPaymentMethod = $paymentMethods?.paymentMethods?.find( (method: PaymentMethodData) => method.$id === $organization?.paymentMethodId ); @@ -106,7 +108,7 @@ - + {#if $organization?.billingPlan !== BillingPlan.STARTER && !!$organization?.billingBudget} diff --git a/src/routes/console/organization-[organization]/billing/+page.ts b/src/routes/console/organization-[organization]/billing/+page.ts index a1eb3b6852..40f5407d09 100644 --- a/src/routes/console/organization-[organization]/billing/+page.ts +++ b/src/routes/console/organization-[organization]/billing/+page.ts @@ -1,4 +1,6 @@ import { Dependencies } from '$lib/constants'; +import type { Address } from '$lib/sdk/billing'; +import type { Organization } from '$lib/stores/organization'; import { sdk } from '$lib/stores/sdk'; import type { PageLoad } from './$types'; @@ -10,9 +12,19 @@ export const load: PageLoad = async ({ parent, depends }) => { depends(Dependencies.INVOICES); depends(Dependencies.ADDRESS); + let billingAddress: Address = null; + const billingAddressId = (organization as Organization)?.billingAddressId; + if (billingAddressId) { + billingAddress = await sdk.forConsole.billing.getOrganizationBillingAddress( + organization.$id, + billingAddressId + ); + } + return { paymentMethods: await sdk.forConsole.billing.listPaymentMethods(), addressList: await sdk.forConsole.billing.listAddresses(), - aggregationList: await sdk.forConsole.billing.listAggregation(organization.$id) + aggregationList: await sdk.forConsole.billing.listAggregation(organization.$id), + billingAddress }; }; diff --git a/src/routes/console/organization-[organization]/billing/billingAddress.svelte b/src/routes/console/organization-[organization]/billing/billingAddress.svelte index aa432851b8..06036d1248 100644 --- a/src/routes/console/organization-[organization]/billing/billingAddress.svelte +++ b/src/routes/console/organization-[organization]/billing/billingAddress.svelte @@ -5,6 +5,7 @@ import DropListItem from '$lib/components/dropListItem.svelte'; import { Dependencies } from '$lib/constants'; import { Button } from '$lib/elements/forms'; + import type { Address } from '$lib/sdk/billing'; import { addressList } from '$lib/stores/billing'; import { addNotification } from '$lib/stores/notifications'; import { organization } from '$lib/stores/organization'; @@ -13,6 +14,8 @@ import EditAddressModal from '$routes/console/account/payments/editAddressModal.svelte'; import ReplaceAddress from './replaceAddress.svelte'; + export let billingAddress: Address = null; + let showDropdown = false; let showBillingAddressDropdown = false; let showCreate = false; @@ -40,10 +43,6 @@ trackError(error, Submit.OrganizationBillingAddressUpdate); } } - - $: billingAddress = $addressList?.billingAddresses?.find( - (address) => address.$id === $organization?.billingAddressId - ); diff --git a/src/routes/console/organization-[organization]/billing/paymentMethods.svelte b/src/routes/console/organization-[organization]/billing/paymentMethods.svelte index ae44de05e3..8936783a72 100644 --- a/src/routes/console/organization-[organization]/billing/paymentMethods.svelte +++ b/src/routes/console/organization-[organization]/billing/paymentMethods.svelte @@ -77,13 +77,13 @@ $: if ($organization?.backupPaymentMethodId) { sdk.forConsole.billing - .getPaymentMethod($organization.backupPaymentMethodId) + .getOrganizationPaymentMethod($organization.$id, $organization.backupPaymentMethodId) .then((res) => (backupPaymentMethod = res)); } $: if ($organization?.paymentMethodId) { sdk.forConsole.billing - .getPaymentMethod($organization.paymentMethodId) + .getOrganizationPaymentMethod($organization.$id, $organization.paymentMethodId) .then((res) => (defaultPaymentMethod = res)); }