从 authorize.net 获取订单交易 ID 授权 AIM

Get order transaction ID from authorize.net Authorize AIM

我正在制作一个自定义 woocommerce 报告插件,它将显示某些信息并将其输出为 .csv。我有 return 诸如姓名、公司名称、产品和金额之类的东西。我按以下方式执行此操作。

/**
 * 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 Woocommerce 插件的 AIM 支付网关,因此会生成交易 ID。

我想将其包含在我的 .csv 导出文件中。我该怎么做呢?我尝试查看插件文件并注意到这是事务 ID $response_array[6] 但无法弄清楚如何 return 它。如果有人能告诉我如何利用 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:实施 John 的代码

                    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;

仍然不明白为什么这行不通。当我尝试 return 和 $transaction_id 时,我得到值 0。当我尝试 $post_response 查看它 returns 时,我得到了这个:

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];

Here is a demo showing you this works.