<?php defined('SYSPATH') or die('No direct access allowed.');

/**
 * This class provides PAYPAL CART support
 *
 * @package    Checkout
 * @category   Plugins
 * @author     Deon George
 * @copyright  (c) 2009-2013 Open Source Billing
 * @license    http://dev.osbill.net/license.html
 */
class Checkout_Plugin_Paypal_Cart extends Checkout_Plugin_Paypal {
	/**
	 * Set payment via Paypal
	 */
	public function before(Cart $co) {
		$output = '';

		$output .= Form::open(sprintf('https://%s/cgi-bin/webscr',$this->test_mode ? $this->url_test : $this->url_prod),array('method'=>'POST'));

		$output .= View::factory('checkout/plugin/paypal/before')
			->set('checkout',$this->co)
			->set('cart',$co);

		$output .= Form::hidden('cmd','_cart');
		$output .= Form::hidden('business',$this->test_mode ? $this->email_test : $this->email_prod);
		$output .= Form::hidden('bn','Graytech_BuyNow_WPS_AU');
		$output .= Form::hidden('cancel_return',URL::site('checkout/cancel/'.$this->co->id,TRUE));
		$output .= Form::hidden('custom',$co->id());
		// @todo This should be dynamic
		$output .= Form::hidden('currency_code','AUD');
		$output .= Form::hidden('notify_url',URL::site('checkout/notify/'.$this->co->id,TRUE));
		$output .= Form::hidden('return',URL::site('checkout/after/'.$this->co->id,TRUE));
		$output .= Form::hidden('upload','1');

		$c = 1;
		foreach ($co->contents() as $cio) {
			$output .= Form::hidden('item_number_'.$c,$cio->id);
			$output .= Form::hidden('item_name_'.$c,$cio->item()->i);
			$output .= Form::hidden('amount_'.$c,$cio->item()->t);
			$c++;
		}

		$output .= Form::hidden('item_number_'.$c,'0:PAYFEE');
		$output .= Form::hidden('item_name_'.$c,'Paypal Fee');
		$output .= Form::hidden('amount_'.$c,$this->co->fee($co->total()));

		$output .= Form::close();

		return $output;
	}
}
?>
