我正在制作一个自定义的woocommerce报告插件,该插件将显示某些信息并将其作为.csv吐出。我让它返回名称、公司名称、产品和金额等内容。我按以下方式执行此操作。
/**
* Check if we need customer phone.
*/
case 'wc_settings_tab_customer_phone':
array_push( $csv_values, self::customer_meta( get_the_ID(), '_billing_phone' ) );
break;
现在我正在使用 Authorize.net AIM支付网关的Woocommerce插件,因此生成了一个交易ID。
我想将其包含在我的.csv导出中。我将如何做到这一点?我尝试查看插件文件,并注意到这就是交易 ID $response_array[6]
,但无法弄清楚如何返回它。如果有人能告诉我如何利用 Authorize.net API 并获得交易 ID,那就太棒了!提前感谢!
编辑:这是我到目前为止得到的。我添加了 php 代码进行连接,但似乎无法拉取订单交易 ID。顺便说一下,"x_login"和"x_tran_key"已被取出并替换为"test123"。
case 'wc_settings_tab_authorize_id':
$post_url = "https://secure.authorize.net/gateway/transact.dll";
$post_values = array(
// the API Login ID and Transaction Key must be replaced with valid values
"x_login" => "test123",
"x_tran_key" => "test123",
"x_version" => "3.1",
"x_delim_data" => "TRUE",
"x_delim_char" => "|",
"x_relay_response" => "FALSE",
// Additional fields can be added here as outlined in the AIM integration
// guide at: http://developer.authorize.net
);
// 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);
array_push( $csv_values, TransactionID() );
break;
编辑2:实现约翰的代码
case 'wc_settings_tab_authorize_id':
$post_url = "https://secure.authorize.net/gateway/transact.dll";
$post_values = array(
// the API Login ID and Transaction Key must be replaced with valid values
"x_login" => "test123",
"x_tran_key" => "test123",
"x_version" => "3.1",
"x_delim_data" => "TRUE",
"x_delim_char" => "|",
"x_relay_response" => "FALSE",
// Additional fields can be added here as outlined in the AIM integration
// guide at: http://developer.authorize.net
);
// 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
$post_response = '1|1|1|This transaction has been approved.|4DHVNH|Y|2230582188|none|Test transaction for ValidateCustomerPaymentProfile.|0.00|CC|auth_only|none|John|Doe||123 Main St.|Bellevue|WA|98004|USA|800-555-1234|800-555-1234|email@example.com|||||||||0.00|0.00|0.00|FALSE|none|E440D094322A0D406E01EDF9CE871A4F||2|||||||||||XXXX1111|Visa||||||||||||||||';
$response_array = explode('|',$post_response);
$transaction_id = $response_array[6];
array_push( $csv_values, $transaction_id );
break;
编辑3:好的,这就是我目前拥有的(不包括api和交易密钥(。
/**
* Check for authorize.net transaction id.
*/
case 'wc_settings_tab_authorize_id':
$post_url = "https://secure.authorize.net/gateway/transact.dll";
$post_values = array(
// the API Login ID and Transaction Key must be replaced with valid values
"x_login" => "TEST",
"x_tran_key" => "TEST",
"x_version" => "3.1",
"x_delim_data" => "TRUE",
"x_delim_char" => "|",
"x_relay_response" => "FALSE",
// Additional fields can be added here as outlined in the AIM integration
// guide at: http://developer.authorize.net
);
// 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, http_build_query($post_values)); // 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);
$transaction_id = $response_array[6];
array_push( $csv_values, $transaction_id );
break;
仍然无法弄清楚为什么这不起作用。当我尝试返回$transaction_id
时,我得到的值0
.当我尝试$post_response
查看它返回的内容时,我得到这个:
3|2|33|Credit card number is required.||P|0|||0.00|CC|auth_capture||||||||||||||||||||||||||THISISANALPHANUMERICNUMBER||||||||||||||||||||||||||||||
以前有一个字母数字字符串,但出于安全目的,我替换了它。您认为这可能是因为我没有设置抄送号码或账单地址吗?
Authorize.Net 返回的响应字符串将如下所示:
1|1|1|This transaction has been approved.|4DHVNH|Y|2230582188|none|Test transaction for ValidateCustomerPaymentProfile.|0.00|CC|auth_only|none|John|Doe||123 Main St.|Bellevue|WA|98004|USA|800-555-1234|800-555-1234|email@example.com|||||||||0.00|0.00|0.00|FALSE|none|E440D094322A0D406E01EDF9CE871A4F||2|||||||||||XXXX1111|Visa||||||||||||||||
这是由|
分隔的结果,是您在此处设置的分隔字符:
"x_delim_char" => "|",
您可以使用explode()
正确分解字符串:
$response_array = explode($post_values["x_delim_char"],$post_response);
这为我们提供了一个名为的数组中的数据 $response_array
.
在 Authorize.Net 的响应中,事务 ID 为 2230582188
。在我们的数组中,这是第七个元素,因此我们可以使用以下方法获取它:
$transaction_id = $response_array[6];
这是一个演示,向您展示了此操作。