Customer Matching

As a developer, use Stax.js customer matching to associate a payment method with an existing customer when you call .tokenize() or .pay().

Recommended starting point: Keep customer matching enabled by omitting match_customer. Pass an existing customer_id only when your application has already identified the customer. Disable matching only when you intentionally want to bypass matching by customer details.

The API looks for exact matches within these groups of customer details (such as names, email addresses, and postal addresses). The names below are API field names, not placeholder values:

  • customer_id
  • email
  • company, address_1, address_city
  • firstname, phone
  • firstname, lastname, address_1, address_zip
  • lastname, phone
📘

A group matches only when every field in that group is present and matches exactly.


Examples

EX1:

Bob Smith at 123 North Way 32801 would not match Bobby Smith at 123 Noth Way 32801.

EX2:

If a match is found based on the provided demographics, the existing customer will be linked to the new payment method and transaction, and any additional demographics will not be stored.

For example, if a match is found for [email protected], but you also passed data like Address Line 1, and the existing Bob Smith didn’t have an address, then the new address will not be saved.

EX3:

If the matched customer has an email, that email address will receive the receipt.

Try customer matching with Stax.js

This browser example tokenizes a card (stores it and returns a payment method ID without charging it). It keeps matching enabled unless you select Disable matching.

  1. In Stax Pay, open Apps > API Keys and copy your Web Payments Token (public key). Never enter a private API key in browser code.
  2. Add the script from the following reusable block to your HTML page's <head>. Copy the rendered script, not the documentation tag.
<script src="https://staxjs.staxpayments.com/staxjs-captcha.js"></script>
  1. Add the following form and script to the page's <body>, then open the page and enter your public token when prompted.
  2. Enter card details, set the matching test card's expiration month and year, and select Tokenize. Use a test card in your test environment.
<form id="matching-form">
  <div id="card-number" style="width: 240px; height: 30px;"></div>
  <div id="card-cvv" style="width: 80px; height: 30px;"></div>
  <label>Expiration month <input id="month" value="10" pattern="0[1-9]|1[0-2]" required /></label>
  <label>Expiration year <input id="year" value="2030" pattern="[0-9]{4}" required /></label>
  <label><input id="disable-matching" type="checkbox" /> Disable matching</label>
  <button id="tokenize" type="submit" disabled>Tokenize</button>
</form>
<pre id="result" role="status">Loading card fields…</pre>

<script>
  const form = document.querySelector("#matching-form");
  const button = document.querySelector("#tokenize");
  const result = document.querySelector("#result");
  let cardComplete = false;
  let submitting = false;

  function updateButton() {
    button.disabled = !cardComplete || submitting;
  }

  async function initialize() {
    // Use only the public token from Stax Pay > Apps > API Keys.
    const token = window.prompt("Enter your Web Payments Token (public key):");
    if (!token || !token.trim()) {
      result.textContent = "No public token provided. Reload the page to try again.";
      return;
    }

    try {
      const staxjs = new StaxJs(token.trim(), {
        number: {
          id: "card-number",
          placeholder: "0000 0000 0000 0000",
          style: "height: 30px; width: 100%;",
          type: "text",
          format: "prettyFormat"
        },
        cvv: {
          id: "card-cvv",
          placeholder: "CVV",
          style: "height: 30px; width: 100%;",
          type: "text"
        }
      });

      staxjs.on("card_form_complete", () => {
        cardComplete = true;
        updateButton();
      });
      staxjs.on("card_form_uncomplete", () => {
        cardComplete = false;
        updateButton();
      });

      await staxjs.showCardForm();
      result.textContent = "Enter card details to enable Tokenize.";

      form.addEventListener("submit", async (event) => {
        event.preventDefault();
        if (!cardComplete || submitting) return;
        submitting = true;
        updateButton();
        result.textContent = "Tokenizing…";

        const extraDetails = {
          firstname: "John",
          lastname: "Doe",
          method: "card",
          month: document.querySelector("#month").value,
          year: document.querySelector("#year").value,
          phone: "5555555555",
          address_1: "100 S Orange Ave",
          address_city: "Orlando",
          address_state: "FL",
          address_zip: "32811",
          address_country: "USA"
        };

        // Omit this option for the recommended matching behavior.
        if (document.querySelector("#disable-matching").checked) {
          extraDetails.match_customer = false;
        }

        try {
          const response = await staxjs.tokenize(extraDetails);
          if (!response || !response.id || !response.customer_id) {
            result.textContent = "Response missing a payment method or customer ID. Inspect the response before continuing.";
            console.warn("Unexpected tokenization response:", response);
            return;
          }
          result.textContent = JSON.stringify({
            id: response.id,
            customer_id: response.customer_id
          }, null, 2);
        } catch (error) {
          result.textContent = "Tokenization failed. Review the error in the console and correct the details before retrying.";
          console.error("Tokenization failed:", error);
        } finally {
          submitting = false;
          updateButton();
        }
      });
    } catch (error) {
      button.disabled = true;
      result.textContent = "Card fields could not load. Check the Stax.js script and public token, then reload.";
      console.error("Stax.js initialization failed:", error);
    }
  }

  initialize();
</script>

On success, the page displays the payment method ID and customer ID. The following output illustrates the response shape; your IDs will differ:

{
  "id": "2e34394f-5f1d-4ec0-a56d-f669509a5b13",
  "customer_id": "cd10fbd4-199c-4753-8db8-66c4e2c2b4e8"
}

Save the actual returned IDs in your application. These sample IDs illustrate the format; do not reuse them in requests.

Optional: Disable customer matching

Select Disable matching in the example to pass match_customer as the boolean false, not the string "false", in extraDetails. Both .tokenize() and .pay() accept this option. Leave the checkbox cleared for the recommended starting behavior.

Optional: Use an existing customer ID

  1. Create a customer with POST /customer, or use a customer your application has already identified.
  2. Read the id string from the create-customer response, or the customer_id string from a previous .tokenize() response. Use a real customer ID from your merchant account, not the sample ID above. This workflow obtains the ID from the API response; it does not require a Dashboard lookup.
  3. Pass that string as the customer_id property in extraDetails when calling .tokenize() or .pay().

When you supply a valid customer_id, Stax.js uses that customer instead of creating or matching another customer by their details. Certain customer fields, including names, phone, and address fields, are ignored when you supply the ID. This lets you implement your own matching logic before calling Stax.js.

After .tokenize() succeeds, confirm that the returned customer_id equals the ID you supplied. The payment method now belongs to that customer. With .pay(), the invoice and payment also use that customer.


Did this page help you?