This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
commonLogic "github.com/perfect-panel/server/internal/logic/common"
|
||||
iapmodel "github.com/perfect-panel/server/internal/model/iap/apple"
|
||||
"github.com/perfect-panel/server/internal/model/payment"
|
||||
"github.com/perfect-panel/server/internal/model/subscribe"
|
||||
"github.com/perfect-panel/server/internal/model/user"
|
||||
"github.com/perfect-panel/server/internal/svc"
|
||||
"github.com/perfect-panel/server/internal/types"
|
||||
@@ -71,18 +73,10 @@ func (l *RestoreLogic) Restore(req *types.RestoreAppleTransactionsRequest) error
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback credentials if missing (dev/debug)
|
||||
if apiCfg.PrivateKey == "" {
|
||||
apiCfg.PrivateKey = `-----BEGIN PRIVATE KEY-----
|
||||
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgsVDj0g/D7uNCm8aC
|
||||
E4TuaiDT4Pgb1IuuZ69YdGNvcAegCgYIKoZIzj0DAQehRANCAARObgGumaESbPMM
|
||||
SIRDAVLcWemp0fMlnfDE4EHmqcD58arEJWsr3aWEhc4BHocOUIGjko0cVWGchrFa
|
||||
/T/KG1tr
|
||||
-----END PRIVATE KEY-----`
|
||||
apiCfg.KeyID = "2C4X3HVPM8"
|
||||
}
|
||||
if apiCfg.IssuerID == "" {
|
||||
apiCfg.IssuerID = "34f54810-5118-4b7f-8069-c8c1e012b7a9"
|
||||
// Fallback credentials if missing
|
||||
if apiCfg.PrivateKey == "" || apiCfg.KeyID == "" || apiCfg.IssuerID == "" {
|
||||
l.Errorw("restore: apple server api credential missing", logger.Field("keyID", apiCfg.KeyID), logger.Field("issuerID", apiCfg.IssuerID), logger.Field("hasPrivateKey", apiCfg.PrivateKey != ""))
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "apple server api credential missing")
|
||||
}
|
||||
// Try to get BundleID
|
||||
if apiCfg.BundleID == "" && l.svcCtx.Config.Site.CustomData != "" {
|
||||
@@ -118,7 +112,79 @@ SIRDAVLcWemp0fMlnfDE4EHmqcD58arEJWsr3aWEhc4BHocOUIGjko0cVWGchrFa
|
||||
|
||||
m, ok := pm.Items[txp.ProductId]
|
||||
if !ok {
|
||||
continue
|
||||
// fallback: 按命名约定(day/month/year + 数字)从订阅列表匹配
|
||||
var parsedUnit string
|
||||
var parsedQuantity int64
|
||||
pid := strings.ToLower(txp.ProductId)
|
||||
parts := strings.Split(pid, ".")
|
||||
for i := len(parts) - 1; i >= 0; i-- {
|
||||
p := parts[i]
|
||||
switch {
|
||||
case strings.HasPrefix(p, "day"):
|
||||
parsedUnit = "Day"
|
||||
p = p[len("day"):]
|
||||
case strings.HasPrefix(p, "month"):
|
||||
parsedUnit = "Month"
|
||||
p = p[len("month"):]
|
||||
case strings.HasPrefix(p, "year"):
|
||||
parsedUnit = "Year"
|
||||
p = p[len("year"):]
|
||||
default:
|
||||
continue
|
||||
}
|
||||
digits := p
|
||||
for j := 0; j < len(digits); j++ {
|
||||
if digits[j] < '0' || digits[j] > '9' {
|
||||
digits = digits[:j]
|
||||
break
|
||||
}
|
||||
}
|
||||
if q, e := strconv.ParseInt(digits, 10, 64); e == nil && q > 0 {
|
||||
parsedQuantity = q
|
||||
break
|
||||
}
|
||||
}
|
||||
if parsedQuantity > 0 {
|
||||
_, subs, e := l.svcCtx.SubscribeModel.FilterList(l.ctx, &subscribe.FilterParams{
|
||||
Page: 1, Size: 9999, Show: true, Sell: true, DefaultLanguage: true,
|
||||
})
|
||||
if e == nil {
|
||||
for _, item := range subs {
|
||||
if parsedUnit != "" && !strings.EqualFold(item.UnitTime, parsedUnit) {
|
||||
continue
|
||||
}
|
||||
var discounts []types.SubscribeDiscount
|
||||
if item.Discount != "" {
|
||||
_ = json.Unmarshal([]byte(item.Discount), &discounts)
|
||||
}
|
||||
for _, d := range discounts {
|
||||
if int64(d.Quantity) == parsedQuantity {
|
||||
var dur int64
|
||||
switch parsedUnit {
|
||||
case "Day":
|
||||
dur = parsedQuantity
|
||||
case "Month":
|
||||
dur = parsedQuantity * 30
|
||||
case "Year":
|
||||
dur = parsedQuantity * 365
|
||||
default:
|
||||
dur = parsedQuantity
|
||||
}
|
||||
m = iapapple.ProductMapping{DurationDays: dur, Tier: item.Name, SubscribeId: item.Id}
|
||||
ok = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
l.Errorw("restore: product mapping not found", logger.Field("productId", txp.ProductId))
|
||||
continue
|
||||
}
|
||||
}
|
||||
// Check if already processed
|
||||
_, e := iapmodel.NewModel(l.svcCtx.DB, l.svcCtx.Redis).FindByOriginalId(l.ctx, txp.OriginalTransactionId)
|
||||
|
||||
Reference in New Issue
Block a user