IvanD
IvanD

Reputation: 2923

Stripe Webhooks Testing in Go

How can I mock the requests from Stripe to my webhooks with Stripe's Go library?

The Node library has the stripe.webhooks.generateTestHeaderString method that allows to mock such requests:

const payload = {
  id: 'evt_test_webhook',
  object: 'event',
};

const payloadString = JSON.stringify(payload, null, 2);
const secret = 'whsec_test_secret';

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

const event = stripe.webhooks.constructEvent(payloadString, header, secret);

// Do something with mocked signed event
expect(event.id).to.equal(payload.id);

However, I do not see such method on the webhook package.

Is there any way we can achieve the same with the Go library?

Upvotes: 1

Views: 1293

Answers (2)

Gregor Zurowski
Gregor Zurowski

Reputation: 2346

A new GenerateTestSignedPayload function (in the webhook package) has been added with the Stripe Go SDK version v73.1.0, see PR #1520. This provides similar functionality to Stripe Node SDK's generateTestHeaderString.

The Testing Webhook signing section in the README provides an example on how to use it:

signedPayload := webhook.GenerateTestSignedPayload(&webhook.UnsignedPayload{Payload: payloadBytes, Secret: testSecret})
event, err := webhook.ConstructEvent(signedPayload.Payload, signedPayload.Header, signedPayload.Secret)

Upvotes: 3

orakaro
orakaro

Reputation: 1971

It looks like Stripe Go SDK doesn't support mocking Webhook. You can either implement one yourself referencing the Node implementation, or use a separated mock server (stripe-ruby-mock) that supports webhook mocking.

Context from this stripe-mock issue.

Upvotes: 2

Related Questions