stripe-react-native-apple-pay
Version:
React Native wrapper around Stripe Apple Pay
87 lines (84 loc) • 2.79 kB
JavaScript
import { AndroidConfig, createRunOncePlugin, IOSConfig, withAndroidManifest, withEntitlementsPlist } from '@expo/config-plugins';
const {
addMetaDataItemToMainApplication,
getMainApplicationOrThrow,
removeMetaDataItemFromMainApplication
} = AndroidConfig.Manifest;
const pkg = require('stripe-react-native-apple-pay/package.json');
const withStripe = (config, props) => {
config = withStripeIos(config, props);
config = withNoopSwiftFile(config);
config = withStripeAndroid(config, props);
return config;
};
const withStripeIos = (expoConfig, {
merchantIdentifier
}) => {
return withEntitlementsPlist(expoConfig, config => {
config.modResults = setApplePayEntitlement(merchantIdentifier, config.modResults);
return config;
});
};
/**
* Adds the following to the entitlements:
*
* <key>com.apple.developer.in-app-payments</key>
* <array>
* <string>[MERCHANT_IDENTIFIER]</string>
* </array>
*/
export function setApplePayEntitlement(merchantIdentifiers, entitlements) {
const key = 'com.apple.developer.in-app-payments';
const merchants = entitlements[key] ?? [];
if (!Array.isArray(merchantIdentifiers)) {
merchantIdentifiers = [merchantIdentifiers];
}
for (const id of merchantIdentifiers) {
if (id && !merchants.includes(id)) {
merchants.push(id);
}
}
if (merchants.length) {
entitlements[key] = merchants;
}
return entitlements;
}
/**
* Add a blank Swift file to the Xcode project for Swift compatibility.
*/
export const withNoopSwiftFile = config => {
return IOSConfig.XcodeProjectFile.withBuildSourceFile(config, {
filePath: 'noop-file.swift',
contents: ['//', '// @generated', '// A blank Swift file must be created for native modules with Swift files to work correctly.', '//', ''].join('\n')
});
};
const withStripeAndroid = (expoConfig, {
enableGooglePay = false
}) => {
return withAndroidManifest(expoConfig, config => {
config.modResults = setGooglePayMetaData(enableGooglePay, config.modResults);
return config;
});
};
/**
* Adds the following to AndroidManifest.xml:
*
* <application>
* ...
* <meta-data
* android:name="com.google.android.gms.wallet.api.enabled"
* android:value="true|false" />
* </application>
*/
export function setGooglePayMetaData(enabled, modResults) {
const GOOGLE_PAY_META_NAME = 'com.google.android.gms.wallet.api.enabled';
const mainApplication = getMainApplicationOrThrow(modResults);
if (enabled) {
addMetaDataItemToMainApplication(mainApplication, GOOGLE_PAY_META_NAME, 'true');
} else {
removeMetaDataItemFromMainApplication(mainApplication, GOOGLE_PAY_META_NAME);
}
return modResults;
}
export default createRunOncePlugin(withStripe, pkg.name, pkg.version);
//# sourceMappingURL=withStripe.js.map