<?php
/**
 * @package		Easyway Shop
 * @copyright	Copyright (C) 2005 - 2012 TA-EDV
 * @license		proprietary
 * @author		Richard Kammermayer <rk@ta-edv.de>
 * 
 * Easyway Shop is a web e-commerce system
 */


class Computop_billpayPaymentMethod {


	private $base_object;
	private $layout_object;


	private $customer_data;
	private $shipping_address;
	private $billing_address;
	private $shoppingcart;
	private $payment_method;
	private $shipping_method;
	private $gift_certificate;
	private $order_id;


	private $return_url;
	private	$cancel_url;


	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;

		$this->customer_data = false;
		$this->shipping_address = false;
		$this->billing_address = false;
		$this->shoppingcart = false;
		$this->payment_method = false;
		$this->shipping_method = false;
		$this->gift_certificate = false;
		$this->order_id = false;

		$this->return_url = 'http://'.$_SERVER['SERVER_NAME'].'/Kasse/';
		$this->cancel_url = $this->return_url.'&cancel=true';
	} // end __construct


	public function set_data($customer, $shipping, $billing, $shoppingcart, $delivery, $payment, $order_id = false) {
		$this->customer_data = $customer;
		$this->shipping_address = $shipping;
		$this->billing_address = $billing;
		$this->shoppingcart = $shoppingcart;
		$this->payment_method = $delivery;
		$this->shipping_method = $payment;
		$this->order_id = $order_id;
	} // end set_data


	public function set_gift_certificate($gift_certificate) {
		$this->gift_certificate = $gift_certificate;
	} // end set_gift_certificate


	public function set_order_id($order_id) {
		$this->order_id = $order_id;
	} // end set_order_id


	public function before_order() {

	} // end before_order


	public function after_order($order_total = false) {
		global $DEVMODE;

		if ($this->gift_certificate) {
			$order_total = $order_total - $this->gift_certificate->with_vat;
		}

		$order = new Order($this->base_object);

		$call = 'payment_open';
		$this->layout_object->assign('call', $call);

		$merchantID = $this->base_object->config->shopConfiguration['computop_creditcard_merchant_id'];
		$this->layout_object->assign('merchantID', $merchantID);
		$trans_id = '10001';
		$amount = intval($this->shoppingcart['order_total'] * 100);
		$currency = 'EUR';
		$url_success = 'https://'.$_SERVER['SERVER_NAME'].'/billpay_success/';
		$url_failure = 'https://'.$_SERVER['SERVER_NAME'].'/billpay_failed/';
		$url_notify = 'https://'.$_SERVER['SERVER_NAME'].'/billpay_failed/';
		$order_desc = 'meine Bestellung';
		$user_data = 'user';

		$uri_string = "TransID=".$trans_id.
			"&Amount=".$amount.
			"&Currency=".$currency.
			"&RefNr=12345".
			"&Language=de".
			"&NewCustomer=YES".
			"&BillPayAction=1".
			"&GtcValue=YES".

			"&bdSalutation=Herr".
			"&bdTitle=Dr".
			"&bdFirstName=Richard".
			"&bdLastName=Kammermayer".
			"&bdStreet=Osterham".
			"&bdStreetNr=54".
			"&bdZip=84082".
			"&bdCity=Laberweinting".
			"&bdCountryCode=DEU".
			"&EMail=richard_kammermayer@web.de".
			"&Phone=234567".
			"&MobileNr=4567890".

			"&UseBillingData=Yes"./*
			"&sdSalutation=".
			"&sdTitle=".
			"&sdFirstName=".
			"&sdLastName=".
			"&sdStreet=".
			"&sdStreet2=".
			"&sdStreetNr=".
			"&sdZip=".
			"&sdCity=".
			"&sdCountryCode=".
			"&sdPhone=".
			"&sdMobileNr=".*/
			"&DateOfBirth=1977-03-16".
			"&IPAddr=192.168.11.10".
			"&CompanyOrPerson=P".
			/*
			"&CompanyName=".
			"&LegalForm=".
			*/
			"&ArticleList=765443;1;ArtikelName;ArtikelBeschreibung;1000;1019".
			"&OrderDesc=DHL;0;0;0;0;1000;1019".
			"&OrderHistory=";
//		echo $uri_string;

		include_once './libs/ctPayGatePHP/includes/function.inc.php';
		$BlowFish = new ctBlowfish;
		//$crypt_string = $BlowFish->ctEncrypt($uri_string, strlen($uri_string), $this->base_object->config->shopConfiguration['computop_creditcard_crypt_password']);
		$crypt_return = $BlowFish->ctEncrypt($uri_string, $this->base_object->config->shopConfiguration['computop_creditcard_crypt_password']);
		$crypt_string = $crypt_return['data'];
		$this->layout_object->assign('crypt_string', $crypt_string);
		$this->layout_object->assign('string_length', $crypt_return['len']);

		// socket connection
		$ch = curl_init('https://www.computop-paygate.com/Billpay.aspx');
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/3.0 (compatible; MSIE 5.01; Windows NT 5.0)");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, 'merchantID='.$merchantID.'&Len='.$crypt_return['len'].'&Data='.$crypt_string);
		curl_setopt($ch, CURLOPT_AUTOREFERER, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, 40);
		$data = curl_exec($ch);
		curl_close($ch);

		//echo $data;
		$array_return = split('\&',$data);
		$result = array();
		foreach ($array_return as $arr1) {
			$arr2 = split('\=',$arr1);
			$result[$arr2[0]] = $arr2[1];
		}

		//echo var_dump($result);
		$entcrypted = $BlowFish->ctDecrypt($result['Data'], $result['Len'], $this->base_object->config->shopConfiguration['computop_creditcard_crypt_password']);
		//echo var_dump($entcrypted);
		$array_return = split('\&',$entcrypted);
		$result = array();
		foreach ($array_return as $arr1) {
			$arr2 = split('\=',$arr1);
			$result[$arr2[0]] = $arr2[1];
		}
		echo var_dump($result);
		return $this->layout_object->_fetch('content_cash_desk/computop_billpay.tpl');
	} // end after_order


	public function change_order() {
		echo 1;exit();
		global $DEVMODE;

	} // end change_order


} // end PaypalPaymentMethod

/* EOF */