feat(iap/apple): 添加 Apple IAP 交易绑定的详细日志记录
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 6m9s
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 6m9s
添加详细的日志记录以跟踪 Apple IAP 交易绑定过程中的关键步骤和错误情况,包括验签结果、幂等检查、商品映射解析、订阅到期时间计算等,便于问题排查和调试
This commit is contained in:
parent
041417a177
commit
9944ab7b8a
@ -38,19 +38,25 @@ func NewAttachTransactionLogic(ctx context.Context, svcCtx *svc.ServiceContext)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *AttachTransactionLogic) Attach(req *types.AttachAppleTransactionRequest) (*types.AttachAppleTransactionResponse, error) {
|
func (l *AttachTransactionLogic) Attach(req *types.AttachAppleTransactionRequest) (*types.AttachAppleTransactionResponse, error) {
|
||||||
|
l.Infow("开始绑定 Apple IAP 交易", logger.Field("orderNo", req.OrderNo))
|
||||||
u, ok := l.ctx.Value(constant.CtxKeyUser).(*user.User)
|
u, ok := l.ctx.Value(constant.CtxKeyUser).(*user.User)
|
||||||
if !ok || u == nil {
|
if !ok || u == nil {
|
||||||
|
l.Errorw("无效访问,用户信息缺失")
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "invalid access")
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "invalid access")
|
||||||
}
|
}
|
||||||
txPayload, err := iapapple.VerifyTransactionJWS(req.SignedTransactionJWS)
|
txPayload, err := iapapple.VerifyTransactionJWS(req.SignedTransactionJWS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
l.Errorw("JWS 验签失败", logger.Field("error", err.Error()))
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "invalid jws")
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "invalid jws")
|
||||||
}
|
}
|
||||||
|
l.Infow("JWS 验签成功", logger.Field("productId", txPayload.ProductId), logger.Field("originalTransactionId", txPayload.OriginalTransactionId), logger.Field("purchaseAt", txPayload.PurchaseDate))
|
||||||
// idempotency: check existing transaction by original id
|
// idempotency: check existing transaction by original id
|
||||||
var existTx *iapmodel.Transaction
|
var existTx *iapmodel.Transaction
|
||||||
existTx, _ = iapmodel.NewModel(l.svcCtx.DB, l.svcCtx.Redis).FindByOriginalId(l.ctx, txPayload.OriginalTransactionId)
|
existTx, _ = iapmodel.NewModel(l.svcCtx.DB, l.svcCtx.Redis).FindByOriginalId(l.ctx, txPayload.OriginalTransactionId)
|
||||||
|
l.Infow("幂等等检查", logger.Field("originalTransactionId", txPayload.OriginalTransactionId), logger.Field("exists", existTx != nil && existTx.Id > 0))
|
||||||
pm, _ := iapapple.ParseProductMap(l.svcCtx.Config.Site.CustomData)
|
pm, _ := iapapple.ParseProductMap(l.svcCtx.Config.Site.CustomData)
|
||||||
m, ok := pm.Items[txPayload.ProductId]
|
m, ok := pm.Items[txPayload.ProductId]
|
||||||
|
l.Infow("商品映射解析", logger.Field("items_count", len(pm.Items)), logger.Field("命中映射", ok))
|
||||||
var duration int64
|
var duration int64
|
||||||
var tier string
|
var tier string
|
||||||
var subscribeId int64
|
var subscribeId int64
|
||||||
@ -58,12 +64,16 @@ func (l *AttachTransactionLogic) Attach(req *types.AttachAppleTransactionRequest
|
|||||||
duration = m.DurationDays
|
duration = m.DurationDays
|
||||||
tier = m.Tier
|
tier = m.Tier
|
||||||
subscribeId = m.SubscribeId
|
subscribeId = m.SubscribeId
|
||||||
|
l.Infow("命中商品映射", logger.Field("productId", txPayload.ProductId), logger.Field("durationDays", duration), logger.Field("tier", tier), logger.Field("subscribeId", subscribeId))
|
||||||
} else {
|
} else {
|
||||||
// fallback from order_no if provided
|
// fallback from order_no if provided
|
||||||
if req.OrderNo != "" {
|
if req.OrderNo != "" {
|
||||||
if ord, e := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo); e == nil && ord != nil && ord.Id != 0 {
|
if ord, e := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo); e == nil && ord != nil && ord.Id != 0 {
|
||||||
duration = ord.Quantity
|
duration = ord.Quantity
|
||||||
subscribeId = ord.SubscribeId
|
subscribeId = ord.SubscribeId
|
||||||
|
l.Infow("使用订单信息回退", logger.Field("orderNo", req.OrderNo), logger.Field("durationDays", duration), logger.Field("subscribeId", subscribeId))
|
||||||
|
} else {
|
||||||
|
l.Infow("订单信息不可用,尝试请求参数回退", logger.Field("orderNo", req.OrderNo))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// final fallback: use request fields
|
// final fallback: use request fields
|
||||||
@ -76,17 +86,21 @@ func (l *AttachTransactionLogic) Attach(req *types.AttachAppleTransactionRequest
|
|||||||
if subscribeId <= 0 {
|
if subscribeId <= 0 {
|
||||||
subscribeId = req.SubscribeId
|
subscribeId = req.SubscribeId
|
||||||
}
|
}
|
||||||
|
l.Infow("使用请求参数回退", logger.Field("durationDays", duration), logger.Field("tier", tier), logger.Field("subscribeId", subscribeId))
|
||||||
if duration <= 0 || subscribeId <= 0 {
|
if duration <= 0 || subscribeId <= 0 {
|
||||||
|
l.Errorw("商品识别失败", logger.Field("durationDays", duration), logger.Field("tier", tier), logger.Field("subscribeId", subscribeId))
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "unknown product")
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "unknown product")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exp := iapapple.CalcExpire(txPayload.PurchaseDate, duration)
|
exp := iapapple.CalcExpire(txPayload.PurchaseDate, duration)
|
||||||
|
l.Infow("计算订阅到期时间", logger.Field("expireAt", exp), logger.Field("expireUnix", exp.Unix()))
|
||||||
|
|
||||||
if existTx != nil && existTx.Id > 0 {
|
if existTx != nil && existTx.Id > 0 {
|
||||||
token := fmt.Sprintf("iap:%s", txPayload.OriginalTransactionId)
|
token := fmt.Sprintf("iap:%s", txPayload.OriginalTransactionId)
|
||||||
existSub, err := l.svcCtx.UserModel.FindOneSubscribeByToken(l.ctx, token)
|
existSub, err := l.svcCtx.UserModel.FindOneSubscribeByToken(l.ctx, token)
|
||||||
if err == nil && existSub != nil && existSub.Id > 0 {
|
if err == nil && existSub != nil && existSub.Id > 0 {
|
||||||
// Already processed, return success
|
// Already processed, return success
|
||||||
|
l.Infow("事务已处理,直接返回", logger.Field("originalTransactionId", txPayload.OriginalTransactionId), logger.Field("tier", tier), logger.Field("expiresAt", exp.Unix()))
|
||||||
return &types.AttachAppleTransactionResponse{
|
return &types.AttachAppleTransactionResponse{
|
||||||
ExpiresAt: exp.Unix(),
|
ExpiresAt: exp.Unix(),
|
||||||
Tier: tier,
|
Tier: tier,
|
||||||
@ -96,6 +110,7 @@ func (l *AttachTransactionLogic) Attach(req *types.AttachAppleTransactionRequest
|
|||||||
|
|
||||||
sum := sha256.Sum256([]byte(req.SignedTransactionJWS))
|
sum := sha256.Sum256([]byte(req.SignedTransactionJWS))
|
||||||
jwsHash := hex.EncodeToString(sum[:])
|
jwsHash := hex.EncodeToString(sum[:])
|
||||||
|
l.Infow("准备写入事务记录", logger.Field("userId", u.Id), logger.Field("transactionId", txPayload.TransactionId), logger.Field("originalTransactionId", txPayload.OriginalTransactionId), logger.Field("productId", txPayload.ProductId), logger.Field("jwsHash", jwsHash))
|
||||||
iapTx := &iapmodel.Transaction{
|
iapTx := &iapmodel.Transaction{
|
||||||
UserId: u.Id,
|
UserId: u.Id,
|
||||||
OriginalTransactionId: txPayload.OriginalTransactionId,
|
OriginalTransactionId: txPayload.OriginalTransactionId,
|
||||||
@ -108,8 +123,10 @@ func (l *AttachTransactionLogic) Attach(req *types.AttachAppleTransactionRequest
|
|||||||
err = l.svcCtx.DB.Transaction(func(tx *gorm.DB) error {
|
err = l.svcCtx.DB.Transaction(func(tx *gorm.DB) error {
|
||||||
if existTx == nil || existTx.Id == 0 {
|
if existTx == nil || existTx.Id == 0 {
|
||||||
if e := tx.Model(&iapmodel.Transaction{}).Create(iapTx).Error; e != nil {
|
if e := tx.Model(&iapmodel.Transaction{}).Create(iapTx).Error; e != nil {
|
||||||
|
l.Errorw("写入事务表失败", logger.Field("error", e.Error()))
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
l.Infow("写入事务表成功", logger.Field("id", iapTx.Id))
|
||||||
}
|
}
|
||||||
// insert user_subscribe
|
// insert user_subscribe
|
||||||
userSub := user.Subscribe{
|
userSub := user.Subscribe{
|
||||||
@ -125,19 +142,24 @@ func (l *AttachTransactionLogic) Attach(req *types.AttachAppleTransactionRequest
|
|||||||
Status: 1,
|
Status: 1,
|
||||||
}
|
}
|
||||||
if e := l.svcCtx.UserModel.InsertSubscribe(l.ctx, &userSub, tx); e != nil {
|
if e := l.svcCtx.UserModel.InsertSubscribe(l.ctx, &userSub, tx); e != nil {
|
||||||
|
l.Errorw("写入用户订阅失败", logger.Field("error", e.Error()))
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
l.Infow("写入用户订阅成功", logger.Field("userId", u.Id), logger.Field("subscribeId", subscribeId), logger.Field("expireUnix", exp.Unix()))
|
||||||
// optional: mark related order as paid and enqueue activation
|
// optional: mark related order as paid and enqueue activation
|
||||||
if req.OrderNo != "" {
|
if req.OrderNo != "" {
|
||||||
orderInfo, e := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
|
orderInfo, e := l.svcCtx.OrderModel.FindOneByOrderNo(l.ctx, req.OrderNo)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
// do not fail transaction if order not found; just continue
|
// do not fail transaction if order not found; just continue
|
||||||
|
l.Infow("订单不存在或查询失败,跳过订单状态更新", logger.Field("orderNo", req.OrderNo))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if orderInfo.Status == 1 {
|
if orderInfo.Status == 1 {
|
||||||
if e := l.svcCtx.OrderModel.UpdateOrderStatus(l.ctx, req.OrderNo, 2, tx); e != nil {
|
if e := l.svcCtx.OrderModel.UpdateOrderStatus(l.ctx, req.OrderNo, 2, tx); e != nil {
|
||||||
|
l.Errorw("更新订单状态失败", logger.Field("orderNo", req.OrderNo), logger.Field("error", e.Error()))
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
l.Infow("更新订单状态成功", logger.Field("orderNo", req.OrderNo), logger.Field("status", 2))
|
||||||
}
|
}
|
||||||
// enqueue activation regardless (idempotent handler downstream)
|
// enqueue activation regardless (idempotent handler downstream)
|
||||||
payload := queueType.ForthwithActivateOrderPayload{OrderNo: req.OrderNo}
|
payload := queueType.ForthwithActivateOrderPayload{OrderNo: req.OrderNo}
|
||||||
@ -146,13 +168,17 @@ func (l *AttachTransactionLogic) Attach(req *types.AttachAppleTransactionRequest
|
|||||||
if _, e := l.svcCtx.Queue.EnqueueContext(l.ctx, task); e != nil {
|
if _, e := l.svcCtx.Queue.EnqueueContext(l.ctx, task); e != nil {
|
||||||
// non-fatal
|
// non-fatal
|
||||||
l.Errorw("enqueue activate task error", logger.Field("error", e.Error()))
|
l.Errorw("enqueue activate task error", logger.Field("error", e.Error()))
|
||||||
|
} else {
|
||||||
|
l.Infow("已加入订单激活队列", logger.Field("orderNo", req.OrderNo))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
l.Errorw("绑定事务提交失败", logger.Field("error", err.Error()))
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "insert error: %v", err.Error())
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DatabaseInsertError), "insert error: %v", err.Error())
|
||||||
}
|
}
|
||||||
|
l.Infow("绑定完成", logger.Field("userId", u.Id), logger.Field("tier", tier), logger.Field("expiresAt", exp.Unix()))
|
||||||
return &types.AttachAppleTransactionResponse{
|
return &types.AttachAppleTransactionResponse{
|
||||||
ExpiresAt: exp.Unix(),
|
ExpiresAt: exp.Unix(),
|
||||||
Tier: tier,
|
Tier: tier,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user