How to Send an Invoice Number Through to Authorize.net Payments

Introduction

Would you like to include the invoice number from your WPForms to any Authorize.net payment? Authorize.net will accept certain metadata they already have mapped to their payment API. In this tutorial, we’ll show you how to send through a payment invoice number inside the Authorize.net payments.

Creating your Authorize.net form

In order to set up your form to accept Authorize.net payments, please review this documentation.

create the form first to send the invoice through to Authorize.net

Adding the snippet to send an invoice number

Once your form is set up to accept these payments, you can now copy and paste this code snippet to your site.

In our example below, we added a Hidden Field (fields[20]) form field that contains the Smart Tag {unique_value} to generate an invoice number when the form submits.

/*
 * Include an invoice number into the set of Authorize.Net args.
 *
 * @link  https://wpforms.com/developers/how-to-send-an-invoice-number-through-to-authorize-net-payments
 */

function wpf_dev_authorize_net_process_payment_single_add_invoice_to_args( $args, $process ) {
	
    // Replace 20 in $process->fields[20] to an id of your invoice field.
    if ( isset( $process->fields[20][ 'value' ] ) ) {
		
        $args[ 'invoice' ] = $process->fields[20][ 'value' ];
		
    }
	
    return $args;
}

add_filter( 'wpforms_authorize_net_process_payment_single_args', 'wpf_dev_authorize_net_process_payment_single_add_invoice_to_args', 10, 2 );

/**
 * Set the transaction order invoice number.
 *
 * @link  https://wpforms.com/developers/how-to-send-an-invoice-number-through-to-authorize-net-payments
 */
 
function wpf_dev_authorize_net_process_transaction_add_invoice_to_transaction( $transaction, $args ) {
    if ( ! isset( $args[ 'invoice' ] ) ) {
        return $transaction;
    }
 
    $order = $transaction->getOrder();
    if ( is_null( $order ) ) {
        $order = new \net\authorize\api\contract\v1\OrderType();
    }
 
    $order->setInvoiceNumber( $args[ 'invoice' ] );
 
    $transaction->setOrder( $order );
 
    return $transaction;
}
 
add_filter( 'wpforms_authorize_net_process_transaction', 'wpf_dev_authorize_net_process_transaction_add_invoice_to_transaction', 10, 2 );


In the first code snippet, we’re using the code to grab our invoice number so the API will know what field we want to pass through as the invoice number. Please remember to update this field ID to match the field ID of your form.

And that’s it, when Authorize.net payments are sent through WPForms, they will now include the invoice number.

Now you can pass invoice through to Authorize.net

Would you like to also learn how to send the address through to Authorize.net? Check out our tutorial on How to Send an Address Through to Authorize.net Payments.

Filter References:

FAQ

Q: What if I don’t have an invoice number?

A: In our above example, we’re using a built-in Smart Tag for the {unique_value} to create an alpha-numeric random number. If you would rather control the unique value to be numeric only or a controlled count, just follow this tutorial.