From 45dd835a08b51578c8a5b242b36962e6a91a168f Mon Sep 17 00:00:00 2001 From: Arman Date: Mon, 20 Mar 2023 17:39:14 +0100 Subject: [PATCH] reafactor: submit function --- .../billing/paymentMethods.svelte | 2 + .../billing/paymentModal.svelte | 72 ++++++++----------- 2 files changed, 33 insertions(+), 41 deletions(-) diff --git a/src/routes/console/organization-[organization]/billing/paymentMethods.svelte b/src/routes/console/organization-[organization]/billing/paymentMethods.svelte index ca4d99693..22b8757d0 100644 --- a/src/routes/console/organization-[organization]/billing/paymentMethods.svelte +++ b/src/routes/console/organization-[organization]/billing/paymentMethods.svelte @@ -3,6 +3,8 @@ import { Button } from '$lib/elements/forms'; import { paymentMethods } from './store'; export let showPayment = false; + + console.log($paymentMethods); diff --git a/src/routes/console/organization-[organization]/billing/paymentModal.svelte b/src/routes/console/organization-[organization]/billing/paymentModal.svelte index 26e81c39d..06f2c01a7 100644 --- a/src/routes/console/organization-[organization]/billing/paymentModal.svelte +++ b/src/routes/console/organization-[organization]/billing/paymentModal.svelte @@ -20,10 +20,10 @@ let name: string; let error: string; - let isCreating = false; let elements: StripeElements; let stripe: Stripe; + let clientSecret: string; let paymentMethod: PaymentMethod; const apperanceLight = { @@ -59,7 +59,6 @@ colorPrimary: '#606a7b', colorText: '#C5C7D8', colorBackground: '#161622', - color: '#606a7b', colorDanger: '#FF453A', fontFamily: 'Inter, arial, sans-serif', borderRadius: '4px' @@ -83,7 +82,7 @@ onMount(async () => { stripe = await loadStripe(publicKey); try { - const clientSecret = $paymentMethods?.paymentMethods[0]?.clientSecret; + clientSecret = $paymentMethods?.paymentMethods[0]?.clientSecret; if (!clientSecret) { paymentMethod = await sdk.forConsole.billing.createPaymentMethod($organization.$id); } @@ -91,7 +90,7 @@ clientSecret: clientSecret ? clientSecret : paymentMethod.clientSecret, appearance: $app.themeInUse === 'dark' ? apperanceDark : apperanceLight }; - console.log(paymentMethod); + 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(); @@ -106,45 +105,37 @@ } async function handleSubmit() { - const { error: StripeError } = await stripe.confirmSetup({ - elements, - confirmParams: { - return_url: 'http://localhost:3000' - }, - redirect: 'if_required' - }); - if (StripeError) { - error = StripeError.message; - // trackError(StripeError, Submit.ProjectCreate); - } else { - if (paymentMethod) { - const { error: PaymentError, setupIntent } = await stripe.retrieveSetupIntent( - paymentMethod.clientSecret + 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.updatePaymentMethod( + $organization.$id, + paymentMethod.$id, + setupIntent.payment_method as string ); - if (PaymentError) { - error = PaymentError.message; - // trackError(StripeError, Submit.ProjectCreate); - } else if (setupIntent && setupIntent.status === 'succeeded') { - try { - await sdk.forConsole.billing.updatePaymentMethod( - $organization.$id, - paymentMethod.$id, - setupIntent.payment_method as string - ); - await invalidate(Dependencies.PAYMENT_METHODS); - show = false; - const paymentElement = elements.getElement('payment'); - paymentElement.destroy(); - } catch (e) { - error = e.message; - } - } - } + await invalidate(Dependencies.PAYMENT_METHODS); + show = false; + 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); } } - + Add Payment Method This payment method will be added to the {$organization.name} billing details. @@ -154,8 +145,7 @@ placeholder="Cardholder name" bind:value={name} required - autofocus={true} - disabled={isCreating} /> + autofocus={true} />
@@ -163,6 +153,6 @@ - +