您好我正在开发一个Web应用程序,需要将它与Authorize.NET AIM集成。我已经测试了Authorize.NET提供的代码,它适用于单一产品。此代码具有用于订单项的选项,默认情况下会进行注释。我取消注释然后使用它,但它只显示单个产品处理没有订单项处理。
请参阅Authorize.NET AIM示例代码
// 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, "& " );
// The following section provides an example of how to add line item details to
// the post string. Because line items may consist of multiple values with the
// same key/name, they cannot be simply added into the above array.
//
// This section is commented out by default.
$line_items = array(
"item1<|>golf balls<|><|>2<|>18.95<|>Y",
"item2<|>golf bag<|>Wilson golf carry bag, red<|>1<|>39.99<|>Y",
"item3<|>book<|>Golf for Dummies<|>1<|>21.99<|>Y");
foreach( $line_items as $value )
{ $post_string .= "&x_line_item=" . urlencode( $value ); }
// 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 . " </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
请使用Authorize.NET AIM帮助发布多个项目。
先谢谢。
答案 0 :(得分:1)
您还可以尝试使用比其SDK更轻的AuthnetXML,并且更易于使用。它甚至包含一个示例,显示如何添加多个订单项:
require('../../config.inc.php');
require('../../AuthnetXML.class.php');
$xml = new AuthnetXML(AUTHNET_LOGIN, AUTHNET_TRANSKEY, AuthnetXML::USE_DEVELOPMENT_SERVER);
$xml->createTransactionRequest(array(
'refId' => rand(1000000, 100000000),
'transactionRequest' => array(
'transactionType' => 'authCaptureTransaction',
'amount' => 5,
'payment' => array(
'creditCard' => array(
'cardNumber' => '4111111111111111',
'expirationDate' => '122016',
'cardCode' => '999',
),
),
'order' => array(
'invoiceNumber' => '1324567890',
'description' => 'this is a test transaction',
),
'lineItems' => array(
'lineItem' => array(
0 => array(
'itemId' => '1',
'name' => 'vase',
'description' => 'Cannes logo',
'quantity' => '18',
'unitPrice' => '45.00'
),
1 => array(
'itemId' => '2',
'name' => 'desk',
'description' => 'Big Desk',
'quantity' => '10',
'unitPrice' => '85.00'
)
)
),
'tax' => array(
'amount' => '4.26',
'name' => 'level2 tax name',
'description' => 'level2 tax',
),
'duty' => array(
'amount' => '8.55',
'name' => 'duty name',
'description' => 'duty description',
),
'shipping' => array(
'amount' => '4.26',
'name' => 'level2 tax name',
'description' => 'level2 tax',
),
'poNumber' => '456654',
'customer' => array(
'id' => '18',
'email' => 'someone@blackhole.tv',
),
'billTo' => array(
'firstName' => 'Ellen',
'lastName' => 'Johnson',
'company' => 'Souveniropolis',
'address' => '14 Main Street',
'city' => 'Pecan Springs',
'state' => 'TX',
'zip' => '44628',
'country' => 'USA',
),
'shipTo' => array(
'firstName' => 'China',
'lastName' => 'Bayles',
'company' => 'Thyme for Tea',
'address' => '12 Main Street',
'city' => 'Pecan Springs',
'state' => 'TX',
'zip' => '44628',
'country' => 'USA',
),
'customerIP' => '192.168.1.1',
'transactionSettings' => array(
'setting' => array(
0 => array(
'settingName' =>'allowPartialAuth',
'settingValue' => 'false'
),
1 => array(
'settingName' => 'duplicateWindow',
'settingValue' => '0'
),
2 => array(
'settingName' => 'emailCustomer',
'settingValue' => 'false'
),
3 => array(
'settingName' => 'recurringBilling',
'settingValue' => 'false'
),
4 => array(
'settingName' => 'testRequest',
'settingValue' => 'false'
)
)
),
'userFields' => array(
'userField' => array(
'name' => 'MerchantDefinedFieldName1',
'value' => 'MerchantDefinedFieldValue1',
),
'userField' => array(
'name' => 'favorite_color',
'value' => 'blue',
),
),
),
));
?>
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<title>AIM :: Authorize and Capture</title>
<style type="text/css">
table
{
border: 1px solid #cccccc;
margin: auto;
border-collapse: collapse;
max-width: 90%;
}
table td
{
padding: 3px 5px;
vertical-align: top;
border-top: 1px solid #cccccc;
}
pre
{
overflow-x: auto; /* Use horizontal scroller if needed; for Firefox 2, not needed in Firefox 3 */
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */ /*
width: 99%; */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
table th
{
background: #e5e5e5;
color: #666666;
}
h1, h2
{
text-align: center;
}
</style>
</head>
<body>
<h1>
AIM :: Authorize and Capture
</h1>
<h2>
Results
</h2>
<table>
<tr>
<th>Response</th>
<td><?php echo $xml->messages->resultCode; ?></td>
</tr>
<tr>
<th>code</th>
<td><?php echo $xml->messages->message->code; ?></td>
</tr>
<tr>
<th>Successful?</th>
<td><?php echo ($xml->isSuccessful()) ? 'yes' : 'no'; ?></td>
</tr>
<tr>
<th>Error?</th>
<td><?php echo ($xml->isError()) ? 'yes' : 'no'; ?></td>
</tr>
<tr>
<th>authCode</th>
<td><?php echo $xml->transactionResponse->authCode; ?></td>
</tr>
<tr>
<th>transId</th>
<td><?php echo $xml->transactionResponse->transId; ?></td>
</tr>
</table>
<h2>
Raw Input/Output
</h2>
<?php
echo $xml;
?>
</body>
</html>
免责声明:我是该代码的作者。
答案 1 :(得分:0)
您在哪里寻找订单项?它没有出现在Authorize.NET的响应中。只有在登录Authorize.NET时才能在事务详细信息页面上找到它。
答案 2 :(得分:0)
我建议使用他们的SDK而不是CURL调用。 https://developer.authorize.net/integration/fifteenminutes/#custom
然后代码就像:
<?php
require_once 'anet_php_sdk/AuthorizeNet.php'; // Make sure this path is correct.
$transaction = new AuthorizeNetAIM('YOUR_API_LOGIN_ID', 'YOUR_TRANSACTION_KEY');
$transaction->amount = '9.99';
$transaction->card_num = '4007000000027';
$transaction->exp_date = '10/16';
$response = $transaction->authorizeAndCapture();
if ($response->approved) {
echo "<h1>Success! The test credit card has been charged!</h1>";
echo "Transaction ID: " . $response->transaction_id;
} else {
echo $response->error_message;
}
?>
如果您需要逐项订单信息,则还必须将项目添加到交易中:
$transaction->addLineItem(
'item1', // Item Id
'Golf tees', // Item Name
'Blue tees', // Item Description
'2', // Item Quantity
'5.00', // Item Unit Price
'N' // Item taxable
);
所有这些Authorize.net列出项目的行为都有点儿错误,所以如果该解决方案不起作用,请查看: http://community.developer.authorize.net/t5/Integration-and-Testing/x-line-item-integration-in-PHP/td-p/9654
答案 3 :(得分:0)
问题是API的命名极具误导性。
你的一系列订单项不应该是这个(错误):
{ lineItems:[
{ lineItem: { ..Line item data..}},
{ lineItem: { ..Line item data..}}
]}
它应该是这样的(正确):
{ lineItems:
{ lineItem: [{ ..Line item data..},
{ ..Line item data..}]
}}
尽管&#34; lineItems&#34;是复数,&#34; lineItem&#34;不是,复数条目(数组)发生在&#34; lineItem&#34;不是&#34; lineItems&#34;。
更糟糕的是,您可以使用这样的设置,它可以用于一个项目,仅适用于一个项目:
{ lineItems:[
{ lineItem: { ..Line item data..}}
]}
因此进一步误导了用户。我通过尝试使用JSON版本的auth.net协议,放弃和使用XML来发现这一点,只是为了实现我的XML解析器是如何工作的,它是复制&#34; lineItems&#34;条目,这使我尝试将阵列向下移动一级,并且神奇地开始工作。
我认为核心问题是auth.net的解析器是如何工作的。以下内容:
{ lineItems:[
{ lineItem: { ..Line item data..}},
{ lineItem: { ..Line item data..}}
]}
在他们的系统中变成了这个:
{[
{lineItems: { lineItem: { ..Line item data..}}},
{lineItems: { lineItem: { ..Line item data..}}}
]}
这可以解释有关无效密钥条目的相当奇怪的错误,然后列出有效的密钥条目。