123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package stripe
- import (
- "encoding/json"
- "fmt"
- "go-nc/configs/global"
- "go-nc/model"
- "os"
- "github.com/gin-gonic/gin"
- "github.com/stripe/stripe-go/v81"
- "github.com/stripe/stripe-go/v81/checkout/session"
- "github.com/stripe/stripe-go/v81/webhook"
- "go.uber.org/zap"
- )
- var Key = "sk_test_51NnEmyIOY9tb2VoEzu6KzLXZDFAcb9MWyCqzTBlRGsj7I9slB2P4JZjf4FBNCRrlNEJibKnPd4hnZjXPAXW0tZ8R00bJhHPBPT"
- // 向stripe支付获取clientSecret给客户端取支付,ID为订单标识
- func Pay(amount int64, currency, order_id, title string) (string, error) {
- stripe.Key = Key
- domain := "http://localhost:8088"
- if os.Getenv("ENV") == "production" {
- domain = "https://app.ainets.net"
- }
- params := &stripe.CheckoutSessionParams{
- PaymentIntentData: &stripe.CheckoutSessionPaymentIntentDataParams{
- Metadata: map[string]string{"orderId": order_id},
- },
- LineItems: []*stripe.CheckoutSessionLineItemParams{
- {
- PriceData: &stripe.CheckoutSessionLineItemPriceDataParams{
- UnitAmount: stripe.Int64(amount),
- Currency: stripe.String(currency),
- ProductData: &stripe.CheckoutSessionLineItemPriceDataProductDataParams{
- Name: stripe.String(title),
- },
- },
- Quantity: stripe.Int64(1),
- },
- },
- Mode: stripe.String(string(stripe.CheckoutSessionModePayment)),
- SuccessURL: stripe.String(domain + "?success=true"), // 成功跳转
- CancelURL: stripe.String(domain + "?canceled=true"), // 取消跳转
- Metadata: map[string]string{"orderId": order_id},
- }
- s, err := session.New(params)
- if err != nil {
- return "", err
- }
- fmt.Println("result", s)
- return s.URL, nil
- }
- func WebhookHandler(c *gin.Context) {
- endpointSecret := "whsec_DQSUSF38MGjZflylKKgdwIeUxkpmg86N"
- payload, err := c.GetRawData()
- if err != nil {
- // 处理错误
- global.App.Log.Error("Webhook 获取数据失败", zap.Error(err))
- return
- }
- event, err := webhook.ConstructEvent(payload, c.GetHeader("Stripe-Signature"), endpointSecret)
- if err != nil {
- global.App.Log.Error("Webhook 验证失败", zap.Error(err))
- return
- }
- var paymentIntent stripe.PaymentIntent
- err = json.Unmarshal(event.Data.Raw, &paymentIntent)
- if err != nil {
- global.App.Log.Error("Webhook 解析数据失败", zap.Error(err))
- return
- }
- orderId := paymentIntent.Metadata["orderId"]
- // 成功
- if event.Type == "charge.succeeded" {
- global.App.DB.Model(&model.Pay_order{}).Where("order_id = ?", orderId).Update("pay_status", "SUCCESS")
- }
- // 失败
- if event.Type == "charge.failed" {
- global.App.DB.Model(&model.Pay_order{}).Where("order_id = ?", orderId).Update("pay_status", "FAIL")
- }
- // 付款过期
- if event.Type == "charge.expires" {
- global.App.DB.Model(&model.Pay_order{}).Where("order_id = ?", orderId).Update("pay_status", "EXPIRED")
- }
- }
- // 创建Webhook
- // func CreateWebhook(){
- // stripe.Key = Key
- // params := &stripe.WebhookEndpointParams{
- // EnabledEvents: []*string{ // 指定要监听的事件
- // stripe.String("charge.succeeded"), // 付款成功
- // stripe.String("charge.failed"), // 付款失败
- // stripe.String("charge.expires"), // 付款过期
- // },
- // URL: stripe.String("http://sim.ainets.net:8083/api/hooks/stripe/webhook"),
- // }
- // result, err := webhookendpoint.New(params)
- // if err != nil {
- // fmt.Println(err)
- // return
- // }
- // // 打印全部数据
- // fmt.Printf("%+v\n", result)
- // }
|