From bef77eaabd1b6589c85301640a8f9bbc1094cdb3 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Fri, 10 Feb 2023 11:02:08 +0545 Subject: [PATCH] update payment method --- src/lib/stores/billing.ts | 15 +++++++------ .../settings/+page.svelte | 21 +++++++++++++++---- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/lib/stores/billing.ts b/src/lib/stores/billing.ts index 093a53a39..b67433c6f 100644 --- a/src/lib/stores/billing.ts +++ b/src/lib/stores/billing.ts @@ -27,19 +27,18 @@ export class Billing { ); } + async updatePaymentMethod(teamId: string, paymentMethodId: string) { + const path = `/teams/${teamId}/payment-methods/${paymentMethodId}`; + const uri = new URL(this.client.config.endpoint + path); + return await this.client.call('PUT', uri, { 'content-type': 'application/json' }, {}); + } + async updateProjectPlan(projectId: string, billingPlan: string) { const path = `/project/${projectId}/plan`; const params: Payload = { billingPlan }; const uri = new URL(this.client.config.endpoint + path); - return await this.client.call( - 'patch', - uri, - { - 'content-type': 'application/json' - }, - params - ); + return await this.client.call('patch', uri, { 'content-type': 'application/json' }, params); } } diff --git a/src/routes/console/organization-[organization]/settings/+page.svelte b/src/routes/console/organization-[organization]/settings/+page.svelte index 509950be9..01ed94d02 100644 --- a/src/routes/console/organization-[organization]/settings/+page.svelte +++ b/src/routes/console/organization-[organization]/settings/+page.svelte @@ -16,6 +16,7 @@ let showDelete = false; let stripe: any; let elements: any; + let paymentMethod: any; onMount(() => { name = $organization.name; @@ -30,7 +31,7 @@ event.preventDefault(); try { - const paymentMethod = await cloudSdk.billing.createPaymentMethod($organization.$id); + paymentMethod = await cloudSdk.billing.createPaymentMethod($organization.$id); const options = { clientSecret: paymentMethod.clientSecret, // Fully customizable with appearance API. @@ -69,9 +70,21 @@ type: 'error' }); } else { - // Your customer will be redirected to your `return_url`. For some payment - // methods like iDEAL, your customer will be redirected to an intermediate - // site first to authorize the payment, then redirected to the `return_url`. + if (paymentMethod) { + const { error, setupIntent } = await stripe.retrieveSetupIntent( + paymentMethod.clientSecret + ); + + if (error) { + addNotification({ + message: error.message, + type: 'error' + }); + } else if (setupIntent && setupIntent.status === 'succeeded') { + //update payment method + cloudSdk.billing.updatePaymentMethod($organization.$id, paymentMethod.$id); + } + } } }