How to Send an Address Through to Authorize.net Payments

Overview

Would you like to include the address 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 the payment’s Address form field 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.

/**
 * Include an address into the set of Authorize.Net args.
 *
 * @link  https://wpforms.com/developers/how-to-send-an-address-through-to-authorize-net-payments/
 *
 * @param array                        $args    Single payment arguments.
 * @param \WPFormsAuthorizeNet\Process $process WPForms Authorize.Net Process object.
 *
 * @return array
 */

function wpf__authorize_net_process_payment_single_add_fields_to_args( $args, $process ) {
   // Replace 3 in $process->fields[3] to an id of your address field.
   $args['address'] = $process->fields[3];
   return $args;
}
add_filter( 'wpforms_authorize_net_process_payment_single_args', 'wpf__authorize_net_process_payment_single_add_fields_to_args', 10, 2 );


/**
 * Set the customer's Bill To address.
 *
 * @link  https://wpforms.com/developers/how-to-send-an-address-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__authorize_net_process_transaction_add_address_to_transaction( $transaction, $args ) {
   $bill_to = $transaction->getBillTo();
   if ( is_null( $bill_to ) ) {
      $bill_to = new \net\authorize\api\contract\v1\CustomerAddressType();
   }
   $address = $args['address']['address1'];
   if ( ! empty( $args['address']['address2'] ) ) {
      $address .= ' ' . $args['address']['address2'];
   }
   $bill_to->setAddress( $address );
   $bill_to->setCity( $args['address']['city'] );
   $bill_to->setState( $args['address']['state'] );
   $bill_to->setZip( $args['address']['postal'] );
   $bill_to->setCountry( $args['address']['country'] );
   $transaction->setBillTo( $bill_to );
   return $transaction;
}
add_filter( 'wpforms_authorize_net_process_transaction', 'wpf__authorize_net_process_transaction_add_address_to_transaction', 10, 2 );

In the first code snippet, we’re using the code to grab our Address form field ID fields[3] so the API will know where to pull the address information from. 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 update the customer’s address.

sending address through to Authorize.net

Would you like to also learn how to send metadata through to Stripe? Take a look at this tutorial on How To Send Metadata to Stripe Payments.

Filter References: