- getStatusLogic 类型断言修复(*user.User) - restoreLogic 事务拆分为单条处理 + appAccountToken 解析 - attachTransactionLogic 提取 ParseProductIdDuration 共享函数 - 新增 config_helper.go 统一 Apple API 配置加载 - reconcileLogic 补充 BundleID 配置读取 - activateOrderLogic 邀请赠送天数逻辑完善 Co-Authored-By: claude-flow <ruv@ruv.net>
This commit is contained in:
@@ -4,14 +4,12 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
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"
|
||||
@@ -47,214 +45,167 @@ func (l *RestoreLogic) Restore(req *types.RestoreAppleTransactionsRequest) error
|
||||
return err
|
||||
}
|
||||
pm, _ := iapapple.ParseProductMap(l.svcCtx.Config.Site.CustomData)
|
||||
// Try to load payment config to get API credentials
|
||||
var apiCfg iapapple.ServerAPIConfig
|
||||
// We need to find *any* apple payment config to get credentials.
|
||||
// In most cases, there is only one apple payment method.
|
||||
// We can try to find by platform "apple"
|
||||
payMethods, err := l.svcCtx.PaymentModel.FindListByPlatform(l.ctx, "apple")
|
||||
if err == nil && len(payMethods) > 0 {
|
||||
// Use the first available config
|
||||
pay := payMethods[0]
|
||||
var cfg payment.AppleIAPConfig
|
||||
if err := cfg.Unmarshal([]byte(pay.Config)); err == nil {
|
||||
apiCfg = iapapple.ServerAPIConfig{
|
||||
KeyID: cfg.KeyID,
|
||||
IssuerID: cfg.IssuerID,
|
||||
PrivateKey: cfg.PrivateKey,
|
||||
Sandbox: cfg.Sandbox,
|
||||
}
|
||||
// Fix private key format if needed (same as in attachTransactionByIdLogic)
|
||||
if !strings.Contains(apiCfg.PrivateKey, "\n") && strings.Contains(apiCfg.PrivateKey, "BEGIN PRIVATE KEY") {
|
||||
apiCfg.PrivateKey = strings.ReplaceAll(apiCfg.PrivateKey, " ", "\n")
|
||||
apiCfg.PrivateKey = strings.ReplaceAll(apiCfg.PrivateKey, "-----BEGIN\nPRIVATE\nKEY-----", "-----BEGIN PRIVATE KEY-----")
|
||||
apiCfg.PrivateKey = strings.ReplaceAll(apiCfg.PrivateKey, "-----END\nPRIVATE\nKEY-----", "-----END PRIVATE KEY-----")
|
||||
}
|
||||
}
|
||||
// Load Apple Server API config from payment table
|
||||
apiCfgPtr, err := LoadAppleServerAPIConfig(l.ctx, l.svcCtx)
|
||||
if err != nil {
|
||||
l.Errorw("restore: load apple api config error", logger.Field("error", err))
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "load apple api config error: %v", err)
|
||||
}
|
||||
|
||||
// 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 != ""))
|
||||
if apiCfgPtr == nil {
|
||||
l.Errorw("restore: apple server api credential missing")
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "apple server api credential missing")
|
||||
}
|
||||
// Try to get BundleID
|
||||
if apiCfg.BundleID == "" && l.svcCtx.Config.Site.CustomData != "" {
|
||||
var customData struct {
|
||||
IapBundleId string `json:"iapBundleId"`
|
||||
apiCfg := *apiCfgPtr
|
||||
|
||||
for _, txID := range req.Transactions {
|
||||
if err := l.processSingleRestore(u, txID, pm, apiCfg); err != nil {
|
||||
l.Errorw("restore: single transaction failed", logger.Field("id", txID), logger.Field("error", err))
|
||||
// continue to next, don't return error
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *RestoreLogic) processSingleRestore(u *user.User, txID string, pm *iapapple.ProductMap, apiCfg iapapple.ServerAPIConfig) error {
|
||||
// 1. Try to verify as JWS first (if client sends JWS)
|
||||
var txp *iapapple.TransactionPayload
|
||||
var err error
|
||||
|
||||
// Try to parse as JWS
|
||||
if len(txID) > 50 && (strings.Contains(txID, ".") || strings.HasPrefix(txID, "ey")) {
|
||||
txp, err = iapapple.VerifyTransactionJWS(txID)
|
||||
} else {
|
||||
// 2. If not JWS, treat as TransactionID and fetch from Apple
|
||||
var jws string
|
||||
jws, err = iapapple.GetTransactionInfo(apiCfg, txID)
|
||||
if err == nil {
|
||||
txp, err = iapapple.VerifyTransactionJWS(jws)
|
||||
}
|
||||
_ = json.Unmarshal([]byte(l.svcCtx.Config.Site.CustomData), &customData)
|
||||
apiCfg.BundleID = customData.IapBundleId
|
||||
}
|
||||
|
||||
return l.svcCtx.DB.Transaction(func(tx *gorm.DB) error {
|
||||
for _, txID := range req.Transactions {
|
||||
// 1. Try to verify as JWS first (if client sends JWS)
|
||||
var txp *iapapple.TransactionPayload
|
||||
var err error
|
||||
if err != nil || txp == nil {
|
||||
l.Errorw("restore: invalid transaction", logger.Field("id", txID), logger.Field("error", err))
|
||||
return fmt.Errorf("invalid transaction %s: %w", txID, err)
|
||||
}
|
||||
|
||||
// Try to parse as JWS
|
||||
if len(txID) > 50 && (strings.Contains(txID, ".") || strings.HasPrefix(txID, "ey")) {
|
||||
txp, err = iapapple.VerifyTransactionJWS(txID)
|
||||
} else {
|
||||
// 2. If not JWS, treat as TransactionID and fetch from Apple
|
||||
var jws string
|
||||
jws, err = iapapple.GetTransactionInfo(apiCfg, txID)
|
||||
if err == nil {
|
||||
txp, err = iapapple.VerifyTransactionJWS(jws)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil || txp == nil {
|
||||
l.Errorw("restore: invalid transaction", logger.Field("id", txID), logger.Field("error", err))
|
||||
continue
|
||||
}
|
||||
|
||||
m, ok := pm.Items[txp.ProductId]
|
||||
if !ok {
|
||||
// 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:
|
||||
m, ok := pm.Items[txp.ProductId]
|
||||
if !ok {
|
||||
// fallback: 按命名约定(day/month/year + 数字)从订阅列表匹配
|
||||
var parsedUnit string
|
||||
var parsedQuantity int64
|
||||
if parsed := iapapple.ParseProductIdDuration(txp.ProductId); parsed != nil {
|
||||
parsedUnit = parsed.Unit
|
||||
parsedQuantity = parsed.Quantity
|
||||
}
|
||||
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
|
||||
}
|
||||
digits := p
|
||||
for j := 0; j < len(digits); j++ {
|
||||
if digits[j] < '0' || digits[j] > '9' {
|
||||
digits = digits[:j]
|
||||
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 q, e := strconv.ParseInt(digits, 10, 64); e == nil && q > 0 {
|
||||
parsedQuantity = q
|
||||
if ok {
|
||||
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)
|
||||
if e == nil {
|
||||
continue // Already processed, skip
|
||||
}
|
||||
iapTx := &iapmodel.Transaction{
|
||||
UserId: u.Id,
|
||||
OriginalTransactionId: txp.OriginalTransactionId,
|
||||
TransactionId: txp.TransactionId,
|
||||
ProductId: txp.ProductId,
|
||||
PurchaseAt: txp.PurchaseDate,
|
||||
RevocationAt: txp.RevocationDate,
|
||||
JWSHash: "",
|
||||
}
|
||||
if err := tx.Model(&iapmodel.Transaction{}).Create(iapTx).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if !ok {
|
||||
l.Errorw("restore: product mapping not found", logger.Field("productId", txp.ProductId))
|
||||
return fmt.Errorf("product mapping not found for %s", txp.ProductId)
|
||||
}
|
||||
}
|
||||
// Check if already processed
|
||||
_, e := iapmodel.NewModel(l.svcCtx.DB, l.svcCtx.Redis).FindByOriginalId(l.ctx, txp.OriginalTransactionId)
|
||||
if e == nil {
|
||||
return nil // Already processed, skip
|
||||
}
|
||||
|
||||
// Try to link with existing order if possible (Best Effort)
|
||||
// Strategy 1: appAccountToken (from JWS) -> OrderNo (UUID)
|
||||
if txp.AppAccountToken != "" {
|
||||
// appAccountToken is usually a UUID string
|
||||
// Try to find order by parsing UUID or matching direct orderNo (if we stored it as uuid)
|
||||
// Since our orderNo is string, we can try to search it.
|
||||
// However, AppAccountToken is strictly UUID format. If our orderNo is not UUID, we might need a mapping.
|
||||
// Assuming orderNo -> UUID conversion was consistent on client side.
|
||||
// Here we just try to update if we find an unpaid order with this ID (if orderNo was used as appAccountToken)
|
||||
_ = l.svcCtx.OrderModel.UpdateOrderStatus(l.ctx, txp.AppAccountToken, 2, tx)
|
||||
return l.svcCtx.DB.Transaction(func(tx *gorm.DB) error {
|
||||
iapTx := &iapmodel.Transaction{
|
||||
UserId: u.Id,
|
||||
OriginalTransactionId: txp.OriginalTransactionId,
|
||||
TransactionId: txp.TransactionId,
|
||||
ProductId: txp.ProductId,
|
||||
PurchaseAt: txp.PurchaseDate,
|
||||
RevocationAt: txp.RevocationDate,
|
||||
JWSHash: "",
|
||||
}
|
||||
if err := tx.Model(&iapmodel.Transaction{}).Create(iapTx).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Try to link with existing order if possible (Best Effort)
|
||||
// Parse "uid_123|orderNo_XXX" format from client
|
||||
if txp.AppAccountToken != "" {
|
||||
orderNo := txp.AppAccountToken
|
||||
if idx := strings.Index(orderNo, "orderNo_"); idx >= 0 {
|
||||
orderNo = orderNo[idx+len("orderNo_"):]
|
||||
}
|
||||
if orderNo != "" {
|
||||
_ = l.svcCtx.OrderModel.UpdateOrderStatus(l.ctx, orderNo, 2)
|
||||
}
|
||||
}
|
||||
|
||||
// Strategy 2: If we had a way to pass orderNo in restore request (optional field in future), we could use it here.
|
||||
// But for now, we only rely on appAccountToken or just skip order linking.
|
||||
|
||||
exp := iapapple.CalcExpire(txp.PurchaseDate, m.DurationDays)
|
||||
if l.svcCtx.Config.Subscribe.SingleModel {
|
||||
anchorSub, anchorErr := findSingleModeMergeTarget(l.ctx, l.svcCtx, u.Id, m.SubscribeId)
|
||||
switch {
|
||||
case errors.Is(anchorErr, commonLogic.ErrSingleModePlanMismatch):
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SingleSubscribePlanMismatch), "single subscribe mode plan mismatch")
|
||||
case anchorErr == nil && anchorSub != nil && anchorSub.Id > 0:
|
||||
if exp.After(anchorSub.ExpireTime) {
|
||||
anchorSub.ExpireTime = exp
|
||||
}
|
||||
anchorSub.Status = 1
|
||||
anchorSub.FinishedAt = nil
|
||||
if err := l.svcCtx.UserModel.UpdateSubscribe(l.ctx, anchorSub, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
continue
|
||||
case errors.Is(anchorErr, gorm.ErrRecordNotFound):
|
||||
case anchorErr != nil:
|
||||
return anchorErr
|
||||
exp := iapapple.CalcExpire(txp.PurchaseDate, m.DurationDays)
|
||||
if l.svcCtx.Config.Subscribe.SingleModel {
|
||||
anchorSub, anchorErr := findSingleModeMergeTarget(l.ctx, l.svcCtx, u.Id, m.SubscribeId)
|
||||
switch {
|
||||
case errors.Is(anchorErr, commonLogic.ErrSingleModePlanMismatch):
|
||||
return errors.Wrapf(xerr.NewErrCode(xerr.SingleSubscribePlanMismatch), "single subscribe mode plan mismatch")
|
||||
case anchorErr == nil && anchorSub != nil && anchorSub.Id > 0:
|
||||
if exp.After(anchorSub.ExpireTime) {
|
||||
anchorSub.ExpireTime = exp
|
||||
}
|
||||
anchorSub.Status = 1
|
||||
anchorSub.FinishedAt = nil
|
||||
if err := l.svcCtx.UserModel.UpdateSubscribe(l.ctx, anchorSub, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
case errors.Is(anchorErr, gorm.ErrRecordNotFound):
|
||||
case anchorErr != nil:
|
||||
return anchorErr
|
||||
}
|
||||
userSub := user.Subscribe{
|
||||
UserId: u.Id,
|
||||
SubscribeId: m.SubscribeId,
|
||||
StartTime: time.Now(),
|
||||
ExpireTime: exp,
|
||||
Traffic: 0,
|
||||
Download: 0,
|
||||
Upload: 0,
|
||||
Token: fmt.Sprintf("iap:%s", txp.OriginalTransactionId),
|
||||
UUID: uuid.New().String(),
|
||||
Status: 1,
|
||||
}
|
||||
if err := l.svcCtx.UserModel.InsertSubscribe(l.ctx, &userSub, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
userSub := user.Subscribe{
|
||||
UserId: u.Id,
|
||||
SubscribeId: m.SubscribeId,
|
||||
StartTime: time.Now(),
|
||||
ExpireTime: exp,
|
||||
Traffic: 0,
|
||||
Download: 0,
|
||||
Upload: 0,
|
||||
Token: fmt.Sprintf("iap:%s", txp.OriginalTransactionId),
|
||||
UUID: uuid.New().String(),
|
||||
Status: 1,
|
||||
}
|
||||
if err := l.svcCtx.UserModel.InsertSubscribe(l.ctx, &userSub, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user