Merchant Account Services

Integrate the Authorize.Net Payment Gateway with PHP

Integrate the Authorize.Net payment gateway seamlessly into your ecommerce website

 

Author: Jim Conners

Rating: 10.0

Pages: 1|2|3|4|5|6|7|8|9|10

Constructor

New in PHP 5 is the __construct() function for creating an instance of an class. This function creates the object and can assign properties to it by assigning values to properties of the class. Inside our constructor we will be assigning values to properties that we will not be changing during the course of using our object. Let's look at what our __construct() function will look like and then break it down:

public function __construct($test = false) { $this->test = trim($test); if ($this->test) { $this->url = "https://test.authorize.net/gateway/transact.dll"; } else { $this->url = "https://secure.authorize.net/gateway/transact.dll"; } $this->params['x_delim_data'] = "TRUE"; $this->params['x_delim_char'] = "|"; $this->params['x_relay_response'] = "FALSE"; $this->params['x_url'] = "FALSE"; $this->params['x_version'] = "3.1"; $this->params['x_method'] = "CC"; $this->params['x_type'] = "AUTH_CAPTURE"; $this->params['x_login'] = $this->login; $this->params['x_tran_key'] = $this->transkey; }

Let's start with the constructor declaration:

public function __construct($test = false)

We've made our constructor function public so our application can access it to create an object. We also have one default parameter. This parameter is optional as we have provided a default value for it if one is not supplied during the creation of a new object.

$this->test = trim($test); if ($this->test == true) { $this->url = "https://test.authorize.net/gateway/transact.dll"; } else { $this->url = "https://secure.authorize.net/gateway/transact.dll"; }

You can see the parameter for the constructor is assigned to the parameter $test. If we pass a value of TRUE as the parameter during the creation of our object we will tell that object to use the test URL for the Authorize.Net API. The if/else statement below it chooses the appropriate URL to use for our test transactions.

The next portion of our constructor makes use the $params array we declared earlier in our class. It assigns values to some parameters the Authorize.Net API will be expecting.

$this->params['x_delim_data'] = "TRUE"; $this->params['x_delim_char'] = "|"; $this->params['x_relay_response'] = "FALSE"; $this->params['x_url'] = "FALSE"; $this->params['x_version'] = "3.1";

These five values collectively tell Authorize.Net how we intend to use their API. Since they have little value to our overall understanding of how to use their API we will avoid going into them in great detail. You can read more about what each of these parameters do in their implementation guide.

$this->params['x_method'] = "CC"; $this->params['x_type'] = "AUTH_CAPTURE";

The next two parameters tell Authorize.Net what kind of transaction we are running. The 'x_method' parameter tells Authorize.Net that this will be a credit card transaction (you can process eCheck transactions if the gateway has been established with that functionality). The 'x_type' parameter tells Authorize.Net that we wish to authorize and capture this transaction.

Authorizing but not charging a credit card

When credit cards are processed it actually is a two stage process. The first stage acquires and authorization number from the card issuing bank. The funds for that transaction are frozen and associated with that authorization number. The second stage if the actual capture of that sale where the funds are deducted from the customer's balance. The vast majority of Internet transactions perform both stages at once. But this is not always the case. It is possible that a merchant may want to freeze the funds on a credit card but not actually charge the customer until they ship the order. This is called an 'authorization only' transaction. In these cases the merchant is provided with the authorization number and uses this authorization number at a future date to claim those funds.

$this->params['x_login'] = $this->login; $this->params['x_tran_key'] = $this->transkey;

These last two parameters are our login and transaction key for the API. Obviously without these our access to the API will be denied.

Building our Class | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Methods