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 authored and biomancer committed Nov 9, 2020
1 parent ed35769 commit 69206c0
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion InAppUtils/InAppUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,18 @@ - (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,
@"currencySymbol": [item.priceLocale objectForKey:NSLocaleCurrencySymbol],
@"currencyCode": [item.priceLocale objectForKey:NSLocaleCurrencyCode],
@"priceString": item.priceString,
@"countryCode": [item.priceLocale objectForKey: NSLocaleCountryCode],
@"downloadable": item.isDownloadable ? @"true" : @"false" ,
@"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 @@ -274,6 +276,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 69206c0

Please sign in to comment.