React with REST API Integration
Integrate Hyperswitch SDK with React and REST API for web checkout
Before you begin
Complete these one-time setup tasks before integrating the SDK.
Setup the server
Complete the Server Setup guide before building the checkout page.
Build checkout page on the client
2.1 Install the hyper-js and react-hyper-js libraries
Install the packages and import it into your code
npm install @juspay-tech/hyper-js
npm install @juspay-tech/react-hyper-js2.2 Add hyper to your React app
Use hyper-js to ensure that you stay PCI compliant by sending payment details directly to Hyperswitch server.
import React, { useState, useEffect } from "react";
import { loadHyper } from "@juspay-tech/hyper-js";
import { hyperElements } from "@juspay-tech/react-hyper-js";2.3 Load hyper-js
Call loadHyper with your publishable API keys to configure the library. To get a publishable Key please find it here.
const hyperPromise = loadHyper("YOUR_PUBLISHABLE_KEY",{
customBackendUrl: "YOUR_BACKEND_URL",
//You can configure this as an endpoint for all the api calls such as session, payments, confirm call.
});2.4 Fetch the Payment and Initialise hyperElements
Immediately make a request to the endpoint on your server to create a new Payment as soon as your checkout page loads. The clientSecret returned by your endpoint is used to complete the payment.
useEffect(() => {
// Create PaymentIntent as soon as the page loads
fetch("/create-payment", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ items: [{ id: "xl-tshirt" }], country: "US" }),
})
.then((res) => res.json())
.then((data) => setClientSecret(data.clientSecret));
}, []);2.5 Initialise HyperElements
Pass the promise from loadHyper to the HyperElements component. This allows the child components to access the Hyper service via the HyperElements parent component. Additionally, pass the client secret as an options to the HyperElements component.
<div className="App">
{clientSecret && (
<HyperElements options={options} hyper={hyperPromise}>
<CheckoutForm />
</HyperElements>
)}
</div>2.7 Store a reference to Hyper
Access the hyper-js library in your CheckoutForm component by using the useHyper() and useWidgets() hooks. If you need to access Widgets via a class component, use the WidgetsConsumer instead. You can find the API for these methods here.
const hyper = useHyper();
const widgets = useWidgets();Complete the checkout on the client
Key Features of Hyperswitch's Express Checkout
Fast Performance: One-click payment at checkout enables a smooth and frictionless payment experience to customers.
Multiple Payment Options: Supports ApplePay, PayPal, Klarna, and GooglePay, giving customers a variety of payment choices on top of the speed in checkout.
Easy Integration: Our SDK can be easily integrated with web applications.
Benefits of Hyperswitch's Express Checkout Feature
Better User Experience: One-click payment makes shopping easier, leading to more sales and fewer abandoned carts.
Time Savings: Speeds up the checkout process, saving time for both customers and merchants.
Great for Mobile: Optimized for mobile shopping with ApplePay, PayPal and GooglePay integration for quick purchases on smartphones.
Collect billing and shipping details directly from ApplePay, Klarna, GooglePay, PayPal
3.1 Add the ExpressCheckout
The Express Checkout Element gives you a single integration for accepting payments through one-click payment buttons. Supported payment methods include ApplePay, GooglePay and PayPal.
Add the ExpressCheckout to your Checkout. This embeds an iframe that displays configured payment method types supported by the browser available for the Payment, allowing your customer to select a payment method. The payment methods automatically collects the associated payment details for the selected payment method type.
Define paymentElementOptions:
var expressCheckoutOptions = {
wallets: {
walletReturnUrl: "https://example.com/complete",
//Mandatory parameter for Wallet Flows such as Googlepay, Paypal and Applepay
},
};3.1.A Add the UnifiedCheckout
Add the UnifiedCheckout to your Checkout. This embeds an iframe with a dynamic form that displays configured payment method types available for the Payment, allowing your customer to select a payment method. The form automatically collects the associated payment details for the selected payment method type.
(Optional) Define paymentElementOptions:
3.1.B Complete the payment and handle errors
Call confirmPayment(), passing along the UnifiedCheckout and a return_url to indicate where hyper should redirect the user after they complete the payment. For payments that require additional authentication, hyper redirects the customer to an authentication page depending on the payment method. After the customer completes the authentication process, they're redirected to the return_url.
If there are any immediate errors (for example, your customer's card is declined), hyper-js returns an error. Show that error message to your customer so they can try again.
3.2 Display payment status message
When Hyperswitch redirects the customer to the return_url, the payment_client_secret query parameter is appended by hyper-js. Use this to retrieve the Payment to determine what to show to your customer.
//Look for a parameter called `payment_intent_client_secret` in the url which gives a payment ID, which is then used to retrieve the status of the payment
const paymentID = new URLSearchParams(window.location.search).get(
"payment_intent_client_secret"
);
if (!paymentID) {
return;
}
hyper.retrievePaymentIntent(paymentID).then(({ paymentIntent }) => {
switch (paymentIntent.status) {
case "succeeded":
setMessage("Payment succeeded!");
break;
case "processing":
setMessage("Your payment is processing.");
break;
case "requires_payment_method":
setMessage("Your payment was not successful, please try again.");
break;
default:
setMessage("Something went wrong.");
break;
}
});Next steps
Last updated
Was this helpful?

