Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add buyWithQuantity iOS function #240

Merged
merged 5 commits into from Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -65,6 +65,7 @@ Also, note that this is our last migration for renaming method names without any
| getAvailablePurchases | | `Promise<Purchase[]>` | Get all purchases made by the user (either non-consumable, or haven't been consumed yet)
| buySubscription | `string` Subscription ID/sku, `string` Old Subscription ID/sku (on Android) | `Promise<Purchase>` | Create (buy) a subscription to a sku. For upgrading/downgrading subscription on Android pass second parameter with current subscription ID, on iOS this is handled automatically by store. |
| buyProduct | `string` Product ID/sku | `Promise<Purchase>` | Buy a product |
| buyProductWithQuantityIOS | `string` Product ID/sku, `number` Quantity | `Promise<Purchase>` | Buy a product with a specified quantity (iOS only) |
| buyProductWithoutFinishTransaction | `string` Product ID/sku | `Promise<Purchase>` | Buy a product without finish transaction call (iOS only) |
| finishTransaction | `void` | `void` | Send finishTransaction call to Apple IAP server. Call this function after receipt validation process |
| consumeProduct | `string` Purchase token | `Promise<void>` | Consume a product (on Android.) No-op on iOS. |
Expand Down
8 changes: 8 additions & 0 deletions index.d.ts
Expand Up @@ -104,6 +104,14 @@ export function buySubscription(sku: string, oldSku?: string) : Promise<Subscrip
*/
export function buyProduct(sku: string) : Promise<ProductPurchase>;

/**
* Buy a product with a specified quantity (iOS only)
* @param {string} sku The product's sku/ID
* @param {number} quantity The amount of product to buy
* @returns {Promise<Purchase>}
*/
export function buyProductWithQuantityIOS(sku: string, quantity: number) : Promise<ProductPurchase>;

/**
* Buy a product without finish transanction to sync with IOS purchasing consumables. Make sure to call finishTransanction when you are done with it or the purchase may not be transferred. Also, note that this method is not changed from buyProduct in android.
* @param {string} sku The product's sku/ID
Expand Down
11 changes: 11 additions & 0 deletions index.js
Expand Up @@ -104,6 +104,16 @@ export const buyProduct = (sku) => Platform.select({
android: () => RNIapModule.buyItemByType(ANDROID_ITEM_TYPE_IAP, sku, null)
})();

/**
* Buy a product with a specified quantity (iOS only)
* @param {string} sku The product's sku/ID
* @param {number} quantity The amount of product to buy
* @returns {Promise<ProductPurchase>}
*/
export const buyProductWithQuantityIOS = (sku, quantity) => Platform.select({
ios: () => RNIapIos.buyProductWithQuantityIOS(sku, quantity),
android: () => Promise.resolve()
})();

/**
* Buy a product without transaction finish (iOS only)
Expand Down Expand Up @@ -227,6 +237,7 @@ export default {
consumeAllItems,
buySubscription,
buyProduct,
buyProductWithQuantityIOS,
buyProductWithoutFinishTransaction,
finishTransaction,
consumePurchase,
Expand Down
23 changes: 23 additions & 0 deletions ios/RNIapIos.m
Expand Up @@ -116,6 +116,29 @@ -(void)rejectPromisesForKey:(NSString*)key code:(NSString*)code message:(NSStrin
}
}

RCT_EXPORT_METHOD(buyProductWithQuantityIOS:(NSString*)sku
quantity:(NSInteger*)quantity
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
NSLog(@"\n\n\n buyProductWithQuantityIOS \n\n.");
autoReceiptConform = true;
SKProduct *product;
for (SKProduct *p in validProducts) {
if([sku isEqualToString:p.productIdentifier]) {
product = p;
break;
}
}
if (product) {
SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:product];
payment.quantity = quantity;
[[SKPaymentQueue defaultQueue] addPayment:payment];
[self addPromiseForKey:RCTKeyForInstance(payment.productIdentifier) resolve:resolve reject:reject];
} else {
reject(@"E_DEVELOPER_ERROR", @"Invalid product ID.", nil);
}
}

RCT_EXPORT_METHOD(buyProductWithoutAutoConfirm:(NSString*)sku
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {
Expand Down