troubleshooting Question

Need PHP help with parsing an XML cURL response

Avatar of Chris Inman
Chris Inman asked on
* arraysPHPXML
4 Comments1 Solution112 ViewsLast Modified:
I'm fairly novice with PHP, but I can usually figure things out. But I'm stuck on working around this legacy code that we use on our sales form.
 I have to shoehorn in another Card processing merchant that does things a little differently with their API.

This is processing the response from a cURL API where I receive a SOAP XML response. I'm having some issues with the actual response as well, so I have it sampled in what appears to be a proper xml format, then attempting to parse out the message objects.

The process:
1. I have the response XML converting into a "SimpleXMLElement" with the simplexml_load_string
2. Then the XML is "converted to an array", I think, with the $array = (array)$xml;
3. Then, the code should be able to look into the array values and process a Success or Failed based on the Status Value
4. Then the $data is json encoded and is passed to the JavaScript file for further processing of the notification and filling in the Auth code field of the sales form.

The Problem(s):
1. You'll see in the PHP code that I have 2 testing sections to process the response. The first uses the $array and the second skips the array and just uses the $xml. One is commented out for testing the other, of course.
2. When using the $xml section to get the values, I get real close to what I need until it get json encoded, then the output just says Object Object in the JS Alert. It appears that the code is finding the proper value of the key, but it's not handled the same way for the json encoding in order to get a good alert.
3. When using the array process, I don't get anything but a full failure to extract the values, so it rolls to the end else statement "$data = array( 'message' => $xml->transaction->description, 'html' => 'Failed, Not seeing Success or TransactionID', 'date' => $curdate->format('m/d/Y h:i:s a') );"

What I really need help with is:
What is the proper way to use the array process to grab that Status value? is it $array->status == "Success" or do I need to go deeper?
I'm hoping that then I will get the proper json encoding to pass on.

Thank you for your time!

Here is what the SimpleXMLElement produces:
SimpleXMLElement Object
(
    [transaction] => SimpleXMLElement Object
        (
            [status] => Success
            [reasoncode] => N/A
            [transactionid] => J7DXXXX8U0
            [description] => The credit card has been accepted.
        )

)

Here is what the array produces:
Array
(
    [transaction] => SimpleXMLElement Object
        (
            [status] => Success
            [reasoncode] => N/A
            [transactionid] => J7DXXXX8U0
            [description] => The credit card has been accepted.
        )

)

Code sample PHP of SOAP XML Response handler:
// Success response sample
    /* $response = "<?xml version='1.0' encoding='utf-8'?>
                    <string xmlns='http://wsclientapi.everyware.com/'>
                        <transaction>
                            <status>Success</status>
                            <reasoncode>N/A</reasoncode>
                            <transactionid>J7DXXXX8U0</transactionid>
                            <description>The credit card has been accepted.</description>
                       </transaction>
                    </string>"; */

//Failed response sample
$response = "<?xml version='1.0' encoding='utf-8'?>
                    <string xmlns='http://wsclientapi.everyware.com/'>
                        <transaction>
                            <status>Failed</status>
                            <reasoncode>409</reasoncode>
                            <transactionid>N/A</transactionid>
                            <description>The transaction has been declined.</description>
                        </transaction>
                    </string>";

    $xml = simplexml_load_string($response); 

    //Used to print out the xml for testing
    ?><pre><?php print_r($xml); ?></pre><?php
 

                $array = (array)$xml;
                //Used to print out the array for testing
                ?><pre><?php print_r($array); ?></pre><?php


                  if ($xml === false) {
                    $data = array('message' => $response, 'html' => $response);
       
                } else {
                    // $data = array('message' => $array[0], 'html' => $array[0]);
                    if ($array->status == "Success") {
                        // Transaction approved! Do your logic here.

                        $curdate = new DateTime('NOW', new DateTimeZone('America/Los_Angeles'));
                        if( isset($array->transactionid) ){
                            $data = array( 'message' => 'Success', 'html' => 'Success', 'transid' => $array->transactionid, 'auth' => $array->transactionid, 'date' => $curdate->format('m/d/Y h:i:s a') );
                        } else{
                            $data = array( 'message' => 'Success', 'html' => 'Success', 'transid' => $array->transactionid, 'date' => $curdate->format('m/d/Y h:i:s a') );
                        }

                    } else {
                        $curdate = new DateTime('NOW', new DateTimeZone('America/Los_Angeles'));
                        if( isset($response->transactionid) ){
                            $data = array( 'message' => $array->description, 'html' => 'Failed', 'date' => $curdate->format('m/d/Y h:i:s a'), 'transid' => $array->transactionid );
                        } else{
                            $data = array( 'message' => $array->description, 'html' => 'Failed, Not seeing Success or TransactionID', 'date' => $curdate->format('m/d/Y h:i:s a') );
                        }
                    }
                }

                /*    if ($xml === false) {
                        $data = array('message' => $response, 'html' => $response);
                        file_put_contents("whereDataFalse.txt", $data);
                    } else {
                        // $data = array('message' => $array[0], 'html' => $array[0]);
                        if ($xml->transaction->status == "Success") {
                            // Transaction approved! Do your logic here.

                            $curdate = new DateTime('NOW', new DateTimeZone('America/Los_Angeles'));
                            if( isset($xml->transaction->transactionid) ){
                                $data = array( 'message' => 'Success', 'html' => 'Success', 'transid' => $xml->transaction->transactionid, 'auth' => $xml->transaction->transactionid, 'date' => $curdate->format('m/d/Y h:i:s a') );
                            } else{
                                $data = array( 'message' => 'Success', 'html' => 'Success', 'transid' => $xml->transaction->transactionid, 'date' => $curdate->format('m/d/Y h:i:s a') );
                            }

                        } else {
                            $curdate = new DateTime('NOW', new DateTimeZone('America/Los_Angeles'));
                            if( isset($xml->transaction->transactionid) ){
                                $data = array( 'message' => $xml->transaction->description, 'html' => 'Failed', 'date' => $curdate->format('m/d/Y h:i:s a'), 'transid' => $xml->transaction->transactionid );
                            } else{
                                $data = array( 'message' => $xml->transaction->description, 'html' => 'Failed, Not seeing Success or TransactionID', 'date' => $curdate->format('m/d/Y h:i:s a') );
                            }
                        }
                    } */
                echo json_encode($data);
   
                die();
            }

JS Code sample of alert handler:
var response = jQuery.parseJSON(data);
          // var response = data;
          console.log(response);
        if( response.message == 'Success' && response.html != 'Failed' && response.html != '' ) {
          
          jQuery('#lead_item_full_trans').hide();
          
          if( response.transid ){
            jQuery('#sales_form input#trans_id').val(response.transid);
          }
          if( response.date ){
            jQuery('#sales_form input#trans_date').val(response.date);
          }
          if( response.auth ){
            jQuery('#sales_form input[name=v_auth]').val(response.auth);
          }

          jQuery('#sales_form input#trans_type').val('Charge');
          alert('Card has been successfully charged.');
        } else {
          if( response.transid ){
            jQuery('#sales_form input#trans_id').val(response.transid);
          }
          if( response.message ){
            jQuery('#sales_form input#trans_note').val(response.message);
          }
          if( response.date ){
            jQuery('#sales_form input#trans_date').val(response.date);
          }
          jQuery('#sales_form input#trans_type').val('Error');
          alert(response.message);
        }
      });
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros