stripe.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package stripe
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "go-nc/configs/global"
  6. "go-nc/model"
  7. "os"
  8. "github.com/gin-gonic/gin"
  9. "github.com/stripe/stripe-go/v81"
  10. "github.com/stripe/stripe-go/v81/checkout/session"
  11. "github.com/stripe/stripe-go/v81/webhook"
  12. "go.uber.org/zap"
  13. )
  14. var Key = "sk_test_51NnEmyIOY9tb2VoEzu6KzLXZDFAcb9MWyCqzTBlRGsj7I9slB2P4JZjf4FBNCRrlNEJibKnPd4hnZjXPAXW0tZ8R00bJhHPBPT"
  15. // 向stripe支付获取clientSecret给客户端取支付,ID为订单标识
  16. func Pay(amount int64, currency, order_id, title string) (string, error) {
  17. stripe.Key = Key
  18. domain := "http://localhost:8088"
  19. if os.Getenv("ENV") == "production" {
  20. domain = "https://app.ainets.net"
  21. }
  22. params := &stripe.CheckoutSessionParams{
  23. PaymentIntentData: &stripe.CheckoutSessionPaymentIntentDataParams{
  24. Metadata: map[string]string{"orderId": order_id},
  25. },
  26. LineItems: []*stripe.CheckoutSessionLineItemParams{
  27. {
  28. PriceData: &stripe.CheckoutSessionLineItemPriceDataParams{
  29. UnitAmount: stripe.Int64(amount),
  30. Currency: stripe.String(currency),
  31. ProductData: &stripe.CheckoutSessionLineItemPriceDataProductDataParams{
  32. Name: stripe.String(title),
  33. },
  34. },
  35. Quantity: stripe.Int64(1),
  36. },
  37. },
  38. Mode: stripe.String(string(stripe.CheckoutSessionModePayment)),
  39. SuccessURL: stripe.String(domain + "?success=true"), // 成功跳转
  40. CancelURL: stripe.String(domain + "?canceled=true"), // 取消跳转
  41. Metadata: map[string]string{"orderId": order_id},
  42. }
  43. s, err := session.New(params)
  44. if err != nil {
  45. return "", err
  46. }
  47. fmt.Println("result", s)
  48. return s.URL, nil
  49. }
  50. func WebhookHandler(c *gin.Context) {
  51. endpointSecret := "whsec_DQSUSF38MGjZflylKKgdwIeUxkpmg86N"
  52. payload, err := c.GetRawData()
  53. if err != nil {
  54. // 处理错误
  55. global.App.Log.Error("Webhook 获取数据失败", zap.Error(err))
  56. return
  57. }
  58. event, err := webhook.ConstructEvent(payload, c.GetHeader("Stripe-Signature"), endpointSecret)
  59. if err != nil {
  60. global.App.Log.Error("Webhook 验证失败", zap.Error(err))
  61. return
  62. }
  63. var paymentIntent stripe.PaymentIntent
  64. err = json.Unmarshal(event.Data.Raw, &paymentIntent)
  65. if err != nil {
  66. global.App.Log.Error("Webhook 解析数据失败", zap.Error(err))
  67. return
  68. }
  69. orderId := paymentIntent.Metadata["orderId"]
  70. // 成功
  71. if event.Type == "charge.succeeded" {
  72. global.App.DB.Model(&model.Pay_order{}).Where("order_id = ?", orderId).Update("pay_status", "SUCCESS")
  73. }
  74. // 失败
  75. if event.Type == "charge.failed" {
  76. global.App.DB.Model(&model.Pay_order{}).Where("order_id = ?", orderId).Update("pay_status", "FAIL")
  77. }
  78. // 付款过期
  79. if event.Type == "charge.expires" {
  80. global.App.DB.Model(&model.Pay_order{}).Where("order_id = ?", orderId).Update("pay_status", "EXPIRED")
  81. }
  82. }
  83. // 创建Webhook
  84. // func CreateWebhook(){
  85. // stripe.Key = Key
  86. // params := &stripe.WebhookEndpointParams{
  87. // EnabledEvents: []*string{ // 指定要监听的事件
  88. // stripe.String("charge.succeeded"), // 付款成功
  89. // stripe.String("charge.failed"), // 付款失败
  90. // stripe.String("charge.expires"), // 付款过期
  91. // },
  92. // URL: stripe.String("http://sim.ainets.net:8083/api/hooks/stripe/webhook"),
  93. // }
  94. // result, err := webhookendpoint.New(params)
  95. // if err != nil {
  96. // fmt.Println(err)
  97. // return
  98. // }
  99. // // 打印全部数据
  100. // fmt.Printf("%+v\n", result)
  101. // }