mirror of
https://github.com/appwrite/console.git
synced 2026-11-18 20:47:21 +00:00
feat: add stripe to org workflow
This commit is contained in:
1 parent
ba691c5ab4
commit
94261e6fc0
5 files changed
+114
-33
No files matched your search
+16
-11
@@ -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<PaymentMethod> {
|
||||
async getPaymentMethod(paymentMethodId: string): Promise<PaymentMethodData> {
|
||||
const path = `/account/payment-methods/${paymentMethodId}`;
|
||||
const params = {
|
||||
paymentMethodId
|
||||
@@ -260,7 +261,7 @@ export class Billing {
|
||||
async updatePaymentMethod(
|
||||
paymentMethodId: string,
|
||||
providerMethodId: string
|
||||
): Promise<PaymentMethod> {
|
||||
): Promise<PaymentMethodData> {
|
||||
const path = `/account/payment-methods/${paymentMethodId}`;
|
||||
const params = {
|
||||
paymentMethodId,
|
||||
@@ -277,7 +278,7 @@ export class Billing {
|
||||
);
|
||||
}
|
||||
|
||||
async deletePaymentMethod(paymentMethodId: string): Promise<PaymentMethod> {
|
||||
async deletePaymentMethod(paymentMethodId: string): Promise<PaymentMethodData> {
|
||||
const path = `/account/payment-methods/${paymentMethodId}`;
|
||||
const params = {
|
||||
paymentMethodId
|
||||
@@ -292,7 +293,7 @@ export class Billing {
|
||||
params
|
||||
);
|
||||
}
|
||||
async setDefaultPaymentMethod(paymentMethodId: string): Promise<PaymentMethod> {
|
||||
async setDefaultPaymentMethod(paymentMethodId: string): Promise<PaymentMethodData> {
|
||||
const path = `/account/payment-methods/${paymentMethodId}/default`;
|
||||
const params = {
|
||||
paymentMethodId
|
||||
@@ -308,7 +309,7 @@ export class Billing {
|
||||
);
|
||||
}
|
||||
|
||||
async addCredit(organizationId: string, couponId: string): Promise<PaymentMethod> {
|
||||
async addCredit(organizationId: string, couponId: string): Promise<PaymentMethodData> {
|
||||
const path = `/accouorganizations/${organizationId}nt/credits`;
|
||||
const params = {
|
||||
couponId
|
||||
@@ -323,7 +324,7 @@ export class Billing {
|
||||
params
|
||||
);
|
||||
}
|
||||
async listCredits(organizationId: string, queries = []): Promise<PaymentMethod> {
|
||||
async listCredits(organizationId: string, queries = []): Promise<PaymentMethodData> {
|
||||
const path = `/organizations/${organizationId}/credits`;
|
||||
const params = {
|
||||
queries
|
||||
@@ -339,7 +340,7 @@ export class Billing {
|
||||
);
|
||||
}
|
||||
|
||||
async getCredit(organizationId: string, creditId: string): Promise<PaymentMethod> {
|
||||
async getCredit(organizationId: string, creditId: string): Promise<PaymentMethodData> {
|
||||
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',
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
</script>
|
||||
|
||||
<WizardStep>
|
||||
<WizardStep beforeSubmit={handleSubmit}>
|
||||
<svelte:fragment slot="title">Payment details</svelte:fragment>
|
||||
<svelte:fragment slot="subtitle">Add a payment method to your organization.</svelte:fragment>
|
||||
|
||||
@@ -71,17 +148,19 @@
|
||||
group={$createOrganization.paymentMethodId} />
|
||||
{/if}
|
||||
{#if $createOrganization.paymentMethodId === null}
|
||||
<InputText
|
||||
id="name"
|
||||
label="Cardholder name"
|
||||
placeholder="Cardholder name"
|
||||
bind:value={name}
|
||||
required
|
||||
autofocus={true} />
|
||||
<FormList>
|
||||
<InputText
|
||||
id="name"
|
||||
label="Cardholder name"
|
||||
placeholder="Cardholder name"
|
||||
bind:value={name}
|
||||
required
|
||||
autofocus={true} />
|
||||
|
||||
<div id="payment-element">
|
||||
<!-- Elements will create form elements here -->
|
||||
</div>
|
||||
<div id="payment-element">
|
||||
<!-- Elements will create form elements here -->
|
||||
</div>
|
||||
</FormList>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user