<?php
/**
 * Marketplace - Component Marketplace 
 * ******************************************************************
 *
 * PHP version 7.0
 *
 * @category   Component
 * @package    Joomla
 * @author     WebKul software private limited <support@webkul.com>
 * @copyright  2010 WebKul software private limited
 * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @version    GIT:5.2
 * @filesource http://store.webkul.com
 * @link       Technical Support:  webkul.uvdesk.com
 * ******************************************************************
 */
// no direct access
defined('_JEXEC') or die('Restricted access');
$amount_recieved=$amount_remain=$amount_paid=0;
$app=JFactory::getApplication();
$_input = $app->input;
$seller_id = $_input->getInt('seller_id', 0);
$amount_pay = $_input->get('amount', 0, 'FLOAT');
$paymode=$_input->get('pay_mode');

$ins=JUri::getInstance();
if ($paymode=='remitasplit') {
    $rrr=$app->input->get('RRR');
    $orderid=$app->input->get('orderID');
    $model=$this->getModel('commisions');
    $params=$model->getRemitaParams();
    $remita_payment_type= $params['remita_payment_type'];  
    if (!$remita_payment_type) {
        $data['remita_mercid'] = trim($params['remita_test_merchant_id']);
        $data['remita_servicetypeid'] = trim($params['remita_test_servicetype_id']);
        $data['remita_apikey'] = trim($params['remita_test_api_key']);         
        $data['status_url'] = 'http://www.remitademo.net/remita/ecomm';
    } else {
        $data['remita_mercid'] = trim($params['remita_live_merchant_id']);
        $data['remita_servicetypeid'] = trim($params['remita_live_servicetype_id']);
        $data['remita_apikey'] = trim($params['remita_live_api_key']);  
        $data['status_url']  = 'https://login.remita.net/remita/ecomm';
    }
    /**
     * [$status_string concat string to genrate hash value for status check]
     */
    $status_string = $orderid.$data['remita_apikey'] .$data['remita_mercid'];
    $hash = hash('sha512', $status_string);
    $status_url  = $data['status_url'] . '/' .
        $data['remita_mercid']  . '/' . $orderid . '/' . $hash .
        '/' . 'orderstatus.reg';

    //  Initiate curl
    $ch = curl_init();
    /*// Disable SSL verification
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);*/
    // Will return the response, if false it print the response
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    // Set the url
    curl_setopt($ch, CURLOPT_URL, $status_url);
    // Execute
    $result=curl_exec($ch);
    // Closing
    curl_close($ch);

    $response = json_decode($result, true);
    $response_code = $response['status'];
    $new_status='';
    
    if ($response_code == '01' || $response_code == '00') {
        $app->enqueueMessage(
            $response['message'], JText::_('PAYMENT_RESPONSE_MESSAGE')
        );
        if ($amount_pay!='') {
            $commision_update=$model->updateSellerCommisionAmount(
                $amount_pay, $seller_id
            );
            $msg= JText::_('SUCCESSFUL_PAYMENT');
            $app->redirect(
                JURI::root().
                'administrator/index.php?option=com_marketplace&view=commisions',
                $msg
            );
        } else {
            $msg=JText::_('NOTHING_TO_PAY');
            $app->redirect(
                JURI::root().
                'administrator/index.php?option=com_marketplace&view=commisions',
                $msg, 'warning'
            );
        }
    } elseif ($response_code == '021') {
        $app->redirect(
            'index.php?option=com_marketplace&view=commisions',
            JText::_('REMITA_PAYMENT_PENDING'),
            'warning'
        );
        $app->close();
    } else {
        $app->redirect(
            'index.php?option=com_marketplace&view=commisions',
            JText::_('REMITA_TRANSACTION_FAILED').$response['message'],
            'error'
        );
        $app->close();
    }
} else {
    $totalAmountToPay=$_input->get('totalAmountToPay', 0);
    $totalCommission=$_input->get('commision_amount', 0);
    $model=$this->getModel('commisions');
    $res_comm =$model->successcommision(
        $seller_id, $amount_pay, $paymode, $totalAmountToPay, $totalCommission
    );
    if ($res_comm) {
        $msg= 'SUCCESSFUL_PAYMENT';
        $type='';
    } else {
        $msg='NOTHING_TO_PAY';
        $type='&type=warning';
    }
    $doc=JFactory::getDocument();
    $doc->addScriptDeclaration(
        'window.parent.location.href="'.JURI::base().
        '?option=com_marketplace&view=commisions&mess='.$msg.$type.'";'
    );
}
