(c *gin.Context)
| 122 | } |
| 123 | |
| 124 | func StripeWebhook(c *gin.Context) { |
| 125 | payload, err := io.ReadAll(c.Request.Body) |
| 126 | if err != nil { |
| 127 | log.Printf("解析Stripe Webhook参数失败: %v\n", err) |
| 128 | c.AbortWithStatus(http.StatusServiceUnavailable) |
| 129 | return |
| 130 | } |
| 131 | |
| 132 | signature := c.GetHeader("Stripe-Signature") |
| 133 | endpointSecret := setting.StripeWebhookSecret |
| 134 | event, err := webhook.ConstructEventWithOptions(payload, signature, endpointSecret, webhook.ConstructEventOptions{ |
| 135 | IgnoreAPIVersionMismatch: true, |
| 136 | }) |
| 137 | |
| 138 | if err != nil { |
| 139 | log.Printf("Stripe Webhook验签失败: %v\n", err) |
| 140 | c.AbortWithStatus(http.StatusBadRequest) |
| 141 | return |
| 142 | } |
| 143 | |
| 144 | switch event.Type { |
| 145 | case stripe.EventTypeCheckoutSessionCompleted: |
| 146 | sessionCompleted(event) |
| 147 | case stripe.EventTypeCheckoutSessionExpired: |
| 148 | sessionExpired(event) |
| 149 | default: |
| 150 | log.Printf("不支持的Stripe Webhook事件类型: %s\n", event.Type) |
| 151 | } |
| 152 | |
| 153 | c.Status(http.StatusOK) |
| 154 | } |
| 155 | |
| 156 | func sessionCompleted(event stripe.Event) { |
| 157 | customerId := event.GetObjectValue("customer") |
nothing calls this directly
no test coverage detected