#rockywearsahat

1 messages ยท Page 1 of 1 (latest)

cosmic ridgeBOT
#

Hello rockywearsahat, we'll be with you shortly! Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
โ€ข https://discord.com/channels/841573134531821608/1164521473659129866, 0 days ago, 36 messages
โ€ข https://discord.com/channels/841573134531821608/1164503998972313661, 0 days ago, 9 messages

novel oasis
#

Hi ๐Ÿ‘‹

What kind of integration are you referring to?

lost eagle
#

hey!

#

I have a next js app which currently creates a checkout session with whatever products are in my apps cart, I'm able to pay and set a shipping address but then how can I create a callback/webhook event to trigger some code when the payment is successful?

#

my code looks somewhat simular to this currently:

export async function POST(req, res) {
  try {
    const session = await stripe.checkout.sessions.create({
      mode: "payment",
      payment_method_types: ["card"],
      line_items: [
        {
          price: "price_1O2sMJAI6vEkS2uiPAxdkWzB",
          quantity: 1,
        },
      ],
      automatic_tax: {
        enabled: true,
      },
      success_url: `http://localhost:3000/success?session_id={CHECKOUT_SESSION_ID}`,
      cancel_url: `http://localhost:3000/cart`,
      shipping_address_collection: { allowed_countries: ["US", "BE", "FR"] },
    });

    return new Response(JSON.stringify(session));
  } catch (err) {
    return new Response({ status: 500, error: err });
  }
}
novel oasis
#

For that I would recommend listening to webhooks. If you are mostly focused on the successful payment I would recommend listening to payment_intent.succeeded.

#

The reason being that, in some cases you could have a Checkout Session that is successful but the customer uses an async payment method (like a bank debit) and that may fail asynchronously. So the session would return a 200 response but the payment could fail

#

By listening to the webhook you are certain the payment succeeded

lost eagle
#

bet, I was just about to ask if there are any docs so I'll read into it, thank you for the help! ๐Ÿ™

novel oasis
#

Happy to do it ๐Ÿ™‚

lost eagle
#

I'm getting a bit of an odd error, I'm trying to set stripe to listen --forward-to my webhook and I'm getting the error bash: stripe: command not found? do you by chance have any idea as to why? I'm typing 'stripe listen --forward-to localhost:3000/api/webhooks/checkout' am I doing something wrong?

novel oasis
#

Have you installed the stripe CLI?

#

try stripe version and see what you get

lost eagle
#

I haven't yet, I just installed it or tried to, but how do I actually load the files into like a bin folder? I have a folder that is being read as a path environment but do I put the stripe-cli-1.7.13 folder in there or do I put the entire contents in there or is there a specific folder/file from the stripe-cli-v that I need to put in /bin?

novel oasis
lost eagle
#

I'm dumb I put the repo not the executable in my /bin file that's my own stupidity

#

do I need to add my webhook signing secret to my .env.local file?

novel oasis
#

You need to use it to validate the webhooks are from us so put it whereever makes the most sense. Honestly I recommend hard-coding to get started

lost eagle
#

bet, I'm running into a slight issue where I'm trying to validate the signature sent back to make sure that it was through stripe, I'm setting const signature = req.headers["stripe-signature"] but it returns undefined, logging out req.headers I can see that there is a value called

'stripe-signature' => {
      name: 'stripe-signature',
      value: 't=1697753818,v1=bf02d2aad107ee63c3532cac2715bc2cd1b4d2d9805d8b465a8ef8927593166e,v0=26ae38c961834f758a945a6bb087a8b6d366e6db49aca0933d3f252a82636b8c'
    }

but how do I actually access and store this stripe signature correctly to then constructEvent from the signature and webhook signing secret? I assume for security I have to construct the event and check it against the webhook secret?

novel oasis
#

You just call the constructEvent function. that will do the matching for you.

lost eagle
#

with the body of the req or with the req object itself?

novel oasis
#
event = stripe.webhooks.constructEvent(
        request.body,
        signature,
        endpointSecret
      );
lost eagle
#

I'm getting a return in the body of error No stripe-signature header value was provided

novel oasis
#

Right, because you said it was coming in as null.

cosmic ridgeBOT
lost eagle
#

yea I got it it is just a quirk of next js, apparently req.headers.get isn't a function req.headers works to get the list but doesn't have search functionality (just a long string of the headers), so I had to just make a const hList = headers(req) then next's header class has a .get function and that seems to work, I'm getting a payload back with type: 'checkout.session.completed'

#

thank you for all the help!