支付成功回调

This commit is contained in:
Hiven 2023-07-28 11:15:42 +08:00
parent dbf500813c
commit 334ea4aa3f
13 changed files with 199 additions and 51 deletions

View File

@ -0,0 +1,27 @@
package gmodel
import (
"gorm.io/gorm"
"time"
)
// fs_resource 资源表
type FsResource struct {
ResourceId string `gorm:"primary_key;default:'';" json:"resource_id"` // 资源 ID
UserId *int64 `gorm:"index;default:0;" json:"user_id"` // 用户 ID
GuestId *int64 `gorm:"index;default:0;" json:"guest_id"` // 访客 ID
ResourceType *string `gorm:"index;default:'';" json:"resource_type"` // 资源类型
ResourceUrl *string `gorm:"default:'';" json:"resource_url"` // 资源 URL
Version *string `gorm:"index;default:'0';" json:"version"` // 版本信息
UploadedAt *time.Time `gorm:"index;default:'0000-00-00 00:00:00';" json:"uploaded_at"` // 上传时间
Metadata *string `gorm:"default:'';" json:"metadata"` // 元数据,json格式,存储图像分率
MetaKey1 *string `gorm:"index;default:'';" json:"meta_key1"` // 需要关键信息查询的自定义属性1,可以动态增加
}
type FsResourceModel struct {
db *gorm.DB
name string
}
func NewFsResourceModel(db *gorm.DB) *FsResourceModel {
return &FsResourceModel{db: db, name: "fs_resource"}
}

View File

@ -0,0 +1,2 @@
package gmodel
// TODO: 使用model的属性做你想做的

View File

@ -79,6 +79,7 @@ type AllModelsGen struct {
FsQuotationRemarkTemplate *FsQuotationRemarkTemplateModel // fs_quotation_remark_template 报价单备注模板 FsQuotationRemarkTemplate *FsQuotationRemarkTemplateModel // fs_quotation_remark_template 报价单备注模板
FsQuotationSaler *FsQuotationSalerModel // fs_quotation_saler 报价单业务员表 FsQuotationSaler *FsQuotationSalerModel // fs_quotation_saler 报价单业务员表
FsRefundReason *FsRefundReasonModel // fs_refund_reason FsRefundReason *FsRefundReasonModel // fs_refund_reason
FsResource *FsResourceModel // fs_resource 资源表
FsResources *FsResourcesModel // fs_resources 资源表 FsResources *FsResourcesModel // fs_resources 资源表
FsStandardLogo *FsStandardLogoModel // fs_standard_logo 标准logo FsStandardLogo *FsStandardLogoModel // fs_standard_logo 标准logo
FsTags *FsTagsModel // fs_tags 产品分类表 FsTags *FsTagsModel // fs_tags 产品分类表
@ -170,6 +171,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen {
FsQuotationRemarkTemplate: NewFsQuotationRemarkTemplateModel(gdb), FsQuotationRemarkTemplate: NewFsQuotationRemarkTemplateModel(gdb),
FsQuotationSaler: NewFsQuotationSalerModel(gdb), FsQuotationSaler: NewFsQuotationSalerModel(gdb),
FsRefundReason: NewFsRefundReasonModel(gdb), FsRefundReason: NewFsRefundReasonModel(gdb),
FsResource: NewFsResourceModel(gdb),
FsResources: NewFsResourcesModel(gdb), FsResources: NewFsResourcesModel(gdb),
FsStandardLogo: NewFsStandardLogoModel(gdb), FsStandardLogo: NewFsStandardLogoModel(gdb),
FsTags: NewFsTagsModel(gdb), FsTags: NewFsTagsModel(gdb),

View File

@ -1,6 +1,7 @@
Name: pay Name: pay
Host: 0.0.0.0 Host: 0.0.0.0
Port: 9915 Port: 9915
Timeout: 15000
SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest SourceMysql: fusentest:XErSYmLELKMnf3Dh@tcp(110.41.19.98:3306)/fusentest
Auth: Auth:
AccessSecret: fusen2023 AccessSecret: fusen2023
@ -9,5 +10,6 @@ Auth:
PayConfig: PayConfig:
Stripe: Stripe:
Key: "sk_test_51IisojHygnIJZeghPVSBhkwySfcyDV4SoAduIxu3J7bvSJ9cZMD96LY1LO6SpdbYquLJX5oKvgEBB67KT9pecfCy00iEC4pp9y" Key: "sk_test_51IisojHygnIJZeghPVSBhkwySfcyDV4SoAduIxu3J7bvSJ9cZMD96LY1LO6SpdbYquLJX5oKvgEBB67KT9pecfCy00iEC4pp9y"
EndpointSecret: "whsec_f5f9a121d43af3789db7459352f08cf523eb9e0fbf3381f91ba6c97c324c174d"
SuccessURL: "http://www.baidu.com" SuccessURL: "http://www.baidu.com"
CancelURL: "http://www.baidu.com" CancelURL: "http://www.baidu.com"

View File

@ -12,6 +12,7 @@ type Config struct {
Auth types.Auth Auth types.Auth
PayConfig struct { PayConfig struct {
Stripe struct { Stripe struct {
EndpointSecret string
Key string Key string
CancelURL string CancelURL string
SuccessURL string SuccessURL string

View File

@ -18,7 +18,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Handler: OrderPaymentIntentHandler(serverCtx), Handler: OrderPaymentIntentHandler(serverCtx),
}, },
{ {
Method: http.MethodGet, Method: http.MethodPost,
Path: "/api/pay/stripe-webhook", Path: "/api/pay/stripe-webhook",
Handler: StripeWebhookHandler(serverCtx), Handler: StripeWebhookHandler(serverCtx),
}, },

View File

@ -1,6 +1,7 @@
package handler package handler
import ( import (
"io"
"net/http" "net/http"
"reflect" "reflect"
@ -14,19 +15,31 @@ import (
func StripeWebhookHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { func StripeWebhookHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
var req types.StripeWebhookReq const MaxBodyBytes = int64(65536)
userinfo, err := basic.RequestParse(w, r, svcCtx, &req) r.Body = http.MaxBytesReader(w, r.Body, MaxBodyBytes)
defer r.Body.Close()
payload, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
return return
} }
var req types.StripeWebhookReq
// userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
// if err != nil {
// return
// }
req.Payload = payload
req.StripeSignature = r.Header.Get("Stripe-Signature")
// 创建一个业务逻辑层实例 // 创建一个业务逻辑层实例
l := logic.NewStripeWebhookLogic(r.Context(), svcCtx) l := logic.NewStripeWebhookLogic(r.Context(), svcCtx)
rl := reflect.ValueOf(l) rl := reflect.ValueOf(l)
basic.BeforeLogic(w, r, rl) basic.BeforeLogic(w, r, rl)
resp := l.StripeWebhook(&req, userinfo) resp := l.StripeWebhook(&req, nil)
if !basic.AfterLogic(w, r, rl, resp) { if !basic.AfterLogic(w, r, rl, resp) {
basic.NormalAfterLogic(w, r, resp) basic.NormalAfterLogic(w, r, resp)

View File

@ -97,6 +97,7 @@ func (l *OrderPaymentIntentLogic) OrderPaymentIntent(req *types.OrderPaymentInte
payConfig := &pay.Config{} payConfig := &pay.Config{}
var generatePrepaymentReq = &pay.GeneratePrepaymentReq{ var generatePrepaymentReq = &pay.GeneratePrepaymentReq{
OrderSn: req.Sn,
ProductName: "支付标题", ProductName: "支付标题",
Amount: payAmount, Amount: payAmount,
Currency: "eur", Currency: "eur",
@ -142,7 +143,6 @@ func (l *OrderPaymentIntentLogic) OrderPaymentIntent(req *types.OrderPaymentInte
if err != nil { if err != nil {
return err return err
} }
resData.RedirectUrl = prepaymentRes.URL
// 订单信息--修改 // 订单信息--修改
err = gmodel.NewFsOrderModel(connGorm).Update(ctx, orderInfo) err = gmodel.NewFsOrderModel(connGorm).Update(ctx, orderInfo)
@ -161,7 +161,7 @@ func (l *OrderPaymentIntentLogic) OrderPaymentIntent(req *types.OrderPaymentInte
} }
fspay.PayAmount = &payAmount fspay.PayAmount = &payAmount
fspay.PayStage = &payStage fspay.PayStage = &payStage
fspay.TradeNo = &prepaymentRes.TradeNo //fspay.TradeNo = &prepaymentRes.TradeNo
fspay.PaymentMethod = &req.PayMethod fspay.PaymentMethod = &req.PayMethod
fspay.OrderSource = &orderSource fspay.OrderSource = &orderSource
fspay.PayStatus = &payStatus fspay.PayStatus = &payStatus
@ -171,6 +171,9 @@ func (l *OrderPaymentIntentLogic) OrderPaymentIntent(req *types.OrderPaymentInte
return err return err
} }
resData.RedirectUrl = prepaymentRes.URL
resData.ClientSecret = prepaymentRes.ClientSecret
return nil return nil
}) })

View File

@ -7,8 +7,6 @@ import (
"fusenapi/model/gmodel" "fusenapi/model/gmodel"
"fusenapi/utils/auth" "fusenapi/utils/auth"
"fusenapi/utils/basic" "fusenapi/utils/basic"
"io"
"net/http"
"time" "time"
"context" "context"
@ -17,6 +15,7 @@ import (
"fusenapi/server/pay/internal/types" "fusenapi/server/pay/internal/types"
"github.com/stripe/stripe-go/v74" "github.com/stripe/stripe-go/v74"
"github.com/stripe/stripe-go/v74/webhook"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -25,7 +24,6 @@ type StripeWebhookLogic struct {
logx.Logger logx.Logger
ctx context.Context ctx context.Context
svcCtx *svc.ServiceContext svcCtx *svc.ServiceContext
Payload []byte
} }
func NewStripeWebhookLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StripeWebhookLogic { func NewStripeWebhookLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StripeWebhookLogic {
@ -37,18 +35,9 @@ func NewStripeWebhookLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Str
} }
// 处理进入前逻辑w,r // 处理进入前逻辑w,r
func (l *StripeWebhookLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { // func (l *StripeWebhookLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
const MaxBodyBytes = int64(65536)
r.Body = http.MaxBytesReader(w, r.Body, MaxBodyBytes) // }
defer r.Body.Close()
payload, err := io.ReadAll(r.Body)
if err != nil {
logx.Error(err)
w.WriteHeader(http.StatusServiceUnavailable)
return
}
l.Payload = payload
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 // 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *StripeWebhookLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { // func (l *StripeWebhookLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
@ -59,15 +48,45 @@ func (l *StripeWebhookLogic) StripeWebhook(req *types.StripeWebhookReq, userinfo
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null // userinfo 传入值时, 一定不为null
stripe.Key = l.svcCtx.Config.PayConfig.Stripe.Key
event := stripe.Event{} event := stripe.Event{}
if err := json.Unmarshal(l.Payload, &event); err != nil { if err := json.Unmarshal(req.Payload, &event); err != nil {
logx.Error(err) logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail") return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail")
} }
endpointSecret := l.svcCtx.Config.PayConfig.Stripe.EndpointSecret
signatureHeader := req.StripeSignature
event, err := webhook.ConstructEvent(req.Payload, signatureHeader, endpointSecret)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "Webhook signature verification failed")
}
// Unmarshal the event data into an appropriate struct depending on its Type // Unmarshal the event data into an appropriate struct depending on its Type
switch event.Type { switch event.Type {
case "charge.succeeded":
// var charge stripe.Charge
// err := json.Unmarshal(event.Data.Raw, &charge)
// if err != nil {
// logx.Error(err)
// return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type charge.succeeded")
// }
case "checkout.session.completed":
// checkout checkout.session.completed 处理逻辑
// var session stripe.CheckoutSession
// err := json.Unmarshal(event.Data.Raw, &session)
// if err != nil {
// logx.Error(err)
// return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type payment_intent.succeeded")
// }
// fmt.Println("checkout.session.completed")
// err = l.handlePaymentSessionCompleted(session.ID, session.PaymentIntent.ID)
// if err != nil {
// return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "checkout.session.completed fail")
// }
case "payment_intent.succeeded": case "payment_intent.succeeded":
var paymentIntent stripe.PaymentIntent var paymentIntent stripe.PaymentIntent
err := json.Unmarshal(event.Data.Raw, &paymentIntent) err := json.Unmarshal(event.Data.Raw, &paymentIntent)
@ -95,13 +114,39 @@ func (l *StripeWebhookLogic) StripeWebhook(req *types.StripeWebhookReq, userinfo
return resp.SetStatus(basic.CodeOK) return resp.SetStatus(basic.CodeOK)
} }
// session完成
// func (l *StripeWebhookLogic) handlePaymentSessionCompleted(sessionId string, tradeNo string) (err error) {
// // 查询支付记录
// payModel := gmodel.NewFsPayModel(l.svcCtx.MysqlConn)
// rsbPay := payModel.RowSelectBuilder(nil)
// rsbPay = rsbPay.Where("session_id = ?", sessionId)
// payInfo, err := payModel.FindOneByQuery(l.ctx, rsbPay, nil)
// if err != nil {
// return err
// }
// if *payInfo.PayStatus == 0 {
// *payInfo.TradeNo = tradeNo
// _, err = payModel.CreateOrUpdate(l.ctx, payInfo)
// if err != nil {
// return err
// }
// } else {
// return errors.New("pay status 1")
// }
// return err
// }
// 成功的付款 // 成功的付款
func (l *StripeWebhookLogic) handlePaymentIntentSucceeded(paymentIntent *stripe.PaymentIntent) error { func (l *StripeWebhookLogic) handlePaymentIntentSucceeded(paymentIntent *stripe.PaymentIntent) error {
orderSn, ok := paymentIntent.Metadata["order_sn"]
if !ok || orderSn == "" {
return errors.New("order_sn not found")
}
// 查询支付记录 // 查询支付记录
payModel := gmodel.NewFsPayModel(l.svcCtx.MysqlConn) payModel := gmodel.NewFsPayModel(l.svcCtx.MysqlConn)
rsbPay := payModel.RowSelectBuilder(nil) rsbPay := payModel.RowSelectBuilder(nil)
rsbPay = rsbPay.Where("trade_no = ?", paymentIntent.ID) rsbPay = rsbPay.Where("order_number = ?", orderSn)
payInfo, err := payModel.FindOneByQuery(l.ctx, rsbPay, nil) payInfo, err := payModel.FindOneByQuery(l.ctx, rsbPay, nil)
if err != nil { if err != nil {
return err return err
@ -112,22 +157,27 @@ func (l *StripeWebhookLogic) handlePaymentIntentSucceeded(paymentIntent *stripe.
//订单信息 //订单信息
orderDetailTemplateModel := gmodel.NewFsOrderDetailTemplateModel(l.svcCtx.MysqlConn) orderDetailTemplateModel := gmodel.NewFsOrderDetailTemplateModel(l.svcCtx.MysqlConn)
orderModel := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn) orderModel := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn)
fsOrderDetailModel := gmodel.NewFsOrderDetailModel(l.svcCtx.MysqlConn)
fsProductDesignModel := gmodel.NewFsProductDesignModel(l.svcCtx.MysqlConn)
rsbOrder := orderModel.RowSelectBuilder(nil) rsbOrder := orderModel.RowSelectBuilder(nil)
rsbOrder = rsbOrder.Where("trade_no =?", paymentIntent.ID).Preload("FsOrderDetails") rsbOrder = rsbOrder.Where("sn =?", orderSn).Preload("FsOrderDetails")
rsbOrder = rsbOrder.Preload("FsOrderDetails", func(dbPreload *gorm.DB) *gorm.DB { rsbOrder = rsbOrder.Preload("FsOrderDetails", func(dbPreload *gorm.DB) *gorm.DB {
return dbPreload.Table(orderModel.TableName()).Preload("FsOrderDetailTemplateInfo", func(dbPreload *gorm.DB) *gorm.DB { return dbPreload.Table(fsOrderDetailModel.TableName()).Preload("FsOrderDetailTemplateInfo", func(dbPreload *gorm.DB) *gorm.DB {
return dbPreload.Table(orderDetailTemplateModel.TableName()).Preload("FsProductDesignInfo") return dbPreload.Table(orderDetailTemplateModel.TableName()).Preload("FsProductDesignInfo", func(dbPreload *gorm.DB) *gorm.DB {
return dbPreload.Table(fsProductDesignModel.TableName())
}) })
}) })
fsOrderRel, err := orderModel.FindOneByQuery(l.ctx, rsbOrder, nil) })
fsOrderRelInfo, err := orderModel.FindOneByQuery(l.ctx, rsbOrder, nil)
if err != nil { if err != nil {
return err return err
} }
var designIds []int64 var designIds []int64
var cartIds []int64 var cartIds []int64
if len(fsOrderRel.FsOrderDetails) > 0 { if len(fsOrderRelInfo.FsOrderDetails) > 0 {
for _, fsOrderDetail := range fsOrderRel.FsOrderDetails { for _, fsOrderDetail := range fsOrderRelInfo.FsOrderDetails {
if fsOrderDetail.FsOrderDetailTemplateInfo.FsProductDesignInfo.Id != 0 { if fsOrderDetail.FsOrderDetailTemplateInfo.FsProductDesignInfo.Id != 0 {
designIds = append(designIds, fsOrderDetail.FsOrderDetailTemplateInfo.FsProductDesignInfo.Id) designIds = append(designIds, fsOrderDetail.FsOrderDetailTemplateInfo.FsProductDesignInfo.Id)
} }
@ -139,8 +189,18 @@ func (l *StripeWebhookLogic) handlePaymentIntentSucceeded(paymentIntent *stripe.
// 支付成功 // 支付成功
if paymentIntent.Status == "succeeded" { if paymentIntent.Status == "succeeded" {
var card string = paymentIntent.LatestCharge.PaymentMethodDetails.Card.Last4 var card string
var brand string = string(paymentIntent.LatestCharge.PaymentMethodDetails.Card.Brand) var brand string
if paymentIntent.LatestCharge.PaymentMethodDetails != nil {
if paymentIntent.LatestCharge.PaymentMethodDetails.Card != nil {
if paymentIntent.LatestCharge.PaymentMethodDetails.Card.Last4 != "" {
card = paymentIntent.LatestCharge.PaymentMethodDetails.Card.Last4
}
if paymentIntent.LatestCharge.PaymentMethodDetails.Card.Brand != "" {
brand = string(paymentIntent.LatestCharge.PaymentMethodDetails.Card.Brand)
}
}
}
err = orderModel.Trans(l.ctx, func(ctx context.Context, connGorm *gorm.DB) (err error) { err = orderModel.Trans(l.ctx, func(ctx context.Context, connGorm *gorm.DB) (err error) {
// 更新支付信息 // 更新支付信息
@ -163,12 +223,17 @@ func (l *StripeWebhookLogic) handlePaymentIntentSucceeded(paymentIntent *stripe.
} }
var orderInfo = &gmodel.FsOrder{} var orderInfo = &gmodel.FsOrder{}
var orderStatus int64
var orderIsPartPay int64
var orderPayedAmount int64
var orderIsPayCompleted int64
// 支付记录是首款 // 支付记录是首款
if *payInfo.PayStage == int64(constants.PAYSTAGE_DEPOSIT) { if *payInfo.PayStage == int64(constants.PAYSTAGE_DEPOSIT) {
*orderInfo.Status = int64(constants.STATUS_NEW_PART_PAY) orderStatus = int64(constants.STATUS_NEW_PART_PAY)
*orderInfo.IsPartPay = 1 orderIsPartPay = 1
*orderInfo.PayedAmount = paymentIntent.Amount orderInfo.IsPartPay = &orderIsPartPay
orderPayedAmount = paymentIntent.Amount
// 删除购物车 // 删除购物车
cartModelT := gmodel.NewFsCartModel(connGorm) cartModelT := gmodel.NewFsCartModel(connGorm)
err = cartModelT.DeleteCartsByIds(ctx, cartIds) err = cartModelT.DeleteCartsByIds(ctx, cartIds)
@ -180,14 +245,18 @@ func (l *StripeWebhookLogic) handlePaymentIntentSucceeded(paymentIntent *stripe.
// 支付记录是尾款 // 支付记录是尾款
if *payInfo.PayStage == int64(constants.PAYSTAGE_REMAINING) { if *payInfo.PayStage == int64(constants.PAYSTAGE_REMAINING) {
if *orderInfo.Status < int64(constants.STATUS_NEW_PAY_COMPLETED) { if *orderInfo.Status < int64(constants.STATUS_NEW_PAY_COMPLETED) {
*orderInfo.Status = int64(constants.STATUS_NEW_PAY_COMPLETED) orderStatus = int64(constants.STATUS_NEW_PAY_COMPLETED)
} }
*orderInfo.IsPayCompleted = 1 orderIsPayCompleted = 1
*orderInfo.PayedAmount = *orderInfo.PayedAmount + paymentIntent.Amount orderInfo.IsPayCompleted = &orderIsPayCompleted
orderPayedAmount = *orderInfo.PayedAmount + paymentIntent.Amount
} }
// 更新订单信息 // 更新订单信息
*orderInfo.Ptime = nowTime orderInfo.Id = fsOrderRelInfo.Id
orderInfo.Status = &orderStatus
orderInfo.Ptime = &nowTime
orderInfo.PayedAmount = &orderPayedAmount
orderModelT := gmodel.NewFsOrderModel(connGorm) orderModelT := gmodel.NewFsOrderModel(connGorm)
err = orderModelT.Update(ctx, orderInfo) err = orderModelT.Update(ctx, orderInfo)
if err != nil { if err != nil {

View File

@ -14,9 +14,12 @@ type OrderPaymentIntentReq struct {
type OrderPaymentIntentRes struct { type OrderPaymentIntentRes struct {
RedirectUrl string `json:"redirect_url"` RedirectUrl string `json:"redirect_url"`
ClientSecret string `json:"clientSecret"`
} }
type StripeWebhookReq struct { type StripeWebhookReq struct {
Payload []byte `json:"base_byte_slice,optional"`
StripeSignature string `json:"Stripe-Signature"`
} }
type Request struct { type Request struct {

View File

@ -15,7 +15,7 @@ service pay {
post /api/pay/payment-intent(OrderPaymentIntentReq) returns (response); post /api/pay/payment-intent(OrderPaymentIntentReq) returns (response);
@handler StripeWebhookHandler @handler StripeWebhookHandler
get /api/pay/stripe-webhook(StripeWebhookReq) returns (response); post /api/pay/stripe-webhook(StripeWebhookReq) returns (response);
} }
// 生成预付款 // 生成预付款
@ -28,11 +28,14 @@ type (
} }
OrderPaymentIntentRes { OrderPaymentIntentRes {
RedirectUrl string `json:"redirect_url"` RedirectUrl string `json:"redirect_url"`
ClientSecret string `json:"clientSecret"`
} }
) )
// StripeWebhook支付通知 // StripeWebhook支付通知
type ( type (
StripeWebhookReq { StripeWebhookReq {
Payload []byte `json:"base_byte_slice,optional"`
StripeSignature string `json:"Stripe-Signature"`
} }
) )

View File

@ -24,6 +24,7 @@ type Pay interface {
} }
type GeneratePrepaymentReq struct { type GeneratePrepaymentReq struct {
OrderSn string `json:"order_sn"` // 订单编号
Amount int64 `json:"amount"` // 支付金额 Amount int64 `json:"amount"` // 支付金额
Currency string `json:"currency"` // 支付货币 Currency string `json:"currency"` // 支付货币
ProductName string `json:"product_name"` // 商品名称 ProductName string `json:"product_name"` // 商品名称
@ -37,4 +38,6 @@ type GeneratePrepaymentReq struct {
type GeneratePrepaymentRes struct { type GeneratePrepaymentRes struct {
URL string `json:"url"` // 支付重定向地址 URL string `json:"url"` // 支付重定向地址
TradeNo string `json:"trade_no"` //交易ID TradeNo string `json:"trade_no"` //交易ID
ClientSecret string `json:"clientSecret"` //交易密钥
SessionId string `json:"session_id"` //SessionId
} }

View File

@ -12,6 +12,7 @@ type Stripe struct {
// 生成预付款 // 生成预付款
func (stripePay *Stripe) GeneratePrepayment(req *GeneratePrepaymentReq) (res *GeneratePrepaymentRes, err error) { func (stripePay *Stripe) GeneratePrepayment(req *GeneratePrepaymentReq) (res *GeneratePrepaymentRes, err error) {
var productData stripe.CheckoutSessionLineItemPriceDataProductDataParams var productData stripe.CheckoutSessionLineItemPriceDataProductDataParams
// productData.Metadata = map[string]string{"order_id": "33333333333333"}
if req.ProductName != "" { if req.ProductName != "" {
productData.Name = stripe.String(req.ProductName) productData.Name = stripe.String(req.ProductName)
@ -30,7 +31,14 @@ func (stripePay *Stripe) GeneratePrepayment(req *GeneratePrepaymentReq) (res *Ge
// productData.Images = images // productData.Images = images
stripe.Key = stripePay.Key stripe.Key = stripePay.Key
// session 方式
params := &stripe.CheckoutSessionParams{ params := &stripe.CheckoutSessionParams{
PaymentIntentData: &stripe.CheckoutSessionPaymentIntentDataParams{Metadata: map[string]string{"order_sn": req.OrderSn}},
// Params: stripe.Params{Metadata: map[string]string{"order_id": "1111111111111"}},
PaymentMethodTypes: stripe.StringSlice([]string{
"card",
// "ideal",
}),
LineItems: []*stripe.CheckoutSessionLineItemParams{ LineItems: []*stripe.CheckoutSessionLineItemParams{
{ {
PriceData: &stripe.CheckoutSessionLineItemPriceDataParams{ PriceData: &stripe.CheckoutSessionLineItemPriceDataParams{
@ -47,12 +55,24 @@ func (stripePay *Stripe) GeneratePrepayment(req *GeneratePrepaymentReq) (res *Ge
} }
result, err := session.New(params) result, err := session.New(params)
// 密钥方式
// params := &stripe.PaymentIntentParams{
// Amount: stripe.Int64(req.Amount),
// Currency: stripe.String(string(req.Currency)),
// AutomaticPaymentMethods: &stripe.PaymentIntentAutomaticPaymentMethodsParams{
// Enabled: stripe.Bool(true),
// },
// }
// result, err := paymentintent.New(params)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return &GeneratePrepaymentRes{ return &GeneratePrepaymentRes{
URL: result.URL, URL: result.URL,
TradeNo: result.ID, //TradeNo: result.ID,
SessionId: result.ID,
// ClientSecret: result.ClientSecret,
}, err }, err
} }