expo-stripe-tap-to-pay
Version:
Expo config plugin to enable Tap to Pay on iPhone entitlements and set Apple Merchant ID.
70 lines (61 loc) • 1.88 kB
JavaScript
// index.js
const {
withEntitlementsPlist,
createRunOncePlugin,
} = require("@expo/config-plugins");
/**
* Helper function to set Tap to Pay entitlements
* on the iOS Entitlements plist object.
*
* @param {Record<string, any>} entitlements The current entitlements plist
* @param {string} merchantId The Apple Merchant ID (e.g., "merchant.com.myapp")
* @returns Updated entitlements plist object
*/
function setTapToPayEntitlement(entitlements, merchantId) {
// Enable Tap to Pay on iPhone
entitlements[
"com.apple.developer.proximity-reader.payment.acceptance"
] = true;
// Ensure the merchant ID array is present
if (!entitlements["com.apple.developer.merchant-id"]) {
entitlements["com.apple.developer.merchant-id"] = [];
}
// Only add the merchantId if it's not already in the array
const merchantIds = entitlements["com.apple.developer.merchant-id"];
if (!merchantIds.includes(merchantId)) {
merchantIds.push(merchantId);
}
return entitlements;
}
/**
* The actual config plugin.
*
* Usage in app.config.js or app.json:
*
* plugins: [
* [
* "expo-stripe-tap-to-pay",
* {
* merchantId: "merchant.com.myapp"
* }
* ]
* ]
*
* @param {import('@expo/config-plugins').ConfigPluginProps} config
* @param {{ merchantId?: string }} props
*/
function withTapToPayOnIphone(config, props = {}) {
// Provide a default or fallback
const merchantId = props.merchantId || "merchant.com.example";
// Modify the iOS entitlements using withEntitlementsPlist
return withEntitlementsPlist(config, (config) => {
config.modResults = setTapToPayEntitlement(config.modResults, merchantId);
return config;
});
}
// Use createRunOncePlugin to ensure the plugin is only run once per build
module.exports = createRunOncePlugin(
withTapToPayOnIphone,
"expo-stripe-tap-to-pay",
"1.0.3"
);