Skip to content

Commit

Permalink
Parsing SKProductDiscount inside SKProduct (chirag04#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
whtlnv committed Sep 22, 2018
1 parent 166499e commit 1fdfd29
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions InAppUtils/InAppUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ - (void)productsRequest:(SKProductsRequest *)request
products = [NSMutableArray arrayWithArray:response.products];
NSMutableArray *productsArrayForJS = [NSMutableArray array];
for(SKProduct *item in response.products) {
NSDictionary *introductoryPrice = [InAppUtils parseIntroductoryPrice: item];
NSDictionary *product = @{
@"identifier": item.productIdentifier,
@"price": item.price,
Expand All @@ -223,6 +224,7 @@ - (void)productsRequest:(SKProductsRequest *)request
@"downloadable": item.downloadable ? @"true" : @"false" ,
@"description": item.localizedDescription ? item.localizedDescription : @"",
@"title": item.localizedTitle ? item.localizedTitle : @"",
@"introductoryPrice": (introductoryPrice == nil) ? [NSNull null] : introductoryPrice,
};
[productsArrayForJS addObject:product];
}
Expand Down Expand Up @@ -265,6 +267,66 @@ - (void)dealloc
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}

#pragma mark Static

+ (NSDictionary *)parseIntroductoryPrice: (SKProduct *)product {
if(@available(iOS 11.2, *)) {
if (product != nil && product.introductoryPrice != nil) {
// paymentMode: Returning as string for ease of use and code resilience
NSString *paymentMode;
switch (product.introductoryPrice.paymentMode) {
case SKProductDiscountPaymentModeFreeTrial:
paymentMode = @"freeTrial";
break;
case SKProductDiscountPaymentModePayAsYouGo:
paymentMode = @"payAsYouGo";
break;
case SKProductDiscountPaymentModePayUpFront:
paymentMode = @"payUpFront";
break;
default:
paymentMode = @"unavailable";
break;
}

// subscriptionPeriod: Returning as Dictionary { unit: NSString, numberOfUnits: NSNumber }
NSString *subscriptionPeriodUnit;
switch (product.introductoryPrice.subscriptionPeriod.unit) {
case SKProductPeriodUnitDay:
subscriptionPeriodUnit = @"day";
break;
case SKProductPeriodUnitWeek:
subscriptionPeriodUnit = @"week";
break;
case SKProductPeriodUnitMonth:
subscriptionPeriodUnit = @"month";
break;
case SKProductPeriodUnitYear:
subscriptionPeriodUnit = @"year";
break;
default:
subscriptionPeriodUnit = @"unavailable";
break;
}

NSDictionary *subscriptionPeriod = @{
@"unit": subscriptionPeriodUnit,
@"numberOfUnits": [[NSNumber alloc] initWithLong:product.introductoryPrice.subscriptionPeriod.numberOfUnits],
};

NSDictionary *introductoryPrice = @{
@"price": product.introductoryPrice.price,
@"numberOfPeriods": [[NSNumber alloc] initWithLong:product.introductoryPrice.numberOfPeriods],
@"paymentMode": paymentMode,
@"subscriptionPeriod": subscriptionPeriod,
};
return introductoryPrice;
}
}

return nil;
}

#pragma mark Private

static NSString *RCTKeyForInstance(id instance)
Expand Down

0 comments on commit 1fdfd29

Please sign in to comment.