<?php 
class PaymentPayPal extends Payment {
	function __construct(&$locator) {
		$this->address  =& $locator->get('address');
		$this->cart     =& $locator->get('cart');
		$this->config   =& $locator->get('config');
		$this->currency =& $locator->get('currency');
		$this->customer =& $locator->get('customer');
		$this->database =& $locator->get('database');
		$this->language =& $locator->get('language');
		$this->mail     =& $locator->create('mail');
		$this->order    =& $locator->get('order');
		$this->request  =& $locator->get('request');
		$this->session  =& $locator->get('session');
		$this->shipping =& $locator->get('shipping');
		$this->tax      =& $locator->get('tax');
		$this->url      =& $locator->get('url');	    
		
		$this->language->load('extension/payment/paypal.php');
  	}
  
	function getTitle() {
		return $this->language->get('text_paypal_title');
  	}
   
  	function getMethod() {
		if ($this->config->get('paypal_status')) {
      		if (!$this->config->get('paypal_geo_zone_id')) {
        		$status = true;
      		} elseif ($this->database->getRows("select * from zone_to_geo_zone where geo_zone_id = '" . (int)$this->config->get('paypal_geo_zone_id') . "' and country_id = '" . (int)$this->address->getCountryId($this->session->get('payment_address_id')) . "' and (zone_id = '" . (int)$this->address->getZoneId($this->session->get('payment_address_id')) . "' or zone_id = '0')")) {
      		  	$status = true;
      		} else {
     	  		$status = false;
			}	
      	} else {
			$status = false;
		}
		
		$method_data = array();
	
		if ($status) {  
      		$method_data = array( 
        		'id'         => 'paypal',
        		'title'      => $this->language->get('text_paypal_title'),
				'sort_order' => $this->config->get('paypal_sort_order')
      		);
    	}
   
    	return $method_data;
  	}
  
  	function getActionUrl() {
		if (!$this->config->get('paypal_test')) {
    		return 'https://www.paypal.com/cgi-bin/webscr';
  		} else {
			return 'https://www.sandbox.paypal.com/cgi-bin/webscr';
		}
	}
    
  	function fields() {	
		$currency_data = explode(',', $this->config->get('paypal_currency'));
		
		if (in_array($this->currency->getCode(), $currency_data)) {
			$currency = $this->currency->getCode();
		} else {
			$currency = $this->config->get('config_currency');
		}
		
	  	$ouput  = '<input type="hidden" name="cmd" value="_xclick" />' . "\n";
		$ouput .= '<input type="hidden" name="business" value="' . $this->config->get('paypal_email') . '" />' . "\n";
		$ouput .= '<input type="hidden" name="item_name" value="' . $this->config->get('config_store') . '" />' . "\n";
		$ouput .= '<input type="hidden" name="currency_code" value="' . $currency . '" />' . "\n";
		$ouput .= '<input type="hidden" name="amount" value="' . $this->currency->format($this->order->get('total'), $currency, FALSE, FALSE) . '" />' . "\n";
		$ouput .= '<input type="hidden" name="first_name" value="' .  $this->order->get('firstname') . '" />' . "\n";
		$ouput .= '<input type="hidden" name="last_name" value="' . $this->order->get('lastname') . '" />' . "\n";
		$ouput .= '<input type="hidden" name="address1" value="' . $this->order->get('payment_address_1') . '" />' . "\n";
		$ouput .= '<input type="hidden" name="address2" value="' . $this->order->get('payment_address_2') . '" />' . "\n";
		$ouput .= '<input type="hidden" name="city" value="' . $this->order->get('payment_city') . '" />' . "\n";
		$ouput .= '<input type="hidden" name="zip" value="' . $this->order->get('payment_postcode') . '" />' . "\n";
		$ouput .= '<input type="hidden" name="country" value="' . $this->order->get('payment_country') . '" />' . "\n";
		$ouput .= '<input type="hidden" name="address_override" value="0" />' . "\n";
		$ouput .= '<input type="hidden" name="notify_url" value="' . $this->url->ssl('checkout_process', 'callback', array('payment' => 'paypal')) . '" />' . "\n";
		$ouput .= '<input type="hidden" name="email" value="' . $this->order->get('email') . '" />' . "\n";
		$ouput .= '<input type="hidden" name="invoice" value="' . $this->order->getReference() . '" />' . "\n";
		$ouput .= '<input type="hidden" name="lc" value="' . $this->language->getCode() . '" />' . "\n";
		$ouput .= '<input type="hidden" name="return" value="' . $this->url->ssl('checkout_process') . '" />' . "\n";
		$ouput .= '<input type="hidden" name="rm" value="2" />' . "\n";
		$ouput .= '<input type="hidden" name="no_note" value="1" />' . "\n";
		$ouput .= '<input type="hidden" name="cancel_return" value="' . $this->url->ssl('checkout_payment') . '" />' . "\n";
		$ouput .= '<input type="hidden" name="paymentaction" value="authorization" />' . "\n";
	
    	return $ouput;
  	}

	function callback() {
		$req = 'cmd=_notify-validate';

		foreach ($_POST as $key => $value) {
			$req .= '&' . $key . '=' . urlencode(stripslashes($value));
		}

		$header  = 'POST /cgi-bin/webscr HTTP/1.0' . "\r\n";
		$header .= 'Content-Type: application/x-www-form-urlencoded' . "\r\n";
		$header .= 'Content-Length: ' . strlen($req) . "\r\n\r\n";
		
		if (!$this->config->get('paypal_test')) {
			$fp = fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30);
		} else {
			$fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
		}
		
		if ($fp) {
			fputs($fp, $header . $req);
			
			while (!feof($fp)) {
				$res = fgets($fp, 1024);
				
				if (strcmp($res, 'VERIFIED') == 0) {
					$this->order->load($this->request->get('invoice', 'post'));
						
					$this->order->process();
				}
			}
			
			fclose($fp);
		}
  	}
}
?>