The @juspay-tech/react-native-hyperswitch provides a seamless way to integrate Hyperswitch payments into your React Native applications. This guide covers installation, configuration, and implementation.
npm install @juspay-tech/react-native-hyperswitch
# or
yarn add @juspay-tech/react-native-hyperswitchThe SDK requires these peer dependencies:
npm install react-native-svg react-native-inappbrowser-reborn
# or
yarn add react-native-svg react-native-inappbrowser-rebornFor iOS, you may need to run:
cd ios && pod installnpx react-native codegenThis will generate all the necessary files which are required for react native new arch
import { HyperProvider } from '@juspay-tech/react-native-hyperswitch';
import {
useHyper,
type InitPaymentSessionParams,
type InitPaymentSessionResult,
} from '@juspay-tech/react-native-hyperswitch';
export default function App() {
const { initPaymentSession, presentPaymentSheet } = useHyper();
const setup = useCallback(async (): Promise<void> => {
try {
// Get client secret from your backend
const clientSecret = getCL() || "clientSecret";
const params: InitPaymentSessionParams = {
paymentIntentClientSecret: clientSecret,
};
const result: InitPaymentSessionResult = await initPaymentSession(params);
if (result.error) {
setStatus(`Initialization failed: ${result.error}`);
console.error('Payment session initialization failed:', result.error);
} else {
setStatus('Ready to checkout');
}
} catch (error) {
console.error('Setup failed:', error);
}
}, [initPaymentSession]);
useEffect(()=>{
setup()
},[setup])
return (
<HyperProvider publishableKey="pk_snd_your_publishable_key_here">
{/* Checkout page */}
</HyperProvider>
);
}import {
useHyper,
type PresentPaymentSheetParams,
type PresentPaymentSheetResult,
} from '@juspay-tech/react-native-hyperswitch';
export default function PaymentScreen() {
const { presentPaymentSheet } = useHyper();
const checkout = async (): Promise<void> => {
try {
const options: PresentPaymentSheetParams = {
appearance: {
theme: 'Dark', // or 'Light'
},
// primaryButtonLabel: 'Complete Purchase', // Optional custom button label
};
const result: PresentPaymentSheetResult = await presentPaymentSheet(options);
const { error, paymentResult } = result;
if (error) {
console.error('Payment failed:', JSON.stringify(error));
// Handle payment error
} else {
console.log('Payment completed with status:', paymentResult?.status);
console.log('Message:', paymentResult?.message);
// Handle successful payment
}
} catch (error) {
console.error('Checkout failed:', error);
}
};
// ... rest of component
}const customAppearance = {
theme: 'Dark',
colors: {
background: '#452061',
componentBackground: 'black',
componentText: 'white',
primary: '#77DF95',
primaryText: 'white',
},
primaryButton: {
shapes: {
borderRadius: 36,
shadow: {
color: '#378C46',
opacity: 0.5,
blurRadius: 10,
offset: {
x: 0,
y: 4,
},
},
},
},
shapes: {
shadow: {
color: '#378C46',
opacity: 1,
blurRadius: 10,
offset: {
x: 0,
y: 6,
},
},
},
};
const options: PresentPaymentSheetParams = {
appearance: customAppearance,
primaryButtonLabel: 'Complete Purchase',
};interface InitPaymentSessionParams {
paymentIntentClientSecret: string | undefined;
}interface InitPaymentSessionResult {
error?: string;
}interface PresentPaymentSheetParams {
appearance?: {
theme?: 'Light' | 'Dark';
colors?: {
background?: string;
componentBackground?: string;
componentText?: string;
primary?: string;
primaryText?: string;
};
primaryButton?: {
shapes?: {
borderRadius?: number;
shadow?: ShadowConfig;
};
};
shapes?: {
shadow?: ShadowConfig;
};
};
primaryButtonLabel?: string;
}interface PresentPaymentSheetResult {
error?: any;
paymentResult?: {
status?: string;
message?: string;
};
}Returns an object with:
initPaymentSession(params: InitPaymentSessionParams): Promise<InitPaymentSessionResult>presentPaymentSheet(params: PresentPaymentSheetParams): Promise<PresentPaymentSheetResult>
Always implement proper error handling:
const setup = async () => {
try {
const paymentIntent = await createPaymentIntent();
if (!paymentIntent) {
setStatus('Failed to create payment intent');
return;
}
const result = await initPaymentSession({
paymentIntentClientSecret: paymentIntent,
});
if (result.error) {
setStatus(`Initialization failed: ${result.error}`);
} else {
setStatus('Ready to checkout');
}
} catch (error) {
console.error('Setup failed:', error);
setStatus('Setup failed');
}
};-
Metro bundler issues with SVG
- Ensure
react-native-svgis properly installed and linked
- Ensure
-
Android network requests failing
- Use
http://10.0.2.2:PORTinstead oflocalhost:PORTfor Android emulator
- Use
-
iOS build issues
- Run
cd ios && pod installafter installing the SDK
- Run
-
Payment initialization fails
- Verify your publishable key is correct
- Ensure your backend returns a valid client secret
- Check network connectivity
-
React Native CodeGen issues
- Clean the android builds using gradlew clean
- run
npx react-native codegento autogenerate the required files // can be skipped for old-arch
- Enable console logging to track payment flow
- Verify client secret format from your backend
- Test with different payment methods
- Check Hyperswitch dashboard for payment status
For issues and questions:
- Check the Hyperswitch documentation
- Review the SDK source code on GitHub
- Contact Hyperswitch support team