stripe js redirect to checkout

Hi All, I’m trying to get Stripe setup.

I’ve got the API call to setup and retrieve the session OK.
Now I’m trying to redirect to checkout. The only way to do this that I can see is to use their
javascript method. It works perfectly as an HTML file but I can’t get it to work in my project.

I have replaced part of my public key with some hashes here.

Here is the working HTML file :

KP Balance and Purchase
<script>
    function checkout() {

        var stripe = Stripe('pk_test_jh#######37v9');

        stripe.redirectToCheckout({
            sessionId:'cs_test_UdVb83XgZ0fT2XJjuPExSboEtLyLgSnx2qWlrbYjpY4L3Ihoq6c0cGNd'

        }).then(function (result) {
            // If `redirectToCheckout` fails due to a browser or network
            // error, display the localized error message to your customer
            // using `result.error.message`.
       
        });

        }

This is what I’ve got in the ‘PageReceived’ event of my socket - after I’ve got Session id etc to achieve the above
I’ve hard coded the session id for the purpose of testing :

self.ExecuteJavaScript(" src="“https://js.stripe.com/v3/”" ")

self.ExecuteJavaScript(" var stripe = Stripe(‘pk_test_jhX0BZ########0bDcx37v9’); " )

Self.ExecuteJavaScript( "stripe.redirectToCheckout({sessionId: ‘cs_test_UdVb83XgZ0fT2XJjuPExSboEtLyLgSnx2qWlrbYjpY4L3Ihoq6c0cGNd’ }).then(function (result) { }); " )

This is the error I’m getting

Could not execute returned javascript: Can’t find variable: Stripe
Source: src=“https://js.stripe.com/v3/
var stripe = Stripe(‘pk_test_jhX0BZV#########D200bDcx37v9’);
stripe.redirectToCheckout({sessionId: ‘cs_test_UdVb83XgZ0fT2XJjuPExSboEtLyLgSnx2qWlrbYjpY4L3Ihoq6c0cGNd’ }).then(function (result) { });

Any help would be great thank you.

Capitalised Stripe vs stripe var ?

Thanks Stephen, I tried that, I got a different error then. Also it broke the HTML file when I tried in there.

[quote=487351:@Barney Hyde]self.ExecuteJavaScript(" src="“https://js.stripe.com/v3/”" ")

self.ExecuteJavaScript(" var stripe = Stripe(‘pk_test_jhX0BZ########0bDcx37v9’); " )

Self.ExecuteJavaScript( "stripe.redirectToCheckout({sessionId: [/quote]

I’m afraid this won’t work.
On the second ExecuteJavaScript js.stripe.com isn’t in the scope.
You have to be very careful with variable scope when using multiple ExecuteJavaScript.

You’d better try:

  • using another name for your stripe variable, eg myStripe, this will help you make a difference between Stripe function and stripe var.
  • declare myStripe globally, otherwise your var is probably not in scope anymore

Thanks Olivier. I will try that. Meanwhile I’ve written the file to disk including the session ID etc then using ShowURL to open it. This is working fine but it’s a shame to do a work around. Thanks.