-1

I am trying to perfom additional functions after a successful payment is made with my stripe account but it does not work here is the code

I do not get the alert messages, it just redirects to the successURL when done

Here is my js code

var stripe = Stripe(
  "pk_test_51Mh3laDjXZ0tQLnuuSyCeBbnlZZyLCcnE2egXZigkcoirfwsBX97MkubHfX0s9SJEekZH5C5oW05eouKFLyFpMQT0004cI7ArY"
);

document.getElementById("checkout").addEventListener("click", function() {
  stripe.redirectToCheckout({
    lineItems: [
      {
        price: "price_1Mh6d4DjXZ0tQLnurDuvk36i",
        quantity: 1
      }
    ],
    mode: "payment",
    successUrl: "http://127.0.0.1:5500/public/payment.html",
    cancelUrl: "http://127.0.0.1:5500/cancel"
  }).then(function(result) {
    // Handle any errors during checkout
    if (result.error) {
      console.log(result.error.message);
    } else {
      console.log("Payment succeeded");
      myCustomFunction();
    }
  });
});

// Define your custom function here
function myCustomFunction() {
  console.log("Payment successful!");
  alert("Payment successful!");
}
  • is it consoling `Payment succeeded`? – brk Mar 05 '23 at 08:12
  • When you are redirecting user to another page. no code will run until user callback URL! So there is no successful payment before redirection! – Alijvhr Mar 05 '23 at 08:14
  • @brk It isn't consoling anything, it just redirects to the successURL page – Kelechi Ezumah Mar 05 '23 at 08:16
  • @AliJavaheri Please can you explain further – Kelechi Ezumah Mar 05 '23 at 08:17
  • It's a deprecated function and also read the docs: https://stripe.com/docs/js/deprecated/redirect_to_checkout there is no then in `redirectToCheckout` because it's not a `Promise` and if its successful you go to another page with no other code execution! in case of failure you can have logs in console. learn the way from documentation – Alijvhr Mar 05 '23 at 08:23

1 Answers1

0

It's a deprecated function and also read the docs:

https://stripe.com/docs/js/deprecated/redirect_to_checkout

because redirectToCheckout is not a Promise and if it's successful you go to another page with no more code execution! You cannot execute more codes after leaving the page and user leaves when you redirect him!

In case of failure you can have logs in console.

Learn the right way from documentation

Alijvhr
  • 1,695
  • 1
  • 3
  • 22