#yen6305

1 messages ยท Page 1 of 1 (latest)

quick mauveBOT
dark vigil
#

Hi ๐Ÿ‘‹ can you tell me more about the concern you have. Do you have logging set up so that you would see entries if everything goes right? Or are you only logging when errors occur?

Setting the logging aside, is the webhook event handler working as expected?

last coyote
#

No I have multiple logs, so its not only when errors occur

#

I believe it is working correctly, because I tested with the Stripe CLI and it didn't show any errors

#

only 200 OK responses

#

So yeah there are no logs on production only locally

#

I hosted it on Vercel, so I tried changing the .env values -> stripe live key + secret

#

and the live webhook secret

dark vigil
#

Sorry, I don't know what your code is doing, so I don't know what you seeing no logs indicates. I don't know if it's good or bad, or what could be causing that either way. I'm going to need a lot more context about what you're seeing and what your concern before I can provide any guidance or suggestions on how to address that.

last coyote
#

okay, what exactly do you want to see?

#

this is my webhook.js:

// import Stripe from 'stripe';

import Stripe from 'stripe'
import { buffer } from "micro";

export const config = {
    api: {
      bodyParser: false,
    },
  };
  
  //

export default async function handler(req, res) {

    console.log('test');

    const stripe = new Stripe(process.env.STRIPE_SECRET_KEY);

    const buf = await buffer(req);
    const signature = req.headers['stripe-signature'];
    const webhookSecret = process.env.STRIPE_WEBHOOK_SECRET;

    let event;

    try {
        // if(!signature || !webhookSecret) return;

        event = stripe.webhooks.constructEvent(buf, signature, webhookSecret);

            //     // Handle the event
        switch (event.type) {
            case 'charge.succeeded':
            const charge = event.data.object;
            console.log(`Charge succeeded for ${charge.amount}`);
            console.log(event.type);

            break;

            case 'payment_intent.created':
                const payment = event.data.object;
                console.log(`Payment intent created`);
    
            // Add more cases to handle other event types as needed
    
            default:
            // Unexpected event type
            console.log(`Unhandled event type ${event.type}`);
        }

        console.log(event.type)

        res.status(200).send('OK');

    } catch(error) {
        console.log(error.message)
        return res.status(400).send(`Webhook error: ${error.message}`);
    }

    console.log(event.type);

    res.status(200).send();
    
}
#

And this is located in /api/stripe/webhook.js

#

I've tested it locally like this: stripe listen --forward-to localhost:3000/api/stripe/webhook. And this worked fine, because it returned a 200 OK response

#

And I've added this webhook endpoint in the webhooks dashboard in Stripe: https://webshop-nextjs-production.vercel.app/api/stripe/webhook

#

So on production, whenever the user clicks on the form onSubmit button it creates a paymentIntent and also confirms the payment. So this all works because the payments are registered in Stripe. Its just the webhook endpoint thats acting weird

dark vigil
#

Okay, then what. What happens when an Event is sent to the endpoint?

last coyote
#

Well when I tested it locally with the Stripe CLI it just logs the event.type for now. You can find it in the switch

dark vigil
#

But that's not what you're asking about, right? You're asking about what happens in production?

last coyote
#

Yes, I am wondering why it doesnt log anything on production

#

Not logging any error messages

#

Not logging the event type either

dark vigil
#

Do you have the ID of an Event that was sent to that endpoint that I could take a closer look at?

last coyote
#

I don't think it registered any event at all :l It says 'No webhook attempts in the past 15 days'

#

Saw this under webhooks -> my endpoint -> All/succeeded/failed

dark vigil
#

That would likely be why you aren't seeing any logs then, if no traffic is going to your endpoint.

last coyote
#

Yeah, so is it because my endpoint is incorrect?

#

Because i've checked the stripe secret key, publishable key and webhooks secret (all live, so no test keys or secrets)

dark vigil
#

Not necessarily, have there been any Events generated that should have been sent to your endpoint? What is your account ID?

last coyote
#

This is my account id: acct_1NHuNJFzWZIV5ElS

#

I have 2

#

the other one (admin): acct_1NaIepD7Shm9DtKO

dark vigil
last coyote
#

Oh huh whut

#

Hows that posibble because I have one webhook endpoint registered

#

Can you find it by name?

#

Jasl-audio

dark vigil
last coyote
#

Oh yes that is correct

#

Ah okay..

dark vigil
#

Looking back at the code you provided, it looks like you're expecting to listen for charge.succeeded and payment_intent.created Events. So you may need to edit the webhook endpoint in the dashboard to listen for additional Event types.

last coyote
#

Okay, so I actually have to add another action to the webhook?

#

Which is the payment_intent.created event

dark vigil
#

Yeah, when you're editing the webhook endpoint in the dashboard, you'll want to make sure to select all Event types that you would like to be sent to the endpoint.

last coyote
#

Okay, ill try this first

#

Okay, now I am finally seeing some errors

#

I've logged the event.type but it is only showing the payment_intent.created

#

But I am also expecting a charge.succeeded

#

Because the payment has succeeded

dark vigil
#

Can you share an example Payment Intent?

last coyote
#

Okay, ehm I've deleted this part case 'payment_intent.created': const payment = event.data.object; console.log(`Payment intent created`);`

#

And I've also removed the payment_intent.created from my webhook

#

And i've changed this case 'charge.succeeded': to this case 'charge.captured ':

#

And also removed the charge.succeeded from my webhook and added the charge.captured instead, because I need to pass this information

#

But it doesnt log charge.captured when I am doing console.log(event.type)

dark vigil
#

Based on what I'm seeing currently, your integration isn't designed in a way that will cause charge.captured events to be generated and sent.

Let's take a step back, what are you trying to accomplish?

last coyote
#

Okay, I want to change my WooCommerce order status from 'Progress' to 'Paid' if the event.type is 'charge.captured'

#

This is my Checkout page code where you can see that I am confirming the payment using stripe.confirmPayment

#

Correct me if I am wrong, but the charge.captured event will be send if the stripe.confirmPayment succeeds right?

dark vigil
#

No, charge.captured is sent when a previously uncaptured Charge is captured:
https://stripe.com/docs/api/events/types#event_types-charge.captured
You don't seem to be using manual captures, so the charge.captured Event is not generated as you don't have Charges moving from uncaptured to captured states.

charge.succeeded or payment_intent.succeeded would be better Events to listen to with your current flow.

last coyote
#

Ahhh

#

Okay, I'll find a away to implement this

#

So thanks a lot for helping me

dark vigil
#

Any time!