The NMI Payment API is a single HTTPS endpoint, https://secure.nmi.com/api/transact.php, that accepts a form-encoded POST and returns a query string. In WordPress you call it with wp_remote_post(), parse the reply with wp_parse_str(), and branch on the response field. This guide gives the request shape, the transaction types, and the mistakes that cost developers the most time.
The request and response shape
Send security_key, a type, an amount and either a payment_token from Collect.js or card fields. In PHP the call looks like this:
- Build an array:
security_key,type(sale),amount(10.00),payment_token,orderid. - Call
wp_remote_post( $url, array( 'body' => $fields, 'timeout' => 45 ) ). - Check
is_wp_error()first. A WP_Error means the request never got an answer, which is a different problem from a decline; see NMI gateway connection errors. - Run
wp_parse_str( wp_remote_retrieve_body( $r ), $out ). - Read
$out['response']: 1 approved, 2 declined, 3 error in the data or the system.
Always keep transactionid, authcode, response_code and responsetext from the reply. The numeric response_code is the field to branch on in code; responsetext is free-form and varies. The details are in reading the transaction response.
Pick the right transaction type
sale: submitted and flagged for settlement immediately.auth: authorized only; you mustcapturelater. A capture can be for an equal or smaller amount than the authorization.void: cancels before settlement, no money moves.refund: reverses a settled or pending-settlement transaction; partial amounts are allowed.validate: checks the account without authorizing; use it when you only want to store a card.
Use the gateway transactionid to reference earlier transactions. Pass your WooCommerce order number as orderid so you can search later. For capture and refund behavior inside WooCommerce, read refunds and voids with NMI.
Handle errors by layer
- Transport: WP_Error, timeout, TLS failure, HTTP 429. NMI documents a rate limit that returns
response=3withresponse_code=301; do not retry immediately. - Gateway rejection:
response_code300 with a text that names the problem (a missing field, permissions, credentials). Fix the request; retrying will not help. - Issuer decline: 2xx codes. Show the customer a safe message and let them try another card.
- Merchant setup: 4xx codes such as 410 or 411. These need your processor or reseller, not code.
The only codes in NMI's published table are the ones in the response code lookup; do not invent handling for others. Use the NMI response code lookup as your reference.
Rules that prevent expensive bugs
- Never log the full request. Strip
security_key, card numbers and CVV before anything reacheserror_log()or a debug file. - Never retry a sale blindly after a timeout. The first attempt may have gone through. Query by
orderidbefore resending, or you create duplicates. - Store the transaction ID on the order in order meta through the WooCommerce CRUD methods, not direct post meta, so HPOS stays happy.
- Use
wp_remote_post, not cURL. It respects WordPress proxy and SSL settings. - Keep sandbox and live keys separate and make the active mode visible in the admin; the go-live checklist covers the switch.
Where the plugin fits
If you use NMI for WooCommerce you rarely need to call the API yourself; the plugin sends the request and records the gateway response on the order. Extension points are documented on the hooks and filters page, so prefer a hook over a parallel API call. Note that NMI's docs now steer new builds toward a REST API, while the classic transact.php API stays documented for existing integrations. Check the current NMI docs before starting a standalone integration, and keep the rest of the developer resources bookmarked.
A worked example: a sale that times out
Your code posts a sale and wp_remote_post() returns a WP_Error after the timeout. The customer sees a spinner, then an error, and clicks again. Was the first attempt charged? You do not know yet.
The safe sequence: mark the order as awaiting payment, look the transaction up by orderid through NMI’s Query API (or in the merchant portal) before doing anything else, and only if nothing exists send a new request. If the first one went through, store its transactionid and complete the order. The customer sees one charge, and you see one order with one transaction ID.
A pre-launch checklist for a custom integration
- Test an approval, a decline and a gateway rejection in sandbox and check that each reaches a different branch in your code.
- Force a timeout and confirm you do not create a duplicate charge.
- Confirm the logs contain no security key, card number or CVV.
- Check that the amount you send is formatted with two decimals and matches the order total.
- Store transactionid, authcode and response_code on the order so refunds and support can find them later.
- Ask your processor whether duplicate checking is active on your account. Support for it depends on the processor, so do not rely on it as your only protection.
Only after these pass should you switch from sandbox credentials to live ones, and keep one small live transaction as a final check.
Last reviewed November 2026.