Link to home
Start Free TrialLog in
Avatar of HunTelWebProgrammer
HunTelWebProgrammerFlag for United States of America

asked on

PHP Code If Then Statement help URGENT!

I am working on a site that is using osCommerce and using Payfuse to handle transactions.  The transactions are getting to Payfuse and processing correctly, however the cart is not completing the transaction and is reporting an error in the returned url, but it contains no error number.  When confirming the order, it returns the user to the payment screen with a URL parameter of error_ message which contains no value.  I am posting the class file, and the module file here.  Anyone that can help will be rewarded.  This needs to be fixed by Friday!  YIKES!  Thanks in advance for all your work!

Please Help!

MODULE FILE --------------------------------------------------------------------------------------------------------------------------------------
<?php

class payfuse {
  var $code, $title, $description, $enabled;

// class constructor
  function payfuse() {
      global $order;
    $this->code = 'payfuse';
     if ($HTTP_GET_VARS['main_page'] != '') {
       $this->title = MODULE_PAYMENT_PAYFUSE_TEXT_CATALOG_TITLE; // Payment module title in Catalog
     } else {
       $this->title = MODULE_PAYMENT_PAYFUSE_TEXT_ADMIN_TITLE; // Payment module title in Admin
     }
    $this->description = MODULE_PAYMENT_PAYFUSE_TEXT_DESCRIPTION;  // Descriptive Info about module in Admin
    $this->enabled = ((MODULE_PAYMENT_PAYFUSE_STATUS == 'True') ? true : false); // Whether the module is installed or not
    $this->sort_order = MODULE_PAYMENT_PAYFUSE_SORT_ORDER; // Sort Order of this payment option on the customer payment page
      $this->form_action_url = tep_href_link(FILENAME_CHECKOUT_PROCESS, '', 'SSL', false); // Page to go to upon submitting page info

        if ((int)MODULE_PAYMENT_PAYFUSE_ORDER_STATUS_ID > 0) {
        $this->order_status = MODULE_PAYMENT_PAYFUSE_ORDER_STATUS_ID;
      }

      if (is_object($order)) $this->update_status();

      }


// class methods

    function update_status() {
      global $order;

      if ( ($this->enabled == true) && ((int)MODULE_PAYMENT_PAYFUSE_ZONE > 0) ) {
        $check_flag = false;
        $check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_PAYMENT_PAYFUSE_ZONE . "' and zone_country_id = '" . $order->billing['country']['id'] . "' order by zone_id");
        while ($check = tep_db_fetch_array($check_query)) {
          if ($check['zone_id'] < 1) {
            $check_flag = true;
            break;
          } elseif ($check['zone_id'] == $order->billing['zone_id']) {
            $check_flag = true;
            break;
          }
        }

        if ($check_flag == false) {
          $this->enabled = false;
        }
      }
    }

    // Validate the credit card information via javascript (Number, Owner, and CVV Lengths)
    function javascript_validation() {
      $js = '  if (payment_value == "' . $this->code . '") {' . "\n" .
            '    var cc_owner = document.checkout_payment.payfuse_cc_owner.value;' . "\n" .
            '    var cc_number = document.checkout_payment.payfuse_cc_number.value;' . "\n";
     $js .= '    if (cc_owner == "" || cc_owner.length < ' . CC_OWNER_MIN_LENGTH . ') {' . "\n" .
            '      error_message = error_message + "' . MODULE_PAYMENT_PAYFUSE_TEXT_JS_CC_OWNER . '";' . "\n" .
            '      error = 1;' . "\n" .
            '    }' . "\n" .
            '    if (cc_number == "" || cc_number.length < ' . CC_NUMBER_MIN_LENGTH . ') {' . "\n" .
            '      error_message = error_message + "' . MODULE_PAYMENT_PAYFUSE_TEXT_JS_CC_NUMBER . '";' . "\n" .
            '      error = 1;' . "\n" .
            '    }' . "\n" ;

      return $js;
    }

    // Display Credit Card Information Submission Fields on the Checkout Payment Page
    function selection() {
      global $order;

      for ($i=1; $i<13; $i++) {
        $expires_month[] = array('id' => sprintf('%02d', $i), 'text' => strftime('%B',mktime(0,0,0,$i,1,2000)));
      }

      $today = getdate();
      for ($i=$today['year']; $i < $today['year']+10; $i++) {
        $expires_year[] = array('id' => strftime('%y',mktime(0,0,0,1,1,$i)), 'text' => strftime('%Y',mktime(0,0,0,1,1,$i)));
      }
        $selection = array('id' => $this->code,
                         'module' => MODULE_PAYMENT_PAYFUSE_TEXT_CATALOG_TITLE,
                                                 'fields' => array(array('title' => MODULE_PAYMENT_PAYFUSE_TEXT_CREDIT_CARD_OWNER,
                                                 'field' => tep_draw_input_field('payfuse_cc_owner', $order->billing['firstname'] . ' ' . $order->billing['lastname'])),
                                           array('title' => MODULE_PAYMENT_PAYFUSE_TEXT_CREDIT_CARD_NUMBER,
                                                 'field' => tep_draw_input_field('payfuse_cc_number')),
                                           array('title' => MODULE_PAYMENT_PAYFUSE_TEXT_CREDIT_CARD_EXPIRES,
                                                 'field' => tep_draw_pull_down_menu('payfuse_cc_expires_month', $expires_month) . '&nbsp;' . tep_draw_pull_down_menu('payfuse_cc_expires_year', $expires_year)),
                                           array('title' => MODULE_PAYMENT_PAYFUSE_TEXT_CVV . ' ' .'<a href="javascript:popupWindow(\'' . tep_href_link(FILENAME_POPUP_CVV_HELP) . '\')">' . MODULE_PAYMENT_PAYFUSE_TEXT_POPUP_CVV_LINK . '</a>',
                                                 'field' => tep_draw_input_field('payfuse_cc_cvv', '', "size=4, maxlength=4"))));

      return $selection;
    }


    // Evaluates the Credit Card Type for acceptance and the validity of the Credit Card Number & Expiration Date
    function pre_confirmation_check() {
      global $HTTP_POST_VARS;

      include(DIR_WS_CLASSES . 'cc_validation.php');

      $cc_validation = new cc_validation();
      $result = $cc_validation->validate($HTTP_POST_VARS['payfuse_cc_number'], $HTTP_POST_VARS['payfuse_cc_expires_month'], $HTTP_POST_VARS['payfuse_cc_expires_year'], $HTTP_POST_VARS['payfuse_cc_cvv']);
      $error = '';
      switch ($result) {
        case -1:
          $error = sprintf(TEXT_CCVAL_ERROR_UNKNOWN_CARD, substr($cc_validation->cc_number, 0, 4));
          break;
        case -2:
        case -3:
        case -4:
          $error = TEXT_CCVAL_ERROR_INVALID_DATE;
          break;
        case false:
          $error = TEXT_CCVAL_ERROR_INVALID_NUMBER;
          break;
      }

      if ( ($result == false) || ($result < 1) ) {
        $payment_error_return = 'payment_error=' . $this->code . '&error=' . urlencode($error) . '&payfuse_cc_owner=' . urlencode($HTTP_POST_VARS['payfuse_cc_owner']) . '&payfuse_cc_expires_month=' . $HTTP_POST_VARS['payfuse_cc_expires_month'] . '&payfuse_cc_expires_year=' . $HTTP_POST_VARS['payfuse_cc_expires_year'];

        tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, $payment_error_return, 'SSL', true, false));
      }

      $this->cc_card_type = $cc_validation->cc_type;
      $this->cc_card_number = $cc_validation->cc_number;
      $this->cc_expiry_month = $cc_validation->cc_expiry_month;
      $this->cc_expiry_year = $cc_validation->cc_expiry_year;
    }

    // Display Credit Card Information on the Checkout Confirmation Page
      function confirmation() {
      global $HTTP_POST_VARS;

          $confirmation = array(//'title' => MODULE_PAYMENT_PAYFUSE_TEXT_CATALOG_TITLE, // Redundant
                                                    'fields' => array(array('title' => MODULE_PAYMENT_PAYFUSE_TEXT_CREDIT_CARD_OWNER,
                                                    'field' => $HTTP_POST_VARS['payfuse_cc_owner']),
                                            array('title' => MODULE_PAYMENT_PAYFUSE_TEXT_CREDIT_CARD_NUMBER,
                                                    'field' => substr($this->cc_card_number, 0, 4) . str_repeat('X', (strlen($this->cc_card_number) - 8)) . substr($this->cc_card_number, -4)),
                                              array('title' => MODULE_PAYMENT_PAYFUSE_TEXT_CREDIT_CARD_EXPIRES,
                                                    'field' => strftime('%B, %Y', mktime(0,0,0,$HTTP_POST_VARS['payfuse_cc_expires_month'], 1, '20' . $HTTP_POST_VARS['payfuse_cc_expires_year'])))));

      return $confirmation;
    }

    function process_button() {
      global $HTTP_POST_VARS;
      // These are hidden fields on the checkout confirmation page
        $process_button_string = tep_draw_hidden_field('cc_owner', $HTTP_POST_VARS['payfuse_cc_owner']) .
                                   tep_draw_hidden_field('cc_expires', $this->cc_expiry_month . substr($this->cc_expiry_year, -2)) .
                                   tep_draw_hidden_field('cc_expires_month', $this->cc_expiry_month) .
                                   tep_draw_hidden_field('cc_expires_year', substr($this->cc_expiry_year, -2)) .
                                   tep_draw_hidden_field('cc_type', $this->cc_card_type) .
                                   tep_draw_hidden_field('cc_number', $this->cc_card_number) .
                                   tep_draw_hidden_field('cc_cvv', $HTTP_POST_VARS['payfuse_cc_cvv']);
      $process_button_string .= tep_draw_hidden_field(tep_session_name(), tep_session_id());

    return $process_button_string;
  }

    function before_process() {
          global $HTTP_POST_VARS, $HTTP_SERVER_VARS, $order, $currency, $customer_id;

      $order->info['cc_type'] = $HTTP_POST_VARS['cc_type'];
      $order->info['cc_owner'] = $HTTP_POST_VARS['cc_owner'];
      $order->info['cc_cvv'] = $HTTP_POST_VARS['cc_cvv'];

      // Build Info to send to Gateway
      require_once(DIR_WS_MODULES . 'payment/payfuse/class.payfuse.php');

    $pf = new PayfusePayment;

    $pf->setGatewayURL(((MODULE_PAYMENT_PAYFUSE_TRANSACTION_MODE == 'Production') ? 'https://xmlic.payfuse.com:443' : 'https://test5x.clearcommerce.com:11500'));
      $pf->setStoreID(MODULE_PAYMENT_PAYFUSE_STORE_ID);
      $pf->setPassPhrase(MODULE_PAYMENT_PAYFUSE_PASSPHRASE); // Assures authenticity
      $pf->setClientID(MODULE_PAYMENT_PAYFUSE_CLIENT_ID);
      $pf->setPaymentType('CC');
      $pf->setCardAction(((MODULE_PAYMENT_PAYFUSE_AUTHORIZATION_MODE == 'Authorize') ? '1' : '0'));
      $pf->setSubTotal($order->info['total']);
      $pf->setCardNumber($HTTP_POST_VARS['cc_number']);
      $pf->setCardExpMonth($HTTP_POST_VARS['cc_expires_month']);
      $pf->setCardExpYear($HTTP_POST_VARS['cc_expires_year']);
      $pf->setUserID($customer_id);
      $pf->setBname($order->billing['firstname'] . ' ' . $order->billing['lastname']);
      $pf->setBcompany($order->billing['company']);
      $pf->setBaddress1($order->billing['street_address']);
      $pf->setBaddress2($order->billing['suburb']);
      $pf->setBcity($order->billing['city']);
      $pf->setBprovince($order->billing['state']);
      $pf->setBpostalCode($order->billing['postcode']);
      $pf->setBcountry($order->billing['country']['iso_code_2']);
      $pf->setSname($order->delivery['firstname'] . ' ' . $order->delivery['lastname']);
      $pf->setScompany($order->delivery['company']);
      $pf->setSaddress1($order->delivery['street_address']);
      $pf->setSaddress2($order->delivery['suburb']);
      $pf->setScity($order->delivery['city']);
      $pf->setSprovince($order->delivery['state']);
      $pf->setSpostalCode($order->delivery['postcode']);
      $pf->setScountry($order->delivery['country']['iso_code_2']);
      $pf->setPhone($order->customer['telephone']);
      $pf->setEmail($order->customer['email_address']);
      $pf->setComments("OsCommerce XML Interface 1.0");
      $pf->setCustomerIP($HTTP_SERVER_VARS['REMOTE_ADDR']);
      $pf->setCardIDCode('1');
      $pf->setCardIDNumber($HTTP_POST_VARS['cc_cvv']);

        if (MODULE_PAYMENT_PAYFUSE_TRANSACTION_MODE == 'Test') {
      switch (MODULE_PAYMENT_PAYFUSE_TRANSACTION_TEST_RESULT) {
    case "Approved": $pf->setMode('Y'); break;
    case "Declined": $pf->setMode('N'); break;
    case "Random": $pf->setMode('R'); break;
    //case "Fraud": $pf->setTestResult('F'); break;
      }
    }
       else
             $pf->setMode('P');

      // Send transaction data to the gateway
    $pf_xml_error = ($pf->doPayment() != PAYFUSE_TRANSACTION_OK);

      // Gateway Response fields in values to put into the database storage
      /*$Pf_TransTime = $pf->getTrxnTransTime();
      $Pf_OrderID = $pf->getTrxnOrderID();
      $Pf_Approved = $pf->getTrxnApproved();
      $Pf_ReturnCode = $pf->getTrxnReturnCode();
      $Pf_ErrMsg = $pf->getTrxnErrMsg();
      $Pf_TaxTotal = $pf->getTrxnTaxTotal();
      $Pf_ShipTotal = $pf->getTrxnShipTotal();
      $Pf_SubTotal = $pf->getTrxnSubTotal();
      $Pf_FullTotal = $pf->getTrxnFullTotal();
      $Pf_PaymentType = $pf->getTrxnPaymentType();
      $Pf_CardNumber = $pf->getTrxnCardNumber();
      $Pf_CardExpMonth = $pf->getTrxnCardExpMonth();
      $Pf_CardExpYear = $pf->getTrxnCardExpYear();
      $Pf_RefNumber = $pf->getTrxnTransRefNumber();
      $Pf_CardIDResult = $pf->getTrxnCardIDResult();
      $Pf_AVSResult = $pf->getTrxnAVSResult();
      $Pf_CardAuthNumber = $pf->getTrxnCardAuthNumber();
      $Pf_CardRefNumber = $pf->getTrxnCardRefNumber();
      $Pf_CardType = $pf->getTrxnCardType();
      $Pf_IPResult = $pf->getTrxnIPResult();
      $Pf_IPCountry = $pf->getTrxnIPCountry();
      $Pf_IPRegion = $pf->getTrxnIPRegion();
      $Pf_IPCity = $pf->getTrxnIPCity();*/

      // Do we want to store the responses from the gateway in the database ?
//        if (MODULE_PAYMENT_PAYFUSE_STORE_DATA == 'True') {
//          tep_db_query("insert into " . TABLE_PAYFUSE . " (id, customer_id, osc_order_id, payfuse_order_id, transaction_time, approved, return_code, error_message, trans_ref, cvv_result, avs_result, card_auth, card_ref, ip_result, ip_country, ip_region, ip_city) values ('', '" . $HTTP_SESSION_VARS['customer_id'] . "', '', '" . $Pf_OrderID . "', '" . $Pf_TransTime . "', '" . $Pf_Approved . "', '" . $Pf_ReturnCode . "', '" . $Pf_ErrMsg . "', '" . $Pf_RefNumber . "', '" . $Pf_CardIDResult . "', '" . $Pf_AVSResult . "', '" . $Pf_CardAuthNumber . "', '" . $Pf_CardRefNumber . "', '" . $Pf_IPResult . "', '" . $Pf_IPCountry . "', '" . $Pf_IPRegion . "', '" . $Pf_IPCity . "')");
//        }

      // Transaction was not approved.  Send to checkout_payment page again and display error to customer
      if ($pf_xml_error) {
        tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . $Pf_ErrMsg . ' - ' . urlencode(MODULE_PAYMENT_PAYFUSE_TEXT_DECLINED_MESSAGE), 'SSL', true, false));
      }
  }

    function after_process() {
        return false;
    }

    function get_error() {
      global $HTTP_GET_VARS;

      $error = array('title' => MODULE_PAYMENT_PAYFUSE_TEXT_ERROR,
                     'error' => stripslashes(urldecode($HTTP_GET_VARS['error'])));

      return $error;
    }

    function check() {
      if (!isset($this->_check)) {
        $check_query = tep_db_query("select configuration_value from " . TABLE_CONFIGURATION . " where configuration_key = 'MODULE_PAYMENT_PAYFUSE_STATUS'");
        $this->_check = tep_db_num_rows($check_query);
      }
      return $this->_check;
    }

    function install() {
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Payfuse Payment Module', 'MODULE_PAYMENT_PAYFUSE_STATUS', 'True', 'Do you want to accept Payfuse credit card payments?', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
//      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Payfuse Store ID - CAD', 'MODULE_PAYMENT_PAYFUSE_STORE_ID_CAD', 'teststore', 'Please enter your Payfuse Store ID for CAD currency', '6', '6', now())");
//      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Payfuse Passphrase - CAD', 'MODULE_PAYMENT_PAYFUSE_PASSPHRASE_CAD', 'psigate1234', 'Please enter your Payfuse Passphrase for CAD currency', '6', '6', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Payfuse Store ID', 'MODULE_PAYMENT_PAYFUSE_STORE_ID', 'teststore', 'Please enter your Payfuse Store ID', '6', '6', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Payfuse Passphrase', 'MODULE_PAYMENT_PAYFUSE_PASSPHRASE', 'psigate1234', 'Please enter your Payfuse Passphrase', '6', '6', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Payfuse Client ID', 'MODULE_PAYMENT_PAYFUSE_CLIENT_ID', '1234', 'Please enter your Payfuse Client ID', '6', '6', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Transaction Mode', 'MODULE_PAYMENT_PAYFUSE_TRANSACTION_MODE', 'Production', 'Transaction mode used for processing orders', '6', '0', 'tep_cfg_select_option(array(\'Test\', \'Production\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Test Mode Result', 'MODULE_PAYMENT_PAYFUSE_TRANSACTION_TEST_RESULT', 'Approved', 'Simulated responses when in test mode.  Not used when in production mode.', '6', '5', 'tep_cfg_select_option(array(\'Approved\', \'Declined\', \'Random\'), ', now())");
//      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Authorization Type', 'MODULE_PAYMENT_PAYFUSE_AUTHORIZATION_MODE', 'Authorize', 'Do you want submitted credit card transactions to be authorized only, or immediately charge?', '6', '0', 'tep_cfg_select_option(array(\'Authorize\', \'Immediate Charge\'), ', now())");
//      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Database Storage', 'MODULE_PAYMENT_PAYFUSE_STORE_DATA', 'False', 'Do you want to save the gateway data to the database? (Note: You must add a table first)', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort order of display.', 'MODULE_PAYMENT_PAYFUSE_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Payment Zone', 'MODULE_PAYMENT_PAYFUSE_ZONE', '0', 'If a zone is selected, only enable this payment method for that zone.', '6', '2', 'tep_get_zone_class_title', 'tep_cfg_pull_down_zone_classes(', now())");
      tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, use_function, date_added) values ('Set Order Status', 'MODULE_PAYMENT_PAYFUSE_ORDER_STATUS_ID', '0', 'Set the status of orders made with this payment module to this value', '6', '0', 'tep_cfg_pull_down_order_statuses(', 'tep_get_order_status_name', now())");
    }

    function remove() {
      tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }

    function keys() {
      return array(
            'MODULE_PAYMENT_PAYFUSE_STATUS',
            //'MODULE_PAYMENT_PAYFUSE_STORE_ID_CAD',
            //'MODULE_PAYMENT_PAYFUSE_PASSPHRASE_CAD',
            'MODULE_PAYMENT_PAYFUSE_STORE_ID',
            'MODULE_PAYMENT_PAYFUSE_PASSPHRASE',
            'MODULE_PAYMENT_PAYFUSE_CLIENT_ID',
            'MODULE_PAYMENT_PAYFUSE_TRANSACTION_MODE',
            'MODULE_PAYMENT_PAYFUSE_TRANSACTION_TEST_RESULT',
            //'MODULE_PAYMENT_PAYFUSE_AUTHORIZATION_MODE',
            //'MODULE_PAYMENT_PAYFUSE_STORE_DATA',
            'MODULE_PAYMENT_PAYFUSE_SORT_ORDER',
            'MODULE_PAYMENT_PAYFUSE_ZONE',
            'MODULE_PAYMENT_PAYFUSE_ORDER_STATUS_ID');

 }
  }
?>


CLASS FILE --------------------------------------------------------------------------------------------------------------------------------------
<?php

define( 'PAYFUSE_CURL_ERROR_OFFSET', 1000 );
define( 'PAYFUSE_ERROR_OFFSET',  2000 );

define( 'PAYFUSE_TRANSACTION_OK',       APPROVED );
define( 'PAYFUSE_TRANSACTION_DECLINED',   DECLINED );
define( 'PAYFUSE_TRANSACTION_ERROR',  ERROR );

class PayfusePayment {

    var $parser;
    var $xmlData;
    var $currentTag;

    var $myGatewayURL;
    var $myStoreID;
    var $myPassphrase;
       var $myClientID;
    var $myPaymentType;
    var $myCardAction;
    var $mySubtotal;
    var $myCardNumber;
    var $myCardExpMonth;
    var $myCardExpYear;
    var $myCardIDCode;
    var $myCardIDNumber;
    var $myMode;
    var $myOrderID;
    var $myUserID;
    var $myBname;
    var $myBcompany;
    var $myBaddress1;
    var $myBaddress2;
    var $myBcity;
    var $myBprovince;
    var $myBpostalcode;
    var $myBcountry;
    var $mySname;
    var $myScompany;
    var $mySaddress1;
    var $mySaddress2;
    var $myScity;
    var $mySprovince;
    var $mySpostalcode;
    var $myScountry;
    var $myPhone;
    var $myFax;
    var $myEmail;
    var $myComments;
    var $myCustomerIP;

    //var $myResultTrxnTransTime;
    //var $myResultTrxnOrderID;
    //var $myResultTrxnApproved;
    //var $myResultTrxnReturnCode;
    //var $myResultTrxnErrMsg;
    //var $myResultTrxnTaxTotal;
    //var $myResultTrxnShipTotal;
    //var $myResultTrxnSubTotal;
    //var $myResultTrxnFullTotal;
    //var $myResultTrxnPaymentType;
    //var $myResultTrxnCardNumber;
    //var $myResultTrxnCardExpMonth;
    //var $myResultTrxnCardExpYear;
    //var $myResultTrxnTransRefNumber;
    //var $myResultTrxnCardIDResult;
    //var $myResultTrxnAVSResult;
    //var $myResultTrxnCardAuthNumber;
    //var $myResultTrxnCardRefNumber;
    //var $myResultTrxnCardType;
    //var $myResultTrxnIPResult;
    //var $myResultTrxnIPCountry;
    //var $myResultTrxnIPRegion;
    //var $myResultTrxnIPCity;
       var $myResultText;
       var $myResultProcReturnMsg;

    var $myError;
    var $myErrorMessage;

    /***********************************************************************
     *** XML Parser - Callback functions                                 ***
     ***********************************************************************/

    function ElementStart ($parser, $tag, $attributes) {
        $this->currentTag = $tag;
    }

    function ElementEnd ($parser, $tag) {
        $this->currentTag = "";
    }

    function charachterData ($parser, $cdata) {
        $this->xmlData[$this->currentTag] = $cdata;
    }

    /***********************************************************************
     *** SET values to send to PsiGate                                   ***
     ***********************************************************************/

   function setGatewayURL($GatewayURL) {
      $this->myGatewayURL = $GatewayURL;
   }

    function setStoreID( $StoreID ) {
        $this->myStoreID = $StoreID;
    }

    function setPassphrase( $Passphrase ) {
        $this->myPassphrase = $Passphrase;
    }

    function setClientID( $ClientID ) {
        $this->myClientID = $ClientID;
    }

    function setPaymentType( $PaymentType ) {
        $this->myPaymentType = $PaymentType;
    }

    function setCardAction( $CardAction ) {
        $this->myCardAction = $CardAction;
    }

    function setSubtotal( $Subtotal ) {
        $this->mySubtotal = $Subtotal;
    }

    function setCardNumber( $CardNumber ) {
        $this->myCardNumber = $CardNumber;
    }

    function setCardExpMonth( $CardExpMonth ) {
        $this->myCardExpMonth = $CardExpMonth;
    }

    function setCardExpYear( $CardExpYear ) {
        $this->myCardExpYear = $CardExpYear;
    }

    function setCardIDCode( $CardIDCode ) {
        $this->myCardIDCode = $CardIDCode;
    }

    function setCardIDNumber( $CardIDNumber ) {
        $this->myCardIDNumber = $CardIDNumber;
    }

    function setMode( $TestResult ) {
        $this->myMode = $TestResult;
    }

    function setOrderID( $OrderID ) {
        $this->myOrderID = $OrderID;
    }

    function setUserID( $UserID ) {
        $this->myUserID = $UserID;
    }

    function setBname( $Bname ) {
        $this->myBname = $Bname;
    }

    function setBcompany( $Bcompany ) {
        $this->myBcompany = $Bcompany;
    }

    function setBaddress1( $Baddress1 ) {
        $this->myBaddress1 = $Baddress1;
    }

    function setBaddress2( $Baddress2 ) {
        $this->myBaddress2 = $Baddress2;
    }

    function setBcity( $Bcity ) {
        $this->myBcity = $Bcity;
    }

    function setBprovince( $Bprovince ) {
        $this->myBprovince = $Bprovince;
    }

    function setBpostalcode( $Bpostalcode) {
      $this->myBpostalcode = $Bpostalcode;
    }

    function setBcountry( $Bcountry) {
      $this->myBcountry = $Bcountry;
    }

    function setSname( $Sname) {
      $this->mySname = $Sname;
    }

    function setScompany( $Scompany) {
      $this->myScompany = $Scompany;
    }

    function setSaddress1( $Saddress1) {
      $this->mySaddress1 = $Saddress1;
    }

    function setSaddress2( $Saddress2) {
      $this->mySaddress2 = $Saddress2;
    }

    function setScity( $Scity) {
      $this->myScity = $Scity;
    }

    function setSprovince( $Sprovince) {
      $this->mySprovince = $Sprovince;
    }

    function setSpostalcode( $Spostalcode) {
      $this->mySpostalcode = $Spostalcode;
    }

    function setScountry( $Scountry) {
      $this->myScountry = $Scountry;
    }

    function setPhone( $Phone) {
      $this->myPhone = $Phone;
    }

    function setFax( $Fax) {
      $this->myFax = $Fax;
    }

    function setEmail( $Email) {
      $this->myEmail = $Email;
    }

    function setComments( $Comments) {
      $this->myComments = $Comments;
    }

    function setCustomerIP( $CustomerIP) {
      $this->myCustomerIP = $CustomerIP;
    }

    /***********************************************************************
     *** GET values returned by PsiGate                                  ***
     ***********************************************************************/

/*    function getTrxnTransTime() {
        return $this->myResultTrxnTransTime;
    }

    function getTrxnOrderID() {
        return $this->myResultTrxnOrderID;
    }

    function getTrxnApproved() {
        return $this->myResultTrxnApproved;
    }

    function getTrxnReturnCode() {
        return $this->myResultTrxnReturnCode;
    }

    function getTrxnErrMsg() {
        return $this->myResultTrxnErrMsg;
    }

    function getTrxnTaxTotal() {
        return $this->myResultTrxnTaxTotal;
    }

    function getTrxnShipTotal() {
        return $this->myResultTrxnShipTotal;
    }

    function getTrxnSubTotal() {
        return $this->myResultTrxnSubTotal;
    }

    function getTrxnFullTotal() {
        return $this->myResultTrxnFullTotal;
    }

    function getTrxnPaymentType() {
        return $this->myResultTrxnPaymentType;
    }

    function getTrxnCardNumber() {
        return $this->myResultTrxnCardNumber;
    }

    function getTrxnCardExpMonth() {
        return $this->myResultTrxnCardExpMonth;
    }

    function getTrxnCardExpYear() {
        return $this->myResultTrxnCardExpYear;
    }

    function getTrxnTransRefNumber() {
        return $this->myResultTrxnTransRefNumber;
    }

    function getTrxnCardIDResult() {
        return $this->myResultTrxnCardIDResult;
    }

    function getTrxnAVSResult() {
        return $this->myResultTrxnAVSResult;
    }

    function getTrxnCardAuthNumber() {
        return $this->myResultTrxnCardAuthNumber;
    }

    function getTrxnCardRefNumber() {
        return $this->myResultTrxnCardRefNumber;
    }

    function getTrxnCardType() {
        return $this->myResultTrxnCardType;
    }

    function getTrxnIPResult() {
        return $this->myResultTrxnIPResult;
    }

    function getTrxnIPCountry() {
        return $this->myResultTrxnIPCountry;
    }

    function getTrxnIPRegion() {
        return $this->myResultTrxnIPRegion;
    }

    function getTrxnIPCity() {
        return $this->myResultTrxnIPCity;
    }
*/

//      function getResultText() { return $this->myResultText; }
//      function getResultProcReturnMsg() { return $this->myResultProcReturnMsg; }
      
    function getError() {

        if( $this->myError != 0 ) {
            // Internal Error
            return $this->myError;
        } else {
                        if($this->myResultProcReturnMsg == 'Approved')
                              return PAYFUSE_TRANSACTION_OK;
                        else if($this->myResultProcReturnMsg == 'Declined')
                              return PAYFUSE_TRANSACTION_DECLINED;
                        else
                              return PAYFUSE_TRANSACTION_ERROR;
                  }
    }

    function getErrorMessage() {

        if( $this->myError != 0 ) {
            // Internal Error
            return $this->myErrorMessage;
        } else {
                    if(!empty($this->myResultText))
                        return $this->myResultText;
                  else
                        return $this->myResultProcReturnMsg;
        }
    }

    /***********************************************************************
     *** Class Constructor                                               ***
     ***********************************************************************/

    function PayfusePayment() {
    }

    /***********************************************************************
     *** Business Logic                                                  ***
     ***********************************************************************/

    function doPayment() {
/*        $xmlRequest = "<Order>".
                "<StoreID>".htmlentities( $this->myStoreID )."</StoreID>".
                "<Passphrase>".htmlentities( $this->myPassphrase)."</Passphrase>".
                "<Subtotal>".htmlentities( $this->mySubtotal )."</Subtotal>".
                "<PaymentType>".htmlentities( $this->myPaymentType )."</PaymentType>".
                "<CardAction>".htmlentities( $this->myCardAction )."</CardAction>".
                "<CardNumber>".htmlentities( $this->myCardNumber )."</CardNumber>".
                "<CardExpMonth>".htmlentities( $this->myCardExpMonth )."</CardExpMonth>".
                "<CardExpYear>".htmlentities( $this->myCardExpYear )."</CardExpYear>".
                "<CardIDCode>".htmlentities( $this->myCardIDCode )."</CardIDCode>".
                "<CardIDNumber>".htmlentities( $this->myCardIDNumber )."</CardIDNumber>".
                "<TestResult>".htmlentities( $this->myTestResult )."</TestResult>".
                "<OrderID>".htmlentities( $this->myOrderID )."</OrderID>".
                "<UserID>".htmlentities( $this->myUserID )."</UserID>".
                "<Bname>".htmlentities( $this->myBname )."</Bname>".
                "<Bcompany>".htmlentities( $this->myBcompany )."</Bcompany>".
                "<Baddress1>".htmlentities( $this->myBaddress1 )."</Baddress1>".
                "<Baddress2>".htmlentities( $this->myBaddress2 )."</Baddress2>".
                "<Bcity>".htmlentities( $this->myBcity )."</Bcity>".
                "<Bprovince>".htmlentities( $this->myBprovince )."</Bprovince>".
                "<Bpostalcode>".htmlentities( $this->myBpostalcode )."</Bpostalcode>".
                "<Bcountry>".htmlentities( $this->myBcountry )."</Bcountry>".
                "<Sname>".htmlentities( $this->mySname )."</Sname>".
                "<Scompany>".htmlentities( $this->myScompany )."</Scompany>".
                "<Saddress1>".htmlentities( $this->mySaddress1 )."</Saddress1>".
                "<Saddress2>".htmlentities( $this->mySaddress2 )."</Saddress2>".
                "<Scity>".htmlentities( $this->myScity )."</Scity>".
                "<Sprovince>".htmlentities( $this->mySprovince )."</Sprovince>".
                "<Spostalcode>".htmlentities( $this->mySpostalcode )."</Spostalcode>".
                "<Scountry>".htmlentities( $this->myScountry )."</Scountry>".
                "<Phone>".htmlentities( $this->myPhone )."</Phone>".
                "<Email>".htmlentities( $this->myEmail )."</Email>".
                "<Comments>".htmlentities( $this->myComments )."</Comments>".
                "<CustomerIP>".htmlentities( $this->myCustomerIP )."</CustomerIP>".
        "</Order>";*/
       $xmlRequest['CLRCMRC_XML'] = 'CLRCMRC_XML=
<EngineDocList>
  <DocVersion>1.0</DocVersion>
  <EngineDoc>
    <ContentType>OrderFormDoc</ContentType>
    <User>
      <Name>'.htmlentities($this->myStoreID).'</Name>
      <Password>'.htmlentities($this->myPassphrase).'</Password>
      <Alias>'.htmlentities($this->myClientID).'</Alias>
    </User>
    <Instructions>
      <Pipeline>PaymentNoFraud</Pipeline>
    </Instructions>
    <OrderFormDoc>
      <Mode>'.htmlentities($this->myMode).'</Mode>
      <Consumer>
        <PaymentMech>
          <CreditCard>
            <Number>'.htmlentities($this->myCardNumber).'</Number>
            <Expires DataType="ExpirationDate" Locale="840">'.htmlentities($this->myCardExpMonth).'/'.htmlentities($this->myCardExpYear).'</Expires>
            <Cvv2Val>'.htmlentities($this->myCardIDNumber).'</Cvv2Val>
            <Cvv2Indicator>1</Cvv2Indicator>
          </CreditCard>
        </PaymentMech>
        <BillTo>
          <Location>
            <Address>
              <Name>'.htmlentities($this->myBname).'</Name>
              <Street1>'.htmlentities($this->myBaddress1).'</Street1>
              <Street2>'.htmlentities($this->myBaddress2).'</Street2>
              <City>'.htmlentities($this->myBcity).'</City>
              <StateProv>'.htmlentities($this->myBprovince).'</StateProv>
              <PostalCode>'.htmlentities($this->myBpostalCode).'</PostalCode>
              <Country>840</Country>
              <Company>'.htmlentities($this->myBcompany).'</Company>
            </Address>
          </Location>
        </BillTo>
      </Consumer>
      <Transaction>
        <Type>Auth</Type>
        <CurrentTotals>
          <Totals>
            <Total DataType="Money" Currency="840">'.(int)($this->mySubtotal * 100).'</Total>
          </Totals>
        </CurrentTotals>
      </Transaction>
    </OrderFormDoc>
  </EngineDoc>
</EngineDocList>';

        /* Use CURL to execute XML POST and write output into a string */
        $ch = curl_init( $this->myGatewayURL );
        curl_setopt( $ch, CURLOPT_POST, 1 );
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $xmlRequest );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_TIMEOUT, 240 );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
        curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
        $xmlResponse = curl_exec( $ch );

        // Check whether the curl_exec worked.
        if( curl_errno( $ch ) == CURLE_OK ) {
            // It worked, so setup an XML parser for the result.
            $this->parser = xml_parser_create();

            // Disable XML tag capitalisation (Case Folding)
            xml_parser_set_option ($this->parser, XML_OPTION_CASE_FOLDING, FALSE);

            // Define Callback functions for XML Parsing
            xml_set_object($this->parser, &$this);
            xml_set_element_handler ($this->parser, "ElementStart", "ElementEnd");
            xml_set_character_data_handler ($this->parser, "charachterData");

            // Parse the XML response
            xml_parse($this->parser, $xmlResponse, TRUE);

            if( xml_get_error_code( $this->parser ) == XML_ERROR_NONE ) {
                // Get the result into local variables.
//                $this->myResultTrxnTransTime = $this->xmlData['TransTime'];
//                $this->myResultTrxnOrderID = $this->xmlData['OrderID'];
//                $this->myResultTrxnApproved = $this->xmlData['Approved'];
//                $this->myResultTrxnReturnCode = $this->xmlData['ReturnCode'];
//                $this->myResultTrxnErrMsg = $this->xmlData['ErrMsg'];
//                $this->myResultTrxnTaxTotal = $this->xmlData['TaxTotal'];
//                $this->myResultTrxnShipTotal = $this->xmlData['ShipTotal'];
//                $this->myResultTrxnSubTotal = $this->xmlData['SubTotal'];
//                $this->myResultTrxnFullTotal = $this->xmlData['FullTotal'];
//                $this->myResultTrxnPaymentType = $this->xmlData['PaymentType'];
//                $this->myResultTrxnCardNumber = $this->xmlData['CardNumber'];
//                $this->myResultTrxnCardExpMonth = $this->xmlData['CardExpMonth'];
//                $this->myResultTrxnCardExpYear = $this->xmlData['CardExpYear'];
//                $this->myResultTrxnTransRefNumber = $this->xmlData['TransRefNumber'];
//                $this->myResultTrxnCardIDResult = $this->xmlData['CardIDResult'];
//                $this->myResultTrxnAVSResult = $this->xmlData['AVSResult'];
//                $this->myResultTrxnCardAuthNumber = $this->xmlData['CardAuthNumber'];
//                $this->myResultTrxnCardRefNumber = $this->xmlData['CardRefNumber'];
//                $this->myResultTrxnCardType = $this->xmlData['CardType'];
//                $this->myResultTrxnIPResult = $this->xmlData['IPResult'];
//                $this->myResultTrxnIPCountry = $this->xmlData['IPCountry'];
//                $this->myResultTrxnIPRegion = $this->xmlData['IPRegion'];
//                $this->myResultTrxnIPCity = $this->xmlData['IPCity'];
                              $this->myResultText = $this->xmlData['Text'];
                              $this->myResultProcReturnMsg = $this->xmlData['ProcReturnMsg'];
                $this->myError = 0;
                $this->myErrorMessage = '';
            } else {
                // An XML error occured. Return the error message and number.
                $this->myError = xml_get_error_code( $this->parser ) + PAYFUSE_ERROR_OFFSET;
                $this->myErrorMessage = xml_error_string( $myError );
            }
            // Clean up our XML parser
            xml_parser_free( $this->parser );
        } else {
            // A CURL Error occured. Return the error message and number. (offset so we can pick the error apart)
            $this->myError = curl_errno( $ch ) + PAYFUSE_CURL_ERROR_OFFSET;
            $this->myErrorMessage = curl_error( $ch );
        }
        // Clean up CURL, and return any error.
        curl_close( $ch );

    /***********************************************************************
     *** Optional commented-out Debug.  Dont mess with it.               ***
     ***********************************************************************/

//echo $xmlRequest['CLRCMRC_XML']."\n\n\n";
//echo $xmlResponse."\n\n\n";
//echo $this->myResultText."\n\n\n";
//echo $this->myResultProcReturnMsg."\n\n\n";
//exit();

        return $this->getError();
    }
}
?>

Avatar of BogoJoker
BogoJoker

I'm sorry I have not found a solution but I noticed that your useing:
$HTTP_GET_VARS
$HTTP_SERVER_VARS
$HTTP_POST_VARS
The new versions of these:
$_GET
$_SERVER
$_POST

They are superglobals and do not need to be made global.  Don't change your code.  Maybe your php is not the most current version, just thought I should mention it.
Do you have any idea at all which function is failing?
I understand you aren't getting any error message,
but you have to narrow it down some more.

If you can reproduce the error, then you should
trace through the process and add var_dump
commands to each function. Assuming you aren't
using Zend Studio, in which case you can set
break points.

Make note of which functions seem to complete
successfully and which one does not. Then
see what data is in the variables for that failing
function.

Hopefully, this gives you some insight on
troubleshooting you're issue.

--brian
Avatar of HunTelWebProgrammer

ASKER

I can tell you this is the redirect lines that is causing it....
      //if ($pf_xml_error) {
          //  tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . $result . ' - ' . urlencode(MODULE_PAYMENT_PAYFUSE_TEXT_DECLINED_MESSAGE), 'SSL', true, false));      
       // tep_redirect(tep_href_link(FILENAME_CHECKOUT_PAYMENT, 'error_message=' . $Pf_ErrMsg . ' - ' . urlencode(MODULE_PAYMENT_PAYFUSE_TEXT_DECLINED_MESSAGE), 'SSL', true, false));
      //}

However I am not sure of the error that is being caused.  When I comment these lines out all goes well.  Otherwise it seems to fail 100% of the time, even thought the funds are changing hands sucessfully.  If I check the variable $pf_xml_error it evalutes to 1.  So we need to trap out the error number one????

I have no idea....unfortunatly I am not a PHP coder, I am jsut trying to make this work before I head from this company.  Thanks for your help so far!  
This is where $pf_xml_error is set:
$pf_xml_error = ($pf->doPayment() != PAYFUSE_TRANSACTION_OK);

If doPayment()'s output does not match the value of PAYFUSE_TRANSACTION_OK,
then $pf_xml_error should equal 1 (effectively boolean True). If they do equal,
it should set $pf_xml_error to 0 (boolean false and basically NULL in PHP).

The doPayment() function will return one of three constants:
    PAYFUSE_TRANSACTION_OK;
    PAYFUSE_TRANSACTION_DECLINED;
    PAYFUSE_TRANSACTION_ERROR;

Since the payment goes through, my guess is that you are not returning DECLINED.
So, that leaves ERROR. The trick now it to get the error to show itself.

In the class.payfuse.php file I would look at the doPayment() function near the bottom:

                $this->myErrorMessage = xml_error_string( $myError );
            }
            // Clean up our XML parser
            xml_parser_free( $this->parser );
        } else {
            // A CURL Error occured. Return the error message and number. (offset so we can pick the error apart)
            $this->myError = curl_errno( $ch ) + PAYFUSE_CURL_ERROR_OFFSET;
            $this->myErrorMessage = curl_error( $ch );

After each $this->myErrorMessage is set, I would probably var_dump($this->myErrorMessage)
to see what is going on. You may have to pipe them out to a file instead, but that is the place
where I think the error must be. If you can read it, maybe it will help you fix this thing.

--brian
I added the following code after each one at a time, replacing the var, and the text file always stayed blank.


                        $your_data = var_dump($this->myError);

                        // Open the file and erase the contents if any
                        $fp = fopen("error_log.txt", "w");

                        // Write the data to the file
                        fwrite($fp, $your_data);
      
                        // Close the file
                        fclose($fp);


Maybe I am doing ti wrong.  
ASKER CERTIFIED SOLUTION
Avatar of Brian Bush
Brian Bush
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial