0

I trying to setup the sagepay reporting api to query transactions.

I got some code from another question on here which almost gets me all the way, but what I want to do is save the XML returned and store it in a database. How would I do this?

<?php

$command = 'getTransactionList';
$vendor = 'vendor';
$user = 'user';
$password = 'password';
$startdate = '01/10/2015 00:00:01';
$enddate = '31/10/2015 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 = new SimpleXMLElement($result);

?>

<html>
<body>

<form method="post" action="https://test.sagepay.com/access/access.htm" enctype="application/x-www-form-urlencoded">
<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>

Thanks for any help you can give.

deceze
  • 510,633
  • 85
  • 743
  • 889

1 Answers1

3

Answer below if anyone is interested.

<?php

$command = 'getTransactionList';
$vendor = 'vendor';
$user = 'user';
$password = 'password';
$startdate = '20/10/2015 00:00:01';
$enddate = '22/10/2015 23:59:59';

$string = '<command>'.$command.'</command><vendor>'.$vendor.'</vendor><user>'.$user.'</user><startdate>'.$startdate.'</startdate><enddate>'.    $enddate.'</enddate><txtypes><txtype>PAYMENT</txtype></txtypes><result>failure</result>';

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

$xml = 'XML=<vspaccess>'.$string.'<signature>'.$crypt.'</signature></vspaccess>';

$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_VERIFYPEER, false);   
curl_setopt($curl, CURLOPT_POST, true);   
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
$result = curl_exec($curl); 

$rxml = new SimpleXMLElement($result);

foreach($rxml->transactions->transaction as $transx)
{
    echo '<p>Payment Type = '.$transx->transactiontype.'</p>';
    echo '<p>Amount = '.$transx->amount.'</p>';
    echo '<p>Name = '.$transx->cardholder.'</p>';
    echo '<p>Result = '.$transx->result.'</p>';
}

?>

I've just echoed the information on screen, however you can take the data returned and store in a database, run a query, etc..