Apr
22

How can We Integrate the Authorize.Net Payment Gateway in Codeigniter?

Authorize.Net is my favorite payment mode for using in web development. It is simple, easy and secure payment connection. I use authorize.Net in my codeigniter based projects and here is the code and downloads.

This is helper for payment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function do_payment ($post_values)
{
        $post_url = "https://test.authorize.net/gateway/transact.dll";		  
        // This section takes the input fields and converts them to the proper format
	$post_string = "";
	foreach( $post_values as $key => $value )
	{ $post_string .= "$key=" . urlencode( $value ) . "&"; }
	   $post_string = rtrim( $post_string, "& " );
 
	// This sample code uses the CURL library for php to establish a connection,
		// submit the post, and record the response.
		// If you receive an error, you may want to ensure that you have the curl
		// library enabled in your php configuration
 
		$request = curl_init($post_url); // initiate curl object
 
		curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
 
		curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
		curl_setopt($request, CURLOPT_POSTFIELDS, $post_string); // use HTTP POST to send form data
		curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response.
		$post_response = curl_exec($request); // execute curl post and store results in $post_response
 
		// additional options may be required depending upon your server configuration
		// you can find documentation on curl options at http://www.php.net/curl_setopt
		curl_close ($request); // close curl object
 
		// This line takes the response and breaks it into an array using the specified delimiting character
		$response_array = explode($post_values["x_delim_char"],$post_response);
 
		// The results are output to the screen in the form of an html numbered list.
		if($response_array)
		{
		return $response_array;
		}
	  else { return ''; }
	}

And here is your controller function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function do_user_payment()
{
     $this->load->library('auth_payment');	
     $card_expiration = $_POST['crMonth'].$_POST['crYear'];
 
     $x_login  	 = "login_key";
     $x_tran_key  = "trans_key";
     $card_number = $_POST['ccn']; 
 
     $invoice_num = '';
 
     $x_card_code = $_POST['CSV'];
 
     $post_values['x_invoice_num'] = $invoice_num;
     $post_values['x_login'] 		 = $x_login;
     $post_values['x_tran_key'] 	 = $x_tran_key;
 
     $post_values['x_card_code'] 	 = $x_card_code;
 
      $post_values['x_version'] 	= "3.1";
      $post_values['x_delim_data'] = "TRUE";
      $post_values['x_delim_char'] = "|";
      $post_values['x_relay_response'] = "FALSE";
 
      $post_values['x_type'] 	= "AUTH_CAPTURE"; //Optional
      $post_values['x_method'] = "CC";
      $post_values['x_card_num'] = $card_number;
      $post_values['x_exp_date'] = $card_expiration;
      $post_values['x_amount']   =  'Your Charges';
 
      $post_values['x_first_name'] = $user_first_name; //Optional (From Client)
      $post_values['x_last_name'] = $user_last_name; //Optional (From Client)
      $post_values['x_address'] = $user_address1; //Optional (From Client)
      $post_values['x_state'] = $user_state; //Optional (From Client)
      $post_values['x_zip'] = $user_zip; //Optional (From Client)
 
      //Calling Payment function
 
     $paymentResponse = $this->auth_payment->do_payment($post_values);
 
     if($paymentResponse[0]==1 && $paymentResponse[1]==1 && $paymentResponse[2]==1)
     {
          // payment is successful. Do your action here
     }
    else
     {
          // payment failed.
          return $paymentResponse[3]; // return error
     }
}

Add To Facebook Stumble This Digg This Add To Del.icio.us Add To Reddit Add To Yahoo Add To Twitter


Written by ZeeShaN

ZeeShaN RasooL is a web developer who loves to work in latest technologies to create more interactive dynamic and beautiful web pages.







Enter your Email:

Click Here for Popular

Who I Am

Zeeshan Rasool

Software Engineer - PHP
Lahore - Pakistan

zeeshan(@)99points.info
Skype: zeeshan-rasool
gTalk: zishan.rasool85

Categories

Tags

Comments