Subversion Repositories php_library

Rev

Rev 6 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

<?php

if(!strlen(session_id())){
        session_start();
}

Class PayPalInterface{
        var $amount;
        var $currency = 'USD';
        var $paymentType = 'Sale';
        var $returnURL;
        var $cancelURL;

        var $API_Endpoint;
        var $version = '2.3';
        var $API_UserName;
        var $API_Password;
        var $API_Signature;
        var $USE_PROXY = FALSE;
        var $PROXY_HOST;
        var $PROXY_PORT;
        var $PROXY_PORT = '808';
        var $gv_ApiErrorURL;
        var $sBNCode = 'PP-ECWizard';


        function PayPalInterface($sandbox=FALSE){
                if($sandbox){
                        $this->API_Endpoint = "https://api-3t.sandbox.paypal.com/nvp";
                        $this->PAYPAL_URL = "https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=";
                }else{
                        $this->API_Endpoint = "https://api-3t.paypal.com/nvp";
                        $this->PAYPAL_URL = "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=";
                }
        }

        function set($details){
                foreach($details AS $k=>$v){
                        $this->$k = $v;
                }
        }

        function process(){
                $success = FALSE;
                $resArray = $this->CallShortcutExpressCheckout();
                $ack = strtoupper($resArray["ACK"]);
                if($ack=="SUCCESS"){
                        $this->RedirectToPayPal($resArray["TOKEN"]);
                }else{
                        return FALSE;
                }
        }

        function RedirectToPayPal($token){
                header("Location: ".$this->PAYPAL_URL.$token);
        }

        function CallShortcutExpressCheckout(){
                // Construct the parameter string that describes the SetExpressCheckout API call in the shortcut implementation

                $nvpstr = '&Amt='.$this->amount.'&PAYMENTACTION='.$this->paymentType.'&ReturnUrl='.$this->returnURL.'&CANCELURL='.$this->cancelURL.'&CURRENCYCODE='.$this->currency;

                $_SESSION["currencyCodeType"] = $this->currency;
                $_SESSION["PaymentType"] = $this->paymentType;

                //'---------------------------------------------------------------------------------------------------------------
                //' Make the API call to PayPal
                //' If the API call succeded, then redirect the buyer to PayPal to begin to authorize payment.
                //' If an error occured, show the resulting errors
                //'---------------------------------------------------------------------------------------------------------------
                $resArray = $this->hash_call('SetExpressCheckout', $nvpstr);
                $ack = strtoupper($resArray["ACK"]);
                if($ack == "SUCCESS"){
                        $token = urldecode($resArray["TOKEN"]);
                        $_SESSION['TOKEN']=$token;
                }

                return $resArray;
        }

        function hash_call($methodName,$nvpStr){
                //setting the curl parameters.
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $this->API_Endpoint);
                curl_setopt($ch, CURLOPT_VERBOSE, 1);

                //turning off the server and peer verification(TrustManager Concept).
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch, CURLOPT_POST, 1);

            //if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
           //Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php
                if($this->USE_PROXY){
                        curl_setopt ($ch, CURLOPT_PROXY, $this->PROXY_HOST. ":" . $this->PROXY_PORT);
                }

                //NVPRequest for submitting to server
                $nvpreq = 'METHOD='.urlencode($methodName).'&VERSION='.urlencode($this->version).'&PWD='.urlencode($this->API_Password).'&USER='.urlencode($this->API_UserName).'&SIGNATURE='.urlencode($this->API_Signature).$nvpStr.'&BUTTONSOURCE='.urlencode($this->sBNCode);

                //setting the nvpreq as POST FIELD to curl
                curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);

                //getting response from server
                $response = curl_exec($ch);

                //convrting NVPResponse to an Associative Array
                $nvpResArray = $this->deformatNVP($response);
                $nvpReqArray = $this->deformatNVP($nvpreq);
                $_SESSION['nvpReqArray'] = $nvpReqArray;

                if(curl_errno($ch)){
                        // moving to display page to display curl errors
                        $_SESSION['curl_error_no']=curl_errno($ch) ;
                        $_SESSION['curl_error_msg']=curl_error($ch);
                        //Execute the Error handling module to display errors.
                }else{
                        //closing the curl
                        curl_close($ch);
                }

                return $nvpResArray;
        }

        function deformatNVP($nvpstr){
                $intial=0;
                $nvpArray = array();

                while(strlen($nvpstr)){
                        //postion of Key
                        $keypos= strpos($nvpstr,'=');
                        //position of value
                        $valuepos = strpos($nvpstr,'&') ? strpos($nvpstr,'&'): strlen($nvpstr);

                        /*getting the Key and Value values and storing in a Associative Array*/
                        $keyval=substr($nvpstr,$intial,$keypos);
                        $valval=substr($nvpstr,$keypos+1,$valuepos-$keypos-1);
                        //decoding the respose
                        $nvpArray[urldecode($keyval)] =urldecode( $valval);
                        $nvpstr=substr($nvpstr,$valuepos+1,strlen($nvpstr));
                }
                return $nvpArray;
        }

        function getStatus(){
                $token = "";
                if(isset($_REQUEST['token'])){
                        $token = $_REQUEST['token'];
                }

                // If the Request object contains the variable 'token' then it means that the user is coming from PayPal site.
                if($token != ""){
                        $resArray = GetShippingDetails($token);
                        $ack = strtoupper($resArray["ACK"]);
                        return $ack;
                }
        }
}

//A debugging function
function pr($r){
        echo '<pre>';
        print_r($r);
        echo '</pre>';
}
?>

Generated by GNU Enscript 1.6.5.90.