Skip to content

Commit 6bf387b

Browse files
akshetpandeychirag04
authored andcommitted
Allow products to be fetched even if purchases are restricted (#123)
Restricting IAPs doesn't prevent us from fetching product information. A better way to handle this is to check canMakePayments method and show appropriate message on the payment screen. Update README to include canMakePayments Also add note about calling canMakePayments before purchaseProduct to show a better error message to users.
1 parent 08e18ea commit 6bf387b

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

InAppUtils/InAppUtils.m

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,15 +191,11 @@ - (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
191191
RCT_EXPORT_METHOD(loadProducts:(NSArray *)productIdentifiers
192192
callback:(RCTResponseSenderBlock)callback)
193193
{
194-
if([SKPaymentQueue canMakePayments]){
195-
SKProductsRequest *productsRequest = [[SKProductsRequest alloc]
196-
initWithProductIdentifiers:[NSSet setWithArray:productIdentifiers]];
197-
productsRequest.delegate = self;
198-
_callbacks[RCTKeyForInstance(productsRequest)] = callback;
199-
[productsRequest start];
200-
} else {
201-
callback(@[@"not_available"]);
202-
}
194+
SKProductsRequest *productsRequest = [[SKProductsRequest alloc]
195+
initWithProductIdentifiers:[NSSet setWithArray:productIdentifiers]];
196+
productsRequest.delegate = self;
197+
_callbacks[RCTKeyForInstance(productsRequest)] = callback;
198+
[productsRequest start];
203199
}
204200

205201
RCT_EXPORT_METHOD(canMakePayments: (RCTResponseSenderBlock)callback)

Readme.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ InAppUtils.loadProducts(products, (error, products) => {
6161

6262
**Troubleshooting:** If you do not get back your product(s) then there's a good chance that something in your iTunes Connect or Xcode is not properly configured. Take a look at this [StackOverflow Answer](http://stackoverflow.com/a/11707704/293280) to determine what might be the issue(s).
6363

64+
### Checking if payments are allowed
65+
66+
```javascript
67+
InAppUtils.canMakePayments((canMakePayments) => {
68+
if(!canMakePayments) {
69+
Alert.alert('Not Allowed', 'This device is not allowed to make purchases. Please check restrictions on device');
70+
}
71+
})
72+
```
73+
74+
**NOTE:** canMakePayments may return false because of country limitation or parental contol/restriction setup on the device.
75+
6476
### Buy product
6577

6678
```javascript
@@ -76,6 +88,8 @@ InAppUtils.purchaseProduct(productIdentifier, (error, response) => {
7688

7789
**NOTE:** Call `loadProducts` prior to calling `purchaseProduct`, otherwise this will return `invalid_product`. If you're calling them right after each other, you will need to call `purchaseProduct` inside of the `loadProducts` callback to ensure it has had a chance to complete its call.
7890

91+
**NOTE:** Call `canMakePurchases` prior to calling `purchaseProduct` to ensure that the user is allowed to make a purchase. It is generally a good idea to inform the user that they are not allowed to make purchases from their account and what they can do about it instead of a cryptic error message from iTunes.
92+
7993
**NOTE:** `purchaseProductForUser(productIdentifier, username, callback)` is also available.
8094
https://stackoverflow.com/questions/29255568/is-there-any-way-to-know-purchase-made-by-which-itunes-account-ios/29280858#29280858
8195

0 commit comments

Comments
 (0)