<?php
require 'config.php';
require 'functions.php';


// clear special characters
function str_without_accents($str, $charset='utf-8'){
	$str = htmlentities($str, ENT_NOQUOTES, $charset);
	$str = preg_replace('#&([A-za-z])(?:acute|cedil|caron|circ|grave|orn|ring|slash|th|tilde|uml);#', '\1', $str);
	$str = preg_replace('#&([A-za-z]{2})(?:lig);#', '\1', $str);
	$str = preg_replace('#&[^;]+;#', '', $str);
	$str = preg_replace('/[^a-zA-Z0-9_ -]/s','',$str);
	return $str;
}
// $fp = fopen('logs.log', 'a+');			   
// fwrite($fp,'--------------  Start  --------------'."\n");
// fwrite($fp,'Data POST: ' . http_build_query($_POST)."\n");
// fwrite($fp,'Data GET: ' . http_build_query($_GET)."\n");

$orderId = filter_var($_GET['oid'], FILTER_VALIDATE_INT);
// fwrite($fp,'orderId: ' . $orderId ."\n");

$orderUrl = "https://{$settings['shopify-store-domain']}/admin/orders/$orderId.json";
$token_access = $settings['shopify-token-access'];
// fwrite($fp,'orderUrl: ' . $orderUrl ."\n");
$get_data = callAPI('GET', $orderUrl, false, $token_access );
// fwrite($fp,'get_data: ' . $get_data ."\n");
$response = json_decode($get_data);
$lang = $_GET['lang'];

$fdata = array();
$fdata['nid']= $orderId;
$fdata['clientid']= $settings['cmi-clientId'];
$fdata['oid']= $response->order->order_number;
$fdata['amount']= $response->order->total_price;
$currentCur = $response->order->currency;
if(isset($_GET['storetype']) && !empty($_GET['storetype']) && $_GET['storetype'] == "offshore"){
		$fdata['currency']= getCurrencyNumericByCode($currentCur);
	} else {
		$currMad = "MAD";
		$currMadCode = "504";
		$currentCur = $response->order->currency;
		$fdata['currency']= $currMadCode;
		if($currentCur != $currMad){
			if(isset($_GET['currency_rate']) && !empty($_GET['currency_rate'])){
				$fdata['amount']= number_format(floatval(($response->order->total_price*1) / $_GET['currency_rate']), 2, '.', '' );
				$fdata['symbolCur']= $currentCur;
				$fdata['amountCur']= $response->order->total_price;
				$fdata['DIMCRITERIA6']= $response->order->total_price.' '.$currentCur;
			}
		}
	}
$fdata['okUrl']= $response->order->order_status_url;
//$fdata['failUrl']= $response->order->referring_site;
//$fdata['shopurl']= $response->order->referring_site;

$fdata['failUrl']= "http://".parse_url($fdata['okUrl'], PHP_URL_HOST);
$fdata['shopurl']= "http://".parse_url($fdata['okUrl'], PHP_URL_HOST);

$fdata['callbackUrl']= $settings['ProxyDomain'].'/proxy.php';
$fdata['refreshtime']= '5';
switch ($lang) {
	case "en":
	case "fr":
	case "ar":
		 $fdata['lang'] = $lang;
	break;
	default:
		 $fdata['lang'] = "en";
}
$fdata['BillToName'] = trim(str_without_accents($response->order->billing_address->name));
$fdata['BillToStreet1'] = trim(str_without_accents($response->order->billing_address->address1.' '.$response->order->billing_address->address2));
$fdata['BillToCompany']= trim(str_without_accents($response->order->billing_address->company));
$fdata['BillToCity']= trim(str_without_accents($response->order->billing_address->city));
$fdata['BillToStateProv']= trim(str_without_accents($response->order->billing_address->province_code));
$fdata['BillToPostalCode']= trim(str_without_accents($response->order->billing_address->zip));
$fdata['BillToCountry']= trim(str_without_accents($response->order->billing_address->country_code));
$fdata['email']= trim($response->order->customer->email);
$fdata['tel']= trim($response->order->billing_address->phone);
$fdata['encoding']= 'UTF-8';
$fdata['TranType']= 'PreAuth';
$fdata['rnd']= microtime();
$fdata['storetype']= '3D_PAY_HOSTING';
$fdata['hashAlgorithm']= 'ver3';
$fdata['DIMCRITERIA1']= explode(".", $settings['shopify-store-domain'])[0];
$fdata['mid']= $mid;
$fdata['mid']= $mid;

// fwrite($fp,'formPay: ' . urldecode(http_build_query($fdata))."\n");

echo  formPay($fdata, $mid,$settings['cmi-storeKey'],$settings['env']);
?>