Skip to content

Commit

Permalink
Implement purchaseProductForUser and restorePurchasesForUser methods (#…
Browse files Browse the repository at this point in the history
…119)

* Implement purchaseProductForUser and restorePurchasesForUser methods
  • Loading branch information
RalfNieuwenhuizen authored and chirag04 committed Aug 24, 2017
1 parent 54ff695 commit 97a8ddd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
32 changes: 31 additions & 1 deletion InAppUtils/InAppUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,22 @@ - (void)paymentQueue:(SKPaymentQueue *)queue
}
}

RCT_EXPORT_METHOD(purchaseProductForUser:(NSString *)productIdentifier
username:(NSString *)username
callback:(RCTResponseSenderBlock)callback)
{
[self doPurchaseProduct:productIdentifier username:username callback:callback];
}

RCT_EXPORT_METHOD(purchaseProduct:(NSString *)productIdentifier
callback:(RCTResponseSenderBlock)callback)
{
[self doPurchaseProduct:productIdentifier username:nil callback:callback];
}

- (void) doPurchaseProduct:(NSString *)productIdentifier
username:(NSString *)username
callback:(RCTResponseSenderBlock)callback
{
SKProduct *product;
for(SKProduct *p in products)
Expand All @@ -89,7 +103,10 @@ - (void)paymentQueue:(SKPaymentQueue *)queue
}

if(product) {
SKPayment *payment = [SKPayment paymentWithProduct:product];
SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:product];
if(username) {
payment.applicationUsername = username;
}
[[SKPaymentQueue defaultQueue] addPayment:payment];
_callbacks[RCTKeyForInstance(payment.productIdentifier)] = callback;
} else {
Expand Down Expand Up @@ -159,6 +176,18 @@ - (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

RCT_EXPORT_METHOD(restorePurchasesForUser:(NSString *)username
callback:(RCTResponseSenderBlock)callback)
{
NSString *restoreRequest = @"restoreRequest";
_callbacks[RCTKeyForInstance(restoreRequest)] = callback;
if(!username) {
callback(@[@"username_required"]);
return;
}
[[SKPaymentQueue defaultQueue] restoreCompletedTransactionsWithApplicationUsername:username];
}

RCT_EXPORT_METHOD(loadProducts:(NSArray *)productIdentifiers
callback:(RCTResponseSenderBlock)callback)
{
Expand Down Expand Up @@ -206,6 +235,7 @@ - (void)productsRequest:(SKProductsRequest *)request
@"currencySymbol": [item.priceLocale objectForKey:NSLocaleCurrencySymbol],
@"currencyCode": [item.priceLocale objectForKey:NSLocaleCurrencyCode],
@"priceString": item.priceString,
@"countryCode": [item.priceLocale objectForKey: NSLocaleCountryCode],
@"downloadable": item.downloadable ? @"true" : @"false" ,
@"description": item.localizedDescription ? item.localizedDescription : @"",
@"title": item.localizedTitle ? item.localizedTitle : @"",
Expand Down
7 changes: 7 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ InAppUtils.loadProducts(products, (error, products) => {
| currencySymbol | string | The currency symbol, i.e. "$" or "SEK" |
| currencyCode | string | The currency code, i.e. "USD" of "SEK" |
| priceString | string | Localised string of price, i.e. "$1,234.00" |
| countryCode | string | Country code of the price, i.e. "GB" or "FR"|
| downloadable | boolean | Whether the purchase is downloadable |
| description | string | Description string |
| title | string | Title string |
Expand All @@ -75,6 +76,9 @@ 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:** `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

**Response:** A transaction object with the following fields:

| Field | Type | Description |
Expand Down Expand Up @@ -108,6 +112,9 @@ InAppUtils.restorePurchases((error, response) => {
});
```

**NOTE:** `restorePurchasesForUser(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

**Response:** An array of transaction objects with the following fields:

| Field | Type | Description |
Expand Down

0 comments on commit 97a8ddd

Please sign in to comment.