Skip to content

Commit

Permalink
Merge pull request #1 from RevenueCat/rich_purchaser_info
Browse files Browse the repository at this point in the history
Rich Purchaser Info updates
  • Loading branch information
vegaro authored Aug 26, 2019
2 parents f38139d + dc7db50 commit 03bf1dd
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
12 changes: 12 additions & 0 deletions ios/RCEntitlementInfo+HybridAdditions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//
// Created by RevenueCat.
// Copyright © 2019 RevenueCat. All rights reserved.
//

#import <Purchases/Purchases.h>

@interface RCEntitlementInfo (HybridAdditions)

- (NSDictionary *)dictionary;

@end
80 changes: 80 additions & 0 deletions ios/RCEntitlementInfo+HybridAdditions.m
Original file line number Diff line number Diff line change
@@ -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
22 changes: 20 additions & 2 deletions ios/RCPurchaserInfo+HybridAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//

#import "RCPurchaserInfo+HybridAdditions.h"
#import "RCEntitlementInfo+HybridAdditions.h"

static NSDateFormatter *formatter;
static dispatch_once_t onceToken;
Expand Down Expand Up @@ -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,
Expand All @@ -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
@end

0 comments on commit 03bf1dd

Please sign in to comment.