支付成功回调
This commit is contained in:
@@ -97,6 +97,7 @@ func (l *OrderPaymentIntentLogic) OrderPaymentIntent(req *types.OrderPaymentInte
|
||||
|
||||
payConfig := &pay.Config{}
|
||||
var generatePrepaymentReq = &pay.GeneratePrepaymentReq{
|
||||
OrderSn: req.Sn,
|
||||
ProductName: "支付标题",
|
||||
Amount: payAmount,
|
||||
Currency: "eur",
|
||||
@@ -142,7 +143,6 @@ func (l *OrderPaymentIntentLogic) OrderPaymentIntent(req *types.OrderPaymentInte
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resData.RedirectUrl = prepaymentRes.URL
|
||||
|
||||
// 订单信息--修改
|
||||
err = gmodel.NewFsOrderModel(connGorm).Update(ctx, orderInfo)
|
||||
@@ -161,7 +161,7 @@ func (l *OrderPaymentIntentLogic) OrderPaymentIntent(req *types.OrderPaymentInte
|
||||
}
|
||||
fspay.PayAmount = &payAmount
|
||||
fspay.PayStage = &payStage
|
||||
fspay.TradeNo = &prepaymentRes.TradeNo
|
||||
//fspay.TradeNo = &prepaymentRes.TradeNo
|
||||
fspay.PaymentMethod = &req.PayMethod
|
||||
fspay.OrderSource = &orderSource
|
||||
fspay.PayStatus = &payStatus
|
||||
@@ -171,6 +171,9 @@ func (l *OrderPaymentIntentLogic) OrderPaymentIntent(req *types.OrderPaymentInte
|
||||
return err
|
||||
}
|
||||
|
||||
resData.RedirectUrl = prepaymentRes.URL
|
||||
resData.ClientSecret = prepaymentRes.ClientSecret
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
|
||||
@@ -7,8 +7,6 @@ import (
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
@@ -17,15 +15,15 @@ import (
|
||||
"fusenapi/server/pay/internal/types"
|
||||
|
||||
"github.com/stripe/stripe-go/v74"
|
||||
"github.com/stripe/stripe-go/v74/webhook"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type StripeWebhookLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
Payload []byte
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewStripeWebhookLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StripeWebhookLogic {
|
||||
@@ -37,18 +35,9 @@ func NewStripeWebhookLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Str
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
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
|
||||
}
|
||||
// func (l *StripeWebhookLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// }
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// 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)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
|
||||
stripe.Key = l.svcCtx.Config.PayConfig.Stripe.Key
|
||||
event := stripe.Event{}
|
||||
|
||||
if err := json.Unmarshal(l.Payload, &event); err != nil {
|
||||
if err := json.Unmarshal(req.Payload, &event); err != nil {
|
||||
logx.Error(err)
|
||||
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
|
||||
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":
|
||||
var paymentIntent stripe.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)
|
||||
}
|
||||
|
||||
// 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 {
|
||||
orderSn, ok := paymentIntent.Metadata["order_sn"]
|
||||
if !ok || orderSn == "" {
|
||||
return errors.New("order_sn not found")
|
||||
}
|
||||
|
||||
// 查询支付记录
|
||||
payModel := gmodel.NewFsPayModel(l.svcCtx.MysqlConn)
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -112,22 +157,27 @@ func (l *StripeWebhookLogic) handlePaymentIntentSucceeded(paymentIntent *stripe.
|
||||
//订单信息
|
||||
orderDetailTemplateModel := gmodel.NewFsOrderDetailTemplateModel(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 = 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 {
|
||||
return dbPreload.Table(orderModel.TableName()).Preload("FsOrderDetailTemplateInfo", func(dbPreload *gorm.DB) *gorm.DB {
|
||||
return dbPreload.Table(orderDetailTemplateModel.TableName()).Preload("FsProductDesignInfo")
|
||||
return dbPreload.Table(fsOrderDetailModel.TableName()).Preload("FsOrderDetailTemplateInfo", func(dbPreload *gorm.DB) *gorm.DB {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
||||
var designIds []int64
|
||||
var cartIds []int64
|
||||
if len(fsOrderRel.FsOrderDetails) > 0 {
|
||||
for _, fsOrderDetail := range fsOrderRel.FsOrderDetails {
|
||||
if len(fsOrderRelInfo.FsOrderDetails) > 0 {
|
||||
for _, fsOrderDetail := range fsOrderRelInfo.FsOrderDetails {
|
||||
if fsOrderDetail.FsOrderDetailTemplateInfo.FsProductDesignInfo.Id != 0 {
|
||||
designIds = append(designIds, fsOrderDetail.FsOrderDetailTemplateInfo.FsProductDesignInfo.Id)
|
||||
}
|
||||
@@ -139,8 +189,18 @@ func (l *StripeWebhookLogic) handlePaymentIntentSucceeded(paymentIntent *stripe.
|
||||
|
||||
// 支付成功
|
||||
if paymentIntent.Status == "succeeded" {
|
||||
var card string = paymentIntent.LatestCharge.PaymentMethodDetails.Card.Last4
|
||||
var brand string = string(paymentIntent.LatestCharge.PaymentMethodDetails.Card.Brand)
|
||||
var card string
|
||||
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) {
|
||||
// 更新支付信息
|
||||
@@ -163,12 +223,17 @@ func (l *StripeWebhookLogic) handlePaymentIntentSucceeded(paymentIntent *stripe.
|
||||
}
|
||||
|
||||
var orderInfo = &gmodel.FsOrder{}
|
||||
|
||||
var orderStatus int64
|
||||
var orderIsPartPay int64
|
||||
var orderPayedAmount int64
|
||||
var orderIsPayCompleted int64
|
||||
// 支付记录是首款
|
||||
if *payInfo.PayStage == int64(constants.PAYSTAGE_DEPOSIT) {
|
||||
*orderInfo.Status = int64(constants.STATUS_NEW_PART_PAY)
|
||||
*orderInfo.IsPartPay = 1
|
||||
*orderInfo.PayedAmount = paymentIntent.Amount
|
||||
orderStatus = int64(constants.STATUS_NEW_PART_PAY)
|
||||
orderIsPartPay = 1
|
||||
orderInfo.IsPartPay = &orderIsPartPay
|
||||
orderPayedAmount = paymentIntent.Amount
|
||||
|
||||
// 删除购物车
|
||||
cartModelT := gmodel.NewFsCartModel(connGorm)
|
||||
err = cartModelT.DeleteCartsByIds(ctx, cartIds)
|
||||
@@ -180,14 +245,18 @@ func (l *StripeWebhookLogic) handlePaymentIntentSucceeded(paymentIntent *stripe.
|
||||
// 支付记录是尾款
|
||||
if *payInfo.PayStage == int64(constants.PAYSTAGE_REMAINING) {
|
||||
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
|
||||
*orderInfo.PayedAmount = *orderInfo.PayedAmount + paymentIntent.Amount
|
||||
orderIsPayCompleted = 1
|
||||
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)
|
||||
err = orderModelT.Update(ctx, orderInfo)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user