#ologbonowiwi

1 messages ยท Page 1 of 1 (latest)

wanton ridgeBOT
stone hollow
spiral saddle
#

Ok! Regarding webhooks, there's any way to target to localhost programmatically (without CLI)?

I tried the paymentIntents.confirm(id), but it's not working. The message is that the payment intent doesn't exist, but most likely, this is happening because I'm not using the CLI to forward the events.

#

What I need to do is:

  • setup the payment
  • confirm the payment
  • test a flow with the payment confirmed on my application

What I need help with is doing all this without the CLI. Do you have any suggestions for a better way to do this?

PS: the payment setup is being done using Playwright, clicking on the interface, and filling/submitting the Stripe payment form

stone hollow
#

Ok! Regarding webhooks, there's any way to target to localhost programmatically (without CLI)?
I'm afraid this is not possible.

#

I tried the paymentIntents.confirm(id), but it's not working. The message is that the payment intent doesn't exist, but most likely, this is happening because I'm not using the CLI to forward the events.
You'd need a Payment Intent to be created before confirming one

#

The flow will be:

  1. Create a Payment Intent and collect the payment method details
  2. Confirm the payment intent
spiral saddle
#

In this case, the official recommendation is to install the CLI on the pipeline that I'll run these tests on?

stone hollow
#

We don't recommend to run automated test against actual API requests/CLI as Stripe places rate limit. They should be mocked in your test system.

spiral saddle
#

but how could I use API mocks + trigger the webhook to confirm the payment intent?

stone hollow
#

One way I can think of is to set up a test service in your system to send a mock webhook after simulating a mock API

#

If the automated tests system isn't that high, you may use Payment Intent API from Create to Confirm (although this is generally not recommended)

#

You can't confirm the payment intent directly without creating one

spiral saddle
#

Send a mock webhook it was actually my first idea of implementation, but I'm struggling a lot to do it. Here's my implementation:


    console.log("๐Ÿš€ ~ file: integrations-stripe.e2e.ts:113 ~ test ~ paymentIntent:", paymentIntentId);

    const paymentConfirmation = {
      id: paymentIntentId,
      type: "payment_intent.succeeded",
      object: "event",
      livemode: false,
      data: {
        object: {
          id: paymentIntentId,
        },
      },
    };

    const payload = JSON.stringify(paymentConfirmation, null, 2);

    const secret = process.env.STRIPE_WEBHOOK_SECRET as string;

    // confirm my payment
    // stripe.paymentIntents.confirm(paymentIntent as string);p

    const stripeSignature = stripe.webhooks.generateTestHeaderString({
      payload,
      secret,
    });

    console.log("๐Ÿš€ ~ file: integrations-stripe.e2e.ts:132 ~ test ~ stripeSignature:", stripeSignature);

    const event = stripe.webhooks.constructEvent(payload, stripeSignature, secret);
    console.log("๐Ÿš€ ~ file: integrations-stripe.e2e.ts:161 ~ test ~ event:", event);

    // await _triggerWebhook(event);

    const result = await page.request.post("/api/integrations/stripepayment/webhook", {
      data: JSON.stringify(event),
      headers: {
        "stripe-signature": stripeSignature,
      },
    });

When I hit the API, I'm receiving this error: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? https://github.com/stripe/stripe-node#webhook-signing

#

A print of the code if makes any easier to read:

stone hollow
#

Does it work with actual webhook event (not the mock one)?

#

Where does the error throw from? Is it from your webhook endpoint or const event = stripe.webhooks.constructEvent(payload, stripeSignature, secret); in this code?

spiral saddle
#

The throw came from the stripe.webhooks.constructEvent on my webhook handler endpoint

#

For Stripe requests it was working earlier Today; I had a non-Stripe related issue and I'm trying to reinstall the dependencies to check if it's still working

stone hollow
#

Below code will generate entire header, but not signature alone. When you send the event to your own endpoint, it shouldn't be put under stripe-signature param

const header = stripe.webhooks.generateTestHeaderString({
  payload: payloadString,
  secret,
});
#

Can you try this instead?

const header = stripe.webhooks.generateTestHeaderString({
  payload: payloadString,
  secret,
});

const result = await page.request.post("/api/integrations/stripepayment/webhook", {
      data: JSON.stringify(event),
      headers: {...header},
    });
spiral saddle
#

didn't worked, the signature is missing

#

I added a log and the result was: ~ file: integrations-stripe.e2e.ts:142 ~ test ~ headers: t=1689738277,v1=9718860a80f3c92245341894817a1d377116c81d1556ca25489c596c794937a6

#

I noticed that in Go there is this method GenerateTestSignedPayload, maybe there's something equivalent on Node that it'll allow me to sign the payload before send it?

stone hollow
#

stripe.webhooks.generateTestHeaderString is the Node version of GenerateTestSignedPayload

#

Can you print the value of header before sending to your webhook endpoint? What is the result?

spiral saddle
#

it worked now!

#

the final implementation:

#

I changed the request body to the payload string that I used to sign the header and added the timestamp

stone hollow
#

ah! great to hear that it works now! timestamp is indeed neccessary

spiral saddle
stone hollow
#

Yup! This can be used

wanton ridgeBOT