<?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
 */

class ExpressCheckout extends NVP {


	protected $order_toal;
	protected $shoppingCart;

	function __construct($return_url, $cancel_url, $order_toal, $shopppingCart = false) {
		parent::__construct($return_url, $cancel_url);

		$this->order_toal = round($order_toal, 2);
		$this->shoppingCart = $shopppingCart;
	} // end __construct


	// TODO get rid of this function and give link as result in set_express_checkout on success
	public function get_link() {

		if (Config::get_value('paypal_dev_mode') == 1) {
			return 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='.$this->return_array['TOKEN']; // sandbox
		} else {
			return 'https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='.$this->return_array['TOKEN']; // production
		}
	} // end get_link


	public function get_token() {
		return $this->return_array['TOKEN'];
	} // end get_token


	public function set_express_checkout() {

		$this->postfields = ""
			."PAYMENTREQUEST_0_PAYMENTACTION="."Sale"
			."&PAYMENTREQUEST_0_AMT=".$this->order_toal
			."&PAYMENTREQUEST_0_ITEMAMT=".$this->order_toal
//			."&L_PAYMENTREQUEST_0_NAME0=Ihre+Bestellung"
//			."&L_PAYMENTREQUEST_0_QTY0=1"
//			."&L_PAYMENTREQUEST_0_AMT0=".$this->order_toal
			."&RETURNURL=".$this->return_url
			."&CANCELURL=".$this->cancel_url
			."&LOCALECODE=".'DE'
			."&PAYMENTREQUEST_0_CURRENCYCODE=".$this->currency
			."&NOSHIPPING="."1"
			."&ALLOWNOTE="."0"
			."&useraction=commit";

		if (is_array($this->shoppingCart)) {
			if (is_array($this->shoppingCart['items'])) {
				$itemPos = 0;
				$itemSum = 0;

				foreach ($this->shoppingCart['items'] as $item) {
					$itemPrice = round($item->price_sum_gross / $item->quantity, 2);
					$itemSum += $itemPrice * $item->quantity;

					$this->postfields .= "&L_PAYMENTREQUEST_0_NAME".$itemPos."=".urlencode($item->name);
					$this->postfields .= "&L_PAYMENTREQUEST_0_QTY".$itemPos."=".$item->quantity;
					$this->postfields .= "&L_PAYMENTREQUEST_0_AMT".$itemPos."=".$itemPrice;

					++$itemPos;
				}

				if ($this->order_toal > $itemSum) {
					$this->postfields .= "&L_PAYMENTREQUEST_0_NAME".$itemPos."=".urlencode('Versand und Gebühren');
					$this->postfields .= "&L_PAYMENTREQUEST_0_QTY".$itemPos."=1";
					$this->postfields .= "&L_PAYMENTREQUEST_0_AMT".$itemPos."=".round($this->order_toal - $itemSum, 2);
				}
			}
		}

		$return = $this->do_post_action('SetExpressCheckout', $this->postfields);

		if ($return['ACK'] == 'Success') {
			return true;
		}

		return false;
	} // end set_express_checkout


	public function get_express_checkout_details($token) {
		$this->postfields = "TOKEN=".$token;

		$return = $this->do_post_action('GetExpressCheckoutDetails', $this->postfields);

		if ($return['ACK'] == 'Success') {
			return true;
		}

		return false;
	} // end get_express_checkout_details


	public function do_express_checkout_payment($token, $payer_id) {
		$this->postfields = "PAYMENTREQUEST_0_PAYMENTACTION=Sale"
			."&PAYERID=".$payer_id
			."&TOKEN=".$token
			."&PAYMENTREQUEST_0_AMT=".$this->order_toal
			."&PAYMENTREQUEST_0_CURRENCYCODE=".$this->currency;

		$return = $this->do_post_action('DoExpressCheckoutPayment', $this->postfields);

		if ($return['ACK'] == 'Success') {
			return true;
		}

		return false;
	} // end do_express_checkout_payment


} // end ExpressCheckout

/* EOF */