Skip to content

Commit

Permalink
fix get available items (#1988)
Browse files Browse the repository at this point in the history
* fix get available items

* fix lint

Co-authored-by: Andres Aguilar <andres.aguilar@nfl.com>
  • Loading branch information
andresesfm and Andres Aguilar authored Sep 22, 2022
1 parent 944d9fa commit 6fded98
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 11 deletions.
2 changes: 2 additions & 0 deletions IapExample/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react';
import {setup} from 'react-native-iap';
import {NavigationContainer} from '@react-navigation/native';

import {StackNavigator} from './navigators';
setup({storekitMode: 'STOREKIT2_MODE'});
export const App = () => (
<NavigationContainer>
<StackNavigator />
Expand Down
2 changes: 1 addition & 1 deletion IapExample/src/screens/AvailablePurchases.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const AvailablePurchases = () => {

{availablePurchases.map((availablePurchase, index) => (
<Row
key={availablePurchase.productId}
key={availablePurchase.productId + `${index}`}
fields={[
{
label: 'Product Id',
Expand Down
4 changes: 4 additions & 0 deletions docs/docs/migrate_to_11.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,7 @@ Note that the Transaction Updated Listener accepts `TransactionEvent`s that can

The new native libraries allow us to move away from sending events and instead returning responses for events that are generated by user interaction. Having both models becomes hard to document so we are phasing it out and keeping only one listener for events outside the normal purchase flow.
For example `getAvaiableItems` was both posting events and returing the list of events. That won't be the case when using Sk2, it will only return the list of Transactions. If you still want to get the events published in Sk1, use the optional parameter `{alsoPublishToEventListener:true}` in `getAvailableItems`

### getAvailableItems on Sk2

Another important distinction is that this method now returns the user's purchased items consistently. Meaning that the state of the transaction won't change when calling it a second time
1 change: 0 additions & 1 deletion ios/RNIapIos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ class RNIapIos: RCTEventEmitter, SKRequestDelegate, SKPaymentTransactionObserver
}
}
@objc public func getAvailableItems(
alsoPublishToEventListener: Bool,
_ resolve: @escaping RCTPromiseResolveBlock = { _ in },
reject: @escaping RCTPromiseRejectBlock = { _, _, _ in }
) {
Expand Down
2 changes: 1 addition & 1 deletion ios/RNIapIosSk2.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ @interface RCT_EXTERN_MODULE (RNIapIosSk2, NSObject)
reject:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(getAvailableItems:
alsoPublishToEventListener:(BOOL)alsoPublishToEventListener
(BOOL)alsoPublishToEventListener
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject)

Expand Down
10 changes: 5 additions & 5 deletions ios/RNIapIosSk2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ class RNIapIosSk2: RCTEventEmitter {
Task {
do {
let products = try await Product.products(for: skus)
self.products = products.reduce(into: [String: Product]()) {(res, prod) in
res[prod.id] = prod
}
products.forEach({(prod) in
self.products[prod.id] = prod
})
resolve(products.map({ (prod: Product) -> [String: Any?]? in
return serialize(prod)
}).compactMap({$0}))
Expand All @@ -144,8 +144,8 @@ class RNIapIosSk2: RCTEventEmitter {
}

@objc public func getAvailableItems(
alsoPublishToEventListener: Bool,
_ resolve: @escaping RCTPromiseResolveBlock = { _ in },
_ alsoPublishToEventListener: Bool,
resolve: @escaping RCTPromiseResolveBlock = { _ in },
reject: @escaping RCTPromiseRejectBlock = { _, _, _ in }
) {
Task {
Expand Down
21 changes: 19 additions & 2 deletions src/iap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ProductSk2,
productSk2Map,
subscriptionSk2Map,
transactionSk2Map,
} from './types/appleSk2';
import {
fillProductsWithAdditionalData,
Expand Down Expand Up @@ -289,7 +290,15 @@ export const getPurchaseHistory = ({
(
Platform.select({
ios: async () => {
return getIosModule().getAvailableItems(alsoPublishToEventListener);
if (isIosStorekit2()) {
return Promise.resolve(
(
await RNIapIosSk2.getAvailableItems(alsoPublishToEventListener)
).map(transactionSk2Map),
);
} else {
return RNIapIos.getAvailableItems();
}
},
android: async () => {
if (RNIapAmazonModule) {
Expand Down Expand Up @@ -397,7 +406,15 @@ export const getAvailablePurchases = ({
(
Platform.select({
ios: async () => {
return getIosModule().getAvailableItems(alsoPublishToEventListener);
if (isIosStorekit2()) {
return Promise.resolve(
(
await RNIapIosSk2.getAvailableItems(alsoPublishToEventListener)
).map(transactionSk2Map),
);
} else {
return RNIapIos.getAvailableItems();
}
},
android: async () => {
if (RNIapAmazonModule) {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/iosSk2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type getItems = (skus: Sku[]) => Promise<ProductSk2[]>;

type getAvailableItems = (
alsoPublishToEventListener?: boolean,
) => Promise<Purchase[]>;
) => Promise<TransactionSk2[]>;

export type BuyProduct = (
sku: Sku,
Expand Down

0 comments on commit 6fded98

Please sign in to comment.