Page 1 of 1

needhelp to post request through curl

Posted: Mon Oct 25, 2010 12:04 am
by amit_n
Hi, I am trying to post the request to payment gateway url using curl lib.
please help me with following code. I am not able post the request.

here is the coed for authorize_net_request.php:

Code: Select all


<?php

class PLANETAuthorizeNetRequest
{
// PLANETAuthorize Server URL
private $_mUrl;
// Will hold the current request to be sent to PLANETAuthorize.net
private $_mRequest;
// Constructor initializes the class with URL of PLANETAuthorize.net
public function __construct($url)
{
// PLANETAuthorize.net URL
$this->_mUrl = $url;
}
public function SetRequest($request)
{
	$this->_mRequest = '';
	$request_init =	array ('username' => '*******',
							'password' => '******',
							 'x_delim_data' => 'TRUE', 
							'x_delim_char' => '|',
							'x_relay_response' => 'FALSE');
	
	$request = array_merge($request_init, $request);
	
	foreach($request as $key => $value )
	$this->_mRequest .= $key . '=' . urlencode($value) . '&';
	
	return $this->_mRequest;
	}
	// Send an HTTP POST request to PLANETAuthorize.net using cURL
	public function GetResponse()
	{
	// Initialize a cURL session
	$ch = curl_init();
	// Prepare for an HTTP POST request
	curl_setopt($ch, CURLOPT_POST, 1);
	// Prepare the request to be POSTed
	curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim($this->_mRequest, '& '));
	
	// Set the URL where we want to POST our data
	curl_setopt($ch, CURLOPT_URL, $this->_mUrl);
	/* Do not verify the Common name of the peer certificate in the SSL
	handshake */
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
	// Prevent cURL from verifying the peer's certificate
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	/* We want cURL to directly return the transfer instead of
	printing it */
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	// Perform a cURL session
	$result = curl_exec($ch);
	// Close a cURL session
	curl_close ($ch);
	// Return the response
	return $result;
}
}

?>

here is the coed for test_authorize_net.php:

Code: Select all


<?php
session_start();
if (empty ($_GET['step']))
{
require_once 'authorize_net_request.php';
$myUrl = "https://urlgoes here";
$request = new PLANETAuthorizeNetRequest($myUrl);
// Auth
$transaction =array (		
			'type'         => 'sale', 
			'orderid'      => '110',
			'ccnumber'	   => '4111111111111111',
			'cvv'          => '9999',
			'ccexp'        => '1201',
			'cctype' 		=> 'American Express',
			'amount'		=> '24.00',
			'firstname'      => 'Andy,
			'lastname'       => 'N,
			'email'          => 'andy.n@xyz.net',
			'ipaddress'      => '',
			'address1'       => 'asdhfasd jkfa jdf',
			'address2'         => 'asgdv fg hasdf',
			'city'           => 'Pune',
			'state'          => 'Maharashtra',
			'zip'            => '411037',
			'country'    => 'India'); // Transaction type


$request->SetRequest($transaction);

$auth_only_response = $request->GetResponse();
$_SESSION['auth_only_response'] = $auth_only_response;

$auth_only_response = explode('|', $auth_only_response);

// Read the transaction ID, which will be necessary for taking the payment
$response = $auth_only_response[0];
$responsetext = $auth_only_response[1];
$authcode =  $auth_only_response[2];
$ref_trans_id = $auth_only_response[3];
$cvvresponse = $auth_only_response[5];
$response_code = $auth_only_response[7];

// Capture

$transaction = array ('transactionid' =>$ref_trans_id, // Transaction id
					  'type' => 'capture',
					  'username'     => '*****',
					  'password'     => '****',
					  'amount'		=> '24.00',
					  'orderid'      => '110'
			
			); // Transaction type
$request->SetRequest($transaction);

$prior_auth_capture_response = $request->GetResponse();

$_SESSION['prior_auth_capture_response'] = $prior_auth_capture_response;

}
else
{
switch ($_GET['step'])
{
case 1:
print $_SESSION['auth_only_response'];
break;
case 2:
print $_SESSION['prior_auth_capture_response'];
break;
}
exit();
}
?>

<frameset cols="50%, 50%">
<frame src="test_authorize_net.php?step=1" />
<frame src="test_authorize_net.php?step=2" />
</frameset><noframes></noframes>
Thanks
Amit

Re: needhelp to post request through curl

Posted: Mon Oct 25, 2010 12:22 am
by requinix
Are you going to explain what "not able post the request" means?

Re: needhelp to post request through curl

Posted: Mon Oct 25, 2010 12:33 am
by amit_n
I have passed the url, post field parameters & values to curl set output but it is not getting executed.

Curl is really new for me.
I have got this code to integrate authorize.net payment gateway. I have modified it for planetauthorize.net payment gateway.

Please help me with this.

Re: needhelp to post request through curl

Posted: Mon Oct 25, 2010 12:41 am
by requinix
What's not getting executed? How do you know this? Have you printed out variables and checked that they have the right values? What output are you getting? What are you expecting to get?

And even if it is just a test account, publishing usernames and passwords on the Internet is not a good idea.

Re: needhelp to post request through curl

Posted: Mon Oct 25, 2010 12:54 am
by amit_n
I have changed the account details. See I passing transaction details & url to curl functions, So that it will hit the url and will perform the transaction.
I have checked all the variables by printing. They are accurate. On my WAMP server curl extension is running.
I don't know where i am making the mistake.

Can I pm you the code?



Thanks
Amit

Re: needhelp to post request through curl

Posted: Mon Oct 25, 2010 1:44 am
by requinix
Do you get any kind of response at all from the server? Take a close look at the value of $auth_only_response.

Re: needhelp to post request through curl

Posted: Mon Oct 25, 2010 2:32 am
by amit_n
Tasairis,

It is not giving any error. It's working well on hosted site. I think my WAMP server is not properly configured for CURL.

Thanks for all your help.
Amit