Skip to content

Commit 97a8ddd

Browse files
RalfNieuwenhuizenchirag04
authored andcommitted
Implement purchaseProductForUser and restorePurchasesForUser methods (#119)
* Implement purchaseProductForUser and restorePurchasesForUser methods
1 parent 54ff695 commit 97a8ddd

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

InAppUtils/InAppUtils.m

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,22 @@ - (void)paymentQueue:(SKPaymentQueue *)queue
7676
}
7777
}
7878

79+
RCT_EXPORT_METHOD(purchaseProductForUser:(NSString *)productIdentifier
80+
username:(NSString *)username
81+
callback:(RCTResponseSenderBlock)callback)
82+
{
83+
[self doPurchaseProduct:productIdentifier username:username callback:callback];
84+
}
85+
7986
RCT_EXPORT_METHOD(purchaseProduct:(NSString *)productIdentifier
8087
callback:(RCTResponseSenderBlock)callback)
88+
{
89+
[self doPurchaseProduct:productIdentifier username:nil callback:callback];
90+
}
91+
92+
- (void) doPurchaseProduct:(NSString *)productIdentifier
93+
username:(NSString *)username
94+
callback:(RCTResponseSenderBlock)callback
8195
{
8296
SKProduct *product;
8397
for(SKProduct *p in products)
@@ -89,7 +103,10 @@ - (void)paymentQueue:(SKPaymentQueue *)queue
89103
}
90104

91105
if(product) {
92-
SKPayment *payment = [SKPayment paymentWithProduct:product];
106+
SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:product];
107+
if(username) {
108+
payment.applicationUsername = username;
109+
}
93110
[[SKPaymentQueue defaultQueue] addPayment:payment];
94111
_callbacks[RCTKeyForInstance(payment.productIdentifier)] = callback;
95112
} else {
@@ -159,6 +176,18 @@ - (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
159176
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
160177
}
161178

179+
RCT_EXPORT_METHOD(restorePurchasesForUser:(NSString *)username
180+
callback:(RCTResponseSenderBlock)callback)
181+
{
182+
NSString *restoreRequest = @"restoreRequest";
183+
_callbacks[RCTKeyForInstance(restoreRequest)] = callback;
184+
if(!username) {
185+
callback(@[@"username_required"]);
186+
return;
187+
}
188+
[[SKPaymentQueue defaultQueue] restoreCompletedTransactionsWithApplicationUsername:username];
189+
}
190+
162191
RCT_EXPORT_METHOD(loadProducts:(NSArray *)productIdentifiers
163192
callback:(RCTResponseSenderBlock)callback)
164193
{
@@ -206,6 +235,7 @@ - (void)productsRequest:(SKProductsRequest *)request
206235
@"currencySymbol": [item.priceLocale objectForKey:NSLocaleCurrencySymbol],
207236
@"currencyCode": [item.priceLocale objectForKey:NSLocaleCurrencyCode],
208237
@"priceString": item.priceString,
238+
@"countryCode": [item.priceLocale objectForKey: NSLocaleCountryCode],
209239
@"downloadable": item.downloadable ? @"true" : @"false" ,
210240
@"description": item.localizedDescription ? item.localizedDescription : @"",
211241
@"title": item.localizedTitle ? item.localizedTitle : @"",

Readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ InAppUtils.loadProducts(products, (error, products) => {
5454
| currencySymbol | string | The currency symbol, i.e. "$" or "SEK" |
5555
| currencyCode | string | The currency code, i.e. "USD" of "SEK" |
5656
| priceString | string | Localised string of price, i.e. "$1,234.00" |
57+
| countryCode | string | Country code of the price, i.e. "GB" or "FR"|
5758
| downloadable | boolean | Whether the purchase is downloadable |
5859
| description | string | Description string |
5960
| title | string | Title string |
@@ -75,6 +76,9 @@ InAppUtils.purchaseProduct(productIdentifier, (error, response) => {
7576

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

79+
**NOTE:** `purchaseProductForUser(productIdentifier, username, callback)` is also available.
80+
https://stackoverflow.com/questions/29255568/is-there-any-way-to-know-purchase-made-by-which-itunes-account-ios/29280858#29280858
81+
7882
**Response:** A transaction object with the following fields:
7983

8084
| Field | Type | Description |
@@ -108,6 +112,9 @@ InAppUtils.restorePurchases((error, response) => {
108112
});
109113
```
110114

115+
**NOTE:** `restorePurchasesForUser(username, callback)` is also available.
116+
https://stackoverflow.com/questions/29255568/is-there-any-way-to-know-purchase-made-by-which-itunes-account-ios/29280858#29280858
117+
111118
**Response:** An array of transaction objects with the following fields:
112119

113120
| Field | Type | Description |

0 commit comments

Comments
 (0)