-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 RCEntitlement (HybridAdditions) | ||
|
||
- (NSDictionary *)dictionary; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// Created by RevenueCat. | ||
// Copyright © 2019 RevenueCat. All rights reserved. | ||
// | ||
|
||
#import "RCEntitlement+HybridAdditions.h" | ||
#import "SKProduct+Purchases.h" | ||
|
||
@implementation RCEntitlement (HybridAdditions) | ||
|
||
- (NSDictionary *)dictionary | ||
{ | ||
NSMutableDictionary *jsonDict = [NSMutableDictionary new]; | ||
for (NSString *key in self.offerings) { | ||
RCOffering *offering = self.offerings[key]; | ||
jsonDict[key] = offering.activeProduct.dictionary; | ||
} | ||
return [NSDictionary dictionaryWithDictionary:jsonDict]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 RCPurchaserInfo (HybridAdditions) | ||
|
||
- (NSDictionary *)dictionary; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// Created by RevenueCat. | ||
// Copyright © 2019 RevenueCat. All rights reserved. | ||
// | ||
|
||
#import "RCPurchaserInfo+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 RCPurchaserInfo (HybridAdditions) | ||
|
||
- (NSDictionary *)dictionary | ||
{ | ||
NSMutableDictionary *allExpirations = [NSMutableDictionary new]; | ||
for (NSString *productIdentifier in self.allPurchasedProductIdentifiers) { | ||
NSDate *date = [self expirationDateForProductIdentifier:productIdentifier]; | ||
allExpirations[productIdentifier] = stringFromDate(date) ?: [NSNull null]; | ||
} | ||
|
||
NSMutableDictionary *expirationsForActiveEntitlements = [NSMutableDictionary new]; | ||
for (NSString *entId in self.activeEntitlements) { | ||
NSDate *date = [self expirationDateForEntitlement:entId]; | ||
expirationsForActiveEntitlements[entId] = stringFromDate(date) ?: [NSNull null];; | ||
} | ||
|
||
NSMutableDictionary *purchaseDatesForActiveEntitlements = [NSMutableDictionary new]; | ||
for (NSString *entId in self.activeEntitlements) { | ||
NSDate *date = [self purchaseDateForEntitlement:entId]; | ||
purchaseDatesForActiveEntitlements[entId] = stringFromDate(date) ?: [NSNull null];; | ||
} | ||
|
||
id latestExpiration = stringFromDate(self.latestExpirationDate) ?: [NSNull null]; | ||
|
||
return @{ | ||
@"activeEntitlements": self.activeEntitlements.allObjects, | ||
@"activeSubscriptions": self.activeSubscriptions.allObjects, | ||
@"allPurchasedProductIdentifiers": self.allPurchasedProductIdentifiers.allObjects, | ||
@"latestExpirationDate": latestExpiration, | ||
@"allExpirationDates": allExpirations, | ||
@"expirationsForActiveEntitlements": expirationsForActiveEntitlements, | ||
@"purchaseDatesForActiveEntitlements": purchaseDatesForActiveEntitlements | ||
}; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// Created by RevenueCat. | ||
// Copyright © 2019 RevenueCat. All rights reserved. | ||
// | ||
|
||
#import <StoreKit/StoreKit.h> | ||
|
||
@interface SKProduct (HybridAdditions) | ||
|
||
- (NSDictionary *)dictionary; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// | ||
// Created by RevenueCat. | ||
// Copyright © 2019 RevenueCat. All rights reserved. | ||
// | ||
|
||
#import "SKProduct+HybridAdditions.h" | ||
|
||
@implementation SKProduct (RCPurchases) | ||
|
||
- (nullable NSString *)rc_currencyCode { | ||
if(@available(iOS 10.0, *)) { | ||
return self.priceLocale.currencyCode; | ||
} else { | ||
return [self.priceLocale objectForKey:NSLocaleCurrencyCode]; | ||
} | ||
} | ||
|
||
- (NSDictionary *)dictionary | ||
{ | ||
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; | ||
formatter.numberStyle = NSNumberFormatterCurrencyStyle; | ||
formatter.locale = self.priceLocale; | ||
NSMutableDictionary *d = [NSMutableDictionary dictionaryWithDictionary:@{ | ||
@"identifier": self.productIdentifier ?: @"", | ||
@"description": self.localizedDescription ?: @"", | ||
@"title": self.localizedTitle ?: @"", | ||
@"price": @(self.price.floatValue), | ||
@"price_string": [formatter stringFromNumber:self.price], | ||
@"currency_code": (self.rc_currencyCode) ? self.rc_currencyCode : [NSNull null] | ||
}]; | ||
|
||
|
||
if (@available(iOS 11.2, *)) { | ||
if (self.introductoryPrice) { | ||
d[@"intro_price"] = @(self.introductoryPrice.price.floatValue) ?: @""; | ||
if (self.introductoryPrice.price) { | ||
d[@"intro_price_string"] = [formatter stringFromNumber:self.introductoryPrice.price]; | ||
} else { | ||
d[@"intro_price_string"] = @""; | ||
} | ||
d[@"intro_price_period"] = [self normalizeSubscriptionPeriod:self.introductoryPrice.subscriptionPeriod] ?: @""; | ||
d[@"intro_price_period_unit"] = [self normalizeSubscriptionPeriodUnit:self.introductoryPrice.subscriptionPeriod.unit] ?: @""; | ||
d[@"intro_price_period_number_of_units"] = @(self.introductoryPrice.subscriptionPeriod.numberOfUnits) ?: @""; | ||
d[@"intro_price_cycles"] = @(self.introductoryPrice.numberOfPeriods) ?: @""; | ||
return d; | ||
} | ||
} | ||
d[@"intro_price"] = @""; | ||
d[@"intro_price_string"] = @""; | ||
d[@"intro_price_period"] = @""; | ||
d[@"intro_price_cycles"] = @""; | ||
|
||
return d; | ||
} | ||
|
||
- (NSString *)normalizeSubscriptionPeriod:(SKProductSubscriptionPeriod *)subscriptionPeriod API_AVAILABLE(ios(11.2)){ | ||
NSString *unit; | ||
switch (subscriptionPeriod.unit) { | ||
case SKProductPeriodUnitDay: | ||
unit = @"D"; | ||
case SKProductPeriodUnitWeek: | ||
unit = @"W"; | ||
case SKProductPeriodUnitMonth: | ||
unit = @"M"; | ||
case SKProductPeriodUnitYear: | ||
unit = @"Y"; | ||
} | ||
return [NSString stringWithFormat:@"%@%@%@", @"P", @(subscriptionPeriod.numberOfUnits), unit]; | ||
} | ||
|
||
- (NSString *)normalizeSubscriptionPeriodUnit:(SKProductPeriodUnit)subscriptionPeriodUnit API_AVAILABLE(ios(11.2)){ | ||
switch (subscriptionPeriodUnit) { | ||
case SKProductPeriodUnitDay: | ||
return @"DAY"; | ||
case SKProductPeriodUnitWeek: | ||
return @"WEEK"; | ||
case SKProductPeriodUnitMonth: | ||
return @"MONTH"; | ||
case SKProductPeriodUnitYear: | ||
return @"YEAR"; | ||
} | ||
} | ||
|
||
@end |