• Documentation
  • API Reference
  • Documentation
  • API Reference
Expand All Collapse All
  • Payment Overview
    • Introduction
    • Choosing an Integration Method
  • Accept Payment
    • Payment Page
      • Quick Start
      • Input Parameters
      • Output Parameters
    • Web SDK
      • Quick Start
      • Nuvei Fields
        • Styling
      • Additional Functions
      • APM Payments
      • Tokenization-Only Flow
      • Scenarios
      • Using ReactJS
        • Full Samples
        • Sandbox Examples
      • FAQs
    • Checkout
      • Quick Start
      • UI Customization
      • Payment Customization
      • Advanced Controls
      • Checkout Examples
    • Server-to-Server
    • Payment Scenarios
    • Mobile SDKs (Beta Release)
      • Android Mobile SDK (Beta Release)
      • iOS Mobile SDK (Beta Release)
    • Flow Diagrams
    • Plugins
      • Magento
        • Rebilling with Magento
      • WooCommerce
        • Rebilling with WooCommerce
      • PrestaShop
        • Rebilling with PrestaShop
      • OpenCart
      • Shopify (via AsiaBill)
      • Mirakl
      • Salesforce
      • SAP
      • WIX
    • Marketplaces
  • Features
    • Authentication
    • Financial Operations
      • Refund
      • Void
      • Auth and Settle
      • Partial Approval
      • Currency Conversion (DCC and MCP)
    • Card Operations
      • Card-on-File
      • PCI and Tokenization
      • Zero-Authorization
      • Merchant-Initiated Transactions (MIT)
      • Blocking Cards
    • Subscription (Rebilling)
    • 3D-Secure
      • 3D-Secure Explained
      • 3DS Implementations
        • 3DS MPI-Only Web SDK
        • 3DS MPI-Only REST
        • 3DS External MPI
        • 3DS Responses
      • 3DS Functions
        • 3D-Secure Fingerprinting
        • 3D-Secure Authentication Challenge
    • Callbacks (DMNs)
      • Configuring the Events API
  • Guides
    • Testing Cards, APIs and APMs
      • Testing Cards
      • Testing APIs with Postman
      • Testing APMs
    • Response Handling
    • Alternative Payment Guides (APMs)
    • Airline Ticket Guides
      • Airline Addendum
      • External Authorization Addendum
    • Payment Facilitators (PayFac)
    • Cashier
      • Cashier Events Guide
      • Cashier Features
    • Withdrawal Guide
    • Risk Guide
      • Appendix 1: Transaction Types
      • Appendix 2: Credits and Payouts
      • Appendix 3: Fraud to Sale Programs
      • Appendix 4: Compliance Programs
      • Appendix 5: Chargebacks
    • eKYC Guide
    • Server SDKs
      • Java SDK
      • .NET SDK
      • PHP SDK
      • Node.JS SDK
    • Fast Track Onboarding Developer Guide
    • Currency Conversion Guides
      • Multiple Currency Pricing (MCP)
      • Dynamic Currency Conversion (DCC)
        • DCC in Cashier or Payment Page
        • DCC in REST API Workflows
        • DCC in Web SDK Workflows
    • Website Compliance Guides
  • Additional Links
    • FAQs
    • API Reference
    • Release Notes
    • Country and Currency Codes

Full Samples

On this page:
  • Overview
  • Three-Part Card Fields
  • One-Part Card Field
  • Without Nuvei Fields

Overview

There are many ways of using ReactJS to collect payment details on your own payment page and send them for processing as part of your own custom payment flow.

For you your convenience, we have provided the following set of full ReactJS code samples, which represents just one of the many ways to use ReactJS:

  • Three-Part Card Fields – Full Implementation
  • One-Part Card Field – Full Implementation
  • Without Nuvei Fields – Full Implementation

Three-Part Card Fields

To try-out sample Web SDK Flow ReactJS code in a sandbox, see the Three-Part Card Fields ReactJS Sandbox topic.

Example App.js
import React, { useEffect, useState } from "react";

import { SafeChargeCC } from "./SafeCharge";
import "./styles.css";

const loadScript = (src) =>
  new Promise((resolve, reject) => {
    const scriptElem = Object.assign(document.createElement("script"), {
      type: "text/javascript",
      defer: true,
      src,
      onerror: (e) => {
        reject(e);
      }
    });
    scriptElem.onload = () => {
      resolve();
    };
    document.body.appendChild(scriptElem);
  });

export default function App() {
  //
  const [safeCharge, setSafeCharge] = useState(null);

  useEffect(() => {
    loadScript(
      "https://cdn.safecharge.com/safecharge_resources/v1/websdk/safecharge.js" //production
    ).then(() => {
      setSafeCharge(
        window.SafeCharge({
          merchantId: "806659927845195034",
          merchantSiteId: "196488"
        })
      );
    });
  }, []);
  //safeCharge component
  return (
    <div className="App">
      <h1>Web SDK</h1>
      <h2>Three-Part Card Field Integration</h2>
      <SafeChargeCC safeCharge={safeCharge} />
    </div>
  );
}
Example SafeCharge.jsx
import React, { memo, useEffect, useState } from "react";

export const SafeChargeCC = memo(({ safeCharge }) => {
  const [cardN, setCardN] = useState(null);
  const [sessionToken, setSessionToken] = useState("");
  const [cardHolderName, setCardHolderName] = useState("CL-BRW1");

  useEffect(() => {
    if (safeCharge) {
      const safeChargeFields = safeCharge.fields({
        fonts: [{ cssUrl: "" }],
        locale: "en",
        fontSize: "16px"
      });

      // Card number----------------------------------------------------

      const cardNumber = safeChargeFields.create("ccNumber", {
        // style: {
        //   base: {
        //     fontSize: "16px"
        //   }
        // }
      });

      cardNumber.attach("#card-number");

      const cardExpiry = safeChargeFields.create("ccExpiration", {
        style: {
          base: {
            fontSize: "16px"
          }
        }
      });

      // cardExpiry.on("change", (evt) => {
      //   console.log("cardExpiry change event >>>", evt);
      // });

      // cardExpiry.on("error", (evt) => {
      //   console.log("cardExpiry error event >>>", evt);
      // });

      cardExpiry.attach("#card-expiry");

      // CVV--------------------------------------------------------------

      const cardCvc = safeChargeFields.create("ccCvc", {
        style: {
          base: {
            fontSize: "16px"
          }
        }
      });

      cardCvc.attach("#card-cvc");
      console.log(cardNumber);
      setCardN(cardNumber);
    }
  }, [safeCharge]);

  const createPayment = (e) => {
    if (cardN) {
      try {
        safeCharge.createPayment(
          {
            sessionToken: sessionToken,
            cardHolderName: cardHolderName,
            paymentOption: cardN,
            billingAddress: {
              email: "someone@somedomain.com",
              country: "GB"
            }
          },
          (resp) => console.log(resp)
        );
      } catch (e) {
        console.error(e);
      }
    }
  };

  const authenticate3d = (e) => {
    if (cardN) {
      try {
        safeCharge.authenticate3d(
          {
            sessionToken: sessionToken,
            cardHolderName: cardHolderName,
            paymentOption: cardN,
            billingAddress: {
              email: "someone@somedomain.com",
              country: "GB"
            }
          },
          (resp) => console.log(resp)
        );
      } catch (e) {
        console.error(e);
      }
    }
  };

  const getToken = (e) => {
    if (cardN) {
      try {
        safeCharge
          .getToken(cardN, {
            sessionToken: sessionToken,
            cardHolderName: cardHolderName,
            paymentOption: cardN,
            billingAddress: {
              email: "someone@somedomain.com",
              country: "GB"
            }
          })
          .then(function (result) {
            if (result.status === "SUCCESS") {
              console.log(result); //pass the paymentOption to the payment API call
            } else {
              console.log(result);
            }
          });
      } catch (e) {
        console.error(e);
      }
    }
  };

  return (
    <>
      <div className="container">
        <div className="item sessionfield">
          <label htmlFor="sessionField">Session Token</label>
          <input
            id="sessionField"
            placeholder="Session Token"
            type="text"
            value={sessionToken}
            onChange={(e) => setSessionToken(e.target.value)}
          />
        </div>

        <div className="item">
          <label htmlFor="cardHolderName">Cardholder Name</label>
          <input
            id="cardHolderName"
            placeholder="Cardholder Name"
            type="text"
            value={cardHolderName}
            onChange={(e) => setCardHolderName(e.target.value)}
          />
        </div>
        <div className="item">
          <label htmlFor="card-number">Card number</label>
          <div id="card-number" className="input" />
        </div>

        <div className="item">
          <label htmlFor="card-expiry">Expiration Date</label>
          <div id="card-expiry" className="input empty" />
        </div>

        <div className="item">
          <label htmlFor="card-cvc">CVC</label>
          <div id="card-cvc" className="input empty" />
        </div>
        <div className="item100">
          <button className="pay-button" onClick={createPayment}>
            Create Payment
          </button>

          <button className="pay-button" onClick={authenticate3d}>
            Authenticate3d
          </button>

          <button className="pay-button" onClick={getToken}>
            getToken
          </button>
        </div>
      </div>
    </>
  );
});

One-Part Card Field

To try-out sample Web SDK Flow ReactJS code in a sandbox, see the One-Part Card Field ReactJS Sandbox topic.

Example App.js
import React, { useEffect, useState } from "react";

import { SafeChargeCC } from "./SafeCharge";
import { loadScript } from "./loadScript";
import "./styles.css";
export default function App() {
  const [safeCharge, setSafeCharge] = useState(null);

  useEffect(() => {
    loadScript(
      "https://cdn.safecharge.com/safecharge_resources/v1/websdk/safecharge.js" //production
    ).then(() => {
      setSafeCharge(
        window.SafeCharge({
          env: "int",
          merchantId: "4182997617632659388",
          merchantSiteId: "208906"
        })
      );
    });
  }, []);

  return (
    <div className="App">
      <h1>Web SDK</h1>
      <h2>Nuvei Fields</h2>
      <SafeChargeCC safeCharge={safeCharge} />
    </div>
  );
}
Example SafeCharge.jsx
import React, { memo, useEffect, useState } from "react";

export const SafeChargeCC = memo(({ safeCharge }) => {
  const [cardNumber, setCardNumber] = useState(null);
  const [sessionToken, setSessionToken] = useState("");
  const [cardHolderName, setCardHolderName] = useState("CL-BRW1");

  useEffect(() => {
    if (safeCharge) {
      const safeChargeFields = safeCharge.fields({
        fonts: [{ cssUrl: "" }],
        locale: "en"
      });

      // Card number----------------------------------------------------

      const cardNumber = safeChargeFields.create("card", {});

      cardNumber.attach("#card-field");

      setCardNumber(cardNumber);
    }
  }, [safeCharge]);

  const createPayment = (e) => {
    if (cardNumber) {
      try {
        safeCharge.createPayment(
          {
            sessionToken: sessionToken,
            cardHolderName: cardHolderName,
            paymentOption: cardNumber,
            billingAddress: {
              email: "someone@somedomain.com",
              country: "GB"
            }
          },
          (resp) => console.log(resp)
        );
      } catch (e) {
        console.error(e);
      }
    }
  };

  const authenticate3d = (e) => {
    if (cardNumber) {
      try {
        safeCharge.authenticate3d(
          {
            sessionToken: sessionToken,
            cardHolderName: cardHolderName,
            paymentOption: cardNumber,
            billingAddress: {
              email: "someone@somedomain.com",
              country: "GB"
            }
          },
          (resp) => console.log(resp)
        );
      } catch (e) {
        console.error(e);
      }
    }
  };

  const getToken = (e) => {
    if (cardNumber) {
      try {
        safeCharge
          .getToken(cardNumber, {
            sessionToken: sessionToken,
            cardHolderName: cardHolderName,
            paymentOption: cardNumber,
            billingAddress: {
              email: "someone@somedomain.com",
              country: "GB"
            }
          })
          .then(function (result) {
            if (result.status === "SUCCESS") {
              console.log(result); //pass the paymentOption to the payment API call
            } else {
              console.log(result);
            }
          });
      } catch (e) {
        console.error(e);
      }
    }
  };

  return (
    <>
      <label htmlFor="sessionField">Session Token</label>
      <div className="item sessionfield">
        <input
          id="sessionField"
          placeholder="Session Token"
          type="text"
          value={sessionToken}
          onChange={(e) => setSessionToken(e.target.value)}
        />
      </div>

      <label htmlFor="cardHolderName">Cardholder Name</label>
      <div className="item cardHolderName">
        <input
          id="cardHolderName"
          placeholder="Cardholder Name"
          type="text"
          value={cardHolderName}
          onChange={(e) => setCardHolderName(e.target.value)}
        />
      </div>

      <div>
        <div className="field">
          <label htmlFor="card-field">Card details</label>
          <div id="card-field" className="input" />
        </div>
      </div>
      <div className="item buttons">
        <button className="pay-button" onClick={createPayment}>
          Create Payment
        </button>
        <button className="pay-button" onClick={authenticate3d}>
          Authenticate3d
        </button>
        <button className="pay-button" onClick={getToken}>
          getToken
        </button>
      </div>
    </>
  );
});

Without Nuvei Fields

To try-out sample Web SDK Flow ReactJS code in a sandbox, see the Without Nuvei Fields ReactJS Sandbox topic.

Example App.js
import React, { useEffect, useState } from "react";

import { SafeChargeCC } from "./SafeCharge";
import "./styles.css";

const loadScript = (src) =>
  new Promise((resolve, reject) => {
    const scriptElem = Object.assign(document.createElement("script"), {
      type: "text/javascript",
      defer: true,
      src,
      onerror: (e) => {
        reject(e);
      }
    });
    scriptElem.onload = () => {
      resolve();
    };
    document.body.appendChild(scriptElem);
  });

export default function App() {
  const [safeCharge, setSafeCharge] = useState(null);

  useEffect(() => {
    loadScript(
      "https://cdn.safecharge.com/safecharge_resources/v1/websdk/safecharge.js" //production
    ).then(() => {
      setSafeCharge(
        window.SafeCharge({
          env: "int",
          merchantId: "479748173730597238",
          merchantSiteId: "180083"
        })
      );
    });
  }, []);

  return (
    <div className="App">
      <h1>Web SDK</h1>
      <h2>without Nuvei Fields</h2>
      <SafeChargeCC safeCharge={safeCharge} />
    </div>
  );
}
Example SafeCharge.jsx
import React, { memo, useState } from "react";

export const SafeChargeCC = memo(({ safeCharge }) => {
  const [cardNumber, setCardNumber] = useState("");
  const [sessionToken, setSessionToken] = useState("");
  const [expDateMM, setExpDateMM] = useState("11");
  const [expDateYY, setExpDateYY] = useState("22");
  const [cardCVV, setCardCVV] = useState("");
  const [cardHN, setCardHN] = useState("CL-BRW1");

  const createPayment = (e) => {
    try {
      safeCharge.createPayment(
        {
          sessionToken: sessionToken,
          paymentOption: {
            card: {
              cardNumber: cardNumber,
              cardHolderName: cardHN,
              expirationMonth: expDateMM,
              expirationYear: expDateYY,
              CVV: cardCVV
            }
          },
          billingAddress: {
            email: "someone@somedomain.com",
            country: "GB"
          }
        },
        (resp) => console.log(resp)
      );
    } catch (e) {
      console.error(e);
    }
  };

  const authenticate3d = (e) => {
    try {
      safeCharge.authenticate3d(
        {
          sessionToken: sessionToken,
          paymentOption: {
            card: {
              cardNumber: cardNumber,
              cardHolderName: cardHN,
              expirationMonth: expDateMM,
              expirationYear: expDateYY,
              CVV: cardCVV
            }
          },
          billingAddress: {
            email: "someone@somedomain.com",
            country: "GB"
          }
        },
        (resp) => console.log(resp)
      );
    } catch (e) {
      console.error(e);
    }
  };

  return (
    <>
      <div className="container">
        <div className="item">
          <label htmlFor="sessionField">Session Token</label>
          <input
            id="sessionField"
            placeholder="Session Token"
            type="text"
            value={sessionToken}
            onChange={(e) => setSessionToken(e.target.value)}
          />
        </div>

        <div className="item">
          <label htmlFor="cardHolderName">Cardholder Name</label>
          <input
            id="cardHolderName"
            placeholder="Cardholder Name"
            type="text"
            value={cardHN}
            onChange={(e) => setCardHN(e.target.value)}
          />
        </div>

        <div className="item">
          <label htmlFor="card-number">Card number</label>
          <input
            id="card-number"
            placeholder="Card number"
            type="text"
            value={cardNumber}
            onChange={(e) => setCardNumber(e.target.value)}
          />
        </div>
        <div className="item">
          <label htmlFor="exp-dateMM">Expiration Date</label>
          <input
            className="exp-date"
            id="exp-dateMM"
            placeholder="MM"
            type="text"
            value={expDateMM}
            onChange={(e) => setExpDateMM(e.target.value)}
          />
          <input
            className="exp-date"
            id="exp-dateYY"
            placeholder="YY"
            type="text"
            value={expDateYY}
            onChange={(e) => setExpDateYY(e.target.value)}
          />
        </div>
        <div className="item">
          <label htmlFor="card-cvc">CVC</label>
          <input
            id="card-cvc"
            placeholder="CVC"
            type="text"
            value={cardCVV}
            onChange={(e) => setCardCVV(e.target.value)}
          />
        </div>
        <div className="item100 buttons">
          <button className="pay-button" onClick={createPayment}>
            Create Payment
          </button>

          <button className="pay-button" onClick={authenticate3d}>
            Authenticate3d
          </button>
        </div>
      </div>
    </>
  );
});
2022 Nuvei. All rights reserved.