chirag04 / react-native-in-app-utils

A react-native wrapper for handling in-app payments
MIT License
892 stars 187 forks source link

Fix crash on simulator with this small change #230

Closed benadamstyles closed 3 years ago

benadamstyles commented 3 years ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

After upgrading to Xcode 12.5 and iOS 14 simulators, I am no longer able to test in-app purchases on the Xcode simulators using the real network – you now need to create a StoreKit configuration file which then allows the simulator to perform in-app purchases locally, however, in this scenario, appStoreReceiptURL (called here) is nil, which causes a complete app crash on this line because dictionaryWithDictionary cannot accept nil values.

My proposed fix is to return null in that case, which dictionaryWithDictionary does accept and which therefore avoids a crash on the simulator.

Here is the diff that solved my problem:

diff --git a/node_modules/react-native-in-app-utils/InAppUtils/InAppUtils.m b/node_modules/react-native-in-app-utils/InAppUtils/InAppUtils.m
index cf860da..f35fff3 100644
--- a/node_modules/react-native-in-app-utils/InAppUtils/InAppUtils.m
+++ b/node_modules/react-native-in-app-utils/InAppUtils/InAppUtils.m
@@ -253,11 +253,12 @@ - (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
 }

 - (NSDictionary *)getPurchaseData:(SKPaymentTransaction *)transaction {
+    NSString *receipt = [self grandUnifiedReceipt];
     NSMutableDictionary *purchase = [NSMutableDictionary dictionaryWithDictionary: @{
         @"transactionDate": @(transaction.transactionDate.timeIntervalSince1970 * 1000),
         @"transactionIdentifier": transaction.transactionIdentifier,
         @"productIdentifier": transaction.payment.productIdentifier,
-        @"transactionReceipt": [self grandUnifiedReceipt]
+        @"transactionReceipt": receipt ? receipt : [NSNull null]
     }];
     // originalTransaction is available for restore purchase and purchase of cancelled/expired subscriptions
     SKPaymentTransaction *originalTransaction = transaction.originalTransaction;

If you are happy with this change I can make a PR.

chirag04 commented 3 years ago

@benadamstyles sure. that sounds like a simple fix. Feel free to open a PR.