<?php

// Define end point URL
$url     = "https://tls.bankaccountchecker.com/";
$service = "listener.php";

// Define parameters for all tests
$key         = "apiTest";
$password    = "apiTest";
$output      = "xml";
$type        = "uk_bankaccount";

// Start callAPI
callAPI("1","Pass modulus 10 check.","01",$key,$password,$output,"uk","089999","66374958");
callAPI("2","Pass modulus 11 check.","01",$key,$password,$output,"uk","107999","88837491");
callAPI("3","Pass modulus 11 and double alternate checks.","01",$key,$password,$output,"uk","202959","63748472");
callAPI("4","Exception 10 & 11 where first check passes and second check fails.","01",$key,$password,$output,"uk","871427","46238510");
callAPI("5","Exception 10 & 11 where first check fails and second check passes.","01",$key,$password,$output,"uk","872427","46238510");
callAPI("6","Exception 10 where in the account number ab=09 and the g=9. The first check passes and second check fails.","01",$key,$password,$output,"uk","871427","09123496");
callAPI("7","Exception 10 where in the account number ab=99 and the g=9. The first check passes and the second check fails.","01",$key,$password,$output,"uk","871427","99123496");
callAPI("8","Exception 3, and the sorting code is the start of a range. As c=6 the second check should be ignored.","01",$key,$password,$output,"uk","820000","73688637");
callAPI("9","Exception 3, and the sorting code is the end of a range. As c=9 the second check should be ignored.","01",$key,$password,$output,"uk","827999","73988638");
callAPI("10","Exception 3. As c6 or 9 perform both checks pass.","01",$key,$password,$output,"uk","827101","28748352");
callAPI("11","Exception 4 where the remainder is equal to the checkdigit.","01",$key,$password,$output,"uk","134020","63849203");
callAPI("12","Exception 1 – ensures that 27 has been added to the accumulated total and passes double alternate modulus check.","01",$key,$password,$output,"uk","118765","64371389");
callAPI("13","Exception 6 where the account fails standard check but is a foreign currency account.","01",$key,$password,$output,"uk","200915","41011166");
callAPI("14","Exception 5 where the check passes.","01",$key,$password,$output,"uk","938611","07806039");
callAPI("15","Exception 5 where the check passes with substitution.","01",$key,$password,$output,"uk","938600","42368003");
callAPI("16","Exception 5 where both checks produce a remainder of 0 and pass.","01",$key,$password,$output,"uk","938063","55065200");
callAPI("17","Exception 7 where passes but would fail the standard check.","01",$key,$password,$output,"uk","772798","99345694");
callAPI("18","Exception 8 where the check passes.","01",$key,$password,$output,"uk","086090","06774744");
callAPI("19","Exception 2 & 9 where the first check passes and second check fails.","01",$key,$password,$output,"uk","309070","02355688");
callAPI("20","Exception 2 & 9 where the first check fails and second check passes with substitution.","01",$key,$password,$output,"uk","309070","12345668");
callAPI("21","Exception 2 & 9 where a?0 and g?9 and passes.","01",$key,$password,$output,"uk","309070","12345677");
callAPI("22","Exception 2 & 9 where a?0 and g=9 and passes.","01",$key,$password,$output,"uk","309070","99345694");
callAPI("23","Exception 5 where the first checkdigit is correct and the second incorrect.","02",$key,$password,$output,"uk","938063","15764273");
callAPI("24","Exception 5 where the first checkdigit is incorrect and the second correct.","02",$key,$password,$output,"uk","938063","15764264");
callAPI("25","Exception 5 where the first checkdigit is incorrect with a remainder of 1.","02",$key,$password,$output,"uk","938063","15763217");
callAPI("26","Exception 1 where it fails double alternate check.","02",$key,$password,$output,"uk","118765","64371388");
callAPI("27","Pass modulus 11 check and fail double alternate check.","02",$key,$password,$output,"uk","203099","66831036");
callAPI("28","Fail modulus 11 check and pass double alternate check.","02",$key,$password,$output,"uk","203099","58716970");
callAPI("29","Fail modulus 10 check.","02",$key,$password,$output,"uk","089999","66374959");
callAPI("30","Fail modulus 11 check.","02",$key,$password,$output,"uk","107999","88837493");
callAPI("31","Exception 12/13 where passes modulus 11 check (in this example, modulus 10 check fails, however, there is no need for it to be performed as the first check passed).","01",$key,$password,$output,"uk","074456","12345112");
callAPI("32","Exception 12/13 where passes the modulus 11check (in this example, modulus 10 check passes as well, however, there is no need for it to be performed as the first check passed).","01",$key,$password,$output,"uk","070116","34012583");
callAPI("33","Exception 12/13 where fails the modulus 11 check, but passes the modulus 10 check.","01",$key,$password,$output,"uk","074456","11104102");
callAPI("34","Exception 14 where the first check fails and the second check passes.","01",$key,$password,$output,"uk","180002","00000190");

// End callAPI

function callAPI($testId,$testDescription,$expectedResult,$key,$password,$output,$type,$sortcode,$bankaccount) {

global $url,$service;

// Output Test details
echo "====================================================================\r\n";
echo "Test ID : $testId\r\n";
echo "Sort code : $sortcode\r\n";
echo "Bank account : $bankaccount\r\n";
echo "Test Description : $testDescription\r\n";
echo "Expected Result : $expectedResult\r\n\r\n";

// Call with Post with url encoded string post parameters
$ch = curl_init() ;
curl_setopt($ch, CURLOPT_URL,$url . $service);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "key=$key&password=$password&output=$output&type=$type&sortcode=$sortcode&bankaccount=$bankaccount");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch) . "\r\n";
    $info = curl_getinfo($ch);
    print_r($info);
    curl_close($ch);
    return;
}

curl_close($ch);

echo "Call with POST with url encoded string\r\nResult:\r\n";
echo $result . "\r\n";


// Analyse result
switch($output) {
    case "xml" :
        $resultXml = simplexml_load_string($result);
        if ($resultXml === false) {
            $resultCode = "XX";
            $resultDescription = "Invalid xml returned";
        }
        else {
            $resultCode = $resultXml->resultCode;
            $resultDescription = $resultXml->resultDescription;
        }
        break;
}

// Display Result
echo "Result Code : " . $resultCode . "\r\n";
echo "Result Dewscription : " . $resultDescription . "\r\n";
if ($expectedResult != $resultXml->resultCode) {
    echo "*** ERROR ***\r\n";
}
echo "\r\n";

//=====================================================================
// Call with Post with post parameters in array
$ch = curl_init() ;
curl_setopt($ch, CURLOPT_URL,$url . $service);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('key' => $key , 'password' => $password , 'output' => $output , 'type' => $type , 'sortcode' => $sortcode , 'bankaccount' => $bankaccount ));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Required to remove "Expect: 100-continue" from the header sent
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));

$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch) . "\r\n";
    $info = curl_getinfo($ch);
    print_r($info);
    curl_close($ch);
    return;
}

curl_close($ch);

echo "Call with POST with array\r\nResult:\r\n";
echo $result . "\r\n";

// Analyse result
switch($output) {
    case "xml" :
        $resultXml = simplexml_load_string($result);
        if ($resultXml === false) {
            $resultCode = "XX";
            $resultDescription = "Invalid xml returned";
        }
        else {
            $resultCode = $resultXml->resultCode;
            $resultDescription = $resultXml->resultDescription;
        }
        break;
}

// Display Result
echo "Result Code : " . $resultCode . "\r\n";
echo "Result Dewscription : " . $resultDescription . "\r\n";
if ($expectedResult != $resultXml->resultCode) {
    echo "*** ERROR ***\r\n";
}
echo "\r\n";

//=====================================================================
// Call with Get
$ch = curl_init() ;
curl_setopt($ch, CURLOPT_URL,$url . $service . "?key=$key&password=$password&output=$output&type=$type&sortcode=$sortcode&bankaccount=$bankaccount");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$result = curl_exec($ch);

if (curl_errno($ch)) {
    echo 'Curl error: ' . curl_error($ch) . "\r\n";
    $info = curl_getinfo($ch);
    print_r($info);
    curl_close($ch);
    return;
}

curl_close($ch);

echo "Call with GET\r\nResult:\r\n";
echo $result . "\r\n";

// Analyse result
switch($output) {
    case "xml" :
        $resultXml = simplexml_load_string($result);
        if ($resultXml === false) {
            $resultCode = "XX";
            $resultDescription = "Invalid xml returned";
        }
        else {
            $resultCode = $resultXml->resultCode;
            $resultDescription = $resultXml->resultDescription;
        }
        break;
}

// Display Result
echo "Result Code : " . $resultCode . "\r\n";
echo "Result Dewscription : " . $resultDescription . "\r\n";
if ($expectedResult != $resultXml->resultCode) {
    echo "*** ERROR ***\r\n";
}
echo "\r\n";

}
?>
