<?php
/**
 * @filename        PayPoint.php
 * @description     This class is for making payment through PayPoint GATEWAY FREEDOM +IMA
 * @version         1.0
 * @package         PaymentLibrary.classes
 * @author          Masuduzzaman
 *
 * @link
 * @created on      November 21, 2011
 * @Dependencies
 * @license
 ***/
  class PayPoint extends PaymentGateway
  {
      var $requesType = 'SALES';

      function PayPoint()
      {
          parent::PaymentGateway();
          $this->requestURL ='https://secure.metacharge.com/mcpe/corporate';
          $this->ignorePeerVarification = true;
          $this->testURL = $this->requestURL;
          $this->supportedCurrencies = array('USD','GBP', 'EUR');
      }

      function initialize()
      {
          $this->logMessage('Initializing basic parameters...');

          if($this->testMode)
          {
              $this->setParam('intTestMode', '1');
          }

          $this->setParam('intInstID', $this->apiUserName);
          $this->setParam('fltAPIVersion', '1.3');
          $this->setParam('strCurrency', $this->currencyCode);
          $this->setParam('fltAmount', $this->amount);
      }

      function setCardParameters()
      {
          $this->logMessage('Initializing card parameters...');
          $this->setParam('strCardNumber', $this->cardNumber);
          $this->setParam('strExpiryDate', $this->expiryMonth . $this->getTwoDigitExpiryYear());
          $this->setParam('strCardType', strtoupper($this->cardType));
          $this->setParam('intCV2', $this->cvv);

      }

      function setBillingInformation()
      {
          $this->logMessage('Initializing billing parameters...');
          $this->setParam('strCardHolder', $this->firstName.$this->lastName);
          $this->setParam('strAddress', trim($this->address1 . ' ' . $this->address2));
          $this->setParam('strCity', $this->city);
          $this->setParam('strState', $this->state);
          $this->setParam('strCountry', $this->countryCode);
          $this->setParam('strTel', $this->phone);
          $this->setParam('strPostcode', $this->zip);
          $this->setParam('strEmail', $this->email);
      }

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

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

          $this->requesType = 'AUTH_ONLY';
          $this->logMessage('Setting parameters for authorize request...');
          $this->initialize();
          $this->setParam('trans_type','AUTH_ONLY');
          $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 = 'PREVIOUS_SALE';

          $this->logMessage('Setting parameters for capture request...');
          $this->initialize();
          $this->setParam('trans_type','PREVIOUS_SALE');
          $this->setParam('transID', $this->transactionId);

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

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

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

          $this->requesType = 'SALES';
          $this->logMessage('Setting parameters for sale request...');
          $this->initialize();
          $this->setParam('strTransType', 'PAYMENT');
          $this->setParam('strCartID', $this->invoiceNumber);
          $this->setParam('strDesc', $this->note);

          $this->setCardParameters();
          $this->setBillingInformation();
          $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('trans_type','RETURN');
          $this->setCardParameters();
          $this->setParam('transID',$this->transactionId);
          $this->makeAPICall();
          return $this->response;
      }

      function prepareResponse()
      {
          /*
          if($this->response->rawResponse)
          {
              echo 'In prepareresponse_ function';
              print_r($this->response->rawResponse);

              $responseArray = explode('|', $this->response->rawResponse);
              print_r($responseArray );

               //exit;
                //$this->response->success = 1;
                //$this->response->ack = ACK_SUCCESS;

          }
          else
          {
              $this->response->ack = ACK_FAILURE;
              $this->response->success = 0;
          }

          $this->response->invoiceNumber = $this->invoiceNumber;
          $this->response->transactionType = $this->requesType;
          $this->response->currency = $this->currencyCode;*/
      }

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

          $this->validateAmount();
      }
  }
?>