React Native
Accept card payments, Apple Pay and Google Pay in a React Native app with the Cardencer payment sheet.
The Cardencer React Native SDK wraps the native iOS and Android payment sheets in one JavaScript API.
Requirements#
- Android 7.0 (API level 24) or later, Android Gradle Plugin 7.3.1+, Gradle 7.5.1+, AndroidX
- iOS 12.4 or later with CocoaPods
1. Set up your server#
Your server creates the payment and returns sdkAuthorization, publishableKey and profileId. Use the same endpoint as for Android. Keep your secret key on the server.
2. Install the SDK#
Install the Cardencer React Native SDK and its peer dependencies:
npm install @juspay-tech/react-native-hyperswitch react-native-inappbrowser-reborn react-native-svg @sentry/react-nativeThen install the iOS pods:
cd ios && pod install3. Initialise the SDK#
Initialise once with your publishable key and profile ID, and reuse the instance across your app:
import { Hyperswitch } from "@juspay-tech/react-native-hyperswitch";
export const sdkPromise = Hyperswitch.init({
publishableKey: "pk_snd_REPLACE_WITH_YOUR_KEY",
profileId: "pro_REPLACE_WITH_YOUR_PROFILE_ID",
customEndpoints: { overrideEndpoints: { customBackendEndpoint: "https://sandbox.cardencer.com" } },
});Use https://api.cardencer.com and your pk_prd_ key in production.
4. Create a payment session#
const response = await fetch("https://your-server.example.com/create-payment", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ customerId: "cus_58211" }),
});
const { sdkAuthorization } = await response.json();
const sdk = await sdkPromise;
const session = await sdk.initPaymentSession({ sdkAuthorization });Create a new session for every payment, with the latest sdkAuthorization from your server.
5. Present the payment sheet#
import { Button } from "react-native";
function CheckoutButton({ session }) {
const checkout = async () => {
const result = await session.presentPaymentSheet({ merchantDisplayName: "Atelier Martin" });
if (result.status === "completed") {
// Ask your server to confirm the payment, then show a success screen.
} else if (result.status === "canceled") {
// The customer closed the sheet: let them try again.
} else {
console.log(result.message);
}
};
return <Button title="Pay €49.00" onPress={checkout} />;
}result.status |
Meaning | What to do |
|---|---|---|
completed |
The sheet completed the payment flow. | Verify the payment on your server before fulfilment. |
canceled |
The customer closed the sheet. | Return to checkout. |
failed |
The payment failed. | Show result.message and allow a retry. |
Confirm the final status on your server, through a webhook or GET /payments/{payment_id}, before you deliver.
Test it#
Use the test cards in the sheet.