PaySwitcher
  • About PaySwitcher
    • 🛒For Online Businesses
    • 🪜For Small & Medium Enterprises
    • 🏢For Enterprises
    • 🖥️For SaaS Providers
    • 🛍️For E-Commerce Businesses
    • 📦For Marketplace/Platforms
    • 🏦For Banks & Financial Institutions
  • PaySwitcher Cloud
    • ⚡Quickstart
      • 📥Migrate from Stripe
        • Web
        • Android
        • iOS
        • React Native
      • 🥗Payment Recipes
        • Use PayPal With Stripe
    • ⚙️Control Centre Account setup
    • 📦Integration guide
      • 🌐Web
        • Node And React
        • Customization
        • Error Codes
        • Node and HTML
        • Vanilla JS and REST API Integration
      • 📱Android
        • Kotlin with Node Backend
        • Customization
        • Features
      • 📱iOS
        • Swift with Node Backend
        • Customization
        • Features
      • ⏺️React Native
        • React Native with Node Backend (Beta)
        • Card Widget (Beta)
        • Customization
      • ⏺️Flutter
        • Flutter with Node Backend
        • Customization
      • Headless SDK
      • Payment Methods Management
    • 💳Payment methods setup
      • 💳Cards
      • 📱Wallets
        • Apple Pay
          • Web Domain
          • iOS Application
        • Google Pay
        • PayPal
      • 📆Pay Later
      • 🏦Banks
        • Bank Debits
        • Bank Redirects
        • Bank Transfers
      • 🪙Crypto
      • 🔑Test Credentials
    • 🔌Connectors
      • 🖲️Available Connectors
        • ACI
        • Adyen
        • Airwallex
        • Authorizedotnet
        • Bambora
        • Bank of America
        • Billwerk
        • Bluesnap
        • Braintree
        • Checkout
        • Coinbase
        • Cybersource
          • Apple Pay
          • Google Pay
        • dLocal
        • Fiserv
        • GlobalPayments
        • GoCardless
        • Klarna
        • Mollie
        • MultiSafepay
        • Nuvei
        • OpenNode
        • Paypal
        • PayU
        • Prophetpay
        • Rapyd
        • Shift4
        • Stripe
        • TrustPay
        • Volt
        • Worldline
        • Worldpay
        • Zen
      • Activate connector on PaySwitcher
      • Test a Payment with connector
    • 🪝Webhooks
  • Features
    • 🔀Payment flows
      • 🔁Saving payment methods & recurring payments
      • 💵Payouts
        • ➕Get started with payouts!
        • 🔗Process payouts using saved payment methods
        • 🛣️Route your payout transactions using Smart Router
        • ♻️Smart Retries in Payout
        • 🔗Payout links
      • 0️ 0️ 0️ Zero Amount Authorization
      • 🔓Tokenization & saved cards
      • 🔗Payment links
      • ⏭️External Authentication for 3DS
      • 💰Manual Capture
      • 🛑Fraud Blocklist
      • 🔁Subscriptions
      • 🔃PG Agnostic Recurring Payments
    • 🕹️Merchant controls
      • 🛣️Smart Router
        • Rule Based Routing
        • Volume Based Routing
        • Default Fallback Routing
      • 🛡️Fraud & risk management
      • 🔃Smart retries
      • 🎛️Analytics & operations
      • 📋3DS decision manager
        • Setup guide
      • 📋Surcharge
        • Surcharge Setup guide
      • 🔼3DS Step-up retries
      • 🚩Disputes/Chargebacks Management
      • 🤝Reconciliation
        • Getting Started with Recon
    • 🔑Account management
      • 🔢Exporting payments data
      • 🤹Multiple accounts & profiles
      • 🛂Manage your team
    • 🛍️E-commerce platform plugins
      • WooCommerce Plugin
        • Setup
        • Compatibility
        • FAQs
  • SECURITY AND COMPLIANCE
    • 🔏Overview
    • 💳PCI Compliance
    • 🔐Data Security
    • 💽GDPR compliance
    • 🕵️Identity and Access Management
  • Learn more
    • 🍡SDK Reference
      • Node
      • React
      • JS
    • 📐PaySwitcher Architecture
      • Router
      • Storage
      • A Payments Switch with virtually zero overhead
    • 🌊Payment flows
Powered by GitBook
On this page
  • Requirements
  • 1. Setup the server
  • 1.1 Install the payswitcher-node library
  • 1.2 Create a payment
  • 2. Build checkout page on the client
  • 2.1 Install the react-native-payswitcher libraries
  • 2.2 Peer Dependencies
  • 2.3 iOS Only
  • 2.4 Android Only
  • 2.5 Add HyperProvider to your React Native app
  • 2.6 Use HyperProvider
  • 3. Complete the checkout on the client
  • 3.1 import useHyper to your checkout page
  • 3.2 Fetch the PaymentIntent client Secret
  • 3.3 Collect Payment details
  • 3.4 Handle Payment Response
  1. PaySwitcher Cloud
  2. Integration guide
  3. React Native

React Native with Node Backend (Beta)

Integrate hyper SDK to your React Native App using payswitcher-node

PreviousReact NativeNextCard Widget (Beta)

Last updated 11 months ago

Currently in beta please contact to get early access

Use this guide to integrate hyper SDK to your React Native app. You can use the following Demo App as a reference with your PaySwitcher credentials to test the setup.

Before following these steps, please configure your payment methods.

Requirements

  • Android 5.0 (API level 21) and above

  • 7.3.1

  • 7.5.1+

  • iOS 12.4 and above

  • CocoaPods

  • npm

1. Setup the server

1.1 Install the payswitcher-node library

Install the package and import it in your code

$ npm install @payswitcher/payswitcher-node

1.2 Create a payment

const hyper = require("@payswitcher/payswitcher-node")(‘YOUR_API_KEY’);

Add an endpoint on your server that creates a Payment. Creating a Payment helps to establish the intent of the customer to start a payment. It also helps to track the customer’s payment lifecycle, keeping track of failed payment attempts and ensuring the customer is only charged once. Return the client_secret obtained in the response to securely complete the payment on the client.

// Create a Payment with the order amount and currency
app.post("/create-payment", async (req, res) => {
  try {
    const paymentIntent = await hyper.paymentIntents.create({
      currency: "USD",
      amount: 100,
    });
    // Send publishable key and PaymentIntent details to client
    res.send({
      clientSecret: paymentIntent.client_secret,
    });
  } catch (err) {
    return res.status(400).send({
      error: {
        message: err.message,
      },
    });
  }
});

2. Build checkout page on the client

2.1 Install the react-native-payswitcher libraries

Install the packages and import it into your code

$ npm install @payswitcher/payswitcher-sdk-react-native

2.2 Peer Dependencies

Install the following dependencies

yarn add react-native-code-push
yarn add react-native-gesture-handler
yarn add react-native-inappbrowser-reborn
yarn add react-native-klarna-inapp-sdk
yarn add react-native-safe-area-context
yarn add react-native-svg
yarn add @sentry/react-native

2.3 iOS Only

Run pod install in iOS folder

pod install

2.4 Android Only

In the Android Folder inside strings.xml file(android/app/src/main/res/values/strings.xml) add the below line

<string name="CodePushDeploymentKey">PaySwitcherRNDemo</string>

In the Android Folder inside settings.gradle file add the following line

include(":react-native-code-push");

project(":react-native-code-push").projectDir = new File(
  rootProject.projectDir,
  "../node_modules/react-native-code-push/android/app"
);

2.5 Add HyperProvider to your React Native app

Use HyperProvider to ensure that you stay PCI compliant by sending payment details directly to PaySwitcher server.

import { HyperProvider } from "@payswitcher/react-native-payswitcher";

2.6 Use HyperProvider

To initialize PaySwitcher in your React Native app, wrap your payment screen with the HyperProvider component. Only the API publishable key in publishableKey is required. The following example shows how to initialize PaySwitcher using the HyperProvider component.

import { HyperProvider } from "@payswitcher/react-native-payswitcher ";

function App() {
  return (
    <HyperProvider publishableKey="YOUR_PUBLISHABLE_KEY">
      // Your app code here
    </HyperProvider>
  );
}

3. Complete the checkout on the client

3.1 import useHyper to your checkout page

In the checkout of your app, import useHyper() hook

import { useHyper } from "@payswitcher/react-native-payswitcher";

3.2 Fetch the PaymentIntent client Secret

Make a network request to the backend endpoint you created in the previous step. The clientSecret returned by your endpoint is used to complete the payment.

const fetchPaymentParams = async () => {
  const response = await fetch(`${API_URL}/create-payment`, {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ items: [{ id: "xl-tshirt" }], country: "US" }),
  });
  const val = await response.json();
  return val;
};

3.3 Collect Payment details

Call initPaymentSession from the useHyper hook to customise paymentsheet, billing or shipping addresses and initialize paymentsheet

const { initPaymentSession, presentPaymentSheet } = useHyper();
const [paymentSession,setPaymentSession]=React.useState(null);
const initializePaymentSheet = async () => {
  const { clientSecret } = await fetchPaymentParams();

  const customAppearance = {
    colors: {
      light: {
        primary: "#00FF00",
      },
    },
  };
  const params={
      merchantDisplayName: "Example, Inc.",
      clientSecret: clientSecret,
      appearance: customAppearance,
  }
  const paymentSession = await initPaymentSession(params);
  setPaymentSession(_=>paymentSession)
};

useEffect(() => {
  initializePaymentSheet();
}, []);

3.4 Handle Payment Response

To display the Payment Sheet, integrate a "Pay Now" button within the checkout page, which, when clicked, invokes the presentPaymentSheet() function. This function will return an asynchronous payment response with various payment status.

const openPaymentSheet = async () => {
  const status = await presentPaymentSheet(paymentSession);
  console.log("presentPaymentSheet response: ", status);
  const { error, paymentOption } = status;
  if (error) {
    switch (error.code) {
      case "cancelled":
        Alert.alert("cancelled", `PaymentSheet was closed`);
        break;
      case "failed":
        Alert.alert("failed", `Payment failed`);
        break;
      default:
        Alert.alert("status not captured", "Please check the integration");
        break;
    }

    Alert.alert(`Error code: ${error.code}`, error.message);
  } else if (paymentOption) {
    switch (paymentOption.label) {
      case "succeeded":
        Alert.alert("succeeded", `Your order is succeeded`);
        break;
      case "requires_capture":
        Alert.alert("requires_capture", `Your order is requires_capture`);
        break;
      default:
        Alert.alert("status not captured", "Please check the integration");
        break;
    }
  } else {
    Alert.alert("Something went wrong", "Please check the integration");
  }
};

return (
  <Screen>
    <Button variant="primary" title="Checkout" onPress={openPaymentSheet} />
  </Screen>
);

Congratulations! Now that you have integrated the payment sheet

Before creating a payment, import the hyper dependencies and initialize it with your API key. Get your API key from .

📦
⏺️
Android Gradle Plugin
Gradle
AndroidX
PaySwitcher dashboard