IAPReceiptModel.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // IAPReceiptModel.m
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2025/1/1.
  6. //
  7. #import "IAPReceiptModel.h"
  8. @implementation IAPReceiptInfoModel
  9. + (instancetype)modelWithDict:(NSDictionary *)dict {
  10. return [[[self class] alloc] initWithDict:dict];
  11. }
  12. - (instancetype)initWithDict:(NSDictionary *)dict {
  13. if (self = [super init]) {
  14. [self setValuesForKeysWithDictionary:dict ? dict : [NSDictionary new]];
  15. }
  16. return self;
  17. }
  18. - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
  19. NSLog(@"forUndefinedKey: %@", key);
  20. }
  21. @end
  22. @implementation IAPPendingRenewalInfoModel
  23. + (instancetype)modelWithDict:(NSDictionary *)dict {
  24. return [[[self class] alloc] initWithDict:dict];
  25. }
  26. - (instancetype)initWithDict:(NSDictionary *)dict {
  27. if (self = [super init]) {
  28. [self setValuesForKeysWithDictionary:dict ? dict : [NSDictionary new]];
  29. }
  30. return self;
  31. }
  32. - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
  33. NSLog(@"forUndefinedKey: %@", key);
  34. }
  35. @end
  36. @implementation IAPInAppReceiptModel
  37. + (instancetype)modelWithDict:(NSDictionary *)dict {
  38. return [[[self class] alloc] initWithDict:dict];
  39. }
  40. - (instancetype)initWithDict:(NSDictionary *)dict {
  41. if (self = [super init]) {
  42. [self setValuesForKeysWithDictionary:dict ? dict : [NSDictionary new]];
  43. NSMutableArray *inAppModelArray = [NSMutableArray arrayWithCapacity:4];
  44. for (NSDictionary *dict in self.in_app) {
  45. IAPReceiptInfoModel *model = [IAPReceiptInfoModel modelWithDict:dict];
  46. [inAppModelArray addObject:model];
  47. }
  48. self.inAppModels = [inAppModelArray copy];
  49. }
  50. return self;
  51. }
  52. - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
  53. NSLog(@"forUndefinedKey: %@", key);
  54. }
  55. @end
  56. @implementation IAPReceiptModel
  57. + (instancetype)modelWithDict:(NSDictionary *)dict {
  58. return [[[self class] alloc] initWithDict:dict];
  59. }
  60. - (instancetype)initWithDict:(NSDictionary *)dict {
  61. if (self = [super init]) {
  62. [self setValuesForKeysWithDictionary:dict ? dict : [NSDictionary new]];
  63. IAPInAppReceiptModel *receiptM = [IAPInAppReceiptModel modelWithDict:self.receipt ? self.receipt : [NSDictionary new]];
  64. self.receiptModel = receiptM;
  65. NSMutableArray *latestReceiptInfoModelArray = [NSMutableArray arrayWithCapacity:4];
  66. for (NSDictionary *dict in self.latest_receipt_info) {
  67. IAPReceiptInfoModel *model = [IAPReceiptInfoModel modelWithDict:dict];
  68. [latestReceiptInfoModelArray addObject:model];
  69. }
  70. self.latestReceiptInfoModels = [latestReceiptInfoModelArray copy];
  71. NSMutableArray *pendingRenewalInfoModelArray = [NSMutableArray arrayWithCapacity:4];
  72. for (NSDictionary *dict in self.pending_renewal_info) {
  73. IAPPendingRenewalInfoModel *model = [IAPPendingRenewalInfoModel modelWithDict:dict];
  74. [pendingRenewalInfoModelArray addObject:model];
  75. }
  76. self.pendingRenewalInfoModels = [pendingRenewalInfoModelArray copy];
  77. }
  78. return self;
  79. }
  80. - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
  81. NSLog(@"forUndefinedKey: %@", key);
  82. }
  83. @end
  84. @implementation IAPReceiptTool
  85. + (BOOL)isFreeTrailingWithModel:(IAPReceiptModel *)model withProductId:(NSString *)productId {
  86. IAPReceiptInfoModel *lastestM = [self fetchProductLastestReceiptsWithModel:model withProductId:productId];
  87. if (lastestM == nil) {
  88. return false;
  89. }
  90. if (lastestM.is_trial_period == false) {
  91. return false;
  92. }
  93. long long expiresDate = [lastestM.expires_date_ms longLongValue]/1000;
  94. long long currentDate = [[NSDate date] timeIntervalSince1970];
  95. BOOL isActive = (expiresDate > currentDate) ? YES : NO;
  96. return isActive;
  97. }
  98. + (BOOL)isNewUserWithModel:(IAPReceiptModel *)model withGroupIdentifier:(NSString *)groupIdentifier {
  99. if ([self hasFreeTrialWithModel:model withGroupIdentifier:groupIdentifier]) {
  100. return false;
  101. }
  102. if ([self hasIntroOfferWithModel:model withGroupIdentifier:groupIdentifier]) {
  103. return false;
  104. }
  105. return true;
  106. }
  107. + (BOOL)hasFreeTrialWithModel:(IAPReceiptModel *)model withGroupIdentifier:(NSString *)groupIdentifier {
  108. for (IAPReceiptInfoModel *m in [self fetchReceiptsWithModel:model withGroupIdentifier:groupIdentifier]) {
  109. if (m.is_trial_period) {
  110. return true;
  111. }
  112. }
  113. return false;
  114. }
  115. + (BOOL)hasIntroOfferWithModel:(IAPReceiptModel *)model withGroupIdentifier:(NSString *)groupIdentifier {
  116. for (IAPReceiptInfoModel *m in [self fetchReceiptsWithModel:model withGroupIdentifier:groupIdentifier]) {
  117. if (m.is_in_intro_offer_period) {
  118. return true;
  119. }
  120. }
  121. return false;
  122. }
  123. + (NSArray<IAPReceiptInfoModel *> *)fetchReceiptsWithModel:(IAPReceiptModel *)model withGroupIdentifier:(NSString *)groupIdentifier {
  124. NSMutableArray *results = [NSMutableArray arrayWithCapacity:4];
  125. for (IAPReceiptInfoModel *m in [self fetchReceiptsWithModel:model]) {
  126. if ([m.subscription_group_identifier isEqualToString:groupIdentifier] == false) {
  127. continue;
  128. }
  129. [results addObject:m];
  130. }
  131. return [results copy];
  132. }
  133. + (IAPReceiptInfoModel *)fetchProductLastestReceiptsWithModel:(IAPReceiptModel *)model withProductId:(NSString *)productId {
  134. IAPReceiptInfoModel *flagM= nil;
  135. long long lastestExpiresDate = 0;
  136. for (IAPReceiptInfoModel *m in [self fetchProductReceiptsWithModel:model withProductId:productId]) {
  137. long long theExpiresDate = [m.expires_date_ms longLongValue]/1000;
  138. if (theExpiresDate > lastestExpiresDate) {
  139. lastestExpiresDate = theExpiresDate;
  140. flagM = m;
  141. }
  142. }
  143. return flagM;
  144. }
  145. + (NSArray<IAPReceiptInfoModel *> *)fetchProductReceiptsWithModel:(IAPReceiptModel *)model withProductId:(NSString *)productId {
  146. NSMutableArray *results = [NSMutableArray arrayWithCapacity:4];
  147. for (IAPReceiptInfoModel *m in [self fetchReceiptsWithModel:model]) {
  148. if ([m.product_id isEqualToString:productId] == false) {
  149. continue;
  150. }
  151. [results addObject:m];
  152. }
  153. return [results copy];
  154. }
  155. + (NSArray<IAPReceiptInfoModel *> *)fetchReceiptsWithModel:(IAPReceiptModel *)model {
  156. NSMutableArray *results = [NSMutableArray arrayWithCapacity:4];
  157. for (IAPReceiptInfoModel *m in model.latestReceiptInfoModels) {
  158. [results addObject:m];
  159. }
  160. for (IAPReceiptInfoModel *m in model.receiptModel.inAppModels) {
  161. [results addObject:m];
  162. }
  163. return [results copy];
  164. }
  165. @end