<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

/**
 * Description of checkorder
 *
 * @author Igor Ratsun
 */
class checkorder extends MX_Controller
{

    public function index()
    {
        $conf = $this->load->config('yandex_kassa/config', TRUE);
        
        $this->load->model($conf['yk_order_model'], 'yk_order_model');
        $orderSumAmount = number_format($this->yk_order_model->get_order_amount($this->input->post('orderNumber')), 2, '.', '');
        
        // Prepare POST
        $action = $this->input->post('action');
        $orderSumCurrencyPaycash = $this->input->post('orderSumCurrencyPaycash');
        $orderSumBankPaycash = $this->input->post('orderSumBankPaycash');
        $invoiceId = $this->input->post('invoiceId');
        $customerNumber = $this->input->post('customerNumber');
        $md5 = $this->input->post('md5');
        $requestDatetime = $this->input->post('requestDatetime');

        $hash = md5($action . ';' . $orderSumAmount . ';' . $orderSumCurrencyPaycash . ';' . $orderSumBankPaycash . ';' . $conf['shopId'] . ';' . $invoiceId . ';' . $customerNumber . ';' . $conf['ShopPassword']);
        if (strtolower($hash) != strtolower($md5)) {
            $code = 1;
        } else {
            $code = 0;
        }
        $data = '<?xml version="1.0" encoding="UTF-8"?>'
                . '<checkOrderResponse performedDatetime="' . $requestDatetime . '" code="' . $code . '"' . ' invoiceId="' . $invoiceId . '" shopId="' . $conf['shopId'] . '"/>';
        
        logger_add('Yandex kassa. checkorder', array(
            'post' => $this->input->post(),
            'data' => $data,
            'uri' => $this->uri->uri_string(),
            'orderSumAmount' => $orderSumAmount,
        ));
        
        $this->output->set_output($data);
    }
    
    public function test()
    {
        return $this->index();
    }

}
