Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Fixed promise acceptation/rejection not propagated to JS #809

Merged
merged 1 commit into from
Oct 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 45 additions & 39 deletions ios/RNIapIos.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ - (void)stopObserving {

- (void)addListener:(NSString *)eventName {
[super addListener:eventName];

SKPayment *promotedPayment = [IAPPromotionObserver sharedObserver].payment;
if ([eventName isEqualToString:@"iap-promoted-product"] && promotedPayment != nil) {
[self sendEventWithName:@"iap-promoted-product" body:promotedPayment.productIdentifier];
Expand All @@ -63,18 +63,18 @@ - (void)addListener:(NSString *)eventName {

-(void)addPromiseForKey:(NSString*)key resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
NSMutableArray* promises = [promisesByKey valueForKey:key];

if (promises == nil) {
promises = [NSMutableArray array];
[promisesByKey setValue:promises forKey:key];
}

[promises addObject:@[resolve, reject]];
}

-(void)resolvePromisesForKey:(NSString*)key value:(id)value {
NSMutableArray* promises = [promisesByKey valueForKey:key];

if (promises != nil) {
for (NSMutableArray *tuple in promises) {
RCTPromiseResolveBlock resolveBlck = tuple[0];
Expand All @@ -86,7 +86,7 @@ -(void)resolvePromisesForKey:(NSString*)key value:(id)value {

-(void)rejectPromisesForKey:(NSString*)key code:(NSString*)code message:(NSString*)message error:(NSError*) error {
NSMutableArray* promises = [promisesByKey valueForKey:key];

if (promises != nil) {
for (NSMutableArray *tuple in promises) {
RCTPromiseRejectBlock reject = tuple[1];
Expand Down Expand Up @@ -148,6 +148,9 @@ - (BOOL)shouldAddStorePayment:(SKPayment *)payment forProduct:(SKProduct *)produ
}
}
if (product) {
NSString *key = RCTKeyForInstance(product.productIdentifier);
[self addPromiseForKey:key resolve:resolve reject:reject];

SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
} else {
Expand Down Expand Up @@ -178,6 +181,9 @@ - (BOOL)shouldAddStorePayment:(SKPayment *)payment forProduct:(SKProduct *)produ
}
}
if (product) {
NSString *key = RCTKeyForInstance(product.productIdentifier);
[self addPromiseForKey:key resolve:resolve reject:reject];

payment = [SKMutablePayment paymentWithProduct:product];
#if __IPHONE_12_2
if (@available(iOS 12.2, *)) {
Expand Down Expand Up @@ -293,7 +299,7 @@ - (BOOL)shouldAddStorePayment:(SKPayment *)payment forProduct:(SKProduct *)produ
else {
NSArray<SKPaymentTransaction *> *transactions = [[SKPaymentQueue defaultQueue] transactions];
NSMutableArray *output = [NSMutableArray array];

for (SKPaymentTransaction *item in transactions) {
NSMutableDictionary *purchase = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@(item.transactionDate.timeIntervalSince1970 * 1000), @"transactionDate",
Expand All @@ -304,7 +310,7 @@ - (BOOL)shouldAddStorePayment:(SKPayment *)payment forProduct:(SKProduct *)produ
];
[output addObject:purchase];
}

resolve(output);
}
}];
Expand All @@ -317,11 +323,11 @@ -(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProduc
[self addProduct:prod];
}
NSMutableArray* items = [NSMutableArray array];

for (SKProduct* product in validProducts) {
[items addObject:[self getProductObject:product]];
}

[self resolvePromisesForKey:RCTKeyForInstance(request) value:items];
}

Expand Down Expand Up @@ -414,7 +420,7 @@ -(void)finishTransactionWithIdentifier:(NSString *)transactionIdentifier {
-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue { //////// RESTORE
NSLog(@"\n\n\n paymentQueueRestoreCompletedTransactionsFinished \n\n.");
NSMutableArray* items = [NSMutableArray arrayWithCapacity:queue.transactions.count];

for(SKPaymentTransaction *transaction in queue.transactions) {
if(transaction.transactionState == SKPaymentTransactionStateRestored
|| transaction.transactionState == SKPaymentTransactionStatePurchased) {
Expand All @@ -424,7 +430,7 @@ -(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
}];
}
}

[self resolvePromisesForKey:@"availableItems" value:items];
}

Expand All @@ -443,7 +449,7 @@ -(void)purchaseProcess:(SKPaymentTransaction *)transaction {
}
[self getPurchaseData:transaction withBlock:^(NSDictionary *purchase) {
[self resolvePromisesForKey:RCTKeyForInstance(transaction.payment.productIdentifier) value:purchase];

// additionally send event
if (hasListeners) {
[self sendEventWithName:@"purchase-updated" body: purchase];
Expand All @@ -465,7 +471,7 @@ -(NSString *)standardErrorCode:(int)code {
@"E_RECEIPT_FAILED",
@"E_RECEIPT_FINISHED_FAILED"
];

if (code > descriptions.count - 1) {
return descriptions[0];
}
Expand All @@ -476,25 +482,25 @@ -(NSDictionary*)getProductObject:(SKProduct *)product {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.locale = product.priceLocale;

NSString* localizedPrice = [formatter stringFromNumber:product.price];
NSString* introductoryPrice = localizedPrice;

NSString* introductoryPricePaymentMode = @"";
NSString* introductoryPriceNumberOfPeriods = @"";
NSString* introductoryPriceSubscriptionPeriod = @"";

NSString* currencyCode = @"";
NSString* periodNumberIOS = @"0";
NSString* periodUnitIOS = @"";

NSString* itemType = @"Do not use this. It returned sub only before";

if (@available(iOS 11.2, *)) {
// itemType = product.subscriptionPeriod ? @"sub" : @"iap";
unsigned long numOfUnits = (unsigned long) product.subscriptionPeriod.numberOfUnits;
SKProductPeriodUnit unit = product.subscriptionPeriod.unit;

if (unit == SKProductPeriodUnitYear) {
periodUnitIOS = @"YEAR";
} else if (unit == SKProductPeriodUnitMonth) {
Expand All @@ -504,17 +510,17 @@ -(NSDictionary*)getProductObject:(SKProduct *)product {
} else if (unit == SKProductPeriodUnitDay) {
periodUnitIOS = @"DAY";
}

periodNumberIOS = [NSString stringWithFormat:@"%lu", numOfUnits];

// subscriptionPeriod = product.subscriptionPeriod ? [product.subscriptionPeriod stringValue] : @"";
//introductoryPrice = product.introductoryPrice != nil ? [NSString stringWithFormat:@"%@", product.introductoryPrice] : @"";
if (product.introductoryPrice != nil) {

//SKProductDiscount introductoryPriceObj = product.introductoryPrice;
formatter.locale = product.introductoryPrice.priceLocale;
introductoryPrice = [formatter stringFromNumber:product.introductoryPrice.price];

switch (product.introductoryPrice.paymentMode) {
case SKProductDiscountPaymentModeFreeTrial:
introductoryPricePaymentMode = @"FREETRIAL";
Expand All @@ -533,7 +539,7 @@ -(NSDictionary*)getProductObject:(SKProduct *)product {
introductoryPriceNumberOfPeriods = @"0";
break;
}

if (product.introductoryPrice.subscriptionPeriod.unit == SKProductPeriodUnitDay) {
introductoryPriceSubscriptionPeriod = @"DAY";
} else if (product.introductoryPrice.subscriptionPeriod.unit == SKProductPeriodUnitWeek) {
Expand All @@ -545,26 +551,26 @@ -(NSDictionary*)getProductObject:(SKProduct *)product {
} else {
introductoryPriceSubscriptionPeriod = @"";
}

} else {
introductoryPrice = @"";
introductoryPricePaymentMode = @"";
introductoryPriceNumberOfPeriods = @"";
introductoryPriceSubscriptionPeriod = @"";
}
}

if (@available(iOS 10.0, *)) {
currencyCode = product.priceLocale.currencyCode;
}

NSArray *discounts;
#if __IPHONE_12_2
if (@available(iOS 12.2, *)) {
discounts = [self getDiscountData:[product.discounts copy]];
}
#endif

NSDictionary *obj = [NSDictionary dictionaryWithObjectsAndKeys:
product.productIdentifier, @"productId",
[product.price stringValue], @"price",
Expand All @@ -582,7 +588,7 @@ -(NSDictionary*)getProductObject:(SKProduct *)product {
discounts, @"discounts",
nil
];

return obj;
}

Expand All @@ -592,15 +598,15 @@ - (NSMutableArray *)getDiscountData:(NSArray *)discounts {
NSString *paymendMode;
NSString *subscriptionPeriods;
NSString *discountType;

if (@available(iOS 11.2, *)) {
for(SKProductDiscount *discount in discounts) {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterCurrencyStyle;
formatter.locale = discount.priceLocale;
localizedPrice = [formatter stringFromNumber:discount.price];
NSString *numberOfPeriods;

switch (discount.paymentMode) {
case SKProductDiscountPaymentModeFreeTrial:
paymendMode = @"FREETRIAL";
Expand All @@ -619,7 +625,7 @@ - (NSMutableArray *)getDiscountData:(NSArray *)discounts {
numberOfPeriods = @"0";
break;
}

switch (discount.subscriptionPeriod.unit) {
case SKProductPeriodUnitDay:
subscriptionPeriods = @"DAY";
Expand All @@ -636,8 +642,8 @@ - (NSMutableArray *)getDiscountData:(NSArray *)discounts {
default:
subscriptionPeriods = @"";
}


NSString* discountIdentifier = @"";
#if __IPHONE_12_2
if (@available(iOS 12.2, *)) {
Expand All @@ -653,10 +659,10 @@ - (NSMutableArray *)getDiscountData:(NSArray *)discounts {
discountType = @"";
break;
}

}
#endif

[mappedDiscounts addObject:[NSDictionary dictionaryWithObjectsAndKeys:
discountIdentifier, @"identifier",
discountType, @"type",
Expand All @@ -669,7 +675,7 @@ - (NSMutableArray *)getDiscountData:(NSArray *)discounts {
]];
}
}

return mappedDiscounts;
}

Expand All @@ -686,14 +692,14 @@ - (void) getPurchaseData:(SKPaymentTransaction *)transaction withBlock:(void (^)
[receiptData base64EncodedStringWithOptions:0], @"transactionReceipt",
nil
];

// originalTransaction is available for restore purchase and purchase of cancelled/expired subscriptions
SKPaymentTransaction *originalTransaction = transaction.originalTransaction;
if (originalTransaction) {
purchase[@"originalTransactionDateIOS"] = @(originalTransaction.transactionDate.timeIntervalSince1970 * 1000);
purchase[@"originalTransactionIdentifierIOS"] = originalTransaction.transactionIdentifier;
}

block(purchase);
}
}];
Expand Down