Skip to content
40 changes: 40 additions & 0 deletions src/lib/sdk/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,46 @@ export class Billing {
);
}

async getOrganizationPaymentMethod(
organizationId: string,
paymentMethodId: string
): Promise<PaymentMethodData> {
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<Address> {
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<PaymentList> {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/stores/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this method used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use this on load to check if there are any expired payments

org.$id,
org.paymentMethodId
);
if (!payment?.expiryYear) return;
const year = new Date().getFullYear();
const month = new Date().getMonth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down Expand Up @@ -106,7 +108,7 @@
<PlanSummary />
<PaymentHistory />
<PaymentMethods />
<BillingAddress />
<BillingAddress billingAddress={data.billingAddress} />
<TaxId />
<BudgetCap />
{#if $organization?.billingPlan !== BillingPlan.STARTER && !!$organization?.billingBudget}
Expand Down
14 changes: 13 additions & 1 deletion src/routes/console/organization-[organization]/billing/+page.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand Down Expand Up @@ -40,10 +43,6 @@
trackError(error, Submit.OrganizationBillingAddressUpdate);
}
}

$: billingAddress = $addressList?.billingAddresses?.find(
(address) => address.$id === $organization?.billingAddressId
);
</script>

<CardGrid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down