From 6c2fba2b3aec301ffea1bcb9b4cc01a52ae384cb Mon Sep 17 00:00:00 2001 From: momo <1012651275@qq.com> Date: Tue, 26 Sep 2023 10:56:57 +0800 Subject: [PATCH 1/8] =?UTF-8?q?fix:=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- constants/orders.go | 30 ++++---- .../pay/internal/logic/stripewebhooklogic.go | 42 +++++----- service/repositories/order.go | 76 ++++++++++++++----- 3 files changed, 93 insertions(+), 55 deletions(-) diff --git a/constants/orders.go b/constants/orders.go index c9eb037b..8556c674 100644 --- a/constants/orders.go +++ b/constants/orders.go @@ -56,18 +56,20 @@ const ( type OrderStatusCode int64 const ( - ORDERSTATUSUNPAIDDEPOSIT OrderStatusCode = 0 // 0,未支付定金 - ORDERSTATUSDIRECTMAILORDERED OrderStatusCode = 10100 // 10100,直邮单--已下单 - ORDERSTATUSDIRECTMAILCANCEL OrderStatusCode = 10101 // 10101,直邮单--已取消 - ORDERSTATUSDIRECTMAILSTARTPRODUCTION OrderStatusCode = 10200 // 10200,直邮单--开始生产 - ORDERSTATUSDIRECTMAILCOMPLETEPRODUCTION OrderStatusCode = 10300 // 10300,直邮单--生产完成 - ORDERSTATUSDIRECTMAILSHIPPED OrderStatusCode = 10400 // 10400,直邮单--已发货 - ORDERSTATUSDIRECTMAILARRIVED OrderStatusCode = 10500 // 10500,直邮单--已到达 - ORDERSTATUSCLOUDSTOREORDERED OrderStatusCode = 20100 // 20100,云仓单--已下单 - ORDERSTATUSCLOUDSTORECANCEL OrderStatusCode = 20101 // 20101,云仓单--已取消 - ORDERSTATUSCLOUDSTORESTARTPRODUCTION OrderStatusCode = 20200 // 20200,云仓单--开始生产 - ORDERSTATUSCLOUDSTOREOMPLETEPRODUCTION OrderStatusCode = 20300 // 20300,云仓单--生产完成 - ORDERSTATUSCLOUDSTOREARRIVEDWAREHOUSE OrderStatusCode = 20400 // 20400,云仓单--直达仓库 + ORDERSTATUSUNPAIDDEPOSIT OrderStatusCode = 0 // 0,未支付定金 + ORDERSTATUSDIRECTMAILORDERED OrderStatusCode = 10100 // 10100,直邮单--已下单 + ORDERSTATUSDIRECTMAILORDEREDMAINING OrderStatusCode = 10100001 // 10100001,直邮单--已下单--尾款 + ORDERSTATUSDIRECTMAILCANCEL OrderStatusCode = 10101 // 10101,直邮单--已取消 + ORDERSTATUSDIRECTMAILSTARTPRODUCTION OrderStatusCode = 10200 // 10200,直邮单--开始生产 + ORDERSTATUSDIRECTMAILCOMPLETEPRODUCTION OrderStatusCode = 10300 // 10300,直邮单--生产完成 + ORDERSTATUSDIRECTMAILSHIPPED OrderStatusCode = 10400 // 10400,直邮单--已发货 + ORDERSTATUSDIRECTMAILARRIVED OrderStatusCode = 10500 // 10500,直邮单--已到达 + ORDERSTATUSCLOUDSTOREORDERED OrderStatusCode = 20100 // 20100,云仓单--已下单 + ORDERSTATUSCLOUDSTOREORDEREDMAINING OrderStatusCode = 20100001 // 20100001,云仓单--已下单-尾款 + ORDERSTATUSCLOUDSTORECANCEL OrderStatusCode = 20101 // 20101,云仓单--已取消 + ORDERSTATUSCLOUDSTORESTARTPRODUCTION OrderStatusCode = 20200 // 20200,云仓单--开始生产 + ORDERSTATUSCLOUDSTOREOMPLETEPRODUCTION OrderStatusCode = 20300 // 20300,云仓单--生产完成 + ORDERSTATUSCLOUDSTOREARRIVEDWAREHOUSE OrderStatusCode = 20400 // 20400,云仓单--直达仓库 ) // 订单状态名称 @@ -85,8 +87,8 @@ var OrderStatusUserCLOUDSTORE []OrderStatusCode func init() { // 订单状态名称 PayStatusMessage = make(map[PayStatusCode]string) - PayStatusMessage[PAYSTATUSUNPAID] = "Paid" - PayStatusMessage[PAYSTATUSPAID] = "Unpaid" + PayStatusMessage[PAYSTATUSUNPAID] = "Unpaid" + PayStatusMessage[PAYSTATUSPAID] = "Paid" PayStatusMessage[PAYSTATUSREFUNDED] = "Refunded" // 订单状态名称 diff --git a/server/pay/internal/logic/stripewebhooklogic.go b/server/pay/internal/logic/stripewebhooklogic.go index e061cb32..b96966ec 100644 --- a/server/pay/internal/logic/stripewebhooklogic.go +++ b/server/pay/internal/logic/stripewebhooklogic.go @@ -219,26 +219,26 @@ func (l *StripeWebhookLogic) HandleChargeRefunded(chargeRefunded *stripe.Charge) } // 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) 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) HandleChargeSucceeded(charge *stripe.Charge, eventId string) error { @@ -254,7 +254,7 @@ func (l *StripeWebhookLogic) HandleChargeSucceeded(charge *stripe.Charge, eventI case "product_order": res, err := l.svcCtx.Repositories.NewOrder.PaymentSuccessful(l.ctx, &repositories.PaymentSuccessfulReq{ EventId: eventId, - PaymentMethod: 1, + PaymentMethod: "stripe", Charge: charge, }) if err != nil { diff --git a/service/repositories/order.go b/service/repositories/order.go index 7fe9544e..be8c51d4 100644 --- a/service/repositories/order.go +++ b/service/repositories/order.go @@ -47,7 +47,7 @@ type ( } PayInfo struct { - PayMethod int64 `json:"pay_method"` // 交易方式 + PayMethod string `json:"pay_method"` // 交易方式 PayTime time.Time `json:"pay_time"` // 支付时间 Status gmodel.PayStatus `json:"status"` // 当前状态 StatusLink []gmodel.PayStatus `json:"status_link"` // 状态链路 @@ -79,7 +79,7 @@ type ( /* 支付成功 */ PaymentSuccessfulReq struct { EventId string - PaymentMethod int64 + PaymentMethod string Charge *stripe.Charge } PaymentSuccessfulRes struct{} @@ -174,6 +174,15 @@ func (d *defaultOrder) PaymentSuccessful(ctx context.Context, in *PaymentSuccess var payAmount int64 var payTitle string var payTime time.Time + + var paymentMethod int64 = 1 + if in.PaymentMethod == "stripe" { + paymentMethod = 1 + } + if in.PaymentMethod == "paypal" { + paymentMethod = 2 + } + if in.Charge != nil { charge := in.Charge orderSn, ok = charge.Metadata["order_sn"] @@ -238,20 +247,30 @@ func (d *defaultOrder) PaymentSuccessful(ctx context.Context, in *PaymentSuccess payInfo.PayMethod = in.PaymentMethod payInfo.PayTime = payTime payInfo.TradeNo = tradeSn - + // 当前状态 + var statusCode constants.OrderStatusCode + var statusCodePre constants.OrderStatusCode if payStage == "deposit" { + if *orderInfo.DeliveryMethod == constants.DELIVERYMETHODDIRECTMAIL { + // 直邮 + statusCode = constants.ORDERSTATUSDIRECTMAILORDERED + } + if *orderInfo.DeliveryMethod == constants.DELIVERYMETHODDSCLOUDSTORE { + // 云仓 + statusCode = constants.ORDERSTATUSCLOUDSTOREORDERED + } payStageInt = 1 orderPayStatusCode = constants.ORDERPAYSTATUSPAIDDEPOSIT status = gmodel.OrderStatus{ Ctime: &ntime, Utime: &ntime, - StatusCode: constants.ORDERSTATUSDIRECTMAILORDERED, - StatusTitle: constants.OrderStatusMessage[constants.ORDERSTATUSDIRECTMAILORDERED], + StatusCode: statusCode, + StatusTitle: constants.OrderStatusMessage[statusCode], } statusLink = order.UpdateOrderStatusLink(ress.OrderDetailOriginal.OrderInfo.StatusLink, gmodel.OrderStatus{ Utime: &ntime, - StatusCode: constants.ORDERSTATUSDIRECTMAILORDERED, - StatusTitle: constants.OrderStatusMessage[constants.ORDERSTATUSDIRECTMAILORDERED], + StatusCode: statusCode, + StatusTitle: constants.OrderStatusMessage[statusCode], }) payInfo.Status = gmodel.PayStatus{ StatusCode: int64(constants.PAYSTATUSPAID), @@ -265,19 +284,38 @@ func (d *defaultOrder) PaymentSuccessful(ctx context.Context, in *PaymentSuccess } } if payStage == "remaining_balance" { + if *orderInfo.DeliveryMethod == constants.DELIVERYMETHODDIRECTMAIL { + // 直邮 + statusCodePre = constants.ORDERSTATUSDIRECTMAILORDERED + statusCode = constants.ORDERSTATUSDIRECTMAILORDEREDMAINING + } + if *orderInfo.DeliveryMethod == constants.DELIVERYMETHODDSCLOUDSTORE { + // 云仓 + statusCodePre = constants.ORDERSTATUSCLOUDSTOREORDERED + statusCode = constants.ORDERSTATUSCLOUDSTOREORDEREDMAINING + } payStageInt = 2 orderPayStatusCode = constants.ORDERPAYSTATUSPAIDDREMAINING - status = gmodel.OrderStatus{ - Ctime: &ntime, - Utime: &ntime, - StatusCode: constants.ORDERSTATUSCLOUDSTOREORDERED, - StatusTitle: constants.OrderStatusMessage[constants.ORDERSTATUSCLOUDSTOREORDERED], + + var statusChildren []*gmodel.OrderStatus + // 更新订单状态链路--子状态 + for oStatusLinkKey, oStatusLink := range ress.OrderDetail.OrderInfo.StatusLink { + if oStatusLink.StatusCode == statusCodePre { + statusChildren = append(oStatusLink.Children, &gmodel.OrderStatus{ + Ctime: &ntime, + Utime: &ntime, + StatusCode: statusCode, + StatusTitle: constants.OrderStatusMessage[statusCode], + }) + ress.OrderDetail.OrderInfo.StatusLink[oStatusLinkKey].Children = statusChildren + } } - statusLink = order.UpdateOrderStatusLink(ress.OrderDetailOriginal.OrderInfo.StatusLink, gmodel.OrderStatus{ - Utime: &ntime, - StatusCode: constants.ORDERSTATUSCLOUDSTOREORDERED, - StatusTitle: constants.OrderStatusMessage[constants.ORDERSTATUSCLOUDSTOREORDERED], - }) + + if ress.OrderDetail.OrderInfo.Status.StatusCode == constants.ORDERSTATUSDIRECTMAILORDERED || ress.OrderDetail.OrderInfo.Status.StatusCode == constants.ORDERSTATUSCLOUDSTOREORDERED { + status = ress.OrderDetail.OrderInfo.Status + status.Children = statusChildren + } + payInfo.StatusLink = append(ress.OrderDetail.OrderAmount.RemainingBalance.StatusLink, payInfo.Status) uOrderDetail["order_amount"] = struct { RemainingBalance PayInfo `json:"remaining_balance"` @@ -294,7 +332,7 @@ func (d *defaultOrder) PaymentSuccessful(ctx context.Context, in *PaymentSuccess TradeSn: &tradeSn, PayAmount: &payAmount, PayStatus: &payStatus, - PaymentMethod: &in.PaymentMethod, + PaymentMethod: &paymentMethod, PayStage: &payStageInt, CardSn: &card, CardBrand: &brand, @@ -1037,7 +1075,6 @@ func (d *defaultOrder) OrderDetailHandler(ctx context.Context, orderInfo *gmodel orderDetail.OrderProduct[orderProductKey].TotalPrice = order.GetAmountInfoFormat(&orderProduct.TotalPrice) orderDetail.OrderProduct[orderProductKey].TotalPrice = order.GetAmountInfoFormat(&orderProduct.TotalPrice) orderDetail.OrderProduct[orderProductKey].PurchaseQuantity = order.GetPurchaseQuantity(&orderProduct.PurchaseQuantity) - orderDetail.OrderProduct[orderProductKey].ShoppingCartSnapshot = nil orderDetail.OrderProduct[orderProductKey].ProductSnapshot = nil } orderDetail.OrderInfo.StatusLink = order.GetOrderStatusLinkUser(orderDetail.OrderInfo.DeliveryMethod, orderDetail.OrderInfo.StatusLink) @@ -1046,7 +1083,6 @@ func (d *defaultOrder) OrderDetailHandler(ctx context.Context, orderInfo *gmodel orderDetail.OrderAmount.Subtotal = order.GetAmountInfoFormat(&orderDetail.OrderAmount.Subtotal) orderDetail.OrderAmount.Total = order.GetAmountInfoFormat(&orderDetail.OrderAmount.Total) orderDetail.PayTimeout = time.Duration(orderDetail.OrderInfo.Ctime.Add(orderDetail.PayTimeout).UTC().Unix() - time.Now().UTC().Unix()) - } return &DetailRes{ From 3bba720f676766543cb61771accb90fcee9e67f7 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 10:58:24 +0800 Subject: [PATCH 2/8] info address --- model/gmodel/fs_address_logic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index 7440e159..ad22851d 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -94,7 +94,7 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i return err } - err = tx.Where(" `user_id` = ? and `status` = ? and `address_id != ? and `ltime` > ?` ", userId, 1, addressId, now.Add(time.Hour*24*365)). + err = tx.Where(" `user_id` = ? and `status` = ? and `address_id` != ? and `ltime` > ? ", userId, 1, addressId, now.Add(time.Hour*24*365)). UpdateColumn("ltime", now).Error if err != nil { logx.Error(err) From c12675698b815c35ce628c09327b86a41894e239 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:01:42 +0800 Subject: [PATCH 3/8] info address --- model/gmodel/fs_address_logic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index ad22851d..936843dc 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -87,7 +87,7 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i now := time.Now().UTC() - err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id = ?` ", userId, 1, addressId). + err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id` = ? ", userId, 1, addressId). UpdateColumn("ltime", now.Add(time.Hour*24*365*100)). UpdateColumn("utime", now).Error if err != nil { From 2331979b657fa574eb5e3b8b9d4cd08888e7fbd0 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:04:12 +0800 Subject: [PATCH 4/8] info address --- model/gmodel/fs_address_logic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index 936843dc..c633e89d 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -88,7 +88,7 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i now := time.Now().UTC() err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id` = ? ", userId, 1, addressId). - UpdateColumn("ltime", now.Add(time.Hour*24*365*100)). + UpdateColumn("ltime", now.Add(time.Hour*24*365*50)). UpdateColumn("utime", now).Error if err != nil { return err From d0323646c5fa59ff522bf1ab9bc8205834a147d7 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:05:31 +0800 Subject: [PATCH 5/8] info address --- model/gmodel/fs_address_logic.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index c633e89d..561e01c7 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -88,13 +88,13 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i now := time.Now().UTC() err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id` = ? ", userId, 1, addressId). - UpdateColumn("ltime", now.Add(time.Hour*24*365*50)). + UpdateColumn("ltime", now.Add(time.Hour*24*365)). UpdateColumn("utime", now).Error if err != nil { return err } - err = tx.Where(" `user_id` = ? and `status` = ? and `address_id` != ? and `ltime` > ? ", userId, 1, addressId, now.Add(time.Hour*24*365)). + err = tx.Where(" `user_id` = ? and `status` = ? and `address_id` != ? and `ltime` > ? ", userId, 1, addressId, now.Add(time.Hour*24*128)). UpdateColumn("ltime", now).Error if err != nil { logx.Error(err) From 0e282e741bfd0146079b7b8aad97a0e1332847e5 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:06:37 +0800 Subject: [PATCH 6/8] info address --- model/gmodel/fs_address_logic.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index 561e01c7..3bfeb8ed 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -88,13 +88,13 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i now := time.Now().UTC() err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id` = ? ", userId, 1, addressId). - UpdateColumn("ltime", now.Add(time.Hour*24*365)). + UpdateColumn("ltime", now.Add(time.Hour*24*365*20)). UpdateColumn("utime", now).Error if err != nil { return err } - err = tx.Where(" `user_id` = ? and `status` = ? and `address_id` != ? and `ltime` > ? ", userId, 1, addressId, now.Add(time.Hour*24*128)). + err = tx.Where(" `user_id` = ? and `status` = ? and `address_id` != ? and `ltime` > ? ", userId, 1, addressId, now.Add(time.Hour*24*365)). UpdateColumn("ltime", now).Error if err != nil { logx.Error(err) From a832a6fa817e341c2df29409f5a58952e4d85743 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:07:45 +0800 Subject: [PATCH 7/8] info address --- model/gmodel/fs_address_logic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index 3bfeb8ed..397ac55c 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -88,7 +88,7 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i now := time.Now().UTC() err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id` = ? ", userId, 1, addressId). - UpdateColumn("ltime", now.Add(time.Hour*24*365*20)). + UpdateColumn("ltime", now.Add(time.Hour*24*365*10)). UpdateColumn("utime", now).Error if err != nil { return err From 87ff25a2e611bfeb553f6af30e0d516d61c0dc7a Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Tue, 26 Sep 2023 11:08:32 +0800 Subject: [PATCH 8/8] info address --- model/gmodel/fs_address_logic.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/model/gmodel/fs_address_logic.go b/model/gmodel/fs_address_logic.go index 397ac55c..c633e89d 100755 --- a/model/gmodel/fs_address_logic.go +++ b/model/gmodel/fs_address_logic.go @@ -88,7 +88,7 @@ func (a *FsAddressModel) SettingUserDefaultAddress(ctx context.Context, userId i now := time.Now().UTC() err = tx.Model(&FsAddress{}).Where(" `user_id` = ? and `status` = ? and `address_id` = ? ", userId, 1, addressId). - UpdateColumn("ltime", now.Add(time.Hour*24*365*10)). + UpdateColumn("ltime", now.Add(time.Hour*24*365*50)). UpdateColumn("utime", now).Error if err != nil { return err