react-native-passkit-apple-pay
Version:
A React Native library for integrating Apple Pay with Conekta payment processor using PassKit framework
151 lines (139 loc) • 5.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useApplePayContext = exports.completeApplePayPayment = exports.ApplePayProvider = exports.ApplePayButton = void 0;
var _react = _interopRequireWildcard(require("react"));
var _reactNative = require("react-native");
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } /**
* React Native PassKit Apple Pay for Conekta
*
* A React Native library for integrating Apple Pay with Conekta as payment processor
* using PassKit framework.
*
* @author Conekta <alexis.nahuel@conekta.com>
* @license MIT
*/
const {
ApplePayModule
} = _reactNative.NativeModules;
/**
* Apple Pay payment method information as returned by PassKit
*/
/**
* Apple Pay payment data header containing cryptographic information
* Required for Conekta's Apple Pay token validation
*/
/**
* Apple Pay payment data containing encrypted payment information
*/
/**
* Complete Apple Pay token structure for Conekta integration
* This token should be sent directly to Conekta's API for processing
*/
/**
* Configuration context for Apple Pay integration with Conekta
*/
/**
* Payment summary for display in Apple Pay sheet
* Amount should be in the smallest currency unit (e.g., cents for MXN)
*/
const ApplePayContext = /*#__PURE__*/(0, _react.createContext)(undefined);
/**
* Provider component that configures global Apple Pay parameters for Conekta integration
* All ApplePayButton components within this provider will use these settings
*
* @param merchantIdentifier - Your merchant ID registered with Conekta
* @param countryCode - Country code where your business operates
* @param currencyCode - Local currency code for your market
* @param supportedNetworks - Payment networks supported by Conekta
*/
const ApplePayProvider = ({
children,
merchantIdentifier,
supportedNetworks,
countryCode,
currencyCode
}) => {
const contextValue = {
merchantIdentifier,
supportedNetworks,
countryCode,
currencyCode
};
return /*#__PURE__*/_react.default.createElement(ApplePayContext.Provider, {
value: contextValue
}, children);
};
/**
* Hook to access Apple Pay configuration context
* Must be used within an ApplePayProvider
*/
exports.ApplePayProvider = ApplePayProvider;
const useApplePayContext = () => {
const context = (0, _react.useContext)(ApplePayContext);
if (!context) {
throw new Error('useApplePayContext must be used within an ApplePayProvider');
}
return context;
};
/**
* A wrapper component that converts its children into an Apple Pay button
* Initiates the native payment flow when pressed and returns a token
*
* The token returned by this component should be sent directly to your
* backend for processing with Conekta's charges/orders API
*/
exports.useApplePayContext = useApplePayContext;
const ApplePayButton = ({
payment,
onPaymentToken,
onError,
children,
...rest
}) => {
const context = useApplePayContext();
const handlePress = async () => {
if (!payment) {
const error = new Error('ApplePayButton: payment prop is required');
onError === null || onError === void 0 || onError(error);
return;
}
try {
const paymentRequest = {
...context,
label: payment.label,
amount: payment.amount
};
const token = await ApplePayModule.pay(paymentRequest);
onPaymentToken(token);
} catch (error) {
onError === null || onError === void 0 || onError(new Error(error.message));
}
};
return /*#__PURE__*/_react.default.createElement(_reactNative.TouchableOpacity, _extends({
onPress: handlePress
}, rest), children);
};
/**
* Completes the Apple Pay transaction from the JavaScript side
* Must be called after Conekta has processed the payment
*
* This function signals to the native Apple Pay UI whether the payment
* succeeded or failed, allowing the user to see the appropriate feedback
*
* @param success - true if Conekta payment was successful, false otherwise
* @param errorMessage - Error message to display if success is false
*/
exports.ApplePayButton = ApplePayButton;
const completeApplePayPayment = async (success, errorMessage = null) => {
try {
await ApplePayModule.completePayment(success, errorMessage);
} catch (error) {
// @todo: Add integration with Conekta's error tracking
throw new Error(`Failed to complete Apple Pay payment: ${error.message}`);
}
};
exports.completeApplePayPayment = completeApplePayPayment;
//# sourceMappingURL=index.js.map