0

I guess this question is not a Sage Pay specific question, although it's being mentioned in relation to my problem, so hope you're able to help me with this tricky issue.

Here's what I want to do:

  • The main task is to retrieve data from the Sage Pay Reporting API - works like a charm
  • Read and collect the variables from the xml output file - not working

The code below does show the list of transactions after redirecting me to the Sage Pay site but I just can't figure out how to get e.g. the amount of a single transaction and store it.

Here is what the xml output looks like:

Any answers/solutions? I appreciate your help!

<?

$command = 'getTransactionList';
$vendor = 'vendorname';
$user = 'username';
$password = 'pwd';
$startdate = '01/05/2013 00:00:01';
$enddate = '31/05/2013 23:59:59';

$string = '<command>'.$command.'</command><vendor>'.$vendor.'</vendor><user>'.$user.'</user><startdate>'.$startdate.'</startdate><enddate>'.$enddate.'</enddate>';


$crypt = MD5($string . '<password>' . $password . '</password>');

$curl = curl_init('https://test.sagepay.com/access/access.htm'); 
curl_setopt($curl, CURLOPT_FAILONERROR, true); 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);   
$result = curl_exec($curl); 

$rxml = simplexml_load_string($result);
echo $rxml->transactions->transaction[0]->amount;

?>

<HTML>

<BODY>


<form method="post" action="https://test.sagepay.com/access/access.htm">

<input type="hidden" name="XML" value="<vspaccess><?php echo $string; ?><signature><?php echo $crypt; ?></signature></vspaccess>">
<input type="submit" name="Button" value="Send">

</form>
</BODY>
</HTML>
gtgaxiola
  • 9,241
  • 5
  • 42
  • 64
Kleni
  • 3
  • 4
  • Show the XML you're getting in `$result` (remove any financial or personal data). – MrCode Jun 11 '13 at 14:38
  • If you want to parse XML, the important bit is not how you connect to an external computer to download the data but how the downloaded XML data looks like (which you don't say). – Álvaro González Jun 11 '13 at 14:40
  • Thank you MrCode and Álvaro. I already had the output in my posting when I noticed that I need to have a reputation of 10 to get it shown. Will try it some other way... – Kleni Jun 11 '13 at 15:28
  • You will now find the output xml format through the link above. – Kleni Jun 11 '13 at 15:41
  • Capture all 'amounts' for all transactions listed or just the first transaction? and what do you mean by **store**? In a DB/file/cookie/session? Does the amount `echo` when you run your code? – nickhar Jun 12 '13 at 18:22
  • Can you clarify which bits of your code are running successfully and which aren't? Your test form appears to submit a slightly different string from your CURL code, so does `$result` contain what you expect? If so, does `$rxml` contain a SimpleXMLElement or is there an error parsing the XML? You could try using [one of these debug functions](https://github.com/IMSoP/simplexml_debug) to echo out the SimpleXMLElement (`print_r` will probably do, but can be slightly misleading for SimpleXML). – IMSoP Jun 12 '13 at 18:36
  • Thanks nickhar and IMSoP. The code opens up the result page but never echoes the result of transaction[0]. – Kleni Jun 20 '13 at 14:01
  • Sorry to scope creep and I know this is a fairly old topic, but according to the official Reporting and Admin API Integration Guide the form should have the following enctype: `enctype="application/x-www-form-urlencoded"`. I thought it would be good knowledge to share in case others reuse your code. – Arbiter Apr 01 '14 at 00:38

1 Answers1

1

Replace $rxml = simplexml_load_string($result);

with

$rxml = new SimpleXMLElement($result);

Chirag B
  • 2,106
  • 2
  • 20
  • 35