This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
iapmodel "github.com/perfect-panel/server/internal/model/iap/apple"
|
||||
"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"
|
||||
iapapple "github.com/perfect-panel/server/pkg/iap/apple"
|
||||
@@ -164,6 +165,47 @@ func (l *AppleIAPNotifyLogic) Handle(signedPayload string) error {
|
||||
}
|
||||
token := "iap:" + txPayload.OriginalTransactionId
|
||||
sub, e := l.svcCtx.UserModel.FindOneSubscribeByToken(l.ctx, token)
|
||||
if (e != nil || sub == nil || sub.Id == 0) && existing != nil && existing.UserId > 0 && days > 0 {
|
||||
// 首购订单创建的订阅 token 不是 iap: 前缀,尝试按 userId+subscribeId 查找
|
||||
l.Infow("iap notify fallback: find subscribe by userId", logger.Field("userId", existing.UserId))
|
||||
userSubs, queryErr := l.svcCtx.UserModel.QueryUserSubscribe(l.ctx, existing.UserId, 0, 1, 2, 3)
|
||||
if queryErr == nil {
|
||||
exp := iapapple.CalcExpire(txPayload.PurchaseDate, days)
|
||||
for _, us := range userSubs {
|
||||
if us == nil {
|
||||
continue
|
||||
}
|
||||
candidate := &user.Subscribe{
|
||||
Id: us.Id,
|
||||
UserId: us.UserId,
|
||||
SubscribeId: us.SubscribeId,
|
||||
ExpireTime: us.ExpireTime,
|
||||
Status: us.Status,
|
||||
Token: us.Token,
|
||||
FinishedAt: us.FinishedAt,
|
||||
}
|
||||
if txPayload.RevocationDate != nil {
|
||||
candidate.Status = 3
|
||||
t := *txPayload.RevocationDate
|
||||
candidate.FinishedAt = &t
|
||||
candidate.ExpireTime = t
|
||||
} else {
|
||||
if exp.After(candidate.ExpireTime) {
|
||||
candidate.ExpireTime = exp
|
||||
}
|
||||
candidate.Status = 1
|
||||
candidate.FinishedAt = nil
|
||||
}
|
||||
if err := l.svcCtx.UserModel.UpdateSubscribe(l.ctx, candidate, db); err != nil {
|
||||
l.Errorw("iap notify fallback update subscribe error", logger.Field("error", err.Error()), logger.Field("userSubscribeId", candidate.Id))
|
||||
return err
|
||||
}
|
||||
l.Infow("iap notify fallback updated subscribe", logger.Field("userSubscribeId", candidate.Id), logger.Field("status", candidate.Status))
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if e == nil && sub != nil && sub.Id != 0 {
|
||||
if txPayload.RevocationDate != nil {
|
||||
// 撤销:订阅置为过期并记录完成时间
|
||||
|
||||
Reference in New Issue
Block a user