Golang解析Stripe事件数据的方法
Golang解析Stripe事件数据的方法 你好
我需要帮助来解析Stripe事件以及事件中的发票信息。
谢谢
3 回复
你是否使用像 https://github.com/stripe/stripe-go 这样的库?请告诉我们更多关于你尝试做什么,以及你已经有哪些代码。
更多关于Golang解析Stripe事件数据的方法的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
是的,我正在使用 https://github.com/stripe/stripe-go,但问题已经解决了,我之前做了一些多余的操作。 谢谢
在Golang中解析Stripe事件数据,可以使用Stripe官方提供的Go库。以下是完整的示例代码:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/stripe/stripe-go/v76"
"github.com/stripe/stripe-go/v76/webhook"
)
// 定义事件结构体
type InvoiceData struct {
ID string `json:"id"`
Amount int64 `json:"amount_paid"`
Paid bool `json:"paid"`
Customer string `json:"customer"`
}
// 解析Stripe Webhook事件
func handleStripeWebhook(w http.ResponseWriter, req *http.Request) {
const endpointSecret = "whsec_your_webhook_secret"
// 读取请求体
body, err := ioutil.ReadAll(req.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusBadRequest)
return
}
// 验证事件签名
event, err := webhook.ConstructEvent(body, req.Header.Get("Stripe-Signature"), endpointSecret)
if err != nil {
http.Error(w, "Invalid signature", http.StatusBadRequest)
return
}
// 根据事件类型处理
switch event.Type {
case "invoice.paid":
var invoice InvoiceData
err := json.Unmarshal(event.Data.Raw, &invoice)
if err != nil {
http.Error(w, "Error parsing invoice data", http.StatusBadRequest)
return
}
// 处理已支付发票
fmt.Printf("Invoice %s paid: %d cents for customer %s\n",
invoice.ID, invoice.Amount, invoice.Customer)
case "invoice.payment_failed":
var invoice InvoiceData
err := json.Unmarshal(event.Data.Raw, &invoice)
if err != nil {
http.Error(w, "Error parsing invoice data", http.StatusBadRequest)
return
}
// 处理支付失败的发票
fmt.Printf("Invoice %s payment failed for customer %s\n",
invoice.ID, invoice.Customer)
default:
fmt.Printf("Unhandled event type: %s\n", event.Type)
}
w.WriteHeader(http.StatusOK)
}
// 使用stripe-go库直接解析事件数据
func parseStripeEvent() {
// 设置Stripe API密钥
stripe.Key = "sk_test_your_secret_key"
// 模拟从Webhook接收的事件数据
eventJSON := `{
"id": "evt_1Mq7L2LkdIwHu7ixY5nR6sdy",
"type": "invoice.paid",
"data": {
"object": {
"id": "in_1Mq7KzLkdIwHu7ix5v5JKLmn",
"amount_paid": 9999,
"paid": true,
"customer": "cus_NffrFeUfNV2Hib"
}
}
}`
// 解析事件
var event stripe.Event
err := json.Unmarshal([]byte(eventJSON), &event)
if err != nil {
panic(err)
}
// 解析发票数据
var invoice stripe.Invoice
err = json.Unmarshal(event.Data.Raw, &invoice)
if err != nil {
panic(err)
}
fmt.Printf("Invoice ID: %s\n", invoice.ID)
fmt.Printf("Amount Paid: %d\n", invoice.AmountPaid)
fmt.Printf("Customer ID: %s\n", invoice.Customer.ID)
}
func main() {
// 启动Webhook服务器
http.HandleFunc("/stripe-webhook", handleStripeWebhook)
fmt.Println("Server starting on port 8080...")
http.ListenAndServe(":8080", nil)
}
安装依赖:
go get github.com/stripe/stripe-go/v76
这个示例展示了两种解析Stripe事件数据的方法:
- 通过Webhook端点接收并验证事件
- 直接解析JSON格式的事件数据
关键点:
- 使用
webhook.ConstructEvent()验证Webhook签名确保安全性 - 通过
event.Type判断事件类型 - 使用
json.Unmarshal()解析事件数据到结构体 - 可以访问发票的具体字段如
ID、AmountPaid、Customer等
根据实际需求,可以扩展InvoiceData结构体以包含更多发票字段。

