简体   繁体   中英

pass parameters through stripe checkout session

I have this on my on my payments.ejs ,

    <head>
    <script src="https://polyfill.io/v3/polyfill.min.js?version=3.52.1&features=fetch"></script>
    <script src="https://js.stripe.com/v3/"></script>
</head>
<div class="box">
    <!-- <form action="/paymentsPage" method="POST"> -->
    <div class="input-container">       
        <input type="text" name="code" id="code" required/>
        <label>Code</label>
    </div>
        <button type="button" class="btn" id="checkout-button">submit</button>
<!-- </form> -->
</div>


<!-- a312b7a0338d -->

  <script type="text/javascript">
    // Create an instance of the Stripe object with your publishable API key
    var stripe = Stripe("pk_test_51J0C0JFtPC3YhhMO5SoK5wnP01gK8apNhjq6TD21T3VHH6a7vet3Cl4PplWk7mkTPtJQwJVpt6ILjJMsnKPQOwCV00RDCU0c9j");
    var checkoutButton = document.getElementById("checkout-button");

   
    checkoutButton.addEventListener("click", function () {
        var code = document.getElementById("code").value
        console.log(code)
      fetch("/create-checkout-session", {
        method: "POST",
      })
        .then(function (response) {
          return response.json();
        })
        .then(function (session) {
          return stripe.redirectToCheckout({ sessionId: session.id, code: code });
        })
        .then(function (result) {
          // If redirectToCheckout fails due to a browser or network
          // error, you should display the localized error message to your
          // customer using error.message.
          if (result.error) {
            alert(result.error.message);
          }
        })
        .catch(function (error) {
          console.error("Error:", error);
        });
    });
  </script>

I want to pass code to the backend. So I tried to pass it though as a parameter of stripe.redirectToCheckout({ sessionId: session.id, code: code }) but it didn't work. How do I pass code to the backend

backend code:

    app.post('/create-checkout-session', async (req, res) => {

    var userCode = req.body.code;
    console.log("51: " + userCode)

    var clientName = "gianluca"

  const session = await stripe.checkout.sessions.create({
    payment_method_types: ['card'],
    line_items: [
      {
        price_data: {
          currency: 'cad',
          product_data: {
            name: "Invoice for " + clientName,
            images: ['https://i.imgur.com/EHyR2nP.png'],
          },
          unit_amount: 2000,
        },
        quantity: 1,
      },
    ],
    mode: 'payment',
    success_url: `${YOUR_DOMAIN}/success.html`,
    cancel_url: `${YOUR_DOMAIN}/cancel.html`,
  });

  res.json({ id: session.id });
   
});

what is the steps to passing the parameter to the backend? Thanks:)

It worked when I did this:

fetch("/create-checkout-session", {
    method: "POST",
    headers: {
        "Content-Type": "application/json"
        },
        body: JSON.stringify({
        code: code
        }),
  })

for future reference of anyone who looks at this:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM