fix:支付

This commit is contained in:
momo 2023-09-25 18:28:53 +08:00
parent 17b98f83ff
commit 95cbaea69a
2 changed files with 51 additions and 47 deletions

View File

@ -83,15 +83,20 @@ func (l *StripeWebhookLogic) StripeWebhook(req *types.StripeWebhookReq, userinfo
}) })
// Unmarshal the event data into an appropriate struct depending on its Type // Unmarshal the event data into an appropriate struct depending on its Type
fmt.Println("事件类型", event.Type)
switch event.Type { switch event.Type {
case "charge.succeeded": case "charge.succeeded":
// var charge stripe.Charge var charge stripe.Charge
// err := json.Unmarshal(event.Data.Raw, &charge) err := json.Unmarshal(event.Data.Raw, &charge)
// if err != nil { if err != nil {
// logx.Error(err) logx.Errorf("err%+vdesc%s", err, "pay notify Unmarshal fail event.Type charge.succeeded")
// return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type charge.succeeded") return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type charge.succeeded")
// } }
err = l.HandleChargeSucceeded(&charge, event.ID)
if err != nil {
logx.Errorf("err%+vdesc%s", err, "pay notify handle payment_intent.succeeded")
return resp.SetStatusWithMessage(basic.CodePaybackNotOk, "pay notify handle payment_intent.succeeded")
}
case "checkout.session.completed": case "checkout.session.completed":
// checkout checkout.session.completed 处理逻辑 // checkout checkout.session.completed 处理逻辑
// var session stripe.CheckoutSession // var session stripe.CheckoutSession
@ -106,24 +111,24 @@ func (l *StripeWebhookLogic) StripeWebhook(req *types.StripeWebhookReq, userinfo
// return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "checkout.session.completed fail") // 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)
if err != nil { // if err != nil {
logx.Errorf("err%+vdesc%s", err, "pay notify Unmarshal fail event.Type payment_intent.succeeded") // logx.Errorf("err%+vdesc%s", err, "pay notify Unmarshal fail event.Type payment_intent.succeeded")
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type payment_intent.succeeded") // return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type payment_intent.succeeded")
} // }
err = l.HandlePaymentIntentSucceeded(&paymentIntent, event.ID) // err = l.HandlePaymentIntentSucceeded(&paymentIntent, event.ID)
if err != nil { // if err != nil {
logx.Errorf("err%+vdesc%s", err, "pay notify handle payment_intent.succeeded") // logx.Errorf("err%+vdesc%s", err, "pay notify handle payment_intent.succeeded")
return resp.SetStatusWithMessage(basic.CodePaybackNotOk, "pay notify handle payment_intent.succeeded") // return resp.SetStatusWithMessage(basic.CodePaybackNotOk, "pay notify handle payment_intent.succeeded")
} // }
case "payment_method.attached": case "payment_method.attached":
var paymentMethod stripe.PaymentMethod // var paymentMethod stripe.PaymentMethod
err := json.Unmarshal(event.Data.Raw, &paymentMethod) // err := json.Unmarshal(event.Data.Raw, &paymentMethod)
if err != nil { // if err != nil {
logx.Error(err) // logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type payment_method.attached") // return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type payment_method.attached")
} // }
case "charge.refunded": case "charge.refunded":
var chargeRefunded stripe.Charge var chargeRefunded stripe.Charge
err := json.Unmarshal(event.Data.Raw, &chargeRefunded) err := json.Unmarshal(event.Data.Raw, &chargeRefunded)
@ -236,11 +241,10 @@ func (l *StripeWebhookLogic) handlePaymentSessionCompleted(sessionId string, tra
} }
// 付款成功 // 付款成功
func (l *StripeWebhookLogic) HandlePaymentIntentSucceeded(paymentIntent *stripe.PaymentIntent, eventId string) error { func (l *StripeWebhookLogic) HandleChargeSucceeded(charge *stripe.Charge, eventId string) error {
fmt.Println(paymentIntent)
// 支付成功 // 支付成功
if paymentIntent.Status == "succeeded" { if charge.Status == "succeeded" {
model, ok := paymentIntent.Metadata["model"] model, ok := charge.Metadata["model"]
if !ok { if !ok {
err := errors.New("model is empty") err := errors.New("model is empty")
logc.Errorf(l.ctx, "PaymentSuccessful failed param, eventId:%s,err:%v", eventId, err) logc.Errorf(l.ctx, "PaymentSuccessful failed param, eventId:%s,err:%v", eventId, err)
@ -251,7 +255,7 @@ func (l *StripeWebhookLogic) HandlePaymentIntentSucceeded(paymentIntent *stripe.
res, err := l.svcCtx.Repositories.NewOrder.PaymentSuccessful(l.ctx, &repositories.PaymentSuccessfulReq{ res, err := l.svcCtx.Repositories.NewOrder.PaymentSuccessful(l.ctx, &repositories.PaymentSuccessfulReq{
EventId: eventId, EventId: eventId,
PaymentMethod: 1, PaymentMethod: 1,
PaymentIntent: paymentIntent, Charge: charge,
}) })
if err != nil { if err != nil {
return err return err

View File

@ -80,7 +80,7 @@ type (
PaymentSuccessfulReq struct { PaymentSuccessfulReq struct {
EventId string EventId string
PaymentMethod int64 PaymentMethod int64
PaymentIntent *stripe.PaymentIntent Charge *stripe.Charge
} }
PaymentSuccessfulRes struct{} PaymentSuccessfulRes struct{}
/* 支付成功 */ /* 支付成功 */
@ -174,43 +174,43 @@ func (d *defaultOrder) PaymentSuccessful(ctx context.Context, in *PaymentSuccess
var payAmount int64 var payAmount int64
var payTitle string var payTitle string
var payTime time.Time var payTime time.Time
if in.PaymentIntent != nil { if in.Charge != nil {
paymentIntent := in.PaymentIntent charge := in.Charge
orderSn, ok = paymentIntent.Metadata["order_sn"] orderSn, ok = charge.Metadata["order_sn"]
if !ok || orderSn == "" { if !ok || orderSn == "" {
err = errors.New("order_sn is empty") err = errors.New("order_sn is empty")
logc.Errorf(ctx, "PaymentSuccessful failed param, eventId:%s,err:%v", in.EventId, err) logc.Errorf(ctx, "PaymentSuccessful failed param, eventId:%s,err:%v", in.EventId, err)
return &PaymentSuccessfulRes{}, err return &PaymentSuccessfulRes{}, err
} }
payStage, ok = paymentIntent.Metadata["pay_stage"] payStage, ok = charge.Metadata["pay_stage"]
if !ok || payStage == "" { if !ok || payStage == "" {
err = errors.New("pay_stage is empty") err = errors.New("pay_stage is empty")
logc.Errorf(ctx, "PaymentSuccessful failed param, eventId:%s,err:%v", in.EventId, err) logc.Errorf(ctx, "PaymentSuccessful failed param, eventId:%s,err:%v", in.EventId, err)
return &PaymentSuccessfulRes{}, err return &PaymentSuccessfulRes{}, err
} }
country, ok = paymentIntent.Metadata["country"] country, ok = charge.Metadata["country"]
if !ok || country == "" { if !ok || country == "" {
err = errors.New("country is empty") err = errors.New("country is empty")
logc.Errorf(ctx, "PaymentSuccessful failed param, eventId:%s,err:%v", in.EventId, err) logc.Errorf(ctx, "PaymentSuccessful failed param, eventId:%s,err:%v", in.EventId, err)
return &PaymentSuccessfulRes{}, err return &PaymentSuccessfulRes{}, err
} }
if paymentIntent.LatestCharge.PaymentMethodDetails != nil { if charge.PaymentMethodDetails != nil {
if paymentIntent.LatestCharge.PaymentMethodDetails.Card != nil { if charge.PaymentMethodDetails.Card != nil {
if paymentIntent.LatestCharge.PaymentMethodDetails.Card.Last4 != "" { if charge.PaymentMethodDetails.Card.Last4 != "" {
card = paymentIntent.LatestCharge.PaymentMethodDetails.Card.Last4 card = charge.PaymentMethodDetails.Card.Last4
} }
if paymentIntent.LatestCharge.PaymentMethodDetails.Card.Brand != "" { if charge.PaymentMethodDetails.Card.Brand != "" {
brand = string(paymentIntent.LatestCharge.PaymentMethodDetails.Card.Brand) brand = string(charge.PaymentMethodDetails.Card.Brand)
} }
} }
} }
if paymentIntent.Currency != "" { if charge.Currency != "" {
currency = string(paymentIntent.Currency) currency = string(charge.Currency)
} }
tradeSn = paymentIntent.ID tradeSn = charge.ID
payAmount = paymentIntent.Amount payAmount = charge.Amount
payTitle = paymentIntent.Description payTitle = charge.Description
payTime = time.Unix(paymentIntent.Created, 0) payTime = time.Unix(charge.Created, 0)
} }
err = d.MysqlConn.WithContext(ctx).Transaction(func(tx *gorm.DB) error { err = d.MysqlConn.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var orderInfo gmodel.FsOrder var orderInfo gmodel.FsOrder