NMI for WooCommerce Refund Reports Success After NMI Decline or Error

A payment gateway refund can fail at NMI while still appearing successful inside WooCommerce if the gateway plugin treats a successful HTTP request as a successful transaction.

Those are not the same thing.

NMI’s Payment API uses a response value in the returned transaction data to indicate the actual result:

response=1  Transaction Approved
response=2  Transaction Declined
response=3  Error in transaction data or system error

An NMI refund can therefore reach the gateway successfully at the HTTP level while NMI returns response=2 or response=3.

WooCommerce expects a payment gateway’s process_refund() method to return success only when the remote refund actually succeeded. If a plugin returns true after a declined NMI response, WooCommerce has no reason to know that the processor rejected the transaction.

The result can be serious: WooCommerce records a refund that did not actually happen at NMI.

Until the gateway correctly distinguishes NMI approvals from declines and errors, automatic refunds through WooCommerce should not be relied on.

Why HTTP 200 Is Not Proof That an NMI Refund Succeeded

Payment API requests have two separate outcomes:

  1. Did the web request reach NMI and receive a response?
  2. Did NMI approve the financial transaction?

An HTTP 200 response generally answers the first question.

The NMI transaction response answers the second.

For refunds, NMI documents a successful result in this general form:

response=1
responsetext=Approved
transactionid=...
type=refund
response_code=100

NMI uses the same transaction-response model for refund operations as for other Payment API operations.

A declined response may instead contain:

response=2
responsetext=...
response_code=...

and a transaction or system error can return:

response=3
responsetext=...

A gateway integration must inspect those values before telling WooCommerce that the refund succeeded.

What WooCommerce Expects From process_refund()

WooCommerce’s payment gateway interface defines:

process_refund( $order_id, $amount = null, $reason = '' )

with this return contract:

bool|WP_Error

The gateway should return:

  • true when the refund succeeds;
  • false for a generic failure; or
  • a WP_Error when it can provide a meaningful failure message.

WooCommerce documents this contract directly in WC_Payment_Gateway.

WooCommerce’s own refund handling then calls the gateway:

$result = $gateway->process_refund(
    $order->get_id(),
    $amount,
    $reason
);

If the result evaluates to false, WooCommerce treats the operation as failed.

If it receives a WP_Error, WooCommerce extracts the error message.

If the gateway returns true, WooCommerce treats the payment-side refund as successful.

That means a gateway must never return true simply because NMI returned an HTTP response.

It must verify the NMI transaction result first.

The Correct NMI Refund Logic

At a minimum, a refund implementation should distinguish three cases.

NMI approves the refund

When:

response=1

the gateway can return:

return true;

NMI declines the refund

When:

response=2

the gateway should return an error rather than success.

For example:

return new WP_Error(
    'nmi_refund_declined',
    $response_text
);

NMI returns a transaction or system error

When:

response=3

the gateway should also return an error:

return new WP_Error(
    'nmi_refund_error',
    $response_text
);

Transport errors—timeouts, DNS failures, TLS failures, or other failures where WordPress cannot obtain a usable NMI response—should also be treated as refund failures.

Example of Safe Response Handling

The following demonstrates the logic a WooCommerce NMI gateway needs.

It is an implementation pattern, not a drop-in replacement for a specific plugin method, because the actual variable names and API wrapper depend on how the gateway is structured.

$response = wp_remote_post( $endpoint, $request_args );

if ( is_wp_error( $response ) ) {
    return new WP_Error(
        'nmi_refund_transport_error',
        $response->get_error_message()
    );
}

$body = wp_remote_retrieve_body( $response );

parse_str( $body, $result );

$nmi_response = isset( $result['response'] )
    ? (string) $result['response']
    : '';

$response_text = isset( $result['responsetext'] )
    ? sanitize_text_field( $result['responsetext'] )
    : 'NMI did not return a refund result.';

if ( '1' !== $nmi_response ) {
    return new WP_Error(
        'nmi_refund_failed',
        $response_text
    );
}

return true;

The critical condition is:

if ( '1' !== $nmi_response )

Only NMI’s approved status should reach:

return true;

A valid HTTP response containing response=2 or response=3 is still a failed refund.

Preserve NMI’s Error Message Where Safe

Returning a WP_Error has another advantage: WooCommerce can display the gateway failure to the administrator instead of silently behaving as though the refund worked.

For example, an implementation can use NMI’s sanitized:

responsetext

as the administrator-facing error message.

That produces a much more useful workflow than returning only:

false

because the store administrator can see why NMI rejected the transaction.

Sensitive values such as API credentials, card data, or complete raw request payloads should never be exposed in the error message.

NMI response and response_code Are Different

Do not confuse:

response

with:

response_code

NMI documents response as the high-level transaction status:

ValueMeaning
1Approved
2Declined
3Error

response_code provides more detailed information about the result.

For example:

response=1
response_code=100

represents an approved transaction.

Other response codes can identify specific decline conditions such as insufficient funds, invalid payment information, an expired card, or an invalid security code.

For determining whether process_refund() should return success, the important rule is straightforward:

Do not report the refund as successful unless NMI confirms that the transaction was approved.

Why This Can Create Accounting Problems

A false-positive refund is more dangerous than an ordinary error message.

Imagine a $150 order.

WooCommerce sends a $150 refund request.

NMI responds:

response=2
responsetext=Refund declined

but the plugin returns:

true

WooCommerce now believes the payment gateway refunded the customer.

The store can consequently have two different realities:

WooCommerce
Refunded: $150

NMI
Refunded: $0

That can affect:

  • customer support;
  • accounting;
  • order reconciliation;
  • inventory decisions;
  • refund emails;
  • financial reporting;
  • customer expectations.

For full refunds, the WooCommerce order may also end up appearing completely refunded even though no money was returned through NMI.

Safe Workaround Until the Gateway Is Fixed

If the installed NMI gateway is known to report failed refunds as successful, stop using its automatic Refund via gateway action.

Instead, use a two-stage manual workflow.

1. Process the refund in NMI

Open the NMI merchant portal and refund the original transaction there.

Confirm that NMI actually reports the refund as approved.

Record the NMI refund transaction ID or other reconciliation information according to your normal accounting process.

2. Record the refund manually in WooCommerce

After confirming the NMI refund succeeded:

  1. Go to WooCommerce → Orders.
  2. Open the affected order.
  3. Select Refund.
  4. Enter the products, quantities, and amount being refunded.
  5. Add a useful refund note.
  6. Choose Refund manually.

WooCommerce’s documentation explicitly distinguishes these actions.

An automatic gateway refund sends the refund through the payment gateway, whereas a manual refund records the refund in WooCommerce without attempting to send money through the payment gateway.

That distinction is exactly what is needed here.

The money has already been returned through NMI, so WooCommerce should only record what happened.

Do Not Use “Refund via NMI” After Refunding in the Portal

Once a refund has successfully been processed in the NMI portal, do not then click the automatic gateway refund button in WooCommerce merely to synchronize the order.

That can attempt a second processor-side refund.

Instead use:

Refund manually

to update WooCommerce’s records.

WooCommerce itself warns that manual refunds do not return money through the gateway—they only record the refund locally.

In this situation, that behavior is desirable because the actual financial refund has already been completed at NMI.

What If WooCommerce Already Recorded a Refund That NMI Declined?

First verify the transaction directly in NMI.

Do not assume that the presence of a WooCommerce refund record proves that funds were returned.

Compare:

  • the original NMI transaction;
  • the attempted refund;
  • the refund amount;
  • NMI’s transaction status;
  • any NMI refund transaction ID;
  • WooCommerce order notes.

If NMI confirms that no refund occurred, the financial refund still needs to be processed.

Be particularly careful before trying another automatic WooCommerce refund because the local order may already contain a refund record and its refundable balance may no longer match the processor.

For an affected production order, the safest reconciliation process is generally:

  1. verify the failed transaction at NMI;
  2. process the required refund at NMI;
  3. record the processor transaction ID;
  4. review the existing WooCommerce refund record before creating any additional local refund;
  5. add an administrative order note documenting the discrepancy.

Do not delete refund records or directly modify WooCommerce database rows simply to make the totals look correct.

If the local order needs correction beyond adding an order note, make a backup and determine exactly what WooCommerce has already recorded before changing it.

Check the NMI for WooCommerce Version

At the time of this research, WordPress.org lists NMI for WooCommerce 1.0.7 as the current release.

Its 1.0.7 changelog describes two Checkout Block fixes:

  • card fields remaining hidden because the classic checkout script also loaded;
  • Expiration/CVV layout overflowing the payment panel.

The published 1.0.7 changelog does not list a process_refund() response-handling fix.

Version 1.0.6 did include several important Checkout Block transaction fixes, including correct failure handling for declined card payments, but that relates to process_payment() rather than proving that refund handling is correct.

Do not assume the payment-decline fix automatically applies to refunds.

process_payment() and process_refund() are separate gateway methods with different WooCommerce contracts.

What a Permanent Plugin Fix Should Do

A robust process_refund() implementation should:

  1. validate the WooCommerce order and original transaction ID;
  2. validate the refund amount;
  3. send the refund request to NMI;
  4. detect WordPress HTTP/transport errors;
  5. safely parse NMI’s response;
  6. confirm that NMI returned response=1;
  7. return a WP_Error for response=2;
  8. return a WP_Error for response=3;
  9. include a sanitized NMI response message where useful;
  10. return true only after confirmed approval;
  11. avoid writing sensitive credentials or payment information to logs.

It should also handle an empty, malformed, or unexpected response as a failure rather than assuming success.

For example:

if ( empty( $result['response'] ) ) {
    return new WP_Error(
        'nmi_invalid_refund_response',
        'NMI returned an invalid refund response.'
    );
}

A payment integration should fail closed: an ambiguous processor response must not be converted into a successful financial transaction.

How to Test the Fix Safely

A corrected version should be tested on staging or an appropriate NMI test environment before production use.

Test at least these scenarios.

Approved refund

NMI returns:

response=1

Expected:

  • process_refund() returns true;
  • WooCommerce creates the refund;
  • the NMI portal shows the refund;
  • amounts match.

Declined refund

NMI returns:

response=2

Expected:

  • process_refund() returns WP_Error;
  • WooCommerce displays the failure;
  • WooCommerce does not treat the remote refund as successful;
  • the error contains a useful sanitized NMI message.

NMI transaction error

NMI returns:

response=3

Expected:

  • process_refund() returns WP_Error;
  • WooCommerce does not record a successful gateway refund.

Network or transport failure

Simulate or safely reproduce an unreachable API endpoint or equivalent failure in a test environment.

Expected:

  • the HTTP error is converted to WP_Error;
  • WooCommerce does not record a successful remote refund.

Partial refund

Test an amount smaller than the order total.

Verify both WooCommerce and NMI show exactly the same amount.

Full refund

Test the complete refundable balance.

Again verify the transaction independently at NMI rather than relying solely on the WooCommerce status.

Do Not Patch WooCommerce Core

WooCommerce is behaving according to its gateway API contract when it trusts a successful return value from process_refund().

The permanent correction belongs in the NMI gateway’s refund implementation.

Do not modify:

wp-content/plugins/woocommerce/

to compensate for a gateway returning an incorrect result.

Likewise, modifying the NMI plugin directly should only be considered a temporary controlled patch because a future plugin update can overwrite that change.

If a developer needs an emergency patch, keep a documented copy, test it on staging, and remove it once the plugin publisher releases an official correction.

The safest production workaround remains processing refunds directly at NMI and then using Refund manually in WooCommerce to reconcile the order.

About the author

Tahrim Naziat

WordPress and Server Troubleshooting Specialist

Tahrim Naziat is a senior WordPress and JavaScript developer with more than 14 years of experience specializing in WordPress troubleshooting, WooCommerce, PHP compatibility, plugin conflicts, malware cleanup, performance optimization, Nginx, Redis, and production server issues. He documents practical solutions based on real WordPress debugging, technical investigations, and client projects.

Leave a Comment