feat(iap/apple): 增强商品ID解析逻辑以支持多种时间单位
扩展商品ID解析功能,支持从day/month/year格式中提取单位和数量 根据不同的时间单位计算对应的订阅时长
This commit is contained in:
parent
5d7ca4b9bd
commit
e98709b511
@ -58,21 +58,40 @@ func (l *AttachTransactionLogic) Attach(req *types.AttachAppleTransactionRequest
|
||||
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))
|
||||
|
||||
// 从 Apple 商品ID中解析购买数量(天数),形如 com.hifastvpn.plan.day7 -> 7
|
||||
// 解析 Apple 商品ID中的单位与数量:支持 dayN / monthN / yearN
|
||||
var parsedUnit string
|
||||
var parsedQuantity int64
|
||||
if idx := strings.Index(strings.ToLower(txPayload.ProductId), "day"); idx >= 0 {
|
||||
sub := txPayload.ProductId[idx+3:]
|
||||
for i := 0; i < len(sub); i++ {
|
||||
if sub[i] < '0' || sub[i] > '9' {
|
||||
sub = sub[:i]
|
||||
break
|
||||
{
|
||||
pid := strings.ToLower(txPayload.ProductId)
|
||||
var unitIdx = -1
|
||||
var unitLen = 0
|
||||
if i := strings.Index(pid, "day"); i >= 0 {
|
||||
unitIdx, unitLen, parsedUnit = i, 3, "Day"
|
||||
}
|
||||
if i := strings.Index(pid, "month"); i >= 0 {
|
||||
if unitIdx == -1 || i < unitIdx {
|
||||
unitIdx, unitLen, parsedUnit = i, 5, "Month"
|
||||
}
|
||||
}
|
||||
if q, e := strconv.ParseInt(sub, 10, 64); e == nil && q > 0 {
|
||||
parsedQuantity = q
|
||||
if i := strings.Index(pid, "year"); i >= 0 {
|
||||
if unitIdx == -1 || i < unitIdx {
|
||||
unitIdx, unitLen, parsedUnit = i, 4, "Year"
|
||||
}
|
||||
}
|
||||
if unitIdx >= 0 {
|
||||
sub := pid[unitIdx+unitLen:]
|
||||
for i := 0; i < len(sub); i++ {
|
||||
if sub[i] < '0' || sub[i] > '9' {
|
||||
sub = sub[:i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if q, e := strconv.ParseInt(sub, 10, 64); e == nil && q > 0 {
|
||||
parsedQuantity = q
|
||||
}
|
||||
}
|
||||
}
|
||||
l.Infow("商品映射解析", logger.Field("productId", txPayload.ProductId), logger.Field("解析数量", parsedQuantity))
|
||||
l.Infow("商品映射解析", logger.Field("productId", txPayload.ProductId), logger.Field("解析单位", parsedUnit), logger.Field("解析数量", parsedQuantity))
|
||||
|
||||
// 基于订阅列表的折扣配置做匹配:UnitTime=Day 且 Discount.quantity == parsedQuantity
|
||||
var duration int64
|
||||
@ -88,7 +107,7 @@ func (l *AttachTransactionLogic) Attach(req *types.AttachAppleTransactionRequest
|
||||
})
|
||||
if e == nil && len(subs) > 0 {
|
||||
for _, item := range subs {
|
||||
if !strings.EqualFold(item.UnitTime, "Day") {
|
||||
if parsedUnit != "" && !strings.EqualFold(item.UnitTime, parsedUnit) {
|
||||
continue
|
||||
}
|
||||
var discounts []types.SubscribeDiscount
|
||||
@ -97,7 +116,16 @@ func (l *AttachTransactionLogic) Attach(req *types.AttachAppleTransactionRequest
|
||||
}
|
||||
for _, d := range discounts {
|
||||
if int64(d.Quantity) == parsedQuantity {
|
||||
duration = parsedQuantity
|
||||
switch parsedUnit {
|
||||
case "Day":
|
||||
duration = parsedQuantity
|
||||
case "Month":
|
||||
duration = parsedQuantity * 30
|
||||
case "Year":
|
||||
duration = parsedQuantity * 365
|
||||
default:
|
||||
duration = parsedQuantity
|
||||
}
|
||||
subscribeId = item.Id
|
||||
tier = item.Name
|
||||
l.Infow("订阅映射命中", logger.Field("subscribeId", subscribeId), logger.Field("name", tier), logger.Field("durationDays", duration))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user