golang Stripe支付API集成客户端插件库stripe的使用
Golang Stripe支付API集成客户端插件库stripe的使用
介绍
这是Stripe官方提供的Go客户端库,用于集成Stripe支付API到你的Go应用中。
要求
- Go 1.18或更高版本
安装
确保你的项目使用了Go Modules(如果已经使用,根目录会有go.mod文件):
go mod init
然后在Go程序中引用stripe-go:
import (
"github.com/stripe/stripe-go/v82"
"github.com/stripe/stripe-go/v82/customer"
)
或者显式地获取包:
go get -u github.com/stripe/stripe-go/v82
使用示例
创建客户
sc := stripe.NewClient(apiKey)
params := &stripe.CustomerCreateParams{
Description: stripe.String("Stripe Developer"),
Email: stripe.String("gostripe@stripe.com"),
PreferredLocales: stripe.StringSlice([]string{"en", "es"}),
}
c, err := sc.V1Customers.Create(context.TODO(), params)
支付意图
sc := stripe.NewClient(apiKey)
params := &stripe.PaymentIntentListParams{
Customer: stripe.String(customer.ID),
}
for pi, err := range sc.V1PaymentIntents.List(context.TODO(), params) {
// 处理错误
// 执行操作
}
事件处理
sc := stripe.NewClient(apiKey)
for e, err := range sc.V1Events.List(context.TODO(), nil) {
// 通过e.GetObjectValue("resource_name_based_on_type", "resource_property_name")访问事件数据
// 或者通过e.Data.Object["resource_name_based_on_type"].(map[string]interface{})["resource_property_name"]访问
// 通过e.GetPreviousValue("resource_name_based_on_type", "resource_property_name")访问先前属性
// 或者通过e.Data.PreviousAttributes["resource_name_based_on_type"].(map[string]interface{})["resource_property_name"]访问
}
完整客户操作示例
import "github.com/stripe/stripe-go/v82"
// 设置
sc := stripe.NewClient("sk_key")
// 创建
c, err := sc.V1Customers.Create(context.TODO(), &stripe.CustomerCreateParams{})
// 检索
c, err := sc.V1Customers.Retrieve(context.TODO(), id, &stripe.CustomerRetrieveParams{})
// 更新
c, err := sc.V1Customers.Update(context.TODO(), id, &stripe.CustomerUpdateParams{})
// 删除
c, err := sc.V1Customers.Delete(context.TODO(), id, &stripe.CustomerDeleteParams{})
// 列表
for c, err := range sc.Customers.List(context.TODO(), &stripe.CustomerListParams{}) {
// 处理错误
// 执行操作
}
Webhook签名验证
Stripe可以对发送到你的端点的webhook事件进行签名,允许你验证它们不是由第三方发送的。
测试Webhook签名
你可以使用stripe.GenerateTestSignedPayload
来模拟来自Stripe的webhook事件:
payload := map[string]interface{}{
"id": "evt_test_webhook",
"object": "event",
"api_version": stripe.APIVersion,
}
testSecret := "whsec_test_secret"
payloadBytes, err := json.Marshal(payload)
signedPayload := stripe.GenerateTestSignedPayload(&webhook.UnsignedPayload{Payload: payloadBytes, Secret: testSecret})
event, err := stripe.ConstructEvent(signedPayload.Payload, signedPayload.Header, signedPayload.Secret)
if event.ID == payload["id"] {
// 对模拟的签名事件执行操作
} else {
// 处理无效的事件负载
}
高级用法
访问最后响应
使用任何APIResource
上的LastResponse
来查看生成当前对象的API响应:
coupon, err := sc.V1Coupons.Create(...)
requestID := coupon.LastResponse.RequestID
自动重试
库会自动在间歇性故障(如连接错误、超时或某些API响应如状态409 Conflict
)时重试请求。
自定义日志
默认情况下,库只记录错误消息(发送到stderr
)。你可以配置默认日志:
stripe.DefaultLeveledLogger = &stripe.LeveledLogger{
Level: stripe.LevelInfo,
}
扩展对象
所有stripe-go中的可扩展对象都采用完整资源结构的形式,但除非请求扩展,否则只填充该结构的ID
字段。
插件开发
如果你正在编写使用该库的插件,我们希望你使用stripe.SetAppInfo
来标识:
stripe.SetAppInfo(&stripe.AppInfo{
Name: "MyAwesomePlugin",
URL: "https://myawesomeplugin.info",
Version: "1.2.34",
})
测试
我们使用just来方便地运行开发任务。你可以直接使用它们,或者从justfile
中复制命令。
这个包依赖于stripe-mock,所以确保从后台终端获取并运行它:
go get -u github.com/stripe/stripe-mock
stripe-mock
运行所有测试:
just test
# 或者: go test ./...
运行一个包的测试:
just test ./invoice
# 或者: go test ./invoice
运行单个测试:
just test ./invoice -run TestInvoiceGet
# 或者: go test ./invoice -run TestInvoiceGet
更多关于golang Stripe支付API集成客户端插件库stripe的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复