123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- //
- // IAPReceiptModel.m
- // PDF Reader Pro
- //
- // Created by User-Tangchao on 2025/1/1.
- //
- #import "IAPReceiptModel.h"
- @implementation IAPReceiptInfoModel
- + (instancetype)modelWithDict:(NSDictionary *)dict {
- return [[[self class] alloc] initWithDict:dict];
- }
- - (instancetype)initWithDict:(NSDictionary *)dict {
- if (self = [super init]) {
- [self setValuesForKeysWithDictionary:dict ? dict : [NSDictionary new]];
- }
- return self;
- }
- - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
- NSLog(@"forUndefinedKey: %@", key);
- }
- @end
- @implementation IAPPendingRenewalInfoModel
- + (instancetype)modelWithDict:(NSDictionary *)dict {
- return [[[self class] alloc] initWithDict:dict];
- }
- - (instancetype)initWithDict:(NSDictionary *)dict {
- if (self = [super init]) {
- [self setValuesForKeysWithDictionary:dict ? dict : [NSDictionary new]];
- }
- return self;
- }
- - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
- NSLog(@"forUndefinedKey: %@", key);
- }
- @end
- @implementation IAPInAppReceiptModel
- + (instancetype)modelWithDict:(NSDictionary *)dict {
- return [[[self class] alloc] initWithDict:dict];
- }
- - (instancetype)initWithDict:(NSDictionary *)dict {
- if (self = [super init]) {
- [self setValuesForKeysWithDictionary:dict ? dict : [NSDictionary new]];
-
- NSMutableArray *inAppModelArray = [NSMutableArray arrayWithCapacity:4];
- for (NSDictionary *dict in self.in_app) {
- IAPReceiptInfoModel *model = [IAPReceiptInfoModel modelWithDict:dict];
- [inAppModelArray addObject:model];
- }
- self.inAppModels = [inAppModelArray copy];
- }
- return self;
- }
- - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
- NSLog(@"forUndefinedKey: %@", key);
- }
- @end
- @implementation IAPReceiptModel
- + (instancetype)modelWithDict:(NSDictionary *)dict {
- return [[[self class] alloc] initWithDict:dict];
- }
- - (instancetype)initWithDict:(NSDictionary *)dict {
- if (self = [super init]) {
- [self setValuesForKeysWithDictionary:dict ? dict : [NSDictionary new]];
-
- IAPInAppReceiptModel *receiptM = [IAPInAppReceiptModel modelWithDict:self.receipt ? self.receipt : [NSDictionary new]];
- self.receiptModel = receiptM;
-
- NSMutableArray *latestReceiptInfoModelArray = [NSMutableArray arrayWithCapacity:4];
- for (NSDictionary *dict in self.latest_receipt_info) {
- IAPReceiptInfoModel *model = [IAPReceiptInfoModel modelWithDict:dict];
- [latestReceiptInfoModelArray addObject:model];
- }
- self.latestReceiptInfoModels = [latestReceiptInfoModelArray copy];
-
- NSMutableArray *pendingRenewalInfoModelArray = [NSMutableArray arrayWithCapacity:4];
- for (NSDictionary *dict in self.pending_renewal_info) {
- IAPPendingRenewalInfoModel *model = [IAPPendingRenewalInfoModel modelWithDict:dict];
- [pendingRenewalInfoModelArray addObject:model];
- }
- self.pendingRenewalInfoModels = [pendingRenewalInfoModelArray copy];
- }
- return self;
- }
- - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
- NSLog(@"forUndefinedKey: %@", key);
- }
- @end
- @implementation IAPReceiptTool
- + (BOOL)isFreeTrailingWithModel:(IAPReceiptModel *)model withProductId:(NSString *)productId {
- IAPReceiptInfoModel *lastestM = [self fetchProductLastestReceiptsWithModel:model withProductId:productId];
- if (lastestM == nil) {
- return false;
- }
- if (lastestM.is_trial_period == false) {
- return false;
- }
- long long expiresDate = [lastestM.expires_date_ms longLongValue]/1000;
- long long currentDate = [[NSDate date] timeIntervalSince1970];
- BOOL isActive = (expiresDate > currentDate) ? YES : NO;
- return isActive;
- }
- + (BOOL)isNewUserWithModel:(IAPReceiptModel *)model withGroupIdentifier:(NSString *)groupIdentifier {
- if ([self hasFreeTrialWithModel:model withGroupIdentifier:groupIdentifier]) {
- return false;
- }
- if ([self hasIntroOfferWithModel:model withGroupIdentifier:groupIdentifier]) {
- return false;
- }
- return true;
- }
- + (BOOL)hasFreeTrialWithModel:(IAPReceiptModel *)model withGroupIdentifier:(NSString *)groupIdentifier {
- for (IAPReceiptInfoModel *m in [self fetchReceiptsWithModel:model withGroupIdentifier:groupIdentifier]) {
- if (m.is_trial_period) {
- return true;
- }
- }
- return false;
- }
- + (BOOL)hasIntroOfferWithModel:(IAPReceiptModel *)model withGroupIdentifier:(NSString *)groupIdentifier {
- for (IAPReceiptInfoModel *m in [self fetchReceiptsWithModel:model withGroupIdentifier:groupIdentifier]) {
- if (m.is_in_intro_offer_period) {
- return true;
- }
- }
- return false;
- }
- + (NSArray<IAPReceiptInfoModel *> *)fetchReceiptsWithModel:(IAPReceiptModel *)model withGroupIdentifier:(NSString *)groupIdentifier {
- NSMutableArray *results = [NSMutableArray arrayWithCapacity:4];
- for (IAPReceiptInfoModel *m in [self fetchReceiptsWithModel:model]) {
- if ([m.subscription_group_identifier isEqualToString:groupIdentifier] == false) {
- continue;
- }
- [results addObject:m];
- }
- return [results copy];
- }
- + (IAPReceiptInfoModel *)fetchProductLastestReceiptsWithModel:(IAPReceiptModel *)model withProductId:(NSString *)productId {
- IAPReceiptInfoModel *flagM= nil;
- long long lastestExpiresDate = 0;
- for (IAPReceiptInfoModel *m in [self fetchProductReceiptsWithModel:model withProductId:productId]) {
- long long theExpiresDate = [m.expires_date_ms longLongValue]/1000;
- if (theExpiresDate > lastestExpiresDate) {
- lastestExpiresDate = theExpiresDate;
- flagM = m;
- }
- }
- return flagM;
- }
- + (NSArray<IAPReceiptInfoModel *> *)fetchProductReceiptsWithModel:(IAPReceiptModel *)model withProductId:(NSString *)productId {
- NSMutableArray *results = [NSMutableArray arrayWithCapacity:4];
- for (IAPReceiptInfoModel *m in [self fetchReceiptsWithModel:model]) {
- if ([m.product_id isEqualToString:productId] == false) {
- continue;
- }
- [results addObject:m];
- }
- return [results copy];
- }
- + (NSArray<IAPReceiptInfoModel *> *)fetchReceiptsWithModel:(IAPReceiptModel *)model {
- NSMutableArray *results = [NSMutableArray arrayWithCapacity:4];
- for (IAPReceiptInfoModel *m in model.latestReceiptInfoModels) {
- [results addObject:m];
- }
- for (IAPReceiptInfoModel *m in model.receiptModel.inAppModels) {
- [results addObject:m];
- }
- return [results copy];
- }
- @end
|