<?php
error_reporting(E_ALL);
ini_set("display_errors",'on');

function getToken($url = 'http://cre8mania.ch/projects2013/moka/web/api/card/generatetoken/'){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,  $url);
    //curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $result = curl_exec($ch);
    $curl_errno = curl_errno($ch);
    $curl_error = curl_error($ch);
    curl_close($ch);

    if ($result ) {
        $result = new SimpleXMLElement($result);
        $result = $result[0];

        $now = $result;
        $half = strlen ( $now ) / 2;
        $left = substr ( $now, 0, $half );
        $right = substr ( $now, $half );
        $thgir = strrev ( $right );
        $tfel = strrev ( $left );
        $newnow = "$tfel$thgir";
        return $token = md5 ( $newnow );
    }else {
        return false;
    }
}


function apiCall($function='', $data='', $token, $url = 'http://cre8mania.ch/projects2013/moka/web/api/card/') {

    $stringData = '';
    if ($data) {
        $stringData = implode('/',$data) .'/';

        $stringData = str_replace('tokenreplace',$token,$stringData);
    }
    $apiCall = $url.$function.'/'.$stringData;

    echo "<b>Token:</b> $token<br><b>Call:</b> ".$apiCall . "<br>";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,  $apiCall);
    //curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $result = curl_exec($ch);
    $curl_errno = curl_errno($ch);
    $curl_error = curl_error($ch);
    curl_close($ch);

    if ($result ) {
        $result = new SimpleXMLElement($result);
        $result = $result[0];
    }

    if ($curl_errno > 0) {
        echo "cURL Error ($curl_errno): $curl_error <br>";
        return false;
    } else {
        echo "Data received:<br /> $result <br><br>";
        return true;
    }
}



$cardid = 'fea7201555729e7326bca530a3e2e4a9';
$currency = 'lbp';
$amount = '15000';
$country_id = 1; // lebanon
$operation_id = 1;// moka & more
$pos = '1';
$employeeid = 'unit_test';

$tests =
array (
    'purchasingpower' =>  array($cardid,'tokenreplace',$currency), // id, token, currency

    'checkpointsbalance' =>  array($cardid,'tokenreplace'), // id, token

    'registerpoints' => array($cardid, $pos, $employeeid, $amount, 'tokenreplace', $currency , $country_id, $operation_id),

    'redeempoints' =>   array($cardid, $pos, $employeeid, $amount, 'tokenreplace', $currency , $country_id, $operation_id),

    'balance' =>        array($cardid, 'tokenreplace', $currency),

     'buy' =>           array($cardid, $pos, $employeeid, $amount, 'tokenreplace', $currency , $country_id, $operation_id ),

     'addfunds' =>      array($cardid, $amount,'tokenreplace', $currency )
);


foreach ($tests AS $test=>$data){
    echo "<h3>$test</h3>";
    $token = getToken();
    if ($token) {
        apiCall($test,$data,$token);
    }
}


