diff --git a/ios/RCEntitlementInfo+HybridAdditions.h b/ios/RCEntitlementInfo+HybridAdditions.h new file mode 100644 index 00000000..dcd7e20a --- /dev/null +++ b/ios/RCEntitlementInfo+HybridAdditions.h @@ -0,0 +1,12 @@ +// +// Created by RevenueCat. +// Copyright © 2019 RevenueCat. All rights reserved. +// + +#import + +@interface RCEntitlementInfo (HybridAdditions) + +- (NSDictionary *)dictionary; + +@end diff --git a/ios/RCEntitlementInfo+HybridAdditions.m b/ios/RCEntitlementInfo+HybridAdditions.m new file mode 100644 index 00000000..7b513944 --- /dev/null +++ b/ios/RCEntitlementInfo+HybridAdditions.m @@ -0,0 +1,80 @@ +// +// Created by RevenueCat. +// Copyright © 2019 RevenueCat. All rights reserved. +// + +#import "RCEntitlement+HybridAdditions.h" +#import "SKProduct+HybridAdditions.h" + +static NSDateFormatter *formatter; +static dispatch_once_t onceToken; + +static NSString * stringFromDate(NSDate *date) +{ + dispatch_once(&onceToken, ^{ + // Here we're not using NSISO8601DateFormatter as we need to support iOS < 10 + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; + dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"]; + dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss'Z'"; + dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]; + formatter = dateFormatter; + }); + + return [formatter stringFromDate:date]; +} + +@implementation RCEntitlementInfo (HybridAdditions) + +- (NSDictionary *)dictionary +{ + NSMutableDictionary *jsonDict = [NSMutableDictionary new]; + jsonDict[@"identifier"] = self.identifier; + jsonDict[@"isActive"] = @(self.isActive); + jsonDict[@"willRenew"] = @(self.willRenew); + + switch (self.periodType) { + case RCIntro: + jsonDict[@"periodType"] = @"INTRO"; + break; + case RCNormal: + jsonDict[@"periodType"] = @"NORMAL"; + break; + case RCTrial: + jsonDict[@"periodType"] = @"TRIAL"; + break; + } + + jsonDict[@"latestPurchaseDate"] = stringFromDate(self.latestPurchaseDate); + jsonDict[@"originalPurchaseDate"] = stringFromDate(self.originalPurchaseDate); + jsonDict[@"expirationDate"] = stringFromDate(self.originalPurchaseDate) ?: [NSNull null]; + + switch (self.store) { + case RCAppStore: + jsonDict[@"store"] = @"APP_STORE"; + break; + case RCMacAppStore: + jsonDict[@"store"] = @"MAC_APP_STORE"; + break; + case RCPlayStore: + jsonDict[@"store"] = @"PLAY_STORE"; + break; + case RCStripe: + jsonDict[@"store"] = @"STRIPE"; + break; + case RCPromotional: + jsonDict[@"store"] = @"PROMOTIONAL"; + break; + case RCUnknownStore: + jsonDict[@"store"] = @"UNKNOWN_STORE"; + break; + } + + jsonDict[@"productIdentifier"] = self.productIdentifier; + jsonDict[@"isSandbox"] = @(self.isSandbox); + jsonDict[@"unsubscribeDetectedAt"] = stringFromDate(self.unsubscribeDetectedAt) ?: [NSNull null]; + jsonDict[@"billingIssueDetectedAt"] = stringFromDate(self.billingIssueDetectedAt) ?: [NSNull null]; + + return [NSDictionary dictionaryWithDictionary:jsonDict]; +} + +@end diff --git a/ios/RCPurchaserInfo+HybridAdditions.m b/ios/RCPurchaserInfo+HybridAdditions.m index 7380b639..a3341e8d 100644 --- a/ios/RCPurchaserInfo+HybridAdditions.m +++ b/ios/RCPurchaserInfo+HybridAdditions.m @@ -4,6 +4,7 @@ // #import "RCPurchaserInfo+HybridAdditions.h" +#import "RCEntitlementInfo+HybridAdditions.h" static NSDateFormatter *formatter; static dispatch_once_t onceToken; @@ -45,6 +46,20 @@ - (NSDictionary *)dictionary } id latestExpiration = stringFromDate(self.latestExpirationDate) ?: [NSNull null]; + + NSMutableDictionary *entitlementInfos = [NSMutableDictionary new]; + NSMutableDictionary *all = [NSMutableDictionary new]; + for (NSString *entId in self.entitlements.all) { + all[entId] = self.entitlements.all[entId].dictionary; + } + entitlementInfos[@"all"] = all; + + NSMutableDictionary *active = [NSMutableDictionary new]; + for (NSString *entId in self.entitlements.active) { + active[entId] = self.entitlements.active[entId].dictionary; + } + entitlementInfos[@"active"] = active; + return @{ @"activeEntitlements": self.activeEntitlements.allObjects, @@ -53,8 +68,11 @@ - (NSDictionary *)dictionary @"latestExpirationDate": latestExpiration, @"allExpirationDates": allExpirations, @"expirationsForActiveEntitlements": expirationsForActiveEntitlements, - @"purchaseDatesForActiveEntitlements": purchaseDatesForActiveEntitlements + @"purchaseDatesForActiveEntitlements": purchaseDatesForActiveEntitlements, + @"entitlements": entitlementInfos, + @"firstSeen": stringFromDate(self.firstSeen), + @"originalAppUserId": self.originalAppUserId, }; } -@end \ No newline at end of file +@end