英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论