Get order transaction ID from authorize.net Author

2020-08-04 04:42发布

I'm making a custom woocommerce reports plugin that will display certain information and spit it out as a .csv. I have it return stuff like name, company name, product, and amount. I do this the following way.

/**
 * 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;

Now I'm using the Authorize.net AIM Payment Gateway for Woocommerce plugin so a Transaction ID is generated.

I want to include this in my .csv export. How would I go about doing this? I tried looking in the plugin files and noticed this is what the transaction id was $response_array[6] but can't figure out how to return it. If someone could show me how to tap into Authorize.net API and get the transaction ID that would be awesome! Thanks in advanced!

EDIT: Here is what I've got so far. I added the php code to connect but can't seem to pull the order transaction id. By the way, the "x_login" and "x_tran_key" have been taken out and replaced with "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;

EDIT 2: Implementing John's code

                    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;

EDIT 3: Okay, this is what I currently have (excluding the api and transaction key).

    /**
    * 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;

Still can't figure out why this won't work. When I try and return the $transaction_id I get the value 0. When I try $post_response to see what it returns I get this:

3|2|33|Credit card number is required.||P|0|||0.00|CC|auth_capture||||||||||||||||||||||||||THISISANALPHANUMERICNUMBER||||||||||||||||||||||||||||||

Before there was an alphanumberic string but I replaced it for security purposes. Do you think this may be happening because I'm not setting a cc number or billing address with it?

1条回答
冷血范
2楼-- · 2020-08-04 05:10

The response string returned by Authorize.Net is going to look something like this:

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

This is the results separated by a | which is the delimiting character you set here:

"x_delim_char"      => "|",

You correctly break apart the string using explode():

$response_array = explode($post_values["x_delim_char"],$post_response);    

which gives us that data in an array called $response_array.

In Authorize.Net's response, the transaction ID is 2230582188. In our array that is the seventh element so we can get it using:

$transaction_id = $response_array[6];

Here is a demo showing you this works.

查看更多
登录 后发表回答