Authorize.net(aim)信用卡支付介绍

要做一个项目,是用信用卡支付,以前还没有接触过信用卡支付接口,今天研究了下Authorize.net的接口,还是挺简单的。Authorize.net也提供了一个供开发人员测试的测试账号,这个功能和paypal的sandbox一样,开发人员只需要到http://developer.authorize.net/这个地方去申请一个账号就可以了,申请后,它会审核,一般审核结果在两天内有结果,会发邮件到你邮箱告诉你的用户名和key之类要用到的东西,当然这些都是英文的,所以你要有一定的英文基础。

下面直接贴代码:

<!–
This sample code is designed to connect to Authorize.net using the AIM method.
For API documentation or additional sample code, please visit:
http://developer.authorize.net
–>

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
“http://www.w3.org/TR/html4/loose.dtd”>
<HTML lang=’en’>
<HEAD>
<TITLE> Sample AIM Implementation </TITLE>
</HEAD>
<BODY>
<P> This sample code is designed to generate a post using Authorize.net’s
Advanced Integration Method (AIM) and display the results of this post to
the screen. </P>
<P> For details on how this is accomplished, please review the readme file,
the comments in the sample code, and the Authorize.net AIM API documentation
found at http://developer.authorize.net </P>
<HR />

<?PHP

// By default, this sample code is designed to post to our test server for
// developer accounts: https://test.authorize.net/gateway/transact.dll
// for real accounts (even in test mode), please make sure that you are
// posting to: https://secure.authorize.net/gateway/transact.dll
$post_url = “https://test.authorize.net/gateway/transact.dll”;

$post_values = array(

// the API Login ID and Transaction Key must be replaced with valid values
“x_login” => “API_LOGIN_ID”,
“x_tran_key” => “TRANSACTION_KEY”,

“x_version” => “3.1”,
“x_delim_data” => “TRUE”,
“x_delim_char” => “|”,
“x_relay_response” => “FALSE”,

“x_type” => “AUTH_CAPTURE”,
“x_method” => “CC”,
“x_card_num” => “4111111111111111”,
“x_exp_date” => “0115”,

“x_amount” => “19.99”,
“x_description” => “Sample Transaction”,

“x_first_name” => “John”,
“x_last_name” => “Doe”,
“x_address” => “1234 Street”,
“x_state” => “WA”,
“x_zip” => “98004”
// Additional fields can be added here as outlined in the AIM integration
// guide at: http://developer.authorize.net
);

// This section takes the input fields and converts them to the proper format
// for an http post. For example: “x_login=username&x_tran_key=a1B2c3D4”
$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.
echo “<OL>n”;
foreach ($response_array as $value)
{
echo “<LI>” . $value . “&nbsp;</LI>n”;
$i++;
}
echo “</OL>n”;
// individual elements of the array could be accessed to read certain response
// fields. For example, response_array[0] would return the Response Code,
// response_array[2] would return the Response Reason Code.
// for a list of response fields, please review the AIM Implementation Guide
?>
</BODY>
</HTML>

把上面的x_login和key改成自己的就可以测试了。
返回的数据是一个以$post_value[‘x_delim_char’]分隔的字符串$post_response,用explode分隔成数组后,数组是一个下标从0-67的数组,其中比较重要的是,看是否支付成功的有几个,具体每个值代表上面含义请参考http://developer.authorize.net/guides/AIM

from http://blog.sina.com.cn/s/blog_5506d7950100v34l.html

分享到:

发表评论

昵称

沙发空缺中,还不快抢~