OxyScripts.com
Menu spacer Home Tutorials Articles Code Forums irc.freenode.net #oxyscripts
Main (PHP)
Home Forums PHP News PHP Tutorials Articles PHP Code Snippets Contact Us Sysadmin Resources Books Template Shop
3rd Party Streams
SlashDot PHPDeveloper.org PHP.Net
Resources
PHP Manual MySQL Manual Smarty Manual PEAR Manual PHP-GTK Manual Symfony Manual
Code Snippets
Authentication Database Graphics HTTP Miscellaneous Time/Date
Affiliates
Scripts TutorialMan TutorialGuide CodingForums.com PHP Scripts Cheap Web Hosting Affordable Web Hosting Dreamweaver Templates

Search This Site :     PHP Function Reference :
 

Submitting Payments Through Authorize.net Using cURL

By admin (2007-10-30. 7723 views.)
Authorize.net is one of the premier credit card payment systems available. One of the interfaces available is to use PHP, usually via cURL to connect and pass information.

cURL is a standard package available to PHP programmers that allows for accessing web pages. The typical cURL request is an HTTP GET request where the program is retrieving the contents of a web page for things like web scanning or auto checking information.

More interestingly cURL can be used to HTTP POST information to web pages and retrieving the response in a programmatic method. In this case we are posting credit card payment information to authorize.net and examining the response to determine if the payment went through.

First, we need a method for POSTing information to a web site:

<?php

function send_request_via_curl($host,$path,$content)
{
   
$posturl "https://" $host $path;
   
$ch curl_init();

   
curl_setopt($chCURLOPT_URL$posturl);
   
curl_setopt($chCURLOPT_RETURNTRANSFER1);
   
curl_setopt($chCURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
   
curl_setopt($chCURLOPT_HEADER1);
   
curl_setopt($chCURLOPT_POSTFIELDS$content);
   
curl_setopt($chCURLOPT_POST1);
   
curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
   
   
//godaddy proxy settings
   
curl_setopt ($chCURLOPT_PROXYTYPECURLPROXY_HTTP);
   
curl_setopt ($chCURLOPT_PROXY,GODADDY_PROXY);
   
curl_setopt ($chCURLOPT_TIMEOUT120);   
   
   
$response curl_exec($ch);
   return 
$response;
}

?>


The $host is the web site we are posting to. $path is the sub-url (if that's the right word). $content is the information we are posting to the site.

Like every other cURL request, the first thing is to init() the package.

Next we set some parameters to determine what is going to happen:
- CURLOPT_URL is the url we are addressing
- CURLOPT_RETURNTRANSFER means we expect a response
- CURLOPT_HTTPHEADER to set standard HTTP header info
- CURLOPT_HEADER ditto
- CURLOPT_POSTFIELDS the data we are POSTing
- CURLOPT_POST flag that we are posting data
- CURLOPT_SSL_VERIFYPEER this is over SSL

In this specific instance we were hosting the package on GoDaddy. All of their traffic (from a shared host) goes through a proxy. So, we have to tell cURL about the proxy using these parameters:
- CURLOPT_PROXYTYPE we have an CURLPROXY_HTTP proxy
- CURLOPT_PROXY the url for the proxy
- CURLOPT_TIMEOUT expand the default timeout (to allow for the proxy delay)

Finally, make the request via curl_exec() and return the response.

Now that we have a way to post our request. Let's see what the request and response look like:

<?php

//Build XML to post
$content = ...  //this is standard stuff provided by authorize.net
    
//Send the XML via curl
$response send_request_via_curl(AUTHNET_HOST,AUTHNET_PATH,$content);

//If the connection and send worked $response holds the Authorize.Net return
$refId 1;
$resultCode 'FAIL';
$code='';
$text='';
$subscriptionId='';

if (
$response) {
 list (
$refId$resultCode$code$text$subscriptionId
    = 
parse_return($response);
            
  
//refid should be our subscription_id 
  //$resultCode - should be OK
  //$code - should be I00001 - they have complete specs on all the error codes
  //$text - should beSuccessful
  //$subscriptionId - their sub id
}
else {
  echo 
"send failed 
"
;
 die;
}        

?>


So, we build up a big XML string with the information needed. In this case we are calling the recurring billing interface. There are others. In any case authorize.net provides pretty exacting requirements for the fields/values in the XML packet.

We pass the XML packet to authorize, they respond with a result package, we parse out the fields, check for errors and usually store everything in a database so we know what happened.

In hindsight it appears easy as pie. I somehow always feel completely at risk coding this kind of thing, even when you are using a test interface.

The above example is fairly complete. You may need to make slight adjustments for your instance of PHP as to the exact settings you need to make for cURL.
 

 
   Print this page

Top Sponsor
Symantec\'s Norton SystemWorks 2006
Sponsors
CA
Sponsors
AdWords Dominator 125*125
Advertisting

Affiliates
VertexTemplates PHPFreaks CodeWalkers StarGeek DevScripts CGI & PHP Scripts PHP CMS

Shopping Rebates   Sell It 4 You   Flash Page Counters   Get Insured
GPS Tracking Service   Charity Donate Info   Web Site Hosting   VOIP Service

Privacy Policy | Links | Site Map | Advertising

All content on OxyScripts.com is (©)2002-2007

 
Powered by Adrastea - Version 1.0.0. Copyright © Rune Solutions, 2004-2005