#slowgen-js-help

1 messages · Page 1 of 1 (latest)

urban heart
#

what's the problem?

viscid niche
#

I want to load the elements on a radio button click that means making an AJAX request to create an intent which is all fine, problem is when confirming the payment elements is undefined. I'll share some code

urban heart
#

the code isn't really indented so I don't get it

#

where is const {error} = await stripe.confirmPayment({ called? inside the callback? elsewhere?

#

you really should have stripe and elements configured/loaded on page load

viscid niche
#

Confirm is called on submit, we offer multiple payment types hence not loading on same page

#

line 7 is where elements is created and on 17 is undefined

urban heart
#

sure but line 7 is inside a callback in an if

#

after that if the entire variables are all deleted/destroyed

#

that usually works this way in all programming languages

#

you want stripe and elements as global variables or at least in a scope that works for the rest of your code using it

#

does that make sense?

viscid niche
#

It certainly does, I am new with async stuff so your help will guide a lot and appreciated. How can I make the variables inside a callback global?

urban heart
#

you can't really, that's not how you would write javascript

#

You'd do this outside of the callback, on page load, you would load Stripe and elements properly there completely outside of this. That's before rendering the PaymentElement

#

similarly you want a global variable for the payment element instance for when you initialize it in the callback

viscid niche
#

OK cheers, you've certainly helped it's sorted now! So thank you again!
Is it possible to load elements without a client secret?

urban heart
#

yes it doesn't take a client secret right?

#

ah my bad you use the payment element I'm dumb so yes it does

#

so you have to defer loading, but you still have a global variable for it

viscid niche
#

Cheers all done! You're not dumb at all. Always helpful, have a great evening/day/night!

urban heart
#

I am not a good JS developer so it's not clean but it shows the logic!