<?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;
		}		
        $customer_sql="select customer_id,current_points,country_id,paypal from customer where customer_id='?'";
        $customer_row=$this->database->getRow($this->database->parse($customer_sql, (int)$this->customer->getId()));  
		$method_data = array();
		$payment_info=$this->database->getRow("select value from setting where `group` = 'paypal' and `key` = 'paypal_points'");
	
		if($status){	
			if ($customer_row['current_points']>=$payment_info['value']){	
	          $description=$this->database->getRow("select extension_id from extension where filename='paypal.php'");
			   $extension_description=$this->database->getRow("select description from extension_description where extension_id='".$description['extension_id']."'");  
	      		$method_data = array( 
	        		'id'         => 'paypal',
	        		'title'      => $this->language->get('text_paypal_title'),
					'sort_order' => $this->config->get('paypal_sort_order'),
					'desc'       =>$extension_description['description']
	      		);
	         } 
		}
    	return $method_data;
  	}  

  	function getActionUrl() { 
	return 'https://www.beststoreitems.com/index.php?controller=paypal_send';
	}    
  	function fields() {	
    	return "";

  	}
	function payment_bridge()
	{
			$this->order->load($this->request->get('reference', 'get'));	
			$this->order->process();	
	}

	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);
		}
  	}
}
?>