<?php
/**
 * @filename        WorldPay.php
 * @description     This class is for making payment through Payleap
 * @version         1.0
 * @package         PaymentLibrary.classes
 * @author          Masuduzzaman
 *
 * @link
 * @created on      MAy 22, 2012
 * @Dependencies
 * @license
 ***/
  class WorldPay extends PaymentGateway
  {
      var $requesType = 'SALE';

      function WorldPay()
      {
          parent::PaymentGateway();
          $this->requestURL = 'https://secure-test.worldpay.com/wcc/purchase';
          $this->testURL = 'https://secure-test.worldpay.com/wcc/purchase';
          $this->supportedCurrencies = array('USD', 'EUR', 'GBP', 'CAD', 'JPY', 'AUD', 'ZAR');
      }

      function initialize()
      {
          $this->logMessage('Initializing basic parameters...');
          $this->setParam('instId', $this->apiKey);
		  $this->setParam('amount', $this->amount);
		  $this->setParam('currency', 'GBP');
      }

      function setCardParameters()
      {
          $this->logMessage('Initializing card parameters...');  
      }

      function setBillingInformation()
      {
	  
	   $this->setParam('testMode', '100');
	   $this->setParam('cartId', '101KT0098');
	   $this->setParam('name', 'masud');
	   $this->setParam('address1', ' The Street');
	   $this->setParam('address1', 'My Suburb');
	   $this->setParam('town', 'mytown');	   
	   $this->setParam('region', 'my region or county');
	   $this->setParam('country', 'GB');
	   $this->setParam('tel', '0123456789');
	   $this->setParam('email', 'enggmasud1983@gmail.com');
         

      }

      function authorize()
      {
          $this->logMessage('Preparing authorize request...');
          $this->validateBasicInput();
          $this->validateCreditCardNumber();
          $this->validateExpiryDate();

          if($this->response->hasError())
          {
              return $this->response;
          }

          $this->requesType = 'AUTH';
          $this->logMessage('Setting parameters for authorize request...');
          $this->initialize();
          $this->setParam('TransType','AUTH');
          $this->setCardParameters();
          $this->setBillingInformation();
          $this->makeAPICall();
          return $this->response;
      }

      function capture()
      {
          $this->logMessage('Preparing capture request...');
          $this->validateBasicInput();
          $this->validateTransactionID();

          if($this->response->hasError())
          {
              $this->logMessage('Error found...<br />' . $this->response->getErrorString('ul'));
              return $this->response;
          }

          $this->requesType = 'Capture';

          $this->logMessage('Setting parameters for capture request...');
          $this->initialize();
          $this->setParam('TransType','Capture');
          $this->setParam('PNRef',$this->transactionId);
          $this->setCardParameters();
          $this->setBillingInformation();

          $this->makeAPICall();
          return $this->response;
      }

      function sale()
      {
	  
	   $this->makeAPICall();
	   exit;
	    $xml = new DOMDocument("1.0", "UTF-8");
		$paymentService = $xml->createElement('paymentService');
		
		$paymentService->appendChild(
		$xml->createAttribute('version'))->appendChild(
		$xml->createTextNode('1.4'));
		
		$paymentService->appendChild(
		$xml->createAttribute('merchantCode'))->appendChild(
		$xml->createTextNode('UNSTOPPABLETM1'));
			
		
		// message info		
	/*	$version = $xml->createElement('version','1.4');
		$paymentService->appendChild($version);
		$merchantCode = $xml->createElement('merchantCode','UNSTOPPABLETM1');
		$paymentService->appendChild($merchantCode);
	*/		

		//submit start
		$submit = $xml->createElement('submit');
			//order start					
			$order = $xml->createElement('order');
			$order->appendChild(
			$xml->createAttribute('orderCode'))->appendChild(
			$xml->createTextNode('ORD-111'));
			
			$order->appendChild(
			$xml->createAttribute('installationId'))->appendChild(
			$xml->createTextNode($this->apiUserName));			    						
				
			$description = $xml->createElement('description','description here');
			$order->appendChild($description);
				
			$amount = $xml->createElement('amount');
			
			$amount->appendChild(
			$xml->createAttribute('currencyCode'))->appendChild(
			$xml->createTextNode('GBP'));
			
			$amount->appendChild(
			$xml->createAttribute('exponent'))->appendChild(
			$xml->createTextNode('2'));
			
			$amount->appendChild(
			$xml->createAttribute('value'))->appendChild(
			$xml->createTextNode('5000'));
			$order->appendChild($amount);
			
			$orderContent = $xml->createElement('orderContent','orderContentvalue here');
			$order->appendChild($orderContent);
			
			// paymentDetails		
			$paymentDetails = $xml->createElement('paymentDetails');			
			$visa_ssl = $xml->createElement('VISA-SSL');

			//cardNumber		
			$cardNumber = $xml->createElement('cardNumber','4111111111111111');
			$visa_ssl->appendChild($cardNumber);
			
			//$expiryDate = $xml->createElement('expiryDate','2013-10-10');
			//$visa_ssl->appendChild($expiryDate);
			
			$expiryDate = $xml->createElement('expiryDate');
			$dateval = $xml->createElement('date');			
			$dateval->appendChild(
			$xml->createAttribute('month'))->appendChild(
			$xml->createTextNode('02'));			
			
			$dateval->appendChild(
			$xml->createAttribute('year'))->appendChild(
			$xml->createTextNode('2013'));	
			$expiryDate->appendChild($dateval);
			$visa_ssl->appendChild($expiryDate);
			
						
			$cardHolderName = $xml->createElement('cardHolderName','card hold');
			$visa_ssl->appendChild($cardHolderName);
			
			$cvc = $xml->createElement('cvc','123');
			$visa_ssl->appendChild($cvc);
				
			//card address
			$cardAddress = $xml->createElement('cardAddress');
			$address = $xml->createElement('address');
			
			$firstName = $xml->createElement('firstName','John');
			$address->appendChild($firstName);
			$lastName = $xml->createElement('lastName','Shopper');
			$address->appendChild($lastName);
			$street = $xml->createElement('street','47A Queensbridge Rd');
			$address->appendChild($street);
			$postalCode = $xml->createElement('postalCode','CB94BQ');
			$address->appendChild($postalCode);
			$city = $xml->createElement('city','Cambridge');
			$address->appendChild($city);
			$countryCode = $xml->createElement('countryCode','GB');
			$address->appendChild($countryCode);
			$telephoneNumber = $xml->createElement('telephoneNumber','01234567890');
			$address->appendChild($telephoneNumber);
			$cardAddress->appendChild($address);			
			$visa_ssl->appendChild($cardAddress);
			//end of card address
				
			$paymentDetails->appendChild($visa_ssl);	
			
			$order->appendChild($paymentDetails);	

//shopper

    $shopper = $xml->createElement('shopper');
	$shopperEmailAddress = $xml->createElement('shopperEmailAddress','enggmasud1983@gmail.com');
	$shopper->appendChild($shopperEmailAddress);
	$order->appendChild($shopper);
	
// shipping address

$shippingAddress = $xml->createElement('shippingAddress');
	$address = $xml->createElement('address');
	$firstName = $xml->createElement('firstName');
	$address->appendChild($firstName);
	$lastName = $xml->createElement('lastName');
	$address->appendChild($lastName);
	
	$street = $xml->createElement('street','47A Queensbridge Rd');
	$address->appendChild($street);
	$postalCode = $xml->createElement('postalCode','CB94BQ');
	$address->appendChild($postalCode);
	$city = $xml->createElement('city','Cambridge');
	$address->appendChild($city);
	$countryCode = $xml->createElement('countryCode','GB');
	$address->appendChild($countryCode);
	$telephoneNumber = $xml->createElement('telephoneNumber','01234567890');
	$address->appendChild($telephoneNumber);
	
	$shippingAddress->appendChild($address);
	$order->appendChild($shippingAddress);
//shipping address ending	
	
			
			
			$submit->appendChild($order);
			//order end

		$paymentService->appendChild($submit);
		//submit end
		$xml->appendChild($paymentService);	
	
		$this->requestString = $xml->saveXML();
	    $this->makeAPICall();
		return $this->response;
			
	  
      }

      function refund()
      {
          $this->logMessage('Preparing refund request...');
          $this->validateBasicInput();
          $this->validateCreditCardNumber();

          $this->validateTransactionID();

          if($this->response->hasError())
          {
              $this->logMessage('Error found...<br />' . $this->response->getErrorString('ul'));
              return $this->response;
          }

          $this->logMessage('Setting parameters for refund request...');
          $this->requesType = 'RETURN';
          $this->initialize();
          $this->setParam('TransType','RETURN');
          $this->setCardParameters();
          $this->setParam('PNRef',$this->transactionId);
          $this->makeAPICall();
          return $this->response;
      }

       function prepareResponse(){
		//print_r($this->response->getRawResponse());      
      }



      /**
      *@desc Basic input validation
      */
      function validateBasicInput()
      {
          if(empty($this->apiKey) )
          {
			$this->response->setError('WorldPay Installation ID have not been configured.');
          }

          $this->validateAmount();
      }
	  
	  
	  function makeAPICall()
      {
	   die('okokok');
          //$url = $this->getRequestURL();
		  $url = "https://UNSTOPPABLETM1:UTe8n7LJ@secure-test.bibit.com/jsp/merchant/xml/paymentService.jsp";

          if(empty($url))
          {
              $this->logMessage('Request URL is missing.');
              $this->response->setError('Request URL has not been set');
          }

          if($this->response->hasError())
          {
              $this->logMessage('Request could not be made because of errors.');
              return;
          }

          $this->logMessage('Preparing request...');
          $this->prepareRequest();
		  $this->requestString = "<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE paymentService PUBLIC '-//Bibit//DTD Bibit PaymentService v1//EN' 'http://dtd.bibit.com/paymentService_v1.dtd'>
<paymentService version='1.4' merchantCode='UNSTOPPABLETM1'>
  <submit>
    <order orderCode = '1347279350'>
      <description>ACME Webshops Int. Inc.</description>
      <amount value='1982' currencyCode = 'EUR' exponent = '2'/>
      <orderContent>
        <![CDATA[<center><table>
<tr><td bgcolor='#CCCCCC'>Your Internet Order:</td><td colspan='2' bgcolor='#ffff00' align='right'>AY 845</td></tr>
<tr><td bgcolor='#ffff00'>Description:</td><td>14 Tulip bulbs</td><td align='right'>1,00</td></tr>
<tr><td colspan='2'>Subtotal:</td><td align='right'>14,00</td></tr>
<tr><td colspan='2'>VAT: 13%</td><td align='right'>1,82</td></tr>
<tr><td colspan='2'>Shipping and Handling:</td><td align='right'>4,00</td></tr>
<tr><td colspan='2' bgcolor='#c0c0c0'>Total cost:</td><td bgcolor='#c0c0c0' align='right'>Euro 19,82</td></tr>
<tr><td colspan='3'>&nbsp;</td></tr>
<tr><td bgcolor='#ffff00' colspan='3'>Your billing address:</td></tr>
<tr><td colspan='3'>Mr. zaman,<br>11 Hereortherestreet,<br>1234 KL yourcity,<br>Thisplace.</td></tr>
<tr><td colspan='3'>&nbsp;</td></tr>
<tr><td bgcolor='#ffff00' colspan='3'>Your shipping address:</td></tr>
<tr><td colspan='3'>Mr. zaman,<br>11 Hereortherestreet,<br>1234 KL yourcity,<br>Thisplace.</td></tr>
<tr><td colspan='3'>&nbsp;</td></tr>
<tr><td bgcolor='#ffff00' colspan='3'>Our contact information:</td></tr>
<tr><td colspan='3'>ACME Webshops Int. Inc.,<br>11 Strangewood Blv.,<br>1255 KZ Thisisit,<br>Nowhereatall.<br><br>acmeweb@acme.inc<br>(555) 1235 456</td></tr>
<tr><td colspan='3'>&nbsp;</td></tr>
<tr><td bgcolor='#c0c0c0' colspan='3'>Billing notice:</td></tr>
<tr><td colspan='3'>Your payment will be handled by Bibit Global Payments Services<br>This name may appear on your bank statement<br>http://www.bibit.com</td></tr>
</table></center>]]>
      </orderContent>
      <paymentMethodMask>
        <include code='ALL'/>
      </paymentMethodMask>      <shopper>
        <shopperEmailAddress>enggmasud1983@gmail.com</shopperEmailAddress>
      </shopper>
      <shippingAddress>
        <address>
          <firstName>masud</firstName>
          <lastName>zaman</lastName>
          <street>11 Hereortherestreet</street>
          <postalCode>1234 KL</postalCode>
          <city>yourcity</city>
          <countryCode>USD</countryCode>
          <telephoneNumber>yourtelephone</telephoneNumber>
        </address>
      </shippingAddress>
    </order>
  </submit>
</paymentService>";
          $this->logMessage($this->requestString);

          $this->logMessage('Initializing cURL...');
          $ch = curl_init();

          if($this->requestMethod == 'POST')
          {
              $this->logMessage('Sending Request to : ' . $url);
              curl_setopt($ch, CURLOPT_URL, $url);
              curl_setopt($ch, CURLOPT_POST, 1);
              curl_setopt($ch, CURLOPT_POSTFIELDS, $this->requestString);
          }
          else
          {
              $url .= $this->requestString; //the URL should contain '?' if it is needed
              $this->logMessage('Sending Request to : ' . $url);
              curl_setopt($ch, CURLOPT_URL, $url);
              curl_setopt($ch, CURLOPT_POST, 0);
          }

          if ($this->proxyHost && $this->proxyPort)
          {
              curl_setopt($ch, CURLOPT_PROXY, $this->proxyHost . ':' . $this->proxyPort);
          }

          if(!empty($this->sslCertificatate))
          {
              $this->logMessage('Using SSL Certificate...' . $this->sslCertificatate);
              curl_setopt ($ch, CURLOPT_SSLCERT, $this->sslCertificatate);
          }

          if(empty($this->headers))
          {
              $this->headers = array();
          }

          $this->headers[] = "Content-Length: " . strlen($this->requestString);
          curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);

          if($this->testMode)
          {
              curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
              curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
              $this->response->isTestMode = 1;
          }
          else if(!$this->verifiy_ssl)
          {
              $this->logMessage('SSL in not verified as using internet secure gateway');
              curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
              curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
          }
          else
          {
              curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
              curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
          }

          curl_setopt($ch,CURLOPT_USERAGENT, $this->userAgent);
          curl_setopt($ch, CURLOPT_TIMEOUT, 60);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($ch, CURLOPT_VERBOSE, ($this->debug ? 1 : 0));
			
			echo $ch;  exit;
          $this->logMessage('Getting response...');
          $this->response->setRawResponse(curl_exec($ch));

          $this->logMessage(urldecode($this->response->getRawResponse()));

          if (curl_errno($ch))
          {
              $this->response->curlErrorNo = curl_errno($ch);
              $this->response->curlErrorMessage = curl_error($ch);
              $error = 'cURL Error # ' . $this->response->curlErrorNo . ' : ' . $this->response->curlErrorMessage;

              $this->logMessage($error);

              if($this->showCurlError)
              {
                  $this->response->setError($error);
              }
          }
          else
          {
              curl_close($ch);
          }
          $this->logMessage('Preparing response...');
          $this->prepareResponse();

          $this->logMessage('Done...');
      }

  }
?>