<?php

defined(IN_PARSER_MODE) or die();

//System configuration (modx)
$modx_tb_prefix = $modx->db->config['table_prefix'];
$modx_base_dir = $modx->config['rb_base_dir'];
$modx_charset = $modx->config['modx_charset'];
$modx_userLogged =  isset($_SESSION['webValidated']) ? true : false;
$modx_thisPage = $modx->makeUrl($modx->documentIdentifier, '', '', 'full');
$modx_thisURL = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$modx_qs = strpos($modx_thisURL, "?")===false ? '?' : '&amp;';
$modx_SHK_table = $modx_tb_prefix."manager_shopkeeper";

//path to tmgco snippet dir
define('TMGCO_PATH', $modx_base_dir . "snippets/tmgco");

//helper functions
require_once(TMGCO_PATH . "/tmgco_helper_functions.inc.php");

//Templates options
$template['tmgco']['startpay'] = isset($TMGCOstartPayTpl) ? $TMGCOstartPayTpl : '@FILE:assets/snippets/tmgco/tpl/tmgcostartpaytpl.tpl';

//load tmgco config or return
$tmgco_config_file = TMGCO_PATH . "/config.php";
$tmgco_config = array();
if(file_exists($tmgco_config_file)) {
    $tmgco_config = require($tmgco_config_file);
} else {
    return;
}


$payment_method = isset($payment_method) ? $payment_method : (isset($_SESSION['shk_payment_method']) ? $_SESSION['shk_payment_method'] : '');
$payment_method = mb_detect_encoding($payment_method,"UTF-8",true)!==false ? mb_strtolower($payment_method,"UTF-8") : strtolower($payment_method);
$payment_modx_orderid = isset($payment_modx_orderid) ? $payment_modx_orderid : (isset($_SESSION['shk_order_id']) ? $_SESSION['shk_order_id'] : '');
$payment_modx_userid = isset($payment_modx_userid) ? $payment_modx_userid : (isset($_SESSION['shk_order_user_id']) ? $_SESSION['shk_order_user_id'] : '');
$payment_useremail = isset($payment_useremail) ? $payment_useremail : (isset($_SESSION['shk_order_user_email']) ? $_SESSION['shk_order_user_email'] : '');
$payment_price = isset($payment_price) ? $payment_price : (isset($_SESSION['shk_order_price']) ? $_SESSION['shk_order_price'] : '');
$payment_currency = isset($payment_currency) ? $payment_currency : (isset($_SESSION['shk_currency']) ? $_SESSION['shk_currency'] : '');

$the_phone = trim($modx->db->getValue($modx->db->select("phone",$modx_SHK_table,"id='$payment_modx_orderid'")));

//if not set payment method then return
if(empty($payment_method) || $payment_method != 'tmgco') return;

//set price of the payment
$payment_price = isset($payment_price) ? number_format(floatval($payment_price),2,'.','') : '';

//check if user logged in
if($modx_userLogged){
    $modx_user = $modx->userLoggedIn();
    $modx_webuser = $modx->getWebUserInfo($modx_user['id']);
} 
//set modx_userid
$modx_userid = !empty($payment_modx_userid) ? $payment_modx_userid : (isset($modx_user) ? $modx_user['id'] : 0);


$output = '';

//determine current action
$action = 'pay_start';

switch($action) {
    case 'pay_start': //initializing payment

$the_clientid	= $payment_modx_userid;	
$the_sum	= $payment_price;
$the_orderid	= $payment_modx_orderid;

 

        $formdata = array(
        "phone" 	=> $the_phone,
        "clientid"	=> $the_clientid,
        "sum"		=> $the_sum,
        "orderid"	=> $the_orderid
        );

        //build the post string
        $poststring = "";
        foreach($formdata AS $key => $val){
            $poststring .= urlencode($key) . "=" . urlencode($val) . "&";
        }
        // strip off trailing ampersand
        $poststring = substr($poststring, 0, -1);

       	$url = $tmgco_config['tmgco_payment_form_url'];

        if(function_exists( "curl_init" )) {
            $CR = curl_init();
            curl_setopt($CR, CURLOPT_URL, $url);
            curl_setopt($CR, CURLOPT_POST, 1);
            curl_setopt($CR, CURLOPT_FAILONERROR, true); 
            curl_setopt($CR, CURLOPT_POSTFIELDS, $poststring);
            curl_setopt($CR, CURLOPT_RETURNTRANSFER, 1);
             
            curl_setopt($CR, CURLOPT_SSL_VERIFYPEER, 0);
            
            $the_payment_form = curl_exec( $CR );
            
            $the_error = curl_error( $CR );
            if($the_error)
              $error = $the_error;

            curl_close( $CR );
        } else{
          $error = "error: curl_init doesn't exist";
        }
        
        if (isset($error) && $error) {
            $output = $error;
            return;
        }

$the_payment_form = <<<endi
<link type="text/css" href="/assets/snippets/tmgco/style.css" rel="stylesheet" />	 
<div id="extpaymentform">
$the_payment_form 
</div>
endi;

        $chunk = fetchTpl($template[$payment_method]['startpay']);
        $chunkArr = array(
            "payment_form" => $the_payment_form,
        );

        foreach ($chunkArr as $key => $value){
            $chunk = str_replace('[+'.$key.'+]', $value, $chunk);
        }
        unset($key,$value);
        $output .= $chunk;
        break;
}

?>
