Skip to content

Commit

Permalink
Allow products to be fetched even if purchases are restricted (#123)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
akshetpandey authored and chirag04 committed Sep 27, 2017
1 parent 08e18ea commit 6bf387b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
14 changes: 5 additions & 9 deletions InAppUtils/InAppUtils.m
Expand Up @@ -191,15 +191,11 @@ - (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
RCT_EXPORT_METHOD(loadProducts:(NSArray *)productIdentifiers
callback:(RCTResponseSenderBlock)callback)
{
if([SKPaymentQueue canMakePayments]){
SKProductsRequest *productsRequest = [[SKProductsRequest alloc]
initWithProductIdentifiers:[NSSet setWithArray:productIdentifiers]];
productsRequest.delegate = self;
_callbacks[RCTKeyForInstance(productsRequest)] = callback;
[productsRequest start];
} else {
callback(@[@"not_available"]);
}
SKProductsRequest *productsRequest = [[SKProductsRequest alloc]
initWithProductIdentifiers:[NSSet setWithArray:productIdentifiers]];
productsRequest.delegate = self;
_callbacks[RCTKeyForInstance(productsRequest)] = callback;
[productsRequest start];
}

RCT_EXPORT_METHOD(canMakePayments: (RCTResponseSenderBlock)callback)
Expand Down
14 changes: 14 additions & 0 deletions Readme.md
Expand Up @@ -61,6 +61,18 @@ InAppUtils.loadProducts(products, (error, products) => {

**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).

### Checking if payments are allowed

```javascript
InAppUtils.canMakePayments((canMakePayments) => {
if(!canMakePayments) {
Alert.alert('Not Allowed', 'This device is not allowed to make purchases. Please check restrictions on device');
}
})
```

**NOTE:** canMakePayments may return false because of country limitation or parental contol/restriction setup on the device.

### Buy product

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

**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.

**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.

**NOTE:** `purchaseProductForUser(productIdentifier, username, callback)` is also available.
https://stackoverflow.com/questions/29255568/is-there-any-way-to-know-purchase-made-by-which-itunes-account-ios/29280858#29280858

Expand Down

0 comments on commit 6bf387b

Please sign in to comment.