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 Elrohil44 committed Sep 25, 2020
1 parent fd8ed98 commit f65bdcd
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions InAppUtils/InAppUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -296,11 +296,12 @@ - (void)productsRequest:(SKProductsRequest *)request
NSString *key = RCTKeyForInstance(request);
NSDictionary *promiseBlock = _promiseBlocks[key];
if (promiseBlock) {
RCTPromiseResolveBlock resolve = promiseBlock[@"resolve"];
if (resolve) {
RCTPromiseResolveBlock resolve = promiseBlock[@"resolve"];
if (resolve) {
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 @@ -311,6 +312,7 @@ - (void)productsRequest:(SKProductsRequest *)request
@"downloadable": item.isDownloadable ? @"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 @@ -383,6 +385,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 f65bdcd

Please sign in to comment.