Founder deal: 75% off the lifetime licence. Unlimited sites, one payment, every update.See the founder plan
Home / Blog / Developer resources

NMI Collect.js, Hosted Fields and Tokenization in WooCommerce

How NMI Collect.js turns card entry into a single-use token, how to style the hosted fields, and what to check when they fail.

NMI Collect.js is a JavaScript library that puts the card fields on your checkout inside NMI-hosted iframes, so the card number goes straight to NMI and your WordPress server only receives a single-use payment_token. You send that token to the Payment API instead of the card number, expiry and CVV. This guide covers how the flow works, how to style the fields, and what to check when it misbehaves.

How the Collect.js flow works

The sequence is the same on every WooCommerce store, whatever plugin sits on top:

  1. The checkout page loads https://secure.nmi.com/token/Collect.js with a public tokenization key.
  2. The shopper types into fields that are really NMI iframes (inline variant) or into a popup (lightbox variant).
  3. On submit, Collect.js asks NMI to store the data and returns a token to a callback.
  4. A hidden payment_token field is added to the form and the form posts to your server.
  5. Your server sends the token to the gateway as part of a sale, auth or validate request.

The token is single use and expires after 24 hours if it is never spent. Because the raw card data never touches your server, your PCI scope is usually the smaller questionnaire. See what tokenisation really is and SAQ A vs SAQ A-EP, and confirm your exact scope with your acquirer. NMI Collect.js and hosted fields are in the free plugin; saved cards on top of it are covered in the Customer Vault guide and need Pro.

Inline vs lightbox

Lightbox is the Collect.js default: a button with the ID payButton opens a popup. Inline renders empty containers with the IDs ccnumber, ccexp and cvv (and checkaccount, checkaba, checkname for bank accounts) and NMI injects the iframes into them. Inline looks native in a checkout, which is why most WooCommerce plugins use it. A custom checkout still needs a submit element matching the payment selector, which defaults to #payButton.

Styling the hosted fields

You cannot style an iframe from your own stylesheet, so Collect.js gives you configuration instead:

  • Style sniffer: on by default, it copies basic styling from the page inputs into the iframes.
  • CSS options: custom-css, invalid-css, valid-css, focus-css and placeholder-css take CSS property lists for each state.
  • Fonts: data-google-font loads a Google font inside the iframes.

Keep the styles small: font family, size, colour, padding and a visible focus and invalid state. If the fields look wrong after a theme change, compare what the style sniffer picked up with the real input styles first, and check how your block or classic checkout renders its inputs. Plugin-specific settings for styling are listed in the docs, so check the hooks and filters page before writing custom JavaScript.

Two keys, two jobs

Collect.js uses a public key with the Tokenization permission. Your server-side API calls use a private security key. They are not interchangeable: an API key does not work with Collect.js, and a tokenization key does not work with the Payment API. The public key is visible in page source by design. Both are created in the NMI Merchant Portal under Settings, Security Keys.

Content Security Policy and other blockers

If your site sends a strict CSP header, NMI documents these minimum sources: script-src https://secure.nmi.com, https://applepay.cdn-apple.com and https://pay.google.com; style-src https://secure.nmi.com; connect-src and frame-src https://secure.nmi.com and https://pay.google.com. With a strict CSP, set blockEval to true and use a named function for callbacks.

Other blockers (script combine or delay plugins, ad blockers) are general troubleshooting suspects rather than something NMI documents, so test with them off.

A quick test routine

  1. Use sandbox keys. NMI documents the token 00000000-000000-000000-000000000000 as returning a test Visa; see testing and sandbox.
  2. Place an order and confirm the response comes back with a response code of 100.
  3. Try an invalid card and read the result using response, responsetext and response_code.
  4. Set data-timeout-duration if you want a hard failure instead of a hung form; the default of 0 disables it.

If NMI declines a card you know is good, the cause is rarely the tokenization step; start with where to look first. More developer material is on the developer resources hub.

Last reviewed November 2026.

NMI for WooCommerce

Hosted fields, saved cards on top

Collect.js keeps card data off your server; Pro adds Customer Vault saved cards.

See saved cardsSee pricing

Free on WordPress.org. Pro adds saved cards.