Skip to content

Commit

Permalink
mapping from sk2 transaction to purchase result (#2141)
Browse files Browse the repository at this point in the history
* mapping from sk2 transaction to purchase result

* rename map method for better readability

* fix lint

Co-authored-by: Andres Aguilar <andres.aguilar@nfl.com>
  • Loading branch information
andresesfm and Andres Aguilar authored Nov 29, 2022
1 parent fd23e83 commit ebf2c33
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/eventEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {EmitterSubscription, NativeEventEmitter} from 'react-native';

import {TransactionEvent, transactionSk2Map} from './types/appleSk2';
import {TransactionEvent, transactionSk2ToPurchaseMap} from './types/appleSk2';
import {isIosStorekit2} from './iap';
import {
getAndroidModule,
Expand Down Expand Up @@ -50,7 +50,7 @@ export const purchaseUpdatedListener = (
const eventEmitter = new NativeEventEmitter(getNativeModule());
const proxyListener = isIosStorekit2()
? (event: Purchase) => {
listener(transactionSk2Map(event as any));
listener(transactionSk2ToPurchaseMap(event as any));
}
: listener;
const emitterSubscription = eventEmitter.addListener(
Expand Down
43 changes: 25 additions & 18 deletions src/iap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ProductSk2,
productSk2Map,
subscriptionSk2Map,
transactionSk2Map,
transactionSk2ToPurchaseMap,
} from './types/appleSk2';
import {
fillProductsWithAdditionalData,
Expand All @@ -28,6 +28,7 @@ import {
Product,
ProductPurchase,
ProductType,
Purchase,
PurchaseResult,
PurchaseStateAndroid,
RequestPurchase,
Expand Down Expand Up @@ -345,7 +346,7 @@ export const getPurchaseHistory = ({
alsoPublishToEventListener?: boolean;
automaticallyFinishRestoredTransactions?: boolean;
onlyIncludeActiveItems?: boolean;
} = {}): Promise<(ProductPurchase | SubscriptionPurchase)[]> =>
} = {}): Promise<Purchase[]> =>
(
Platform.select({
ios: async () => {
Expand All @@ -356,7 +357,7 @@ export const getPurchaseHistory = ({
alsoPublishToEventListener,
onlyIncludeActiveItems,
)
).map(transactionSk2Map),
).map(transactionSk2ToPurchaseMap),
);
} else {
return RNIapIos.getAvailableItems(
Expand Down Expand Up @@ -472,7 +473,7 @@ export const getAvailablePurchases = ({
alsoPublishToEventListener?: boolean;
automaticallyFinishRestoredTransactions?: boolean;
onlyIncludeActiveItems?: boolean;
} = {}): Promise<(ProductPurchase | SubscriptionPurchase)[]> =>
} = {}): Promise<Purchase[]> =>
(
Platform.select({
ios: async () => {
Expand All @@ -483,7 +484,7 @@ export const getAvailablePurchases = ({
alsoPublishToEventListener,
onlyIncludeActiveItems,
)
).map(transactionSk2Map),
).map(transactionSk2ToPurchaseMap),
);
} else {
return RNIapIos.getAvailableItems(
Expand Down Expand Up @@ -603,13 +604,16 @@ export const requestPurchase = (
if (isIosStorekit2()) {
const offer = offerSk2Map(withOffer);

return RNIapIosSk2.buyProduct(
sku,
andDangerouslyFinishTransactionAutomaticallyIOS,
appAccountToken,
quantity ?? -1,
offer,
const purchase = transactionSk2ToPurchaseMap(
await RNIapIosSk2.buyProduct(
sku,
andDangerouslyFinishTransactionAutomaticallyIOS,
appAccountToken,
quantity ?? -1,
offer,
),
);
return Promise.resolve(purchase);
} else {
return RNIapIos.buyProduct(
sku,
Expand Down Expand Up @@ -757,13 +761,16 @@ export const requestSubscription = (
if (isIosStorekit2()) {
const offer = offerSk2Map(withOffer);

return RNIapIosSk2.buyProduct(
sku,
andDangerouslyFinishTransactionAutomaticallyIOS,
appAccountToken,
quantity ?? -1,
offer,
const purchase = transactionSk2ToPurchaseMap(
await RNIapIosSk2.buyProduct(
sku,
andDangerouslyFinishTransactionAutomaticallyIOS,
appAccountToken,
quantity ?? -1,
offer,
),
);
return Promise.resolve(purchase);
} else {
return RNIapIos.buyProduct(
sku,
Expand Down Expand Up @@ -845,7 +852,7 @@ export const finishTransaction = ({
isConsumable,
developerPayloadAndroid,
}: {
purchase: ProductPurchase | SubscriptionPurchase;
purchase: Purchase;
isConsumable?: boolean;
developerPayloadAndroid?: string;
}): Promise<PurchaseResult | boolean> => {
Expand Down
4 changes: 2 additions & 2 deletions src/modules/iosSk2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {NativeModules} from 'react-native';

import type {Product, ProductPurchase, Purchase, Sku} from '../types';
import type {Product, ProductPurchase, Sku} from '../types';
import type {
PaymentDiscountSk2,
ProductSk2,
Expand All @@ -25,7 +25,7 @@ export type BuyProduct = (
applicationUsername: string | undefined,
quantity: number,
withOffer: Record<keyof PaymentDiscountSk2, string> | undefined,
) => Promise<Purchase>;
) => Promise<TransactionSk2>;

type clearTransaction = () => Promise<void>;
type clearProducts = () => Promise<void>;
Expand Down
2 changes: 1 addition & 1 deletion src/types/appleSk2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export type ProductStatus = {
state: SubscriptionStatus;
};

export const transactionSk2Map = ({
export const transactionSk2ToPurchaseMap = ({
id,
originalPurchaseDate,
productID,
Expand Down

0 comments on commit ebf2c33

Please sign in to comment.