This commit is contained in:
@@ -107,7 +107,7 @@ func (l *ActivateOrderLogic) ProcessTask(ctx context.Context, task *asynq.Task)
|
||||
logger.Field("order_type", orderInfo.Type),
|
||||
logger.Field("user_id", orderInfo.UserId))
|
||||
|
||||
if err = l.processOrderByType(ctx, orderInfo); err != nil {
|
||||
if err = l.processOrderByType(ctx, orderInfo, payload.IAPExpireAt); err != nil {
|
||||
logger.WithContext(ctx).Error("[ActivateOrderLogic] 处理订单失败,将重试",
|
||||
logger.Field("order_no", orderInfo.OrderNo),
|
||||
logger.Field("order_type", orderInfo.Type),
|
||||
@@ -169,12 +169,12 @@ func (l *ActivateOrderLogic) validateAndGetOrder(ctx context.Context, orderNo st
|
||||
}
|
||||
|
||||
// processOrderByType routes order processing based on the order type
|
||||
func (l *ActivateOrderLogic) processOrderByType(ctx context.Context, orderInfo *order.Order) error {
|
||||
func (l *ActivateOrderLogic) processOrderByType(ctx context.Context, orderInfo *order.Order, iapExpireAt int64) error {
|
||||
switch orderInfo.Type {
|
||||
case OrderTypeSubscribe:
|
||||
return l.NewPurchase(ctx, orderInfo)
|
||||
case OrderTypeRenewal:
|
||||
return l.Renewal(ctx, orderInfo)
|
||||
return l.Renewal(ctx, orderInfo, iapExpireAt)
|
||||
case OrderTypeResetTraffic:
|
||||
return l.ResetTraffic(ctx, orderInfo)
|
||||
case OrderTypeRecharge:
|
||||
@@ -716,7 +716,7 @@ func (l *ActivateOrderLogic) clearServerCache(ctx context.Context, sub *subscrib
|
||||
|
||||
// Renewal handles subscription renewal including subscription extension,
|
||||
// traffic reset (if configured), commission processing, and notifications
|
||||
func (l *ActivateOrderLogic) Renewal(ctx context.Context, orderInfo *order.Order) error {
|
||||
func (l *ActivateOrderLogic) Renewal(ctx context.Context, orderInfo *order.Order, iapExpireAt int64) error {
|
||||
userInfo, err := l.getExistingUser(ctx, orderInfo.UserId)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -732,8 +732,16 @@ func (l *ActivateOrderLogic) Renewal(ctx context.Context, orderInfo *order.Order
|
||||
return err
|
||||
}
|
||||
|
||||
if err = l.updateSubscriptionForRenewal(ctx, userSub, sub, orderInfo); err != nil {
|
||||
return err
|
||||
if iapExpireAt > 0 {
|
||||
// Apple IAP 续费:attachTransactionLogic 已通过 payload 传入 Apple 端计算的到期时间,
|
||||
// 直接使用该时间,避免在现有 expire_time 基础上再叠加天数导致双重计算。
|
||||
if err = l.updateSubscriptionWithIAPExpire(ctx, userSub, sub, iapExpireAt); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err = l.updateSubscriptionForRenewal(ctx, userSub, sub, orderInfo); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Clear user subscription cache
|
||||
@@ -768,6 +776,33 @@ func (l *ActivateOrderLogic) getUserSubscription(ctx context.Context, token stri
|
||||
return userSub, nil
|
||||
}
|
||||
|
||||
// updateSubscriptionWithIAPExpire 用于 Apple IAP 续费:直接将 Apple 服务端计算的
|
||||
// 到期时间写入订阅,同时处理流量重置和 FinishedAt 清零,不再叠加天数。
|
||||
func (l *ActivateOrderLogic) updateSubscriptionWithIAPExpire(ctx context.Context, userSub *user.Subscribe, sub *subscribe.Subscribe, iapExpireAt int64) error {
|
||||
now := time.Now()
|
||||
newExpire := time.Unix(iapExpireAt, 0)
|
||||
today := now.Day()
|
||||
resetDay := newExpire.Day()
|
||||
|
||||
if sub.RenewalReset != nil && *sub.RenewalReset || today == resetDay {
|
||||
userSub.Download = 0
|
||||
userSub.Upload = 0
|
||||
}
|
||||
|
||||
if userSub.FinishedAt != nil {
|
||||
userSub.FinishedAt = nil
|
||||
}
|
||||
|
||||
userSub.ExpireTime = newExpire
|
||||
userSub.Status = 1
|
||||
|
||||
if err := l.svc.UserModel.UpdateSubscribe(ctx, userSub); err != nil {
|
||||
logger.WithContext(ctx).Error("Update user subscribe (IAP) failed", logger.Field("error", err.Error()))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// updateSubscriptionForRenewal updates subscription details for renewal including
|
||||
// expiration time extension and traffic reset if configured
|
||||
func (l *ActivateOrderLogic) updateSubscriptionForRenewal(ctx context.Context, userSub *user.Subscribe, sub *subscribe.Subscribe, orderInfo *order.Order) error {
|
||||
|
||||
Reference in New Issue
Block a user