Skip to content

Commit 2a9a7bc

Browse files
committed
Add sandbox agnostic receipt verification
1 parent 7ee92fd commit 2a9a7bc

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

index.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ export const getSubscriptions = (skus: string[]): Promise<Subscription[]> =>
420420
* @returns {Promise<(InAppPurchase | SubscriptionPurchase)[]>}
421421
*/
422422
export const getPurchaseHistory = (): Promise<(
423-
InAppPurchase | SubscriptionPurchase
423+
InAppPurchase | SubscriptionPurchase
424424
)[]> =>
425425
Platform.select({
426426
ios: async () => {
@@ -450,7 +450,7 @@ InAppPurchase | SubscriptionPurchase
450450
* @returns {Promise<(InAppPurchase | SubscriptionPurchase)[]>}
451451
*/
452452
export const getAvailablePurchases = (): Promise<(
453-
InAppPurchase | SubscriptionPurchase
453+
InAppPurchase | SubscriptionPurchase
454454
)[]> =>
455455
Platform.select({
456456
ios: async () => {
@@ -649,7 +649,7 @@ export const finishTransaction = (
649649
} else if (
650650
purchase.userIdAmazon ||
651651
(!purchase.isAcknowledgedAndroid &&
652-
purchase.purchaseStateAndroid === PurchaseStateAndroid.PURCHASED)
652+
purchase.purchaseStateAndroid === PurchaseStateAndroid.PURCHASED)
653653
) {
654654
return myRNIapModule.acknowledgePurchase(
655655
purchase.purchaseToken,
@@ -807,10 +807,46 @@ export const validateReceiptIos = async (
807807
receiptBody: Record<string, unknown>,
808808
isTest?: boolean,
809809
): Promise<Apple.ReceiptValidationResponse | false> => {
810+
if (isTest == null) {
811+
return await requestAgnosticReceiptValidationIos(receiptBody);
812+
}
813+
810814
const url = isTest
811815
? 'https://sandbox.itunes.apple.com/verifyReceipt'
812816
: 'https://buy.itunes.apple.com/verifyReceipt';
813817

818+
const response = await fetchJsonOrThrow(
819+
url,
820+
receiptBody,
821+
);
822+
return response;
823+
};
824+
825+
const requestAgnosticReceiptValidationIos = async (
826+
receiptBody: Record<string, unknown>,
827+
): Promise<Apple.ReceiptValidationResponse | false> => {
828+
const response = await fetchJsonOrThrow(
829+
'https://buy.itunes.apple.com/verifyReceipt',
830+
receiptBody,
831+
);
832+
833+
// Best practice is to check for test receipt and check sandbox instead
834+
// https://developer.apple.com/documentation/appstorereceipts/verifyreceipt
835+
if (response && response.status === Apple.ReceiptValidationStatus.TEST_RECEIPT) {
836+
const response = await fetchJsonOrThrow(
837+
'https://sandbox.itunes.apple.com/verifyReceipt',
838+
receiptBody,
839+
);
840+
return response;
841+
}
842+
843+
return response;
844+
};
845+
846+
const fetchJsonOrThrow = async (
847+
url: string,
848+
receiptBody: Record<string, unknown>,
849+
): Promise<Apple.ReceiptValidationResponse | false> => {
814850
const response = await fetch(url, {
815851
method: 'POST',
816852
headers: new Headers({

0 commit comments

Comments
 (0)