fix:支付

This commit is contained in:
momo
2023-09-22 16:27:17 +08:00
parent ddb4ec944d
commit 7e400ed761
7 changed files with 303 additions and 148 deletions

View File

@@ -3,8 +3,10 @@ package logic
import (
"encoding/json"
"errors"
"fmt"
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/service/repositories"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"time"
@@ -52,7 +54,7 @@ func (l *StripeWebhookLogic) StripeWebhook(req *types.StripeWebhookReq, userinfo
event := stripe.Event{}
if err := json.Unmarshal(req.Payload, &event); err != nil {
logc.Errorf(l.ctx, "StripeWebhookLogic StripeWebhook Unmarshal err:", err)
logc.Errorf(l.ctx, "StripeWebhookLogic StripeWebhook Unmarshal err:%v", err)
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail")
}
@@ -106,7 +108,7 @@ func (l *StripeWebhookLogic) StripeWebhook(req *types.StripeWebhookReq, userinfo
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)
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")
@@ -230,119 +232,27 @@ func (l *StripeWebhookLogic) handlePaymentSessionCompleted(sessionId string, tra
}
// 付款成功
func (l *StripeWebhookLogic) HandlePaymentIntentSucceeded(paymentIntent *stripe.PaymentIntent) error {
orderSn, ok := paymentIntent.Metadata["order_sn"]
if !ok || orderSn == "" {
return errors.New("order_sn not found")
func (l *StripeWebhookLogic) HandlePaymentIntentSucceeded(paymentIntent *stripe.PaymentIntent, eventId string) error {
// 支付成功
if paymentIntent.Status == "succeeded" {
model, ok := paymentIntent.Metadata["model"]
if !ok {
err := errors.New("model is empty")
logc.Errorf(l.ctx, "PaymentSuccessful failed param, eventId:%s,err:%v", eventId, err)
return err
}
switch model {
case "product_order":
res, err := l.svcCtx.Repositories.NewOrder.PaymentSuccessful(l.ctx, &repositories.PaymentSuccessfulReq{
EventId: eventId,
PaymentMethod: 1,
PaymentIntent: paymentIntent,
})
if err != nil {
return err
}
fmt.Println(res)
}
}
return nil
// 查询订单
// 支付成功
// if paymentIntent.Status == "succeeded" {
// 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)
// }
// }
// }
// ctx := l.ctx
// err = l.svcCtx.MysqlConn.Transaction(func(connGorm *gorm.DB) error {
// // 更新支付信息
// payModelT := gmodel.NewFsPayModel(connGorm)
// *payInfo.PayStatus = 1
// *payInfo.PayTime = nowTime
// *payInfo.CardNo = card
// *payInfo.Brand = brand
// *payInfo.TradeNo = paymentIntent.ID
// _, err = payModelT.RBCreateOrUpdate(ctx, payInfo)
// if err != nil {
// return err
// }
// // 更新设计数据
// productDesignModelT := gmodel.NewFsProductDesignModel(connGorm)
// productDesignModelTRSB := productDesignModelT.BuilderTrans(ctx, nil)
// var isPay int64 = 1
// err = productDesignModelT.RBUpdateByIds(productDesignModelTRSB, designIds, &gmodel.FsProductDesign{IsPay: &isPay})
// if err != nil {
// return err
// }
// var orderInfo = &gmodel.FsOrder{}
// var orderStatus int64
// var orderIsPartPay int64
// var orderPayedAmount int64
// var orderIsPayCompleted int64
// // 支付记录是首款
// if *payInfo.PayStage == int64(constants.PAYSTAGE_DEPOSIT) {
// orderStatus = int64(constants.STATUS_NEW_PART_PAY)
// orderIsPartPay = 1
// orderInfo.IsPartPay = &orderIsPartPay
// orderPayedAmount = paymentIntent.Amount
// // 删除购物车
// cartModelT := gmodel.NewFsCartModel(connGorm)
// cartModelTRSB := cartModelT.BuilderTrans(ctx, nil)
// err = cartModelT.RBDeleteCartsByIds(cartModelTRSB, cartIds)
// if err != nil {
// return err
// }
// }
// // 支付记录是尾款
// if *payInfo.PayStage == int64(constants.PAYSTAGE_REMAINING) {
// if *fsOrderRelInfo.Status < int64(constants.STATUS_NEW_PAY_COMPLETED) {
// orderStatus = int64(constants.STATUS_NEW_PAY_COMPLETED)
// }
// orderIsPayCompleted = 1
// orderInfo.IsPayCompleted = &orderIsPayCompleted
// orderPayedAmount = *fsOrderRelInfo.PayedAmount + paymentIntent.Amount
// }
// // 更新订单信息
// orderInfo.Id = fsOrderRelInfo.Id
// orderInfo.Status = &orderStatus
// orderInfo.Ptime = &nowTime
// orderInfo.PayedAmount = &orderPayedAmount
// orderModelT := gmodel.NewFsOrderModel(connGorm)
// err = orderModelT.RBUpdate(ctx, orderInfo)
// if err != nil {
// return err
// }
// return err
// })
// if err != nil {
// return err
// }
//千人千面的处理
// $renderServer = (new RenderService());
// $renderServer->thousandsFacesV2($order->id);
// //清除用户最新的设计
// $cache = \Yii::$app->cache;
// $cache->delete(CacheConfigHelper::LAST_DESIGN . $order->user_id);
// //缓存最新订单编号
// $cache->set(CacheConfigHelper::USER_ORDERNO . $order->user_id, $order->sn);
// //查询用户邮箱信息
// $user = \api\models\User::find()->where(['id' => $order->user_id])->one();
// $redisData = [
// 'key' => 'receipt_download',
// 'param' => [
// 'email' => $user->email,
// 'order_id' => $order->id,
// 'pay_id' => $pay->id,
// 'type' => 1,//付款成功为1
// ]
// ];
// Email::timely($redisData);
}

View File

@@ -18,8 +18,9 @@ type ServiceContext struct {
Config config.Config
SharedState *shared.SharedState
MysqlConn *gorm.DB
AllModels *gmodel.AllModelsGen
MysqlConn *gorm.DB
AllModels *gmodel.AllModelsGen
Repositories *initalize.Repositories
}
func NewServiceContext(c config.Config) *ServiceContext {
@@ -31,6 +32,9 @@ func NewServiceContext(c config.Config) *ServiceContext {
MysqlConn: conn,
SharedState: nil,
AllModels: gmodel.NewAllModels(initalize.InitMysql(c.SourceMysql)),
Repositories: initalize.NewAllRepositories(&initalize.NewAllRepositorieData{
GormDB: conn,
}),
}
}