Location: PHPKode > scripts > PSIGate classes > psigate-classes/psigate.inc.php
<?php

 /*
  * PSIGate.php
  * Electronic Payment XML Interface for PSIGate
  *
  * Author :Harish Chauhan
  *
  * Date:    7/8/2005
  * Version: 1.0
  *
  */

define( 'DEFAULT_GATEWAY_URL', 'https://dev.psigate.com:7989/Messenger/XMLMessenger' );
define( 'DEFAULT_STORE_ID', 'teststore' );
define( 'DEFAULT_Passphrase', 'psigate1234' );

define( 'CURL_ERROR_OFFSET', 1000 );
define( 'XML_ERROR_OFFSET',  2000 );

define( 'TRANSACTION_OK',       0 );
define( 'TRANSACTION_FAILED',   1 );
define( 'TRANSACTION_UNKNOWN',  2 );


class PSIGate {
    var $parser;
    var $xmlData;
    var $currentTag;
    
    var $myGatewayURL;
    var $myStoreID;
    var $myPassphrase;
    
    var $myTotalAmount;

	var $myCustomerEmail;
    var $myCustomerAddress1;
    var $myCustomerAddress2;
    var $myCustomerPostcode;
    var $myCity;
    var $myState;
    var $myCountry;
    var $myPhone;
    var $myFax;
    var $myComments;
    var $myCompany;
    var $myCardHoldersName;
    var $myCardNumber;
    var $myCardExpiryMonth;
    var $myCardExpiryYear;
    var $myCardCVN;
    var $myTrxnNumber;

	var $TransTime;
	var $OrderID;
	var $Approved;
	var $ReturnCode;
	var $ErrMsg;
	var $TransRefNumber;
	var $AVSResult;
	var $CardAuthNumber;
	var $CardRefNumber;
	var $CardType;
    
    var $myError;
    var $myErrorMessage;

    /***********************************************************************
     *** XML Parser - Callback functions                                 ***
     ***********************************************************************/
    function epXmlElementStart ($parser, $tag, $attributes) {
        $this->currentTag = $tag;
    }
    
    function epXmlElementEnd ($parser, $tag) {
        $this->currentTag = "";
    }
    
    function epXmlData ($parser, $cdata) {
        $this->xmlData[$this->currentTag] = $cdata;
    }
    
    /***********************************************************************
     *** SET values to send to eWAY                                      ***
     ***********************************************************************/
    function setStoreID( $customerID ) {
        $this->myStoreID = $customerID;
    }

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

	function setComments( $myComments ) {
        $this->myComments = $myComments;
    }
    
    function setCustomerCompany( $company ) {
        $this->myCompany = $company;
    }
    
   
    function setCustomerEmail( $customerEmail ) {
        $this->myCustomerEmail = $customerEmail;
    }
    
    function setCustomerAddress( $customerAddress1,$customerAddress2 ) {
        $this->myCustomerAddress1 = $customerAddress1;
        $this->myCustomerAddress2 = $customerAddress2;
    }
    
    function setCustomerPostcode( $customerPostcode ) {
        $this->myCustomerPostcode = $customerPostcode;
    }
    
    function setCustomerCity( $city ) {
        $this->myCity = $city;
    }
    
    function setCustomerState( $state ) {
        $this->myState = $state;
    }
    
    function setCustomerCountry( $myCountry ) {
        $this->myCountry = $myCountry;
    }

	function setCustomerPhone( $myPhone ) {
        $this->myPhone = $myPhone;
    }
    
	function setCustomerFax( $myFax ) {
        $this->myFax = $myFax;
    }
    
    function setCardHoldersName( $cardHoldersName ) {
        $this->myCardHoldersName = $cardHoldersName;
    }
    
    function setCardNumber( $cardNumber ) {
        $this->myCardNumber = $cardNumber;
    }
    
    function setCardExpiryMonth( $cardExpiryMonth ) {
        $this->myCardExpiryMonth = $cardExpiryMonth;
    }
    
    function setCardExpiryYear( $cardExpiryYear ) {
        $this->myCardExpiryYear = $cardExpiryYear;
    }
    
    function setCardCVN( $cardCVN ) {
        $this->myCardCVN = $cardCVN;
    }
    
    function setTrxnNumber( $trxnNumber ) {
        $this->myTrxnNumber = $trxnNumber;
    }

	function setOrderID( $trxnNumber ) {
        $this->myTrxnNumber = $trxnNumber;
    }
    
	function setAmount( $myTotalAmount ) {
        $this->myTotalAmount = $myTotalAmount;
    }
    

    /***********************************************************************
     *** GET values returned by PSI                                     ***
     ***********************************************************************/
    function getTrxnStatus() {
        return $this->Approved;
    }
    
    function getTrxnNumber() {
        return $this->OrderID;
    }
    
    function getTrxnReference() {
        return $this->TransRefNumber;
    }
    
    function getTrxnError() {
        return $this->ErrMsg;
    }
    
    function getAuthCode() {
        return $this->TransRefNumber;
    }
    
    function getError()
    {
        if( $this->myError != 0 ) {
            // Internal Error
            return $this->myError;
        } else {
            // PSI Error
            if( $this->Approved == 'APPROVED' ) {
                return TRANSACTION_OK;
            } elseif( $this->Approved == 'DECLINED' ) {
                return TRANSACTION_FAILED;
            } else {
                return TRANSACTION_UNKNOWN;
            }
        }
    }

    function getErrorMessage()
    {
        if( $this->myError != 0 ) {
            // Internal Error
            return $this->myErrorMessage;
        } else {
            // eWAY Error
            return $this->ErrMsg;
        }
    }

    /***********************************************************************
     *** Class Constructor                                               ***
     ***********************************************************************/
    function PSIGate( $storeID = DEFAULT_STORE_ID, $passphrase = DEFAULT_Passphrase  ) {
        $this->myStoreID = $storeID;
        $this->myPassphrase = $passphrase;
        $this->myGatewayURL = DEFAULT_GATEWAY_URL;
    }

    /***********************************************************************
     *** Business Logic                                                  ***
     ***********************************************************************/
    function doPayment() {
        $xmlRequest = "<?xml version='1.0' encoding='UTF-8'?><Order>".
                "<StoreID>".htmlentities( $this->myStoreID )."</StoreID>".
                "<Passphrase>".htmlentities( $this->myPassphrase )."</Passphrase>".
                "<Bname>".htmlentities( $this->myCardHoldersName )."</Bname>".
                "<Bcompany>".htmlentities( $this->myCompany )."</Bcompany>".
                "<ewayCustomerEmail>".htmlentities( $this->myCustomerEmail )."</ewayCustomerEmail>".
                "<Baddress1>".htmlentities( $this->myCustomerAddress1 )."</Baddress1>".
                "<Baddress2>".htmlentities( $this->myCustomerAddress2 )."</Baddress2>".
                "<Bcity>".htmlentities( $this->myCity )."</Bcity>".
                "<Bprovince>".htmlentities( $this->myState )."</Bprovince>".
                "<Bpostalcode>".htmlentities( $this->myCustomerPostcode )."</Bpostalcode>".
                "<Bcountry>".htmlentities( $this->myCountry )."</Bcountry>".
                "<Phone>".htmlentities( $this->myPhone )."</Phone>".
                "<Fax>".htmlentities( $this->myFax )."</Fax>".
                "<Comments>".htmlentities( $this->myComments )."</Comments>".
                "<OrderID>".htmlentities( $this->myTrxnNumber)."</OrderID>".
                "<Subtotal>".htmlentities( $this->myTotalAmount)."</Subtotal>".
                "<PaymentType>CC</PaymentType>".
                "<CardAction>0</CardAction>".
                "<CardNumber>".htmlentities( $this->myCardNumber )."</CardNumber>".
                "<CardExpMonth>".htmlentities( $this->myCardExpiryMonth )."</CardExpMonth>".
                "<CardExpYear>".htmlentities( $this->myCardExpiryYear )."</CardExpYear>".
                "<CardAuthNumber>".htmlentities( $this->myCardCVN )."</CardAuthNumber>".
                "</Order>";

        /* 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_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt( $ch, CURLOPT_POSTFIELDS, $xmlRequest );
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt( $ch, CURLOPT_TIMEOUT, 240 );
        $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, "epXmlElementStart", "epXmlElementEnd");
            xml_set_character_data_handler ($this->parser, "epXmlData");
            
            // 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->TransTime = $this->xmlData['TransTime'];
                $this->OrderID = $this->xmlData['OrderID'];
                $this->Approved = $this->xmlData['Approved'];
                $this->ReturnCode = $this->xmlData['ReturnCode'];
                $this->ErrMsg = $this->xmlData['ErrMsg'];
                $this->TransRefNumber = $this->xmlData['TransRefNumber'];
                $this->AVSResult = $this->xmlData['AVSResult'];
                $this->CardAuthNumber = $this->xmlData['CardAuthNumber'];
                $this->CardRefNumber = $this->xmlData['CardRefNumber'];
                $this->CardType = $this->xmlData['CardType'];
                $this->myError = 0;
                $this->myErrorMessage = '';
            } else {
                // An XML error occured. Return the error message and number.
                $this->myError = xml_get_error_code( $this->parser ) + XML_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 ) + CURL_ERROR_OFFSET;
            $this->myErrorMessage = curl_error( $ch );
        }
        // Clean up CURL, and return any error.
        curl_close( $ch );
        return $this->getError();
    }
}
?>
Return current item: PSIGate classes