Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

javascript - Stripe subscription checkout with Firebase Functions - IntegrationError: stripe.redirectToCheckout

I'm trying to test the process of creating a checkout session for a stripe subscription. However I get this error on the client: IntegrationError: stripe.redirectToCheckout: You must provide one of lineItems, items, or sessionId.

Here is my code on the front end:

var priceId = "price_1IGpOIFE3UXETakjtSs1Wq6x";

var createCheckoutSession = function(priceId) {
  return fetch("https://us-central1-streamline-14fc8.cloudfunctions.net/createCheckoutSession", {
    method: "POST",
    headers: {
      "Content-Type": "application/json"
    },
    body: JSON.stringify({
      priceId: priceId
    })
  }).then(function(result) {
    return result.json();
  });
};

document
  .getElementById("checkout")
  .addEventListener("click", function(evt) {
    createCheckoutSession(priceId).then(function(data) {
      // Call Stripe.js method to redirect to the new Checkout page
      console.log(data);
      stripe
        .redirectToCheckout({
          sessionId: data.sessionId
        })
        .then(handleResult);
    });
  });

Then my code in Firebase Functions:

//stripe checkout
exports.createCheckoutSession = functions.https.onCall(async (req, res) => {
    const {
      priceId
    } = req.body;

    // See https://stripe.com/docs/api/checkout/sessions/create
    // for additional parameters to pass.
    try {
      const session = await stripe.checkout.sessions.create({
        mode: "subscription",
        payment_method_types: ["card"],
        line_items: [{
          price: priceId,
          // For metered billing, do not pass quantity
          quantity: 1,
        }, ],
        // {CHECKOUT_SESSION_ID} is a string literal; do not change it!
        // the actual Session ID is returned in the query parameter when your customer
        // is redirected to the success page.
        success_url: 'https://example.com/success.html?session_id={CHECKOUT_SESSION_ID}',
        cancel_url: 'https://example.com/canceled.html',
      });
        res.send({
          sessionId: session.id,
        });
    } catch (e) {
      res.status(400);
      return res.send({
        error: {
          message: e.message,
        }
      });
    }
})
question from:https://stackoverflow.com/questions/66055721/stripe-subscription-checkout-with-firebase-functions-integrationerror-stripe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...