<?php
class Total_Kadobon_CheckoutController extends Mage_Core_Controller_Front_Action
{
    function redirectAction()
    {
        $api_url='https://www.kadobon-online.nl/orderpayment.aspx';
        
        // Alogoritme check
        if(function_exists("hash_algos"))
        {
            if(!in_array("sha512",hash_algos()))
            {
                //error, sha512 not available on this server
                echo "PHP 5 is needed for function hash() with sha512.";
                exit();
            }
        }
        else 
        {
            //not possible to check
            echo "PHP 5 is needed for function hash() with sha512.";
            exit();
        }
        

        $order_id=Mage::getSingleton('checkout/session')->getLastRealOrderId();
        $order=Mage::getModel('sales/order')->loadByIncrementId($order_id);
        
        $amount= $order->getGrandTotal();
    
        // kadobon accepts only numbers with 2 digits in decimal part
        // and it need only ',' as separator of parts
        $amount=number_format($amount, 2, ',','');
        
        // calculate signature
        $signature = hash("sha512",
            Mage::getStoreConfig('payment/kadobon/merchant_key') .
            "-" .
            $order_id .
            "-" .
            $amount .
            Mage::getStoreConfig('payment/kadobon/saltcode') 
        );
        
        $signature = strtoupper($signature);
        
        //redirect url to kadobon-online
        $redirect_url = $api_url."?merchantId=".Mage::getStoreConfig('payment/kadobon/merchant_key')."&reference=".$order_id."&value=".$amount."&sign=".$signature;

        return $this->_redirectUrl($redirect_url);
    }
}