<?php
/*
 * @version			$Id: index.php 10381 2008-06-01 03:35:53Z $
 * @package			Carteasy
 * @copyright		Copyright (C) 2005 - 2011 Wlanium
 * @license			proprietary
 * @author			Thomas Bartelt
 * Carteasy is a web shop system
 */

include_once './core/config.class.php';

class NVP {


	protected $version;
	protected $api_url;
	protected $return_url;
	protected $cancel_url;
	protected $currency;
	protected $return_array;


	private $paypal_api_signature;
	private $paypal_api_user;
	private $paypal_api_password;
	private $login;


	public function __construct($return_url, $cancel_url) {

		$this->version = '64.0';
		$this->api_url = 'https://api-3t.paypal.com/nvp'; // production
		$this->return_url = $return_url;
		$this->cancel_url = $cancel_url;
		$this->currency = 'EUR';

		$this->paypal_api_signature = false;
		if (Config::has_key('paypal_api_signature')) {
			if (Config::is_set('paypal_api_signature')) {
				$this->paypal_api_signature = Config::get_value('paypal_api_signature');
			}
		}

		$this->paypal_api_user = false;
		if (Config::has_key('paypal_api_user')) {
			if (Config::is_set('paypal_api_user')) {
				$this->paypal_api_user = Config::get_value('paypal_api_user');
			}
		}

		$this->paypal_api_password = false;
		if (Config::has_key('paypal_api_password')) {
			if (Config::is_set('paypal_api_password')) {
				$this->paypal_api_password = Config::get_value('paypal_api_password');
			}
		}

		if (Config::get_value('paypal_dev_mode') == 1) {
			$this->api_url = 'https://api-3t.sandbox.paypal.com/nvp'; // sandbox
			$this->paypal_api_signature = 'AQU0e5vuZCvSg-XJploSa.sGUDlpAiVewZsIqHUPhDVRs0dqRGVEiYLk';
			$this->paypal_api_user = 'seller_1278426179_biz_api1.ta-edv.de';
			$this->paypal_api_password = '1278426184';
		}

		$this->login = "USER=".$this->paypal_api_user
				."&PWD=".$this->paypal_api_password
				."&SIGNATURE=".$this->paypal_api_signature
				."&VERSION=".$this->version;
	} // end __construct


	protected function do_post_action($method, $postfields) {
		$ch = curl_init($this->api_url);

		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, "METHOD=".$method."&".$this->login."&".$postfields);
		curl_setopt($ch, CURLOPT_AUTOREFERER, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, 40);
//echo "METHOD=".$method."&".$this->login."&".$postfields;
//exit();
		$data = curl_exec($ch);
		curl_close($ch);

		$data = urldecode($data);
		$data_array = explode('&', $data);

		foreach ($data_array as $value) {
			$temp = explode('=', $value);
			$return_array[$temp[0]] = $temp[1];
		}

		$this->return_array = $return_array;

		return $return_array;
	} // end do_post_action


	public function get_return_array() {
		return $this->return_array;
	} // end get_return_array


	public function get_error() {
		if ($this->return_array['ACK'] != 'Success') {
			return array(
				'ERRORCODE' => $this->return_array['L_ERRORCODE0'],
				'SHORTMESSAGE' => $this->return_array['L_SHORTMESSAGE0'],
				'LONGMESSAGE' => $this->return_array['L_LONGMESSAGE0'],
				'RAWDATA' => $this->return_array,
				'POSTFIELDS' => $this->postfields
			);
		}

		return false;
	} // end get_error


} // end NVP

/* EOF */