<?php
  class Payment_model extends CI_Model
  {
      var $ipAddress = '207.150.203.26';

      public function __construct()
      {
          parent::__construct();
          $this->load->library('PaymentProcessor');
      }

      function getPaymentProcessor($realTimeGateway)
      {
          switch($realTimeGateway)
          {
              case $this->config->item('payment_gateway_authorize_dot_net'):
                return new PaymentProcessor(AUTHORIZE_DOT_NET);
              case $this->config->item('payment_gateway_first_data'):
              case $this->config->item('payment_gateway_first_data_mobile'):
                return new PaymentProcessor(FIRST_DATA);
              case $this->config->item('payment_gateway_payflow_pro'):
                return new PaymentProcessor(PAYFLOW_PRO);
              case $this->config->item('payment_gateway_paypal_website_payment_pro'):
                return new PaymentProcessor(PAYPAL_PAYMENT_PRO);
              case $this->config->item('payment_gateway_eway'):
                return new PaymentProcessor(EWAY);
              case $this->config->item('payment_gateway_transfirst'):
                return new PaymentProcessor(TRANS_FIRST);
              case $this->config->item('payment_gateway_sagepay'):
                return new PaymentProcessor(SAGEPAY);
              case $this->config->item('payment_gateway_beanstream'):
                return new PaymentProcessor(BEANSTREAM);
              case $this->config->item('payment_gateway_quickbooks_intuit'):
                return new PaymentProcessor(QUICKBOOKS_MERCHANT_SERVICES);
              case $this->config->item('payment_gateway_usaepay'):
                return new PaymentProcessor(USAEPAY);
              case $this->config->item('payment_gateway_merchantone'):
                return new PaymentProcessor(MERCHANTONE);
              case $this->config->item('payment_gateway_fastcharge'):
                return new PaymentProcessor(FASTCHARGE);
              case $this->config->item('payment_gateway_internet_secure'):
                return new PaymentProcessor(INTERNET_SECURE);
              case $this->config->item('payment_gateway_caledon'):
                return new PaymentProcessor(CALEDON_CARD_SERVICES);
              case $this->config->item('payment_gateway_virtual'):
                return new PaymentProcessor(VIRTUAL_CARD_SERVICES);
              case $this->config->item('payment_gateway_virtual_merchant'):
                return new PaymentProcessor(VIRTUAL_MERCHANT);
              case $this->config->item('payment_gateway_psigate'):
                return new PaymentProcessor(PSIGATE);
              case $this->config->item('payment_gateway_moneris_eselect'):
                return new PaymentProcessor(MONERIS_ESELECT_PLUS);
              case $this->config->item('payment_gateway_network_merchants'):
                return new PaymentProcessor(NETWORK_MERCHANTS);
              case $this->config->item('payment_gateway_exact'):
                return new PaymentProcessor(EXACT_TRANSACTIONS);
              case $this->config->item('payment_gateway_payment_express'):
                return new PaymentProcessor(PAYMENT_EXPRESS);
              case $this->config->item('payment_gateway_pay_junction'):
                return new PaymentProcessor(PAY_JUNCTION);
              case $this->config->item('payment_gateway_pay_simple'):
                return new PaymentProcessor(PAY_SIMPLE);
              case $this->config->item('payment_gateway_moneris_canada'):
                return new PaymentProcessor(MONERIS_ESELECT_PLUS_CANADA);
              case $this->config->item('payment_gateway_quantum'):
                return new PaymentProcessor(QUANTUM_GATEWAY);
			  case $this->config->item('payment_gateway_samurai'):
                return new PaymentProcessor(SAMURAI);
              case $this->config->item('payment_gateway_payleap'):
              case $this->config->item('payment_gateway_payleap_mobile'):
                return new PaymentProcessor(PAYLEAP);
              case $this->config->item('payment_gateway_securepay'):
                return new PaymentProcessor(SECUREPAY);
              case $this->config->item('payment_gateway_wonderpay'):
               return new PaymentProcessor(WONDERPAY);
			  case $this->config->item('payment_gateway_cardsave'):
               return new PaymentProcessor(CARDSAVE);
              case $this->config->item('payment_gateway_paypal_advanced'):
               return new PaymentProcessor(PAYPAL_ADVANCED);
              case $this->config->item('payment_gateway_first_atlantic_commerce'):
               return new PaymentProcessor(FIRST_ATLANTIC_COMMERCE);
              case $this->config->item('payment_gateway_optimal_payments'):
               return new PaymentProcessor(OPTIMAL_PAYMENTS);
              case $this->config->item('payment_gateway_plugnpay'):
               return new PaymentProcessor(PLUGNPAY);
              case $this->config->item('payment_gateway_msdpay'):
               return new PaymentProcessor(MSDPAY);
              case $this->config->item('payment_gateway_beanstream_mobile'):
              return new PaymentProcessor(BEANSTREAM_MOBILE);
              case $this->config->item('payment_gateway_bluepay'):
               return new PaymentProcessor(BLUEPAY);
			 case $this->config->item('payment_gateway_eprocessing_network'):
               return new PaymentProcessor(EPROCESSING_NETWORK);
              case $this->config->item('payment_gateway_stripe'):
              return new PaymentProcessor(STRIPE);
              case $this->config->item('payment_gateway_sage_payment'):
              return new PaymentProcessor(SAGE_PAYMENT);
              case $this->config->item('payment_gateway_wepay'):
              return new PaymentProcessor(WEPAY);
              case $this->config->item('payment_gateway_first_data_gloabal_e4'):
              return new PaymentProcessor(FIRST_DATA_GLOBAL_E4);
          }
      }

      function chargeOrder($orderDetails, $gatewaySetup, $cvv2 = '', $fromAPI = 0)
      {
          $processor = $this->getPaymentProcessor($gatewaySetup->real_time_gateway);

          $processor->setAPIUserName($gatewaySetup->authorize_api_login_key);
          $processor->setAPIKey($gatewaySetup->authorize_api_transaction_key);
          $processor->setAPISignature($gatewaySetup->authorize_api_signature);

          $processor->setIPAddress($this->ipAddress);

          $getInstance = &get_instance();
          $getInstance->load->model('customers/Customer_Setup_model', 'Customer_Setup_model', TRUE);
          //$customer = $getInstance->Customer_Setup_model->getDecryptCreditCartInfo($orderDetails);
          $customer = $getInstance->Customer_Setup_model->getDecryptCreditCartInfoFromOrder($orderDetails);

          $processor->setFirstName($orderDetails->first_name);
          $processor->setLastName($orderDetails->last_name); 
          $processor->setEmail($orderDetails->customer_email);

          $phone = '';

          if (! empty($orderDetails->home_phone_number))
          {
              $phone = $orderDetails->home_phone_number;
          }
          else if (! empty($orderDetails->work_phone_number))
          {
              $phone = $orderDetails->work_phone_number;
          }

          $processor->setPhone($phone);
          $processor->setFax($orderDetails->fax);
          $processor->setBillingAddress1($orderDetails->address_line1);
          $customer->address_line2 = $orderDetails->address_line2 ? $orderDetails->address_line2 : '';
          $processor->setBillingAddress2($orderDetails->address_line2);
          $processor->setCompanyName($orderDetails->company_name);
          $processor->setBillingCity($orderDetails->customer_city);
          $processor->setBillingState($orderDetails->customer_state);
          $processor->setBillingZip($orderDetails->customer_zip);
          $processor->setBillingCountry($orderDetails->country);

          $customer->shipping_name = str_replace('  ', ' ', trim($orderDetails->shipping_name));
          $shippingNameParts = explode(' ', $orderDetails->shipping_name);
          $cnt = count($shippingNameParts);

          if($cnt == 2)
          {
              $firstName = $shippingNameParts[0];
              $lastName = $shippingNameParts[1];
          }
          else if($cnt > 2)
          {
              $firstName = '';

              for($i = 0; $i < ($cnt - 1); $i++)
              {
                  $firstName .= $shippingNameParts[$i] . ' ';
              }
              $firstName = trim($firstName);
              $lastName = $shippingNameParts[$cnt - 1];
          }
          else
          {
              $firstName = $shippingNameParts[0];
              $lastName = '-';
          }

          $processor->setShippingFirstName($firstName);
          $processor->setShippingLastName($lastName);
          $processor->setShippingAddress1($orderDetails->shipping_address_line1);
          $customer->shipping_address_line2 = $orderDetails->shipping_address_line2 ? $orderDetails->shipping_address_line2 : '';
          $processor->setShippingAddress2($orderDetails->shipping_address_line2);
          $processor->setShippingCompany($orderDetails->shipping_company_name);
          $processor->setShippingCity($orderDetails->shipping_customer_city);
          $processor->setShippingState($orderDetails->shipping_customer_state);
          $processor->setShippingZip($orderDetails->shipping_customer_zip);
          $processor->setShippingCountry($orderDetails->shipping_country);

          $processor->setCardNumber($customer->card_number);
          $processor->setCardType($orderDetails->card_type);
          $processor->setNameOnCard($customer->card_holder_contactname);
          $processor->setExpiryMonth($customer->card_exp_month);
          $processor->setExpiryYear($customer->card_exp_year);
          $processor->setCVV($cvv2);
          if(isset($orderDetails->card_track_data))
             $processor->setMagData($orderDetails->card_track_data);
          $processor->setNote($orderDetails->order_product_names);
          
          if($this->input->post('gateway_card_profile_id'))
             $processor->setCustomVariable1($this->input->post('gateway_card_profile_id'), 'gateway_card_profile_id');
             
          $processor->setCustomVariable2($gatewaySetup->merchant_row_id, 'merchant_row_id');   
             
          if(($cvv2 && $gatewaySetup->real_time_gateway == $this->config->item('payment_gateway_paypal_website_payment_pro')))
          {
              //temporary storage of CVV2. This will be deleted in next processes.
              //if failed to delete, a schedule job will delete it. Deletion is required
              //for PCI complience.
              if($orderDetails->customer_id)
              {
                  $sql = "DELETE FROM tbl_customer_temp_data WHERE customer_id = " . $orderDetails->customer_id;
                  $this->db->query($sql);
              }

              $tempData = array();
              $tempData['customer_id'] = $orderDetails->customer_id;
              $tempData['data'] = encrypt($cvv2);
              $this->db->insert('tbl_customer_temp_data', $tempData);
          }

          $totalAmount = 0.00;

          if($orderDetails->gift_certificate_payment)
          {
              $totalAmount = $orderDetails->total_amount - $orderDetails->gift_certificate_payment;
          }
          else
          {
              $totalAmount = $orderDetails->total_amount;
          }

          $totalAmount = $totalAmount == 0 ? 0.01 : $totalAmount;

          $processor->setAmount($totalAmount);
          $processor->setCurrencyCode($gatewaySetup->real_time_currency);
          $processor->setInvoiceNumber($orderDetails->order_id);                        

          if($this->config->item('test_mode') || in_array($gatewaySetup->merchant_row_id, $this->config->item('test_accounts')))
          {
              $processor->enableTestMode();
          }

          //$processor->enableDebugging();

          $paymentDetails = array();

          if(!$fromAPI && $gatewaySetup->payment_process_model == $this->config->item('process_model_authorize'))
          {
              $paymentDetails['payment_process_model'] = $gatewaySetup->payment_process_model;
              $response = $processor->authorize();
          }                                                                       
          else
          {     
              $response = $processor->sale();   
          }

          $paymentDetails['reason_code'] = $response->reasonCode;
          $paymentDetails['response_code'] = $response->responseCode;
          $paymentDetails['response_reason_code'] = $response->reasonCode;
          $paymentDetails['payment_gateway'] = $gatewaySetup->real_time_gateway;
          $paymentDetails['gateway_account_no'] = $gatewaySetup->real_gateway_account_no;
          $paymentDetails['amount'] = $response->amount;
          $paymentDetails['curl_error_message'] = addslashes($response->curlErrorMessage);
          $paymentDetails['version'] = $processor->getAPIVersion();
          $paymentDetails['auth_code'] = $response->authorizationId;
          $paymentDetails['security_code'] = $response->securityKey;
          $paymentDetails['transaction_id'] = $response->transactionId;
          $paymentDetails['vendor_tx_code'] = $response->invoiceNumber;
          $paymentDetails['transaction_type'] = $response->transactionType;
          $paymentDetails['correlation_id'] = $response->correlationId;
          $paymentDetails['success'] = $response->success;
          $paymentDetails['ack'] = $response->ack;
          $paymentDetails['error_text'] = addslashes($response->getErrorString());
          $paymentDetails['currency'] = $gatewaySetup->real_time_currency;

          return $paymentDetails;
      }

      function capture($transaction, $gatewaySetup, $orderDetails)
      {
          $processor = $this->getPaymentProcessor($gatewaySetup->real_time_gateway);

          $processor->setAPIUserName($gatewaySetup->authorize_api_login_key);
          $processor->setAPIKey($gatewaySetup->authorize_api_transaction_key);
          $processor->setAPISignature($gatewaySetup->authorize_api_signature);

          $processor->setAuthorizationId($transaction->auth_code);
          $processor->setTransactionId($transaction->transaction_id);
          $processor->setInvoiceNumber($transaction->vendor_tx_code);
          $processor->setSecurityCode($transaction->security_code);
          $processor->setAmount($transaction->amount);

          $getInstance = &get_instance();
          //$getInstance->load->model('customers/Customer_Setup_model', 'Customer_Setup_model', TRUE);
          //$customer = $getInstance->Customer_Setup_model->getCustomerWithCardInfo($transaction->customer_id);
          $processor->setNameOnCard($orderDetails->card_holder_contactname);
          $processor->setCardNumber($orderDetails->card_number);
          $processor->setExpiryMonth($orderDetails->card_exp_month);
          $processor->setExpiryYear($orderDetails->card_exp_year);

          if($this->config->item('test_mode') || in_array($gatewaySetup->merchant_row_id, $this->config->item('test_accounts')))
          {
              $processor->enableTestMode();
          }
          $response = $processor->capture();

          $details = array();
          $details['transaction_id'] = $response->transactionId ? $response->transactionId : $transaction->transaction_id;
          $details['authorization_id'] = $response->authorizationId;
          $details['correlation_id'] = $response->correlationId;
          $details['is_success'] = $response->success;
          $details['reason_code'] = $response->responseCode;
          $details['error_text'] = $response->getErrorString();
          $details['created_on'] = getCurrentDateTimeForDB();
          $this->db->insert('tbl_payment_capture', $details);

          return $response;
      }

      function chargeRecurringOrder($orderInfo, $customer, $gatewaySetup)
      {
          $processor = $this->getPaymentProcessor($gatewaySetup->real_time_gateway);

          $processor->setAPIUserName($gatewaySetup->authorize_api_login_key);
          $processor->setAPIKey($gatewaySetup->authorize_api_transaction_key);
          $processor->setAPISignature($gatewaySetup->authorize_api_signature);

          $processor->setFirstName($customer->first_name);
          $processor->setLastName($customer->last_name);
          $processor->setEmail($customer->customer_email);

          $phone = '';

          if (! empty($customer->home_phone_number))
          {
              $phone = $customer->home_phone_number;
          }
          else if (! empty($customer->work_phone_number))
          {
              $phone = $customer->work_phone_number;
          }

          $processor->setPhone($phone);
          $processor->setFax($customer->fax);
          $processor->setBillingAddress1($customer->address_line1);
          $customer->address_line2 = $customer->address_line2 ? $customer->address_line2 : '';
          $processor->setBillingAddress2($customer->address_line2);
          $processor->setCompanyName($customer->company_name);
          $processor->setBillingCity($customer->customer_city);
          $processor->setBillingState($customer->customer_state);
          $processor->setBillingZip($customer->customer_zip);
          $processor->setBillingCountry($customer->country);
          if($customer->gateway_card_profile_id)
             $processor->setCustomVariable1($customer->gateway_card_profile_id, 'gateway_card_profile_id');
          
          $processor->setCustomVariable2($gatewaySetup->merchant_row_id, 'merchant_row_id');
          
          $shippingNameParts = explode(' ', $customer->shipping_name);
          $cnt = count($shippingNameParts);

          if($cnt == 2)
          {
              $firstName = $shippingNameParts[0];
              $lastName = $shippingNameParts[1];
          }
          else if($cnt > 2)
          {
              $firstName = '';

              for($i = 0; $i < ($cnt - 1); $i++)
              {
                  $firstName .= $shippingNameParts[$i] . ' ';
              }
              $firstName = trim($firstName);
              $lastName = $shippingNameParts[$cnt - 1];
          }
          else
          {
              $firstName = $shippingNameParts[0];
              $lastName = '-';
          }

          $processor->setShippingFirstName($firstName);
          $processor->setShippingLastName($lastName);
          $processor->setShippingAddress1($customer->shipping_address_line1);
          $customer->shipping_address_line2 = $customer->shipping_address_line2 ? $customer->shipping_address_line2 : '';
          $processor->setShippingAddress2($customer->shipping_address_line2);
          $processor->setShippingCompany($customer->shipping_company_name);
          $processor->setShippingCity($customer->shipping_customer_city);
          $processor->setShippingState($customer->shipping_customer_state);
          $processor->setShippingZip($customer->shipping_customer_zip);
          $processor->setShippingCountry($customer->shipping_country);

          $processor->setCardNumber($customer->card_number);
          $processor->setCardType($customer->card_type);
          $processor->setNameOnCard($customer->card_holder_contactname);
          $processor->setExpiryMonth($customer->card_exp_month);
          $processor->setExpiryYear($customer->card_exp_year);
          $processor->setAmount($orderInfo->recurring_price);
          $processor->setCurrencyCode($gatewaySetup->real_time_currency);

          //$processor->setNote('Charging order of' . $customer->first_name . ' ' . $customer->last_name);
          $processor->setNote($orderInfo->product_description);

          $getInstance = &get_instance();
          $getInstance->load->model('orders/Order_model','',TRUE);
          $orderId = $getInstance->Order_model->getOrderId($orderInfo->order_row_id);
          $invoiceNumber = $orderId . '-' . date('dmHms');
          $processor->setInvoiceNumber($invoiceNumber); //New Order ID for recurring order

          if($this->config->item('test_mode'))
          {
              $processor->enableTestMode();
          }

          if($gatewaySetup->cvv2_required && $gatewaySetup->real_time_gateway == $this->config->item('payment_gateway_sagepay'))
          {
              $mainOrderPaymentDetails = $getInstance->Order_model->getTransactionIDByOrderRowID($orderInfo->order_row_id);
              $processor->setRelatedInvoiceNumber($mainOrderPaymentDetails->vendor_tx_code);
              $processor->setTransactionId($mainOrderPaymentDetails->transaction_id);
              $processor->setAuthorizationId($mainOrderPaymentDetails->auth_code);
              $processor->setSecurityCode($mainOrderPaymentDetails->security_code);
              $processor->setTransactionAsRecurring();
          }

          //$processor->enableDebugging();
          $response = $processor->sale();

          $paymentDetails['reason_code'] = $response->reasonCode;
          $paymentDetails['response_code'] = $response->responseCode;
          $paymentDetails['response_reason_code'] = $response->reasonCode;
          $paymentDetails['payment_gateway'] = $gatewaySetup->real_time_gateway;
          $paymentDetails['gateway_account_no'] = $gatewaySetup->real_gateway_account_no;
          $paymentDetails['amount'] = $response->amount;
          $paymentDetails['curl_error_message'] = addslashes($response->curlErrorMessage);
          $paymentDetails['version'] = $processor->getAPIVersion();
          $paymentDetails['auth_code'] = $response->authorizationId;
          $paymentDetails['security_code'] = $response->securityKey;
          $paymentDetails['transaction_id'] = $response->transactionId;
          $paymentDetails['vendor_tx_code'] = $response->invoiceNumber;
          $paymentDetails['transaction_type'] = $response->transactionType;
          $paymentDetails['success'] = $response->success;
          $paymentDetails['ack'] = $response->ack;
          $paymentDetails['error_text'] = addslashes($response->getErrorString());
          $paymentDetails['currency'] = $gatewaySetup->real_time_currency;

          return $paymentDetails;
      }

      function refundPayment($paymentInfo, $orderRowId)
      {
          $paymentDetails = array();

          $getInstance = &get_instance();

          $getInstance->load->model('carts/Cart_Setup_model', 'Cart_Setup_model', TRUE);
          $gatewaySetup = $getInstance->Cart_Setup_model->getRealTimeGatewaySetupByID($paymentInfo['payment_gateway'], getCurrentMerchantId(), $paymentInfo['gateway_account_no']);

          if(empty($gatewaySetup))
          {
              $paymentDetails['success'] = 0;
              $gatewayName = getGatewayName($paymentInfo['payment_gateway']) . ($paymentInfo['gateway_account_no'] > 1 ? ' (' . $paymentInfo['gateway_account_no'] . ')' : '');
              $paymentDetails['error_text'] = 'The order could not be refunded because it was processed with ' . $gatewayName . '.';
              return $paymentDetails;
          }

          $processor = $this->getPaymentProcessor($gatewaySetup->real_time_gateway);

          $processor->setAPIUserName($gatewaySetup->authorize_api_login_key);
          $processor->setAPIKey($gatewaySetup->authorize_api_transaction_key);
          $processor->setAPISignature($gatewaySetup->authorize_api_signature);

          $processor->setTransactionId( $paymentInfo['transactionID'] );
          $processor->setAuthorizationId($paymentInfo['auth_code']);
          $processor->setSecurityCode($paymentInfo['security_code']);

          if(isset($paymentInfo['vendor_tx_code']) && $paymentInfo['vendor_tx_code']) //specially for SagePay
          {
              $processor->setInvoiceNumber($paymentInfo['vendor_tx_code']);
          }
          else
          {
              $getInstance->load->model('orders/Order_model','',TRUE);
              $orderId = $getInstance->Order_model->getOrderId($orderRowId);
              $processor->setInvoiceNumber($orderId);
          }

          $processor->setAmount($paymentInfo['amount']);
          $processor->setCurrencyCode($paymentInfo['currency']);

          $getInstance->load->model('customers/Customer_Setup_model','',TRUE);
          $customer = $getInstance->Customer_Setup_model->getCustomerByOrderRowId($orderRowId);

          $processor->setFirstName($customer->first_name);
          $processor->setLastName($customer->last_name);
          $processor->setEmail($customer->customer_email);

          if(isset($paymentInfo['memo']) && $paymentInfo['memo'])
          {
              $processor->setNote($paymentInfo['memo']);
          }
          else
          {
              $processor->setNote('Refunding order of ' . $customer->first_name . ' ' . $customer->last_name);
          }

          $phone = '';

          if (! empty($customer->home_phone_number))
          {
              $phone = $customer->home_phone_number . ' (Home), ';
          }

          if (! empty($customer->work_phone_number))
          {
              $phone .= $customer->work_phone_number. ' (Work)';
          }

          $processor->setPhone($phone);
          $processor->setFax($customer->fax);
          $processor->setBillingAddress1($customer->address_line1);
          $customer->address_line2 = $customer->address_line2 ? $customer->address_line2 : '';
          $processor->setBillingAddress2($customer->address_line2);
          $processor->setCompanyName($customer->company_name);
          $processor->setBillingCity($customer->customer_city);
          $processor->setBillingState($customer->customer_state);
          $processor->setBillingZip($customer->customer_zip);
          $processor->setBillingCountry($customer->country);

          $processor->setNameOnCard($customer->card_holder_contactname);
          $processor->setCardNumber($customer->card_number);
          $processor->setExpiryMonth($customer->card_exp_month);
          $processor->setExpiryYear($customer->card_exp_year);

          if($this->config->item('test_mode') || in_array($gatewaySetup->merchant_row_id, $this->config->item('test_accounts')))
          {
              $processor->enableTestMode();
          }
          //$processor->enableDebugging();
          $response = $processor->refund();

          $paymentDetails['reason_code'] = $response->reasonCode;
          $paymentDetails['payment_gateway'] = $gatewaySetup->real_time_gateway;
          $paymentDetails['gateway_account_no'] = $gatewaySetup->real_gateway_account_no;
          $paymentDetails['amount'] = $response->amount;
          $paymentDetails['error_text'] = addslashes($response->getErrorString());

          $paymentDetails['version'] = $processor->getAPIVersion();
          $paymentDetails['auth_code'] = $response->authorizationId;
          $paymentDetails['transaction_id'] = $response->transactionId;
          $paymentDetails['transaction_type'] = $response->transactionType;
          $paymentDetails['response_code'] = $response->responseCode;
          $paymentDetails['response_reason_code'] = $response->reasonCode;

          $paymentDetails['success'] = $response->success;
          $paymentDetails['ack'] = $response->ack;
          $paymentDetails['curl_error_message'] = addslashes($response->curlErrorMessage);

          return $paymentDetails;
      }

      function chargeForMerchantSignUp($merchantData, $gatewaySetup, $isFromSignUp = 0, $test_mode = 0)
      {
          $processor = $this->getPaymentProcessor($gatewaySetup->real_time_gateway);


          $processor->setAPIUserName($gatewaySetup->paypal_api_user_name);
          $processor->setAPIKey($gatewaySetup->paypal_api_key);
          $processor->setAPISignature($gatewaySetup->paypal_api_signature);


          $processor->setFirstName($merchantData['first_name']);
          $processor->setLastName($merchantData['last_name']);
          $processor->setEmail($merchantData['email_address']);
          $processor->setCompanyName($merchantData['company_name']);
          $processor->setPhone($merchantData['merchant_contact_phone']);

          $processor->setEmail($merchantData['email_address']);
          $processor->setBillingAddress1($merchantData['billing_address']);
          $processor->setCompanyName($merchantData['company_name']);
          $processor->setBillingCity($merchantData['billing_city']);
          $processor->setBillingState($merchantData['billing_state']);
          $processor->setBillingZip($merchantData['billing_zip_code']);
          $processor->setBillingCountry($merchantData['billing_country']);


          $processor->setCardType($merchantData['card_type']);
          $processor->setCardNumber($merchantData['card_number']);
          $processor->setExpiryMonth($merchantData['card_exp_month']);
          $processor->setExpiryYear($merchantData['card_exp_year']);
          $processor->setNameOnCard($merchantData['card_holder_contactname']);
          $processor->setAmount($merchantData['signup_amount']);

          $processor->setNote($merchantData['description']);
          
          
          $processor->setCurrencyCode('USD');
          $processor->setInvoiceNumber($merchantData['invoice_id']);
          

          //echo 'TEST MODE:'.$this->config->item('test_mode');
          //die();

          if($this->config->item('test_mode'))
          {
              //echo 'TEST MODE HERE';
              //$processor->enableTestMode();
          }
          //echo '>> '.$this->config->item('test_mode').'<<';die();
          
          
          if($test_mode == 1){
          	$processor->enableTestMode();
          }
          
          //$processor->enableDebugging();
          //die('here in debugging mode');

          $response = $processor->sale();

          $paymentDetails['reason_code'] = $response->reasonCode;
          $paymentDetails['response_code'] = $response->responseCode;
          $paymentDetails['response_reason_code'] = $response->reasonCode;
          $paymentDetails['payment_gateway'] = $gatewaySetup->real_time_gateway;
          $paymentDetails['amount'] = $response->amount;
          $paymentDetails['curl_error_message'] = addslashes($response->curlErrorMessage);
          $paymentDetails['version'] = $processor->getAPIVersion();
          $paymentDetails['auth_code'] = $response->authorizationId;
          $paymentDetails['transaction_id'] = $response->transactionId;
          $paymentDetails['transaction_type'] = $response->transactionType;
          $paymentDetails['success'] = $response->success;
          $paymentDetails['ack'] = $response->ack;
          $paymentDetails['error_text'] = addslashes($response->getErrorString());
          $paymentDetails['description'] = $merchantData['description'];

          return $paymentDetails;
      }
  }
?>