From 94261e6fc09bd08a1c100dfa5c5ac0b91fd3a11a Mon Sep 17 00:00:00 2001 From: Arman Date: Thu, 6 Jul 2023 21:12:58 -0700 Subject: [PATCH] feat: add stripe to org workflow --- src/lib/stores/billing.ts | 27 +++-- .../account/payments/paymentModal.svelte | 13 +-- src/routes/console/account/payments/store.ts | 2 - .../billing/paymentModal.svelte | 2 +- src/routes/console/wizard/step2.svelte | 103 ++++++++++++++++-- 5 files changed, 114 insertions(+), 33 deletions(-) diff --git a/src/lib/stores/billing.ts b/src/lib/stores/billing.ts index 018b5ef69..ce09734e2 100644 --- a/src/lib/stores/billing.ts +++ b/src/lib/stores/billing.ts @@ -1,7 +1,8 @@ import type { Client } from '@appwrite.io/console'; import type { Organization } from './organization'; +import type { PaymentMethod } from '@stripe/stripe-js'; -export type PaymentMethod = { +export type PaymentMethodData = { $id: string; providerMethodId: string; expiryMonth: number; @@ -12,7 +13,7 @@ export type PaymentMethod = { }; export type PaymentList = { - paymentMethods: PaymentMethod[]; + paymentMethods: PaymentMethodData[]; total: number; }; @@ -103,7 +104,7 @@ export class Billing { ); } - async updateOrganizationPaymentMethod(organizationId: string, paymentMethodId: string) { + async setOrganizationPaymentMethod(organizationId: string, paymentMethodId: string) { const path = `/organizations/${organizationId}/payment-method`; const params = { organizationId, @@ -120,7 +121,7 @@ export class Billing { ); } - async updateOrganizationPaymentMethodBackup(organizationId: string, paymentMethodId: string) { + async setOrganizationPaymentMethodBackup(organizationId: string, paymentMethodId: string) { const path = `/organizations/${organizationId}/payment-method/backup`; const params = { organizationId, @@ -228,7 +229,7 @@ export class Billing { ); } - async getPaymentMethod(paymentMethodId: string): Promise { + async getPaymentMethod(paymentMethodId: string): Promise { const path = `/account/payment-methods/${paymentMethodId}`; const params = { paymentMethodId @@ -260,7 +261,7 @@ export class Billing { async updatePaymentMethod( paymentMethodId: string, providerMethodId: string - ): Promise { + ): Promise { const path = `/account/payment-methods/${paymentMethodId}`; const params = { paymentMethodId, @@ -277,7 +278,7 @@ export class Billing { ); } - async deletePaymentMethod(paymentMethodId: string): Promise { + async deletePaymentMethod(paymentMethodId: string): Promise { const path = `/account/payment-methods/${paymentMethodId}`; const params = { paymentMethodId @@ -292,7 +293,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 @@ -308,7 +309,7 @@ export class Billing { ); } - async addCredit(organizationId: string, couponId: string): Promise { + async addCredit(organizationId: string, couponId: string): Promise { const path = `/accouorganizations/${organizationId}nt/credits`; const params = { couponId @@ -323,7 +324,7 @@ export class Billing { params ); } - async listCredits(organizationId: string, queries = []): Promise { + async listCredits(organizationId: string, queries = []): Promise { const path = `/organizations/${organizationId}/credits`; const params = { queries @@ -339,7 +340,7 @@ export class Billing { ); } - async getCredit(organizationId: string, creditId: string): Promise { + async getCredit(organizationId: string, creditId: string): Promise { const path = `/organizations/${organizationId}/credits/${creditId}`; const params = { creditId @@ -376,6 +377,10 @@ export class Billing { // } } +export const publicStripeKey = import.meta.env?.VITE_STRIPE_PUBLIC_KEY?.toString() as + | string + | undefined; + export const tierFree = { price: 0, name: 'Free', diff --git a/src/routes/console/account/payments/paymentModal.svelte b/src/routes/console/account/payments/paymentModal.svelte index 742e286b5..bd3035008 100644 --- a/src/routes/console/account/payments/paymentModal.svelte +++ b/src/routes/console/account/payments/paymentModal.svelte @@ -2,7 +2,7 @@ // import { Submit, trackEvent, trackError } from '$lib/actions/analytics'; import { Modal } from '$lib/components'; import { InputText, Button, FormList } from '$lib/elements/forms'; - import { paymentMethods, publicKey } from './store'; + import { paymentMethods } from './store'; import { loadStripe, type Stripe, @@ -15,7 +15,7 @@ import { invalidate } from '$app/navigation'; import { Dependencies } from '$lib/constants'; import { app } from '$lib/stores/app'; - import { apperanceDark, apperanceLight } from '$lib/stores/billing'; + import { apperanceDark, apperanceLight, publicStripeKey } from '$lib/stores/billing'; export let show = false; @@ -28,11 +28,11 @@ let paymentMethod: PaymentMethod; onMount(async () => { - stripe = await loadStripe(publicKey); + stripe = await loadStripe(publicStripeKey); try { clientSecret = $paymentMethods?.paymentMethods[0]?.clientSecret; if (!clientSecret) { - paymentMethod = await sdk.forConsole.billing.createPaymentMethod($organization.$id); + paymentMethod = await sdk.forConsole.billing.createPaymentMethod(); } const options = { clientSecret: clientSecret ? clientSecret : paymentMethod.clientSecret, @@ -64,10 +64,9 @@ paymentMethod = await sdk.forConsole.billing.createPaymentMethod($organization.$id); const { setupIntent } = await stripe.retrieveSetupIntent(paymentMethod.clientSecret); if (setupIntent && setupIntent.status === 'succeeded') { - await sdk.forConsole.billing.updateOrganizationPaymentMethod( + await sdk.forConsole.billing.setOrganizationPaymentMethod( $organization.$id, - paymentMethod.$id, - setupIntent.payment_method as string + paymentMethod.$id ); await invalidate(Dependencies.PAYMENT_METHODS); show = false; diff --git a/src/routes/console/account/payments/store.ts b/src/routes/console/account/payments/store.ts index 2a4322e3e..c9f2b970f 100644 --- a/src/routes/console/account/payments/store.ts +++ b/src/routes/console/account/payments/store.ts @@ -1,6 +1,4 @@ import { page } from '$app/stores'; import { derived } from 'svelte/store'; -export const publicKey = import.meta.env?.VITE_STRIPE_PUBLIC_KEY?.toString() as string | undefined; - export const paymentMethods = derived(page, ($page) => $page.data.paymentMethods); diff --git a/src/routes/console/organization-[organization]/billing/paymentModal.svelte b/src/routes/console/organization-[organization]/billing/paymentModal.svelte index 742e286b5..29d064b8b 100644 --- a/src/routes/console/organization-[organization]/billing/paymentModal.svelte +++ b/src/routes/console/organization-[organization]/billing/paymentModal.svelte @@ -64,7 +64,7 @@ paymentMethod = await sdk.forConsole.billing.createPaymentMethod($organization.$id); const { setupIntent } = await stripe.retrieveSetupIntent(paymentMethod.clientSecret); if (setupIntent && setupIntent.status === 'succeeded') { - await sdk.forConsole.billing.updateOrganizationPaymentMethod( + await sdk.forConsole.billing.setOrganizationPaymentMethod( $organization.$id, paymentMethod.$id, setupIntent.payment_method as string diff --git a/src/routes/console/wizard/step2.svelte b/src/routes/console/wizard/step2.svelte index 868e7635d..c931dec15 100644 --- a/src/routes/console/wizard/step2.svelte +++ b/src/routes/console/wizard/step2.svelte @@ -16,6 +16,17 @@ import UsageRates from './usageRates.svelte'; import type { PaymentList } from '$lib/stores/billing'; import { paymentMethods } from '../account/payments/store'; + import { + loadStripe, + type Stripe, + type StripeElements, + type PaymentMethod + } from '@stripe/stripe-js'; + import { organization } from '$lib/stores/organization'; + import { invalidate } from '$app/navigation'; + import { Dependencies } from '$lib/constants'; + import { app } from '$lib/stores/app'; + import { apperanceDark, apperanceLight, publicStripeKey } from '$lib/stores/billing'; let methods: PaymentList; let name: string; @@ -24,11 +35,18 @@ let budgetEnabled = false; let showRates = false; + let error: string; + let elements: StripeElements; + let stripe: Stripe; + let clientSecret: string; + let paymentMethod: PaymentMethod; + let isStripeInitialized = false; + onMount(async () => { methods = await sdk.forConsole.billing.listPaymentMethods(); if (methods?.total) { $createOrganization.paymentMethodId = methods[0].id; - } + } else if (!isStripeInitialized) initialize(); }); async function removeCode() { @@ -40,9 +58,68 @@ redeemedCodes = redeemedCodes; promo = ''; } + + async function initialize() { + stripe = await loadStripe(publicStripeKey); + isStripeInitialized = true; + + try { + clientSecret = $paymentMethods?.paymentMethods[0]?.clientSecret; + if (!clientSecret) { + paymentMethod = await sdk.forConsole.billing.createPaymentMethod(); + } + const options = { + clientSecret: clientSecret ? clientSecret : paymentMethod.clientSecret, + appearance: $app.themeInUse === 'dark' ? apperanceDark : apperanceLight + }; + console.log(clientSecret); + // Set up Stripe.js and Elements to use in checkout form, passing the client secret obtained in step 3 + elements = stripe.elements(options); + createForm(); + } catch (e) { + error = e.message; + } + } + + async function createForm() { + const paymentElement = elements.create('payment'); + paymentElement.mount('#payment-element'); + } + + async function handleSubmit() { + try { + await stripe.confirmSetup({ + elements, + confirmParams: { + return_url: 'http://localhost:3000' + }, + redirect: 'if_required' + }); + paymentMethod = await sdk.forConsole.billing.createPaymentMethod($organization.$id); + const { setupIntent } = await stripe.retrieveSetupIntent(paymentMethod.clientSecret); + if (setupIntent && setupIntent.status === 'succeeded') { + await sdk.forConsole.billing.setOrganizationPaymentMethod( + $organization.$id, + paymentMethod.$id + ); + await invalidate(Dependencies.PAYMENT_METHODS); + console.log('test'); + // const paymentElement = elements.getElement('payment'); + // paymentElement.destroy(); + } else console.log('test2'); + } catch (e) { + error = e.message; + console.log(e); + // trackError(StripeError, Submit.ProjectCreate); + } + } + + $: if ($createOrganization.paymentMethodId === null && !isStripeInitialized) { + initialize(); + } - + Payment details Add a payment method to your organization. @@ -71,17 +148,19 @@ group={$createOrganization.paymentMethodId} /> {/if} {#if $createOrganization.paymentMethodId === null} - + + -
- -
+
+ +
+
{/if}