<?php
/**
 * @filename        PayFlow.php
 * @description     This class is for doing transaction with PayPal's PayFlow API
 * @version         1.0
 * @package         PaymentLibrary.classes
 * @author          Mohammad Sajjad Hossain - info@sajjadhossain.com
 *
 * @link            http://sajjadhossain.com
 * @created on      August 10, 2009
 * @Dependencies    PaymentGateway
 * @license
 ***/
  class PaypalProRecurring extends PaymentGateway
  {
      /**
      *@desc If one or more additional users on the account, this value is the ID
      * of the user authorized to process transactions. If, however, there are no
      * additional user on the account, this variable will have the same value as vendor
      *
      * @var string
      */
      var $user = '';

      var $host = 'payflowpro.paypal.com';
      var $testHost = 'pilot-payflowpro.paypal.com';

      function PayFlowRecurring()
      {
          parent::PaymentGateway();

          $this->apiVersion = '';
          $this->requestURL = 'https://payflowpro.paypal.com';
          $this->testURL = 'https://pilot-payflowpro.paypal.com';
          $this->supportedCurrencies = array('USD', 'EUR', 'GBP', 'CAD', 'JPY', 'AUD');
      }

      function recurringProfile()
      {


      }


      /**
      *@desc Prepares request string form the parameters
      *
      * @return null
      */
      function prepareRequest()
      {
          if(count($this->params))
          {
              $nameValuePairs = array();

              foreach($this->params as $key => $value)
              {
                  if($value)
                  {
                      $valueLength = strlen($value);
                      $hasAmp = (bool) strstr($value, '&');
                      $value = urlencode(str_replace('"', '', $value));

                      $nameValuePairs[] = $key . ($hasAmp ?  '[' . $valueLength . ']=' . $value : '=' . $value);
                  }
              }
              $this->requestString = implode('&', $nameValuePairs);
          }

      }

      /**
      *@desc This function is for preparing basic response. Gateway classes will have to implement them to work.
      * Gateway classes may use different functions to prepare response but must implement this function.
      */
      function prepareResponse()
      {
          if($this->response->rawResponse)
          {
              $parts = explode('&', $this->response->rawResponse);

              if(count($parts))
              {
                  $responseArray = array();

                  foreach($parts as $part)
                  {
                      list($key, $value) = explode('=', $part);

                      $responseArray[$key] = $value;
                  }

                  $this->response->transactionId = isset($responseArray['PNREF']) ? $responseArray['PNREF'] : '';
                  $this->response->authorizationId = isset($responseArray['AUTHCODE']) ? $responseArray['AUTHCODE'] : '';
                  $this->response->responseCode = isset($responseArray['RESULT']) ? $responseArray['RESULT'] : '';
                  $this->response->ccvResponse = isset($responseArray['CVV2MATCH']) ? $responseArray['CVV2MATCH'] : '';
                  $this->response->reasonText = isset($responseArray['RESPMSG']) ? $responseArray['RESPMSG'] : '';
                  $this->response->avsResponse = isset($responseArray['AVSADDR']) ? $responseArray['AVSADDR'] : '';
                  $this->response->amount = $this->amount;

                  if($this->response->responseCode)
                  {
                      $this->response->success = 0;
                      $this->response->ack = ACK_FAILURE;

                      $responseText = $this->response->reasonText;
                      if(strstr($responseText, ':'))
                      {
                          $temp = explode(':', $responseText);
                          $responseText = $temp[0];
                      }
                      $this->response->setError($this->response->reasonText);
                  }
                  else
                  {
                      $this->response->success = 1;
                      $this->response->ack = ACK_SUCCESS;
                  }
              }
          }
      }

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

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