In this section, you will get details to Integrate PaySwitcher SDK using Node Backend and React Frontend
Before following these steps, please configure your payment methods here. Use this guide to integrate payswitcher SDK to your React app. You can also use this demo app as a reference with your PaySwitcher credentials to test the setup.
Demo App
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
Before creating a payment, import the hyper dependencies and initialize it with your API key. Get your API key from .
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 hyper-js and react-hyper-js libraries
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.
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. If you need to access Widgets via a class component, use the WidgetsConsumer instead. You can find the API for these methods here.
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:
var unifiedCheckoutOptions = {
wallets: {
walletReturnUrl: "https://example.com/complete",
//Mandatory parameter for Wallet Flows such as Googlepay, Paypal and Applepay
},
};
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.
const handleSubmit = async (e) => {
setMessage("");
//e.preventDefault();
if (!hyper || !widgets) {
return;
}
setIsLoading(true);
const { error, status } = await hyper.confirmPayment({
elements,
confirmParams: {
// Make sure to change this to your payment completion page
return_url: "https://example.com/complete",
},
redirect: "always", // if you wish to redirect always, otherwise it is defaulted to "if_required"
});
if (error) {
if (error.type === "card_error" || error.type === "validation_error") {
setMessage(error.message);
} else {
if (error.message) {
setMessage(error.message);
} else {
setMessage("An unexpected error occurred.");
}
}
}
if (status) {
handlePaymentStatus(status); //handle payment status
}
setIsLoading(false);
};
Alternate Implementation: SDK handles the Confirm Button
For SDK to render the confirm button and handle the confirm payment, in paymentElementOptions, you can send:
handleConfirm (required) - A boolean value indicating whether the SDK should handle the confirmation of the payment.
confirmParams (required) - Itβs an object which takes return_url. return_url parameter specifies the URL where the user should be redirected after payment confirmation.
buttonText (optional) - The text to display on the payment button.
Default value: Pay Now
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
},
};
When PaySwitcher 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;
}
});
4. Elements Events
Some events are emitted by payment elements, listening to those events is the only way to communicate with these elements. All events have a payload object with the type of the Element that emitted the event as an elementType property. Following events are emitted by payment elements.
change
ready
focus
blur
4.1 Calling Elements events
First create instance of widgets using getElement function. It will return null if no matching type is found.
// Create instance of widgets
var paymentElement = widgets.getElement("payment");
// handle event
if (paymentelement) {
// in place of "EVENT" use "change", "ready", "focus" etc.
paymentElement.on("EVENT", callbackFn);
}
4.2 "change" event
The "change" event will be triggered when value changes in Payment element.
paymentElement.on("change", function (event) {
// YOUR CODE HERE
});
Callback function will be fired when the event will be triggered. When called it will be passed an event object with the following properties.
{
elementType: 'payment', // The type of element that emitted this event.
complete: false, // If all required field are complete
empty: false, // if the value is empty.
value: { type: "card" }, // current selected payment method like "card", "klarna" etc
}
4.3 "ready" event
The "ready" event will be triggered when payment element is full rendered and can accept "focus" event calls.
Callback for ready event will be triggered with following event object
{
ready: boolean, // true when payment element is full rendered
}
4.4 "focus", "blur" event
Focus and blur event triggered when respective event will be triggered in payment element.
Callback for these event will be triggered with following event object.
// Event object for focus event
{
focus: boolean, // true when focused on payment element
}
// Event object for blur event
{
blur: boolean,
}
Congratulations! Now that you have integrated the PaySwitcher SDK on your app, you can customize the payment elements to blend with the rest of your app.
Next step:
In case your integrating the ExpressCheckout (mentioned later below), instead of creating multiple paymentIntents for the same customer session, you can also use for better analytics.
Call loadHyper with your publishable API keys to configure the library. To get an publishable Key please find it .
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 to the HyperElements component.