diff --git a/src/lib/sdk/billing.ts b/src/lib/sdk/billing.ts index 84ebc5c72..45fa6c555 100644 --- a/src/lib/sdk/billing.ts +++ b/src/lib/sdk/billing.ts @@ -4,14 +4,18 @@ import type { PaymentMethod } from '@stripe/stripe-js'; export type PaymentMethodData = { $id: string; + $createdAt: string; + $updatedAt: string; providerMethodId: string; + providerUserId: string; expiryMonth: number; expiryYear: number; + expired: boolean; last4: string; country: string; brand: string; - clientSecret?: string; - name?: string; + clientSecret: string; + failed: boolean; }; export type PaymentList = { @@ -127,6 +131,20 @@ export type AggregationList = { aggregations: Aggregation[]; total: number; }; + +export type Region = { + $id: string; + name: string; + disabled: boolean; + default: boolean; + flag: string; +}; + +export type RegionList = { + regions: Region[]; + total: number; +}; + export class Billing { client: Client; @@ -500,23 +518,17 @@ export class Billing { ); } - // async listPaymentMethods(teamId: string) { - // const path = `/teams/${teamId}/payment-methods`; - // const uri = new URL(this.client.config.endpoint + path); - // return await this.client.call('GET', uri); - // } - - // async createPaymentMethod(teamId: string): Promise { - // const path = `/teams/${teamId}/payment-methods`; - // const params = {}; - // const uri = new URL(this.client.config.endpoint + path); - // return await this.client.call( - // 'POST', - // uri, - // { - // 'content-type': 'application/json' - // }, - // params - // ); - // } + async listRegions(): Promise { + const path = `/console/regions`; + const params = {}; + const uri = new URL(this.client.config.endpoint + path); + return await this.client.call( + 'GET', + uri, + { + 'content-type': 'application/json' + }, + params + ); + } } diff --git a/src/lib/stores/stripe.ts b/src/lib/stores/stripe.ts index 7e831378f..586178a65 100644 --- a/src/lib/stores/stripe.ts +++ b/src/lib/stores/stripe.ts @@ -1,5 +1,10 @@ import { hasStripePublicKey, VARS } from '$lib/system'; -import { loadStripe, type Stripe, type StripeCardElement } from '@stripe/stripe-js'; +import { + loadStripe, + type Stripe, + type StripeElement, + type StripeElements +} from '@stripe/stripe-js'; import { sdk } from './sdk'; import { app } from './app'; import { get, writable } from 'svelte/store'; @@ -9,7 +14,8 @@ import type { PaymentMethodData } from '$lib/sdk/billing'; let stripe: Stripe; let paymentMethod: PaymentMethodData; let clientSecret: string; -let paymentElement: StripeCardElement; +let elements: StripeElements; +let paymentElement: StripeElement; export const isStripeInitialized = writable(false); @@ -38,8 +44,8 @@ export async function initializeStripe() { appearance: get(app).themeInUse === 'dark' ? apperanceDark : apperanceLight }; // Set up Elements and then create form - const elements = stripe.elements(options); - paymentElement = elements.create('card'); + elements = stripe.elements(options); + paymentElement = elements.create('payment'); paymentElement.mount('#payment-element'); } catch (e) { console.log(e); @@ -53,17 +59,26 @@ export async function submitStripeCard(name: string) { paymentMethod = await sdk.forConsole.billing.createPaymentMethod(); clientSecret = paymentMethod.clientSecret; } - await stripe.retrieveSetupIntent(clientSecret); - // Confirm the setup intent with the payment method - const { setupIntent } = await stripe.confirmCardSetup(clientSecret, { - payment_method: { - card: paymentElement, - billing_details: { - name: name + + // Element needs to be submitted before confirming the setup intent + elements.submit(); + + const { setupIntent } = await stripe.confirmSetup({ + elements, + clientSecret, + confirmParams: { + return_url: 'https://example.com', + payment_method_data: { + billing_details: { + name + } } - } + }, + redirect: 'if_required' }); + // console.log(setupIntent); + if (setupIntent && setupIntent.status === 'succeeded') { const method = await sdk.forConsole.billing.updatePaymentMethod( paymentMethod.$id, diff --git a/src/routes/console/account/payments/paymentMethods.svelte b/src/routes/console/account/payments/paymentMethods.svelte index c703feeeb..5d400bbfd 100644 --- a/src/routes/console/account/payments/paymentMethods.svelte +++ b/src/routes/console/account/payments/paymentMethods.svelte @@ -19,6 +19,10 @@ } from '$lib/elements/table'; import DeletePayment from './deletePayment.svelte'; import { paymentMethods } from './store'; + import type { PaymentMethodData } from '$lib/sdk/billing'; + import { organizationList, type Organization } from '$lib/stores/organization'; + import { tooltip } from '$lib/actions/tooltip'; + import { base } from '$app/paths'; export let showPayment = false; let showDropdown = []; @@ -27,7 +31,11 @@ console.log($paymentMethods); - $: filteredMethods = $paymentMethods?.paymentMethods.filter((method) => !!method?.last4); + $: orgList = $organizationList.teams as unknown as Organization[]; + + $: filteredMethods = $paymentMethods?.paymentMethods.filter( + (method: PaymentMethodData) => !!method?.last4 + ); @@ -38,18 +46,61 @@ created.

- {#if $paymentMethods.total > 0} + {#if $paymentMethods?.total} Payment methods {#each filteredMethods as paymentMethod, i} + {@const linkedOrgs = orgList?.filter( + (org) => + paymentMethod.$id === org.paymentMethodId || + paymentMethod.$id === org.backupPaymentMethodId + )} + -
- test +
+ {#if linkedOrgs?.length > 0} +
+

This payment method is linked to the following organizations:

+ ${linkedOrgs + .map( + (org) => + `${org.name}` + ) + .join('')} +
` + + // onShow(instance) { + // if (isFetching || data) { + // return; + // } + // getData(role) + // .then((n) => { + // data = n; + // }) + // .finally(() => { + // tick().then(() => { + // instance.setContent(content); + // }); + // }); + // } + // } + }}> + + linked to organization + +
+ {/if} - - Plan summary +{#if $organization} + + Plan summary -

- Your current project plan. For more information on Appwrite plans, -

- -
-
-
- {currentTier.name} -
- {#if $organization.billingPlan !== 'tier-0' && $organization.billingTrialDays} - FREE TRIAL - {/if} +

+ Your current project plan. For more information on Appwrite plans, +

+ +
+
+
+ {currentTier.name} +
+ {#if $organization.billingPlan !== 'tier-0' && $organization.billingTrialDays} + FREE TRIAL + {/if} +
+ +

+ {#if $organization.billingPlan !== 'tier-0' && $organization.billingTrialDays} + Esimated total: + {/if} + ${currentTier.price} +

- -

- {#if $organization.billingPlan !== 'tier-0' && $organization.billingTrialDays} - Esimated total: - {/if} - ${currentTier.price} -

-
-
-

- Billing period: {toLocaleDate($organization.billingCurrentInvoiceDate)} - {toLocaleDate( - $organization.billingNextInvoiceDate - )} -

-
- - - - - - - +
+

+ Billing period: {toLocaleDate($organization?.billingCurrentInvoiceDate)} - {toLocaleDate( + $organization?.billingNextInvoiceDate + )} +

+
+ + + + + + + +{/if} diff --git a/src/routes/console/organization-[organization]/wizard/step2.svelte b/src/routes/console/organization-[organization]/wizard/step2.svelte index 67fec33be..bb4b54fab 100644 --- a/src/routes/console/organization-[organization]/wizard/step2.svelte +++ b/src/routes/console/organization-[organization]/wizard/step2.svelte @@ -3,27 +3,14 @@ import { Pill } from '$lib/elements'; import { WizardStep } from '$lib/layout'; import { sdk } from '$lib/stores/sdk'; + import { onMount } from 'svelte'; import { createProject } from './store'; + import type { RegionList } from '$lib/sdk/billing'; - const regions = [ - { name: 'eu-central-1', label: 'EU (Frankfurt)', flag: 'de', disabled: false }, - { name: 'eu-west-1', label: 'EU (Ireland)', flag: 'ie', disabled: true }, - { name: 'eu-west-2', label: 'EU (London)', flag: 'gb', disabled: true }, - { name: 'eu-west-3', label: 'EU (Paris)', flag: 'fr', disabled: true }, - { name: 'eu-north-1', label: 'EU (Stockholm)', flag: 'se', disabled: true }, - { name: 'eu-south-1', label: 'EU (Milan)', flag: 'it', disabled: true }, - { name: 'us-east-1', label: 'US (Virginia)', flag: 'us', disabled: true }, - { name: 'us-east-2', label: 'US (Ohio)', flag: 'us', disabled: true }, - { name: 'us-west-1', label: 'US (N. California)', flag: 'us', disabled: true }, - { name: 'us-west-2', label: 'US (Oregon)', flag: 'us', disabled: true }, - { name: 'ap-northeast-1', label: 'Asia Pacific (Tokyo)', flag: 'jp', disabled: true }, - { name: 'ap-northeast-2', label: 'Asia Pacific (Seoul)', flag: 'kr', disabled: true }, - { name: 'ap-northeast-3', label: 'Asia Pacific (Osaka-Local)', flag: 'jp', disabled: true }, - { name: 'ap-southeast-1', label: 'Asia Pacific (Singapore)', flag: 'sg', disabled: true }, - { name: 'ap-southeast-2', label: 'Asia Pacific (Sydney)', flag: 'au', disabled: true }, - { name: 'ap-south-1', label: 'Asia Pacific (Mumbai)', flag: 'in', disabled: true }, - { name: 'ca-central-1', label: 'Canada (Central)', flag: 'ca', disabled: true } - ]; + let regions: RegionList; + onMount(async () => { + regions = await sdk.forConsole.billing.listRegions(); + }); function getFlag(country: string) { let flag = sdk.forProject.avatars.getFlag(country, 80, 60)?.toString(); @@ -39,50 +26,51 @@ Choose a deployment region for your project. This region cannot be changed. + {#if regions?.total} +
    + {#each regions.regions as region} +
  • + +
    + {#if region.disabled} + {region.name} +

    {region.name}

    + + + {:else} + {region.name} + {region.name} + {/if} +
    +
    +
  • + {/each} +
-
    - {#each regions as region} -
  • - -
    - {#if region.disabled} - {region.name} -

    {region.label}

    - - - {:else} - {region.name} - {region.label} - {/if} -
    -
    -
  • - {/each} -
- -

- Edge Network is enabled on all regions. For more details, check out our Documentation. -

+

+ Edge Network is enabled on all regions. For more details, check out our Documentation. +

+ {/if} diff --git a/src/routes/console/wizard/cloudOrganization/step4.svelte b/src/routes/console/wizard/cloudOrganization/step4.svelte index 03f84f387..30c4346fd 100644 --- a/src/routes/console/wizard/cloudOrganization/step4.svelte +++ b/src/routes/console/wizard/cloudOrganization/step4.svelte @@ -2,7 +2,7 @@ import { Pill } from '$lib/elements'; import { toLocaleDate } from '$lib/helpers/date'; import { WizardStep } from '$lib/layout'; - import { tierToPlan } from '$lib/stores/billing'; + import { getCreditCardImage, tierToPlan } from '$lib/stores/billing'; import { sdk } from '$lib/stores/sdk'; import { createOrganization } from './store'; @@ -52,7 +52,16 @@
{:then card} -

Card ending in {card.last4} {card.brand}

+ +

+ {card?.brand} ending in {card?.last4} +

+ {card?.brand} +
{/await}
{/if}