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

Overview

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’s invoice number inside the Authorize.net payments.

Setup

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

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 Numbers (fields[6]) form field that contains the invoice number.


/*
 * 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
 * 
 * @param array                        $args    Single payment arguments.
 * @param \WPFormsAuthorizeNet\Process $process WPForms Authorize.Net Process object.
 *
 * @return array
 */
function wpf_dev_authorize_net_process_payment_single_add_invoice_to_args( $args, $process ) {
	// Replace 6 in $process->fields[6] to an id of your invoice field.
	if ( isset( $process->fields[6]['value'] ) ) {
		$args['invoice'] = $process->fields[6]['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
 *
 * @param \net\authorize\api\contract\v1\TransactionRequestType $transaction Single transaction object.
 * @param array                                                 $args        Single transaction arguments.
 *
 * @return \net\authorize\api\contract\v1\TransactionRequestType
 */
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 you 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: