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
fmt.Println("事件类型", 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")
// }
var charge stripe.Charge
err := json.Unmarshal(event.Data.Raw, &charge)
if err != nil {
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")
}
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":
// checkout checkout.session.completed 处理逻辑
// 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")
// }
case "payment_intent.succeeded":
var paymentIntent stripe.PaymentIntent
err := json.Unmarshal(event.Data.Raw, &paymentIntent)
if err != nil {
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")
}
err = l.HandlePaymentIntentSucceeded(&paymentIntent, 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")
}
// var paymentIntent stripe.PaymentIntent
// err := json.Unmarshal(event.Data.Raw, &paymentIntent)
// if err != nil {
// 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")
// }
// err = l.HandlePaymentIntentSucceeded(&paymentIntent, 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 "payment_method.attached":
var paymentMethod stripe.PaymentMethod
err := json.Unmarshal(event.Data.Raw, &paymentMethod)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type payment_method.attached")
}
// var paymentMethod stripe.PaymentMethod
// err := json.Unmarshal(event.Data.Raw, &paymentMethod)
// if err != nil {
// logx.Error(err)
// return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type payment_method.attached")
// }
case "charge.refunded":
var chargeRefunded stripe.Charge
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 {
fmt.Println(paymentIntent)
func (l *StripeWebhookLogic) HandleChargeSucceeded(charge *stripe.Charge, eventId string) error {
// 支付成功
if paymentIntent.Status == "succeeded" {
model, ok := paymentIntent.Metadata["model"]
if charge.Status == "succeeded" {
model, ok := charge.Metadata["model"]
if !ok {
err := errors.New("model is empty")
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{
EventId: eventId,
PaymentMethod: 1,
PaymentIntent: paymentIntent,
Charge: charge,
})
if err != nil {
return err

View File

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