Tokenize a Bank Account on your Website

Tokenize a Bank Account on your Website

Use Stax.js to tokenize a bank account (securely store its details and receive a payment method ID for future use) on your website without charging it.

Supply bank account and routing information instead of a card expiration date or CVV (the card's security code). Stax.js sends sensitive payment information directly from your user's browser to Stax's servers. This supports your compliance efforts; it does not guarantee compliance. Consult your legal and compliance teams about the requirements for your integration.

Setup and Initialization

Add Stax.js to your site

  1. Add the script shown below to the <head> of your HTML file.

    <script src="https://staxjs.staxpayments.com/staxjs-captcha.js"></script>
  2. Add a button to your page's <body>:

    <button id="tokenizebutton" type="button">Tokenize bank account</button>
  3. Run the JavaScript examples on the same page, after the button's HTML: initialize Stax.js, define extraDetails, then register the tokenization click handler.

Initialize Stax.js

  1. In Stax Pay > Apps > API Keys, copy your Web Payments Token (public key). Never use your private API key in browser code.
  2. Initialize Stax.js using the code below. Enter your actual public token when prompted rather than using an invented credential.
// Copy your public token from Stax Pay > Apps > API Keys.
const webPaymentsToken = window.prompt("Enter your Web Payments Token (public key):");
if (!webPaymentsToken || !webPaymentsToken.trim()) {
  throw new Error("A Web Payments Token is required to initialize Stax.js.");
}
var staxjs = new StaxJs(webPaymentsToken.trim(), {});

If you cancel the prompt or leave it empty, initialization stops with the error above. Reload the page and enter your public token to continue.

Bank Account Data Configuration

Insert the Data

Define extraDetails, the object you pass to staxjs.tokenize() with the customer's name, billing details, and bank account information. Start with validate: true to check the submitted details, and leave customer matching enabled by omitting match_customer. Customer matching links the payment method to an existing customer when their details match.

These are recommended integration settings, not a statement of compliance requirements.

Properties of extraDetails (When tokenizing via Bank)

Field NameDescriptionRequired
customer_idIt must be a string matching a valid customer_id in your merchant account. If supplied, we will not create or match a new customer based on values. Instead, the supplied ID will be assigned to this new payment method and transaction. If a customer_id is supplied, certain customer fields, such as firstname, lastname, phone, all address fields, etc., will be ignored.No
firstnamemax of 50 charactersYes
lastnamemax of 50 charactersYes
phonemust be at least ten charactersNo
emailmust be a valid email. Receipts will be sent here automatically if the send_receipt flag is set to true.No
adddress_1Max 255 characters. This field is required for AVS if the account is configured to perform an AVS check on the street address. Required if customer_id is not passed into detailsNo*
address_2Max of 255 charactersNo
address_cityRequired if customer_id is not passed into details, max of 255 charactersNo*
address_stateRequired if customer_id is not passed into details, max of 2 characters (e.g. FL)No*
address_countryCharacter country code (e.g. USA)No
address_zipNot required unless AVS or surcharging is enabled.No*
companyStringNo
method“card” or “bank”Yes
monthA string representing the 2-digit month when the card expires(“05”)Yes*
yearA string representing the 4-digit year when the card expires (“2020”)Yes*
match_customerBoolean. This determines whether Stax.js matches the customer to an existing customer based on some combination of the same data sent in the JS request. See the Best Practices: Customer matching section for more details.No
validateDetermines whether or not Stax.js does client-side validation.No

Bank-Specific Required Fields

The following are specific to accepting a bank tokenization:

ParameterTypeRequiredDefaultConstraints
bank_typestringYesNone; supply a value"checking" or "savings"
bank_holder_typestringYesNone; supply a value"personal" or "business"
bank_accountstringYesNone; supply a valueThe customer's bank account number
bank_routingstringYesNone; supply a valueThe bank routing number, exactly 9 digits

Example Configuration

Replace the sample bank and customer details with the values collected by your form before submitting. These values illustrate the request structure; they are not verified test credentials.

const extraDetails = {
  firstname: "John",
  lastname: "Doe",
  person_name: "John Doe",
  method: "bank",
  bank_type: "savings",
  bank_name: "Bank INC",
  bank_account: "9876543210",
  bank_routing: "021000021",
  bank_holder_type: "personal",
  phone: "5555555555",
  address_1: "100 S Orange Ave",
  address_2: "Suite 400",
  address_city: "Orlando",
  address_state: "FL",
  address_zip: "32811",
  address_country: "USA",
  // Validate submitted details instead of disabling client-side checks.
  validate: true
};
Form Validation and Event Handling

Handling Form Completion

The following events apply to Stax.js hosted card fields, not the bank details supplied in extraDetails. Skip these handlers for the bank-only example on this page; use validate: true and handle errors from tokenize() instead. If your page also includes hosted card fields, register these handlers on the same staxjs instance that hosts them.

Handle the card_form_uncomplete event when input fields are invalid:

staxjs.on("card_form_uncomplete", (message) => {
  console.log(message);
  // Prevent submission while hosted card fields are incomplete or invalid.
  var tokenizeButton = document.querySelector("#tokenizebutton");
  if (tokenizeButton) tokenizeButton.disabled = true;
});
Tokenization Process

Call the tokenize() method with extraDetails

Register this handler after initializing staxjs and defining extraDetails, then click Tokenize bank account. Disable the button while the request is pending to prevent duplicate submissions.

const bankTokenizeButton = document.querySelector("#tokenizebutton");
if (!bankTokenizeButton) {
  throw new Error("Add the tokenizebutton button before running this script.");
}

bankTokenizeButton.onclick = async () => {
  bankTokenizeButton.disabled = true;
  try {
    const response = await staxjs.tokenize(extraDetails);
    if (!response || !response.id) {
      console.error("No payment method ID returned. Do not treat this as success.", response);
      return;
    }
    console.log("payment method object:", response);
    console.log("payment method ID:", response.id);
    if (response.customer) {
      console.log("customer object:", response.customer);
    } else {
      console.log("No customer object returned; customer ID:", response.customer_id);
    }
  } catch (err) {
    console.error("unsuccessful tokenization:", err);
  } finally {
    bankTokenizeButton.disabled = false;
  }
};

On success, your browser console shows payment method object: and the returned payment method ID. If you see unsuccessful tokenization:, review the returned error, confirm your public token, and correct the submitted bank and customer details before retrying. Do not retry automatically. If no payment method ID is returned, do not continue as though the account was stored successfully.

Response

A successful response contains the bank payment method. The top-level id is the payment method ID; customer_id identifies the associated customer. These sample IDs illustrate the response format, not values to copy into requests. Use the IDs returned by your own tokenize() call.

{
  "address_1": "100 S Orange Ave",
  "address_2": null,
  "address_city": "Orlando",
  "address_country": "USA",
  "address_state": "FL",
  "address_zip": "32811",
  "bank_holder_type": "personal",
  "bank_name": "Bank INC",
  "bank_type": "savings",
  "card_exp": null,
  "card_exp_datetime": null,
  "card_last_four": "3210",
  "card_type": null,
  "created_at": "2020-07-06 16:22:16",
  "customer": {
    "address_1": "100 S Orange Ave",
    "address_2": "",
    "address_city": "Orlando",
    "address_country": "USA",
    "address_state": "FL",
    "address_zip": "32811",
    "allow_invoice_credit_card_payments": true,
    "cc_emails": null,
    "cc_sms": null,
    "company": "",
    "created_at": "2020-07-06 16:22:16",
    "deleted_at": null,
    "email": "",
    "firstname": "Jon",
    "gravatar": false,
    "id": "cd10fbd4-199c-4753-8db8-66c4e2c2b4e8",
    "lastname": "Doe",
    "notes": null,
    "options": null,
    "phone": "111111111111111",
    "reference": "",
    "updated_at": "2020-07-06 16:22:16"
  },
  "customer_id": "cd10fbd4-199c-4753-8db8-66c4e2c2b4e8",
  "has_cvv": false,
  "id": "2e34394f-5f1d-4ec0-a56d-f669509a5b14",
  "is_default": 0,
  "is_usable_in_vt": true,
  "method": "bank",
  "nickname": "personal savings, Bank INC (ending in: 3210)",
  "person_name": "Jon Doe",
  "updated_at": "2020-07-06 16:22:18"
}

Your bank account is stored when the successful response includes a payment method id. Tokenization does not charge the account.


Did this page help you?