UNPKG

react-native-passkit-apple-pay

Version:

A React Native library for integrating Apple Pay with Conekta payment processor using PassKit framework

142 lines (130 loc) 4.38 kB
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 */ import React, { createContext, useContext } from 'react'; import { NativeModules, TouchableOpacity } from 'react-native'; const { ApplePayModule } = 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__*/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 */ export const ApplePayProvider = ({ children, merchantIdentifier, supportedNetworks, countryCode, currencyCode }) => { const contextValue = { merchantIdentifier, supportedNetworks, countryCode, currencyCode }; return /*#__PURE__*/React.createElement(ApplePayContext.Provider, { value: contextValue }, children); }; /** * Hook to access Apple Pay configuration context * Must be used within an ApplePayProvider */ export const useApplePayContext = () => { const context = 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 */ export 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.createElement(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 */ export 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}`); } }; //# sourceMappingURL=index.js.map