#earth-checkout-webhook

1 messages · Page 1 of 1 (latest)

covert swiftBOT
tall trench
#
app.post("/Checkout/:userID", async (req, res) => {
  let items = [];

  if (!req.session.user) {
    return res.json({ success: false, message: "User not logged in." });
  }

  try {
    const cart = await CartModel.findOne({
      cartUser: req.params.userID,
    });

    if (cart) {
      const cartItems = await CartModel.find({
        cartUser: req.params.userID,
      });

      items = cartItems.map((item) => ({
        name: item.name,
        price: item.price,
      }));

      const customer = await stripe.customers.create({
        email: req.session.user.email,
        name: req.session.user.name,
        // You can include additional customer details as needed
      });

      const session = await stripe.checkout.sessions.create({
        payment_method_types: ["card"],
        line_items: items.map((item) => ({
          price_data: {
            currency: "usd",
            product_data: {
              name: item.name,
            },
            unit_amount: item.price * 100,
          },
          quantity: 1,
        })),
        customer: customer.id,
        mode: "payment",
        success_url:
          "https://20e4-2606-40-878b-32b-00-1260-c6ed.ngrok-free.app/home",
        cancel_url:
          "https://20e4-2606-40-878b-32b-00-1260-c6ed.ngrok-free.app/shop",
      });
      res.json({ sessionId: session.id });
    }
  } catch (error) {
    console.error(error);
    res.status(500).json({ success: false, message: "Server error." });
  }
});

#

When I complete the checkout session, the checkout.session.completed event is never called. However, using CLI. Everything works perfectly when I call a test..

#

quoting here

slender crypt
#

thanks!

tall trench
#

checkout.session.completed is a webhook event. It will be sent to a registered endpoint if you have that configured in your account

#
  1. Do you have a publicly reachable webhook endpoint configured for your Stripe account?
  2. Is it listening for the checkout.session.completed event?
slender crypt
tall trench
#

Is it currently configured to do so?

slender crypt
#

Are you talking about the one configured on Stripe?

tall trench
#

Yes. You said the CLI works. I'm guessing you are referring to the stripe listen --forward-to command

slender crypt
#

When I go through the checkout session. It doesn't trigger the event.

tall trench
#

Can you share the checkout session ID of a recently completed checkout session?

slender crypt
#

cs_test_b1H7PlrGHO8ZeaO2kxMlg9KjzY2PbHU40jCcQQUaJev2icsNIGirvfWSGM

#

This is an ID of a checkout session created with StripeJS.

tall trench
#

When you say created with Stripe.js, do you mean using redirectToCheckout or the Node.js code you shared above?

#

stripe-node and Stripe.js are two separate libraries

#

Okay, Node.js

#

This Checkout Session was never completed, so no webhook event will fire

slender crypt
#

Yeah. When I use stripe.checkout.sessions.create

#

Here, let me actually go through a checkout.

#

When I create a session and go through it using a test card and random information, it completes. Yet, I don't think it posts to the webhook.

tall trench
#

Can you share the ID of a completed checkout session?

slender crypt
tall trench
covert swiftBOT
slender crypt
twilit cargo
#

earth-checkout-webhook

tall trench
#

The event fired but it was not delivered

twilit cargo
slender crypt
#

Okay! I was searching through my logs for my webhook, and just now, the events appeared. Earlier, they were not there.

I have an error from the events:

Webhook Error: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?
Learn more about webhook signing and explore webhook integration examples for various frameworks at https://github.com/stripe/stripe-node#webhook-signing

twilit cargo
#

Okay so the next step is to read those docs and ensure that you are passing the exact raw body of the request. I think you're using Node.js which can get in the way a lot sadly