Avatar of Chris Inman
Chris Inman
 asked on

Need PHP help with parsing an XML cURL response

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

)

Open in new window


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

)

Open in new window


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();
            }

Open in new window


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);
        }
      });

Open in new window

* arraysPHPXML

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Chris Inman

ASKER
Thank you for the awesome response. I will attempt the changes over the weekend and let you know how it turns out.
Thank you, thank you!
Chris Inman

ASKER
Chris,
Phenomenal response as I actually was able to understand it as you explained and walked through it, even at my novice level.
Getting rid of the array conversion altogether was great and converting the xml data to string worked like a charm for the json.

With some other minor tweaks, I have this bad boy running pretty good now.
I still have a minor issue with the vendors end, but that is on them to fix.

Many Thanks to you Sir!

~Chris
Chris Stanyon

No worries Chris,

Pleased you got it working, but more importantly, I'm pleased you know why you got it working.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck