Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into develop

This commit is contained in:
eson
2023-09-25 18:42:53 +08:00
11 changed files with 157 additions and 246 deletions

View File

@@ -16,8 +16,8 @@ import (
"fusenapi/server/pay/internal/svc"
"fusenapi/server/pay/internal/types"
"github.com/stripe/stripe-go/v74"
"github.com/stripe/stripe-go/v74/webhook"
"github.com/stripe/stripe-go/v75"
"github.com/stripe/stripe-go/v75/webhook"
"github.com/zeromicro/go-zero/core/logc"
"github.com/zeromicro/go-zero/core/logx"
)
@@ -57,12 +57,15 @@ func (l *StripeWebhookLogic) StripeWebhook(req *types.StripeWebhookReq, userinfo
logc.Errorf(l.ctx, "StripeWebhookLogic StripeWebhook Unmarshal err:%v", err)
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail")
}
// fmt.Println(req)
// fmt.Println(event)
endpointSecret := l.svcCtx.Config.PayConfig.Stripe.EndpointSecret
signatureHeader := req.StripeSignature
fmt.Println(endpointSecret)
event, err := webhook.ConstructEvent(req.Payload, signatureHeader, endpointSecret)
if err != nil {
logx.Error(err)
logc.Errorf(l.ctx, "webhook.ConstructEvent err:%v", err)
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "Webhook signature verification failed")
}
@@ -70,24 +73,30 @@ func (l *StripeWebhookLogic) StripeWebhook(req *types.StripeWebhookReq, userinfo
var payMethod = int64(constants.PAYMETHOD_STRIPE)
var nowTime = time.Now().UTC()
var eventData = []byte(event.Data.Raw)
eventType := string(event.Type)
l.HandlePayEventCreate(&gmodel.FsOrderTradeEvent{
PayMethod: &payMethod,
EventId: &event.ID,
EventType: &event.Type,
EventType: &eventType,
EventData: &eventData,
Ctime: &nowTime,
})
// 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
@@ -102,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)
@@ -232,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)
@@ -247,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

@@ -34,7 +34,6 @@ func NewGetPriceByPidLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Get
svcCtx: svcCtx,
}
}
func (l *GetPriceByPidLogic) GetPriceByPid(req *types.GetPriceByPidReq, userinfo *auth.UserInfo) (resp *basic.Response) {
req.Pid = strings.Trim(req.Pid, " ")
if req.Pid == "" {

View File

@@ -1,16 +1,10 @@
package logic
import (
"context"
"errors"
"fmt"
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/format"
"fusenapi/utils/step_price"
"strings"
"context"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
@@ -32,157 +26,18 @@ func NewGetSizeByProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *
}
}
// 获取分类下的产品以及尺寸
// 处理进入前逻辑w,r
// func (l *GetSizeByProductLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *GetSizeByProductLogic) GetSizeByProduct(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
if userinfo.GetIdType() != auth.IDTYPE_User {
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in first")
}
//获取所有网站目录
tagsModel := gmodel.NewFsTagsModel(l.svcCtx.MysqlConn)
tagsList, err := tagsModel.GetAllByLevel(l.ctx, constants.TYPE_WEBSITE)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get website tags")
}
if len(tagsList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "tag list is null")
}
tagIds := make([]int64, 0, len(tagsList))
for _, v := range tagsList {
tagIds = append(tagIds, v.Id)
}
//获取这些类型的产品
productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByIds(l.ctx, tagIds, "sort-desc")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get tag product list")
}
productIds := make([]int64, 0, len(productList))
for _, v := range productList {
productIds = append(productIds, v.Id)
}
productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeList, err := productSizeModel.GetAllByProductIds(l.ctx, productIds, "sort DESC")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product size list")
}
sizeIds := make([]int64, 0, len(productSizeList))
for _, v := range productSizeList {
sizeIds = append(sizeIds, v.Id)
}
//获取价格列表
productPriceModel := gmodel.NewFsProductPriceModel(l.svcCtx.MysqlConn)
productPriceList, err := productPriceModel.GetPriceListByProductIdsSizeIds(l.ctx, productIds, sizeIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product proce list")
}
mapProductPrice := make(map[string]gmodel.FsProductPrice)
for _, v := range productPriceList {
mapProductPrice[fmt.Sprintf("%d_%d", *v.ProductId, *v.SizeId)] = v
}
//组装返回
list := make([]types.GetSizeByProductRsp, 0, len(tagsList))
for _, tag := range tagsList {
//获取第一层子类
firstChildrenList, err := l.GetFirstChildrenList(tag, productList, productSizeList, mapProductPrice)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get first level children list")
}
data := types.GetSizeByProductRsp{
Id: tag.Id,
Name: *tag.Title,
Children: firstChildrenList,
}
list = append(list, data)
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", list)
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
return resp.SetStatus(basic.CodeOK)
}
// 第一层子层
func (l *GetSizeByProductLogic) GetFirstChildrenList(tag gmodel.FsTags, productList []gmodel.FsProduct, productSizeList []gmodel.FsProductSize, mapProductPrice map[string]gmodel.FsProductPrice) (childrenList []types.Children, err error) {
childrenList = make([]types.Children, 0, len(productList))
for _, product := range productList {
if *product.Type != tag.Id {
continue
}
childrenObjList, err := l.GetSecondChildrenList(product, productSizeList, mapProductPrice)
if err != nil {
return nil, err
}
//获取第二层子类
data := types.Children{
Id: product.Id,
Name: *product.Title,
Cycle: int(*product.DeliveryDays + *product.ProduceDays),
ChildrenList: childrenObjList,
}
childrenList = append(childrenList, data)
}
return
}
// 第2层子层
func (l *GetSizeByProductLogic) GetSecondChildrenList(product gmodel.FsProduct, productSizeList []gmodel.FsProductSize, mapProductPrice map[string]gmodel.FsProductPrice) (childrenObjList []types.ChildrenObj, err error) {
childrenObjList = make([]types.ChildrenObj, 0, len(productSizeList))
for _, productSize := range productSizeList {
if product.Id != *productSize.ProductId {
continue
}
priceList := make([]types.PriceObj, 0, len(productSizeList))
price, ok := mapProductPrice[fmt.Sprintf("%d_%d", *productSize.ProductId, productSize.Id)]
//无对应尺寸价格
if !ok {
priceList = []types.PriceObj{
{Num: 1, Price: 0},
{Num: 1, Price: 0},
{Num: 1, Price: 0},
}
childrenObjList = append(childrenObjList, types.ChildrenObj{
Id: productSize.Id,
Name: *productSize.Capacity,
PriceList: priceList,
})
continue
}
if price.StepNum == nil || price.StepPrice == nil {
continue
}
*price.StepNum = strings.Trim(*price.StepNum, " ")
*price.StepPrice = strings.Trim(*price.StepPrice, " ")
//阶梯数量切片
stepNum, err := format.StrSlicToIntSlice(strings.Split(*price.StepNum, ","))
if err != nil {
return nil, err
}
//阶梯价格切片
stepPrice, err := format.StrSlicToIntSlice(strings.Split(*price.StepPrice, ","))
if err != nil {
return nil, err
}
if len(stepNum) == 0 || len(stepPrice) == 0 {
return nil, errors.New(fmt.Sprintf("stepNum count or stepPrice count is empty: product size id :%d ,product price id :%d", productSize.Id, price.Id))
}
index := 0
// 最小购买数量小于 最大阶梯数量+5
for int(*price.MinBuyNum) < (stepNum[len(stepNum)-1]+5) && index < 3 {
priceList = append(priceList, types.PriceObj{
Num: int(*price.MinBuyNum * *price.EachBoxNum),
Price: step_price.GetStepPrice(int(*price.MinBuyNum), stepNum, stepPrice),
})
*price.MinBuyNum++
index++
}
data := types.ChildrenObj{
Id: productSize.Id,
Name: *productSize.Capacity,
PriceList: priceList,
}
childrenObjList = append(childrenObjList, data)
}
return
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *GetSizeByProductLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }

View File

@@ -440,10 +440,10 @@ type File struct {
}
type Meta struct {
TotalCount int64 `json:"totalCount"`
PageCount int64 `json:"pageCount"`
CurrentPage int `json:"currentPage"`
PerPage int `json:"perPage"`
TotalCount int64 `json:"total_count"`
PageCount int64 `json:"page_count"`
CurrentPage int `json:"current_page"`
PerPage int `json:"per_page"`
}
// Set 设置Response的Code和Message值

View File

@@ -369,7 +369,7 @@ func (w *wsConnectItem) assembleRenderDataToUnity(taskId string, combineImage st
}
//发送运行阶段消息
w.sendRenderDataToUnityStepResponseMessage(info.RenderId)
logx.Info("发送到unity成功,刀版图:", combineImage /*, " 请求unity的数据:", string(postDataBytes)*/)
logx.Info("发送到unity成功,刀版图:", combineImage , " 请求unity的数据:", string(postDataBytes))
return nil
}