Skip to content

Instantly share code, notes, and snippets.

@stefanoeb
Last active November 9, 2020 18:51
Show Gist options
  • Save stefanoeb/305f32f1cd3617cc6ba9785d3e63d955 to your computer and use it in GitHub Desktop.
Save stefanoeb/305f32f1cd3617cc6ba9785d3e63d955 to your computer and use it in GitHub Desktop.
import { Alert } from 'react-native';
import * as IAP from 'react-native-iap';
/**
* This is the development mock for the IAP Module
*/
const MockIAP = {
async initConnection(): Promise<boolean> {
return true;
},
async getProducts(skus: string[]): Promise<IAP.Product[]> {
return skus.map((sku) => ({
title: 'Test product',
description: 'Test description',
price: '1.00',
currency: 'USD',
localizedPrice: `$1.00`,
type: 'iap',
productId: sku,
}));
},
flushFailedPurchasesCachedAsPendingAndroid() {
return;
},
purchaseUpdatedListener() {
return { remove: () => false };
},
purchaseErrorListener() {
return { remove: () => false };
},
async finishTransaction() {
return;
},
requestPurchase() {
Alert.alert(
'Proceed?',
'This is not the real payment flow. You are in development mode.',
[
{
text: 'Simulate payment',
onPress: () => {
// IMPORTANT: Don't do anything here except calling the same success handler you use for production
const handlers = require('~/lib/IapHandlers');
handlers.confirmPurchase({
productId: 'dev-env-fake-product',
transactionDate: Date.now(),
transactionReceipt: 'dev-env-fake-receipt',
});
},
style: 'default',
},
{
text: 'Cancel',
style: 'cancel',
onPress: () => {
// IMPORTANT: Don't do anything here except calling the same cancellation handler you use for production
const handlers = require('~/lib/IapHandlers'); // avoid require cycles
handlers.cancelledPurchase('User cancelled in dev mode');
},
},
],
);
},
};
export default __DEV__ ? MockIAP : IAP;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment