Skip to content

Commit f65bdcd

Browse files
whtlnvElrohil44
authored andcommitted
Parsing SKProductDiscount inside SKProduct (chirag04#186)
1 parent fd8ed98 commit f65bdcd

File tree

1 file changed

+64
-2
lines changed

1 file changed

+64
-2
lines changed

InAppUtils/InAppUtils.m

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,12 @@ - (void)productsRequest:(SKProductsRequest *)request
296296
NSString *key = RCTKeyForInstance(request);
297297
NSDictionary *promiseBlock = _promiseBlocks[key];
298298
if (promiseBlock) {
299-
RCTPromiseResolveBlock resolve = promiseBlock[@"resolve"];
300-
if (resolve) {
299+
RCTPromiseResolveBlock resolve = promiseBlock[@"resolve"];
300+
if (resolve) {
301301
products = [NSMutableArray arrayWithArray:response.products];
302302
NSMutableArray *productsArrayForJS = [NSMutableArray array];
303303
for(SKProduct *item in response.products) {
304+
NSDictionary *introductoryPrice = [InAppUtils parseIntroductoryPrice: item];
304305
NSDictionary *product = @{
305306
@"identifier": item.productIdentifier,
306307
@"price": item.price,
@@ -311,6 +312,7 @@ - (void)productsRequest:(SKProductsRequest *)request
311312
@"downloadable": item.isDownloadable ? @"true" : @"false" ,
312313
@"description": item.localizedDescription ? item.localizedDescription : @"",
313314
@"title": item.localizedTitle ? item.localizedTitle : @"",
315+
@"introductoryPrice": (introductoryPrice == nil) ? [NSNull null] : introductoryPrice,
314316
};
315317
[productsArrayForJS addObject:product];
316318
}
@@ -383,6 +385,66 @@ - (void)dealloc
383385
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
384386
}
385387

388+
#pragma mark Static
389+
390+
+ (NSDictionary *)parseIntroductoryPrice: (SKProduct *)product {
391+
if(@available(iOS 11.2, *)) {
392+
if (product != nil && product.introductoryPrice != nil) {
393+
// paymentMode: Returning as string for ease of use and code resilience
394+
NSString *paymentMode;
395+
switch (product.introductoryPrice.paymentMode) {
396+
case SKProductDiscountPaymentModeFreeTrial:
397+
paymentMode = @"freeTrial";
398+
break;
399+
case SKProductDiscountPaymentModePayAsYouGo:
400+
paymentMode = @"payAsYouGo";
401+
break;
402+
case SKProductDiscountPaymentModePayUpFront:
403+
paymentMode = @"payUpFront";
404+
break;
405+
default:
406+
paymentMode = @"unavailable";
407+
break;
408+
}
409+
410+
// subscriptionPeriod: Returning as Dictionary { unit: NSString, numberOfUnits: NSNumber }
411+
NSString *subscriptionPeriodUnit;
412+
switch (product.introductoryPrice.subscriptionPeriod.unit) {
413+
case SKProductPeriodUnitDay:
414+
subscriptionPeriodUnit = @"day";
415+
break;
416+
case SKProductPeriodUnitWeek:
417+
subscriptionPeriodUnit = @"week";
418+
break;
419+
case SKProductPeriodUnitMonth:
420+
subscriptionPeriodUnit = @"month";
421+
break;
422+
case SKProductPeriodUnitYear:
423+
subscriptionPeriodUnit = @"year";
424+
break;
425+
default:
426+
subscriptionPeriodUnit = @"unavailable";
427+
break;
428+
}
429+
430+
NSDictionary *subscriptionPeriod = @{
431+
@"unit": subscriptionPeriodUnit,
432+
@"numberOfUnits": [[NSNumber alloc] initWithLong:product.introductoryPrice.subscriptionPeriod.numberOfUnits],
433+
};
434+
435+
NSDictionary *introductoryPrice = @{
436+
@"price": product.introductoryPrice.price,
437+
@"numberOfPeriods": [[NSNumber alloc] initWithLong:product.introductoryPrice.numberOfPeriods],
438+
@"paymentMode": paymentMode,
439+
@"subscriptionPeriod": subscriptionPeriod,
440+
};
441+
return introductoryPrice;
442+
}
443+
}
444+
445+
return nil;
446+
}
447+
386448
#pragma mark Private
387449

388450
static NSString *RCTKeyForInstance(id instance)

0 commit comments

Comments
 (0)