diff --git a/InAppUtils/InAppUtils.m b/InAppUtils/InAppUtils.m index 4c3e3ae..66d4506 100644 --- a/InAppUtils/InAppUtils.m +++ b/InAppUtils/InAppUtils.m @@ -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, @@ -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]; } @@ -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)