Integrate hyper SDK to any Web App using hyperswitch-node
In this section, you will get details to Integrate Hyperswitch SDK using Node Backend and Vanilla JS
Before following these steps, please configure your payment methodshere. Use this guide to integrate hyperswitch SDK to you app with any framework. If you are using React framework please go through React Integration to use a dedicated wrapper.
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 intentconstapp=express();app.post("/create-payment",async (req, res) => {try {constresponse=awaitfetch(`https://sandbox.hyperswitch.io/payments`, { method:"POST", headers: { "Content-Type":"application/json","api-key":"YOUR_API_KEY" }, body:JSON.stringify(req.body),//body to have amount, currency and profile_id });constpaymentIntent=awaitresponse.json()// Send publishable key and PaymentIntent details to clientres.send({ clientSecret:paymentIntent.client_secret, }); } catch (err) {returnres.status(400).send({ error: { message:err.message, }, }); }});
In case your integrating the ExpressCheckout (mentioned later below), instead of creating multiple paymentIntents for the same customer session, you can also use paymentsUpdate API for better analytics.
2. Build checkout page on the client
2.1 Define the payment form
This step is recommended for the Unified Checkout for an enhanced user experience. In case you are integrating Express Checkout (mentioned later below), this step is not required.
Add one empty placeholder div to your checkout form for each Widget that you’ll mount. HyperLoader inserts an iframe into each div to securely collect the customer’s email address and payment information.
2.2 Fetch the Payment and create the Unified Checkout
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.
Important: Make sure to never share your API key with your client application as this could potentially compromise your payment flow
Following this, create a unifiedCheckout and mount it to the placeholder div in your payment form. This embeds an iframe with a dynamic form that displays configured payment method types available from the Payment, allowing your customer to select a payment method. The form automatically collects the associated payment details for the selected payment method type.
Incase, you want the SDK to be loaded on a particular event (like button click), you can call the initialize function on that event.
// Fetches a payment intent and captures the client secretasyncfunctioninitialize() {constresponse=awaitfetch("/create-payment", { method:"POST", headers: { "Content-Type":"application/json" }, body:JSON.stringify({currency:"USD",amount:100}), });const { clientSecret } =awaitresponse.json();// Initialise Hyperloader.jsvar script =document.createElement('script');script.type ='text/javascript';script.src ="https://beta.hyperswitch.io/v1/HyperLoader.js";let hyper; script.onload= () => { hyper =window.Hyper("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. })constappearance= { theme:"midnight", };constwidgets=hyper.widgets({ appearance, clientSecret });constunifiedCheckoutOptions= { layout:"tabs", wallets: { walletReturnUrl:"https://example.com/complete",//Mandatory parameter for Wallet Flows such as Googlepay, Paypal and Applepay }, };constunifiedCheckout=widgets.create("payment", unifiedCheckoutOptions);unifiedCheckout.mount("#unified-checkout"); };document.body.appendChild(script);}
2.2 Fetch the Payment and create the Express Checkout
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.
Make a request to the endpoint on your server to create a new Payment. The clientSecret returned by your endpoint is used to complete the payment.
Important: Make sure to never share your API key with your client application as this could potentially compromise your payment flow
Create an expressCheckout and mount it to the placeholder div in your payment form. 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.
// Fetches a payment intent and captures the client secretasyncfunctioninitialize() {constresponse=awaitfetch("/create-payment", { method:"POST", headers: { "Content-Type":"application/json" }, body:JSON.stringify({currency:"USD",amount:100}), });const { clientSecret } =awaitresponse.json();// Initialise Hyperloader.jsvar script =document.createElement('script');script.type ='text/javascript';script.src ="https://beta.hyperswitch.io/v1/HyperLoader.js";let hyper; script.onload= () => { hyper =window.Hyper("YOUR_PUBLISHABLE_KEY")constappearance= { theme:"midnight", };constwidgets=hyper.widgets({ appearance, clientSecret });constexpressCheckoutOptions= { wallets: { walletReturnUrl:"https://example.com/complete",//Mandatory parameter for Wallet Flows such as Googlepay, Paypal and Applepay }, };constexpressCheckout=widgets.create("expressCheckout", expressCheckoutOptions);expressCheckout.mount("#express-checkout"); };document.body.appendChild(script);}
3. Complete payment on the client
3.1 Handle the submit event and complete the payment
Note: This step is not required for ExpressCheckout
Listen to the form’s submit event to know when to confirm the payment through the hyper API.
Call confirmPayment(), passing along the unifiedCheckout and a return_url to indicate where Hyper should redirect the user after they complete the payment. 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.
asyncfunctionhandleSubmit(e) {setMessage("");e.preventDefault();if (!hyper ||!widgets) {return; }setIsLoading(true);const { error,status } =awaithyper.confirmPayment({ widgets, 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);}
Also if there are any immediate errors (for example, your customer’s card is declined), HyperLoader returns an error. Show that error message to your customer so they can try again.
3.2 Display a payment status message
When Hyper redirects the customer to the return_url, the payment_intent_client_secret query parameter is appended by HyperLoader. Use this to retrieve the Payment to determine what to show to your customer.
// Fetches the payment status after payment submissionasyncfunctioncheckStatus() {constclientSecret=newURLSearchParams(window.location.search).get("payment_intent_client_secret" );if (!clientSecret) {return; }const { payment } =awaithyper.retrievePayment(clientSecret);switch (payment.status) {case"succeeded":showMessage("Payment succeeded!");break;case"processing":showMessage("Your payment is processing.");break;case"requires_payment_method":showMessage("Your payment was not successful, please try again.");break;default:showMessage("Something went wrong.");break; }}
Congratulations! Now that you have integrated the Hyperswitch SDK on your app, you can customize the payment elements to blend with the rest of your app.