Stripe Webhooks在Go中的测试

huangapple go评论104阅读模式
英文:

Stripe Webhooks Testing in Go

问题

如何使用Stripe的Go库模拟Stripe对我的Webhooks的请求?

Node库中有一个stripe.webhooks.generateTestHeaderString方法,可以模拟这样的请求:

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);

// 使用模拟的签名事件进行操作
expect(event.id).to.equal(payload.id);

然而,在Go库的webhook包中我没有看到这样的方法。

是否有任何方法可以使用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?

答案1

得分: 2

新的GenerateTestSignedPayload函数已经添加到webhook包中,该函数是在Stripe Go SDK版本v73.1.0中添加的,详见PR #1520。该函数提供了与Stripe Node SDK的generateTestHeaderString类似的功能。

README中的Testing Webhook signing section提供了如何使用该函数的示例:

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

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)

答案2

得分: 1

看起来 Stripe Go SDK 不支持模拟 Webhook。你可以自己实现一个,参考Node 实现,或者使用一个支持模拟 Webhook 的独立模拟服务器(stripe-ruby-mock)。

这个stripe-mock 问题提供了一些背景信息。

英文:

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.

huangapple
  • 本文由 发表于 2022年1月17日 03:08:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/70733308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定