<?php

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

/**
 * Description of paymentaviso
 *
 * @author Igor Ratsun
 */
class paymentaviso 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, '.', '');

        $hash = md5($this->input->post('action') . ';' . $orderSumAmount . ';' . $this->input->post('orderSumCurrencyPaycash') . ';' . $this->input->post('orderSumBankPaycash') . ';' . $conf['shopId'] . ';' . $this->input->post('invoiceId') . ';' . $this->input->post('customerNumber') . ';' . $conf['ShopPassword']);
        if (strtolower($hash) != strtolower($this->input->post('md5'))) {
            $code = 1;
        } else {
            $code = 0;

            // Update order
            $this->load->model('yandex_kassa/yk_order_model');
            $this->yk_order_model->update_order($this->input->post('orderNumber'));

            logger_add('Yandex kassa. paymentaviso. Update order', array(
                'orderNumber'      => $this->input->post('orderNumber'),
                'uri'              => $this->uri->uri_string(),
                'enable_demo_mode' => $conf['enable_demo_mode'],
            ));
        }
        $data = '<?xml version="1.0" encoding="UTF-8"?>'
                . '<paymentAvisoResponse performedDatetime="' . $this->input->post('requestDatetime') . '" code="' . $code . '" invoiceId="' . $this->input->post('invoiceId') . '" shopId="' . $conf['shopId'] . '"/>';

        logger_add('Yandex kassa. paymentaviso', array(
            'post'             => $this->input->post(),
            'data'             => $data,
            'uri'              => $this->uri->uri_string(),
            'order_sum_amount' => $orderSumAmount,
            'enable_demo_mode' => $conf['enable_demo_mode'],
        ));

        $this->output->set_output($data);
    }

    public function test()
    {
        return $this->index();
    }

}
