Paypal NVP 出现 php 错误

发布于 2024-12-21 20:57:19 字数 8579 浏览 1 评论 0原文

我正在尝试使用 Paypal 的 NVP 界面启动并运行我的网站来处理我的自定义购物车。我已经下载了他们的示例代码(此处< /a>) 并尝试按原样运行但没有成功。我选择的任何函数(例如快速结账销售)都会出现curl_errno()错误:错误号:3错误消息:未设置URL!我已经在本地主机和我的托管站点上尝试过此操作。

hash_call() 是处理 Paypal 调用的函数。下面还包括调用 hash_call() 的代码。他们设置 url 的代码是:

$returnURL =urlencode($url.'/ReviewOrder.php?currencyCodeType='.$currencyCodeType.'& paymentType='.$ paymentType) ;

$cancelURL =urlencode("$url/SetExpressCheckout.php? paymentType=$ paymentType" );

我以为cancelURL行看起来不对,并将其修改为:

$cancelURL =urlencode($url.'/SetExpressCheckout.php? paymentType='.$ paymentType );

两者都没有起作用。任何帮助将不胜感激。我从来没有和curl一起工作过。

require_once 'CallerService.php';
session_start();
    if(! isset($_REQUEST['token'])) {

            /* The servername and serverport tells PayPal where the buyer
               should be directed back to after authorizing payment.
               In this case, its the local webserver that is running this script
               Using the servername and serverport, the return URL is the first
               portion of the URL that buyers will return to after authorizing payment
               */
               $serverName = $_SERVER['SERVER_NAME'];
               $serverPort = $_SERVER['SERVER_PORT'];
               $url=dirname('http://'.$serverName.':'.$serverPort.$_SERVER['REQUEST_URI']);


               $currencyCodeType=$_REQUEST['currencyCodeType'];
               $paymentType=$_REQUEST['paymentType'];


               $personName        = $_REQUEST['PERSONNAME'];
               $SHIPTOSTREET      = $_REQUEST['SHIPTOSTREET'];
               $SHIPTOCITY        = $_REQUEST['SHIPTOCITY'];
               $SHIPTOSTATE       = $_REQUEST['SHIPTOSTATE'];
               $SHIPTOCOUNTRYCODE = $_REQUEST['SHIPTOCOUNTRYCODE'];
               $SHIPTOZIP         = $_REQUEST['SHIPTOZIP'];
               $L_NAME0           = $_REQUEST['L_NAME0'];
               $L_AMT0            = $_REQUEST['L_AMT0'];
               $L_QTY0            = $_REQUEST['L_QTY0'];
               $L_NAME1           = $_REQUEST['L_NAME1'];
               $L_AMT1            = $_REQUEST['L_AMT1'];
               $L_QTY1            = $_REQUEST['L_QTY1'];



             /* The returnURL is the location where buyers return when a
                payment has been succesfully authorized.
                The cancelURL is the location buyers are sent to when they hit the
                cancel button during authorization of payment during the PayPal flow
                */

               $returnURL =urlencode($url.'/ReviewOrder.php?currencyCodeType='.$currencyCodeType.'&paymentType='.$paymentType);
               $cancelURL =urlencode($url.'/SetExpressCheckout.php?paymentType='.$paymentType );

             /* Construct the parameter string that describes the PayPal payment
                the varialbes were set in the web form, and the resulting string
                is stored in $nvpstr
                */
               $itemamt = 0.00;
               $itemamt = $L_QTY0*$L_AMT0+$L_AMT1*$L_QTY1;
               $amt = 5.00+2.00+1.00+$itemamt;
               $maxamt= $amt+25.00;
               $nvpstr="";

               /*
                * Setting up the Shipping address details
                */
               $shiptoAddress = "&SHIPTONAME=$personName&SHIPTOSTREET=$SHIPTOSTREET&SHIPTOCITY=$SHIPTOCITY&SHIPTOSTATE=$SHIPTOSTATE&SHIPTOCOUNTRYCODE=$SHIPTOCOUNTRYCODE&SHIPTOZIP=$SHIPTOZIP";

               $nvpstr="&ADDRESSOVERRIDE=1$shiptoAddress&L_NAME0=".$L_NAME0."&L_NAME1=".$L_NAME1."&L_AMT0=".$L_AMT0."&L_AMT1=".$L_AMT1."&L_QTY0=".$L_QTY0."&L_QTY1=".$L_QTY1."&MAXAMT=".(string)$maxamt."&AMT=".(string)$amt."&ITEMAMT=".(string)$itemamt."&CALLBACKTIMEOUT=4&L_SHIPPINGOPTIONAMOUNT1=8.00&L_SHIPPINGOPTIONlABEL1=UPS Next Day Air&L_SHIPPINGOPTIONNAME1=UPS Air&L_SHIPPINGOPTIONISDEFAULT1=true&L_SHIPPINGOPTIONAMOUNT0=3.00&L_SHIPPINGOPTIONLABEL0=UPS Ground 7 Days&L_SHIPPINGOPTIONNAME0=Ground&L_SHIPPINGOPTIONISDEFAULT0=false&INSURANCEAMT=1.00&INSURANCEOPTIONOFFERED=true&CALLBACK=https://www.ppcallback.com/callback.pl&SHIPPINGAMT=8.00&SHIPDISCAMT=-3.00&TAXAMT=2.00&L_NUMBER0=1000&L_DESC0=Size: 8.8-oz&L_NUMBER1=10001&L_DESC1=Size: Two 24-piece boxes&L_ITEMWEIGHTVALUE1=0.5&L_ITEMWEIGHTUNIT1=lbs&ReturnUrl=".$returnURL."&CANCELURL=".$cancelURL ."&CURRENCYCODE=".$currencyCodeType."&PAYMENTACTION=".$paymentType;



                /* Make the call to PayPal to set the Express Checkout token
                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=hash_call("SetExpressCheckout",$nvpstr);

********************************************************************************* 


       /**
          * hash_call: Function to perform the API call to PayPal using API signature
          * @methodName is name of API  method.
          * @nvpStr is nvp string.
          * returns an associtive array containing the response from the server.
        */


        function hash_call($methodName,$nvpStr)
        {
            //declaring of global variables
            global $API_Endpoint,$version,$API_UserName,$API_Password,$API_Signature,$nvp_Header, $subject, $AUTH_token,$AUTH_signature,$AUTH_timestamp;
            // form header string
            $nvpheader=nvpHeader();
            //setting the curl parameters.
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$API_Endpoint);
            curl_setopt($ch, CURLOPT_VERBOSE, 1);

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

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



        //WARNING: this would prevent curl from detecting a 'man in the middle' attack
            $ch = curl_init();
        //curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
        //curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 



        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, TRUE); 
        curl_setopt ($ch, CURLOPT_CAINFO, "c:/xampp/apache/cacert.pem");        


            //in case of permission APIs send headers as HTTPheders
            if(!empty($AUTH_token) && !empty($AUTH_signature) && !empty($AUTH_timestamp))
             {
                $headers_array[] = "X-PP-AUTHORIZATION: ".$nvpheader;

            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_array);
            curl_setopt($ch, CURLOPT_HEADER, false);
            }
            else 
            {
                $nvpStr=$nvpheader.$nvpStr;
            }
            //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(USE_PROXY)
            curl_setopt ($ch, CURLOPT_PROXY, PROXY_HOST.":".PROXY_PORT); 

            //check if version is included in $nvpStr else include the version.
            if(strlen(str_replace('VERSION=', '', strtoupper($nvpStr))) == strlen($nvpStr)) {
                $nvpStr = "&VERSION=" . urlencode($version) . $nvpStr;  
            }

            $nvpreq="METHOD=".urlencode($methodName).$nvpStr;

            //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=deformatNVP($response);
            $nvpReqArray=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);
                  $location = "APIError.php";
                  header("Location: $location");
             } else {
                 //closing the curl
                    curl_close($ch);
              }

        return $nvpResArray;
        }

I am trying to get up and running using Paypal's NVP interface for my website to process my custom cart. I've downloaded their sample code (here) and have tried to run unaltered without success. Any function I choose (e.g. express checkout sale) gives the curl_errno() error: Error Number: 3 Error Message: No URL set! I have tried this both on localhost and on my hosted site.

hash_call() is the function processing the call to Paypal. The code which calls hash_call() is also included below.Their code that sets urls is:

$returnURL =urlencode($url.'/ReviewOrder.php?currencyCodeType='.$currencyCodeType.'&paymentType='.$paymentType);

$cancelURL =urlencode("$url/SetExpressCheckout.php?paymentType=$paymentType" );

I thought the cancelURL line looked off and modified it to:

$cancelURL =urlencode($url.'/SetExpressCheckout.php?paymentType='.$paymentType );

Neither have worked. Any help would be appreciated. I've never worked with curl.

require_once 'CallerService.php';
session_start();
    if(! isset($_REQUEST['token'])) {

            /* The servername and serverport tells PayPal where the buyer
               should be directed back to after authorizing payment.
               In this case, its the local webserver that is running this script
               Using the servername and serverport, the return URL is the first
               portion of the URL that buyers will return to after authorizing payment
               */
               $serverName = $_SERVER['SERVER_NAME'];
               $serverPort = $_SERVER['SERVER_PORT'];
               $url=dirname('http://'.$serverName.':'.$serverPort.$_SERVER['REQUEST_URI']);


               $currencyCodeType=$_REQUEST['currencyCodeType'];
               $paymentType=$_REQUEST['paymentType'];


               $personName        = $_REQUEST['PERSONNAME'];
               $SHIPTOSTREET      = $_REQUEST['SHIPTOSTREET'];
               $SHIPTOCITY        = $_REQUEST['SHIPTOCITY'];
               $SHIPTOSTATE       = $_REQUEST['SHIPTOSTATE'];
               $SHIPTOCOUNTRYCODE = $_REQUEST['SHIPTOCOUNTRYCODE'];
               $SHIPTOZIP         = $_REQUEST['SHIPTOZIP'];
               $L_NAME0           = $_REQUEST['L_NAME0'];
               $L_AMT0            = $_REQUEST['L_AMT0'];
               $L_QTY0            = $_REQUEST['L_QTY0'];
               $L_NAME1           = $_REQUEST['L_NAME1'];
               $L_AMT1            = $_REQUEST['L_AMT1'];
               $L_QTY1            = $_REQUEST['L_QTY1'];



             /* The returnURL is the location where buyers return when a
                payment has been succesfully authorized.
                The cancelURL is the location buyers are sent to when they hit the
                cancel button during authorization of payment during the PayPal flow
                */

               $returnURL =urlencode($url.'/ReviewOrder.php?currencyCodeType='.$currencyCodeType.'&paymentType='.$paymentType);
               $cancelURL =urlencode($url.'/SetExpressCheckout.php?paymentType='.$paymentType );

             /* Construct the parameter string that describes the PayPal payment
                the varialbes were set in the web form, and the resulting string
                is stored in $nvpstr
                */
               $itemamt = 0.00;
               $itemamt = $L_QTY0*$L_AMT0+$L_AMT1*$L_QTY1;
               $amt = 5.00+2.00+1.00+$itemamt;
               $maxamt= $amt+25.00;
               $nvpstr="";

               /*
                * Setting up the Shipping address details
                */
               $shiptoAddress = "&SHIPTONAME=$personName&SHIPTOSTREET=$SHIPTOSTREET&SHIPTOCITY=$SHIPTOCITY&SHIPTOSTATE=$SHIPTOSTATE&SHIPTOCOUNTRYCODE=$SHIPTOCOUNTRYCODE&SHIPTOZIP=$SHIPTOZIP";

               $nvpstr="&ADDRESSOVERRIDE=1$shiptoAddress&L_NAME0=".$L_NAME0."&L_NAME1=".$L_NAME1."&L_AMT0=".$L_AMT0."&L_AMT1=".$L_AMT1."&L_QTY0=".$L_QTY0."&L_QTY1=".$L_QTY1."&MAXAMT=".(string)$maxamt."&AMT=".(string)$amt."&ITEMAMT=".(string)$itemamt."&CALLBACKTIMEOUT=4&L_SHIPPINGOPTIONAMOUNT1=8.00&L_SHIPPINGOPTIONlABEL1=UPS Next Day Air&L_SHIPPINGOPTIONNAME1=UPS Air&L_SHIPPINGOPTIONISDEFAULT1=true&L_SHIPPINGOPTIONAMOUNT0=3.00&L_SHIPPINGOPTIONLABEL0=UPS Ground 7 Days&L_SHIPPINGOPTIONNAME0=Ground&L_SHIPPINGOPTIONISDEFAULT0=false&INSURANCEAMT=1.00&INSURANCEOPTIONOFFERED=true&CALLBACK=https://www.ppcallback.com/callback.pl&SHIPPINGAMT=8.00&SHIPDISCAMT=-3.00&TAXAMT=2.00&L_NUMBER0=1000&L_DESC0=Size: 8.8-oz&L_NUMBER1=10001&L_DESC1=Size: Two 24-piece boxes&L_ITEMWEIGHTVALUE1=0.5&L_ITEMWEIGHTUNIT1=lbs&ReturnUrl=".$returnURL."&CANCELURL=".$cancelURL ."&CURRENCYCODE=".$currencyCodeType."&PAYMENTACTION=".$paymentType;



                /* Make the call to PayPal to set the Express Checkout token
                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=hash_call("SetExpressCheckout",$nvpstr);

********************************************************************************* 


       /**
          * hash_call: Function to perform the API call to PayPal using API signature
          * @methodName is name of API  method.
          * @nvpStr is nvp string.
          * returns an associtive array containing the response from the server.
        */


        function hash_call($methodName,$nvpStr)
        {
            //declaring of global variables
            global $API_Endpoint,$version,$API_UserName,$API_Password,$API_Signature,$nvp_Header, $subject, $AUTH_token,$AUTH_signature,$AUTH_timestamp;
            // form header string
            $nvpheader=nvpHeader();
            //setting the curl parameters.
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$API_Endpoint);
            curl_setopt($ch, CURLOPT_VERBOSE, 1);

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

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



        //WARNING: this would prevent curl from detecting a 'man in the middle' attack
            $ch = curl_init();
        //curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
        //curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 



        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, TRUE); 
        curl_setopt ($ch, CURLOPT_CAINFO, "c:/xampp/apache/cacert.pem");        


            //in case of permission APIs send headers as HTTPheders
            if(!empty($AUTH_token) && !empty($AUTH_signature) && !empty($AUTH_timestamp))
             {
                $headers_array[] = "X-PP-AUTHORIZATION: ".$nvpheader;

            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers_array);
            curl_setopt($ch, CURLOPT_HEADER, false);
            }
            else 
            {
                $nvpStr=$nvpheader.$nvpStr;
            }
            //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(USE_PROXY)
            curl_setopt ($ch, CURLOPT_PROXY, PROXY_HOST.":".PROXY_PORT); 

            //check if version is included in $nvpStr else include the version.
            if(strlen(str_replace('VERSION=', '', strtoupper($nvpStr))) == strlen($nvpStr)) {
                $nvpStr = "&VERSION=" . urlencode($version) . $nvpStr;  
            }

            $nvpreq="METHOD=".urlencode($methodName).$nvpStr;

            //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=deformatNVP($response);
            $nvpReqArray=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);
                  $location = "APIError.php";
                  header("Location: $location");
             } else {
                 //closing the curl
                    curl_close($ch);
              }

        return $nvpResArray;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╰◇生如夏花灿烂 2024-12-28 20:57:19

尝试这个类,

<?php
class ZC_Paypal
{       

        public $API_USERNAME;
    public $API_PASSWORD;    
    public $API_SIGNATURE='xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    public $API_ENDPOINT;
    public $USE_PROXY;
    public $PROXY_HOST;
    public $PROXY_PORT;
    public $PAYPAL_URL;
    public $VERSION;
    public $NVP_HEADER;

        public $SSLCERTPATH;
    function __construct($PROXY_HOST, $PROXY_PORT, $IS_ONLINE = FALSE, $USE_PROXY = FALSE, $VERSION = '51.0', $api_url)
    {
        /*$this->API_USERNAME = $API_USERNAME;
        $this->API_PASSWORD = $API_PASSWORD;
        $this->API_SIGNATURE = $API_SIGNATURE;*/

                //$this->API_ENDPOINT = 'https://api-3t.sandbox.paypal.com/nvp';
                $this->API_ENDPOINT = $api_url;
        $this->USE_PROXY = $USE_PROXY;
        if($this->USE_PROXY == true)
        {
            $this->PROXY_HOST = $PROXY_HOST;
            $this->PROXY_PORT = $PROXY_PORT;
        }
        else
        {
            $this->PROXY_HOST = '127.0.0.1';
            $this->PROXY_PORT = '808';
        }
        if($IS_ONLINE == FALSE)
        {
            $this->PAYPAL_URL = 'https://api-3t.sandbox.paypal.com/nvp';
        }
        else
        {
            $this->PAYPAL_URL = 'https://api-3t.paypal.com/nvp';
        }
        $this->VERSION = $VERSION;
    }

    function hash_call($methodName,$nvpStr,$uname,$pwd,$api_url,$sslcerpath)
    {
                $this->API_ENDPOINT = $api_url;
                $this->API_PASSWORD = $pwd;
                $this->API_USERNAME = $uname;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$this->API_ENDPOINT);
                //curl_setopt($ch, CURLOPT_SSLCERT, $sslcerpath);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        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($this->USE_PROXY)
        {
            curl_setopt ($ch, CURLOPT_PROXY, $this->PROXY_HOST.":".$this->PROXY_PORT); 
        }

        $nvpreq="METHOD=".urlencode($methodName)."&VERSION=".urlencode($this->VERSION)."&PWD=".urlencode($this->API_PASSWORD)."&USER=".urlencode($this->API_USERNAME)."&SIGNATURE=".urlencode($this->API_SIGNATURE).$nvpStr;
        curl_setopt($ch,CURLOPT_POSTFIELDS,$nvpreq);
        $response = curl_exec($ch);
        $nvpResArray=$this->deformatNVP($response);
        $nvpReqArray=$this->deformatNVP($nvpreq);
        $_SESSION['nvpReqArray']=$nvpReqArray;
        if (curl_errno($ch))
        {
            die("CURL send a error during perform operation: ".curl_errno($ch));
        } 
        else 
        {
            curl_close($ch);
        }

    return $nvpResArray;
    }

    function deformatNVP($nvpstr)
    {

        $intial=0;
        $nvpArray = array();
        while(strlen($nvpstr))
        {
            $keypos= strpos($nvpstr,'='); 
            $valuepos = strpos($nvpstr,'&') ? strpos($nvpstr,'&'): strlen($nvpstr); 
            $keyval=substr($nvpstr,$intial,$keypos);
            $valval=substr($nvpstr,$keypos+1,$valuepos-$keypos-1);
            $nvpArray[urldecode($keyval)] =urldecode( $valval);
            $nvpstr=substr($nvpstr,$valuepos+1,strlen($nvpstr));
         }
        return $nvpArray;
    }

    function __destruct() 
    {

    }
}

并调用上面的类,

....
        $currencyCode="USD";
        //$paymentAction = urlencode("Sale");
                $paymentAction = urlencode("Authorization");
        $nvpRecurring = '';
        $methodToCall = 'doDirectPayment';
                        $nvpstr='&PAYMENTACTION='.$paymentAction.'&AMT='.$amount.'&CREDITCARDTYPE='.$creditCardType.'&ACCT='.$creditCardNumber.'&EXPDATE='.$padDateMonth.$expDateYear.'&CVV2='.$cvv2Number.'&FIRSTNAME='.$firstName.'&LASTNAME='.$lastName.'&STREET='.$address1.'&CITY='.$city.'&STATE='.$state.'&ZIP='.$zip.'&COUNTRYCODE='.$country.'&CURRENCYCODE='.$currencyCode.$nvpRecurring;
        $paypalPro = new ZC_Paypal( '', '', FALSE, FALSE,'51.0',Zend_Registry::get("admin_paypal_pro_url"));
                $resArray = $paypalPro->hash_call($methodToCall,$nvpstr,Zend_Registry::get("admin_paypal_user_name"), Zend_Registry::get("admin_paypal_password"),Zend_Registry::get("admin_paypal_pro_url"),Zend_Registry::get("admin_paypal_ssl_path"));
        $ack = strtoupper($resArray["ACK"]);

        //echo '<pre>'; print_r($resArray); exit;
        if($ack!="SUCCESS")
        {
            if(!in_array($resArray["L_LONGMESSAGE0"], array('Internal Error', 'CURL send a error during perform operation: 6')))
                echo $resArray["L_LONGMESSAGE0"];
            else
                echo 'Transaction Failed, Try again later.';
        }
        else{   
// if success .....
}

在各自的变量中传递凭据,我正在使用 zend,所以尝试在这里使用核心 php...

Try this class,

<?php
class ZC_Paypal
{       

        public $API_USERNAME;
    public $API_PASSWORD;    
    public $API_SIGNATURE='xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    public $API_ENDPOINT;
    public $USE_PROXY;
    public $PROXY_HOST;
    public $PROXY_PORT;
    public $PAYPAL_URL;
    public $VERSION;
    public $NVP_HEADER;

        public $SSLCERTPATH;
    function __construct($PROXY_HOST, $PROXY_PORT, $IS_ONLINE = FALSE, $USE_PROXY = FALSE, $VERSION = '51.0', $api_url)
    {
        /*$this->API_USERNAME = $API_USERNAME;
        $this->API_PASSWORD = $API_PASSWORD;
        $this->API_SIGNATURE = $API_SIGNATURE;*/

                //$this->API_ENDPOINT = 'https://api-3t.sandbox.paypal.com/nvp';
                $this->API_ENDPOINT = $api_url;
        $this->USE_PROXY = $USE_PROXY;
        if($this->USE_PROXY == true)
        {
            $this->PROXY_HOST = $PROXY_HOST;
            $this->PROXY_PORT = $PROXY_PORT;
        }
        else
        {
            $this->PROXY_HOST = '127.0.0.1';
            $this->PROXY_PORT = '808';
        }
        if($IS_ONLINE == FALSE)
        {
            $this->PAYPAL_URL = 'https://api-3t.sandbox.paypal.com/nvp';
        }
        else
        {
            $this->PAYPAL_URL = 'https://api-3t.paypal.com/nvp';
        }
        $this->VERSION = $VERSION;
    }

    function hash_call($methodName,$nvpStr,$uname,$pwd,$api_url,$sslcerpath)
    {
                $this->API_ENDPOINT = $api_url;
                $this->API_PASSWORD = $pwd;
                $this->API_USERNAME = $uname;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$this->API_ENDPOINT);
                //curl_setopt($ch, CURLOPT_SSLCERT, $sslcerpath);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        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($this->USE_PROXY)
        {
            curl_setopt ($ch, CURLOPT_PROXY, $this->PROXY_HOST.":".$this->PROXY_PORT); 
        }

        $nvpreq="METHOD=".urlencode($methodName)."&VERSION=".urlencode($this->VERSION)."&PWD=".urlencode($this->API_PASSWORD)."&USER=".urlencode($this->API_USERNAME)."&SIGNATURE=".urlencode($this->API_SIGNATURE).$nvpStr;
        curl_setopt($ch,CURLOPT_POSTFIELDS,$nvpreq);
        $response = curl_exec($ch);
        $nvpResArray=$this->deformatNVP($response);
        $nvpReqArray=$this->deformatNVP($nvpreq);
        $_SESSION['nvpReqArray']=$nvpReqArray;
        if (curl_errno($ch))
        {
            die("CURL send a error during perform operation: ".curl_errno($ch));
        } 
        else 
        {
            curl_close($ch);
        }

    return $nvpResArray;
    }

    function deformatNVP($nvpstr)
    {

        $intial=0;
        $nvpArray = array();
        while(strlen($nvpstr))
        {
            $keypos= strpos($nvpstr,'='); 
            $valuepos = strpos($nvpstr,'&') ? strpos($nvpstr,'&'): strlen($nvpstr); 
            $keyval=substr($nvpstr,$intial,$keypos);
            $valval=substr($nvpstr,$keypos+1,$valuepos-$keypos-1);
            $nvpArray[urldecode($keyval)] =urldecode( $valval);
            $nvpstr=substr($nvpstr,$valuepos+1,strlen($nvpstr));
         }
        return $nvpArray;
    }

    function __destruct() 
    {

    }
}

and call above class like,

....
        $currencyCode="USD";
        //$paymentAction = urlencode("Sale");
                $paymentAction = urlencode("Authorization");
        $nvpRecurring = '';
        $methodToCall = 'doDirectPayment';
                        $nvpstr='&PAYMENTACTION='.$paymentAction.'&AMT='.$amount.'&CREDITCARDTYPE='.$creditCardType.'&ACCT='.$creditCardNumber.'&EXPDATE='.$padDateMonth.$expDateYear.'&CVV2='.$cvv2Number.'&FIRSTNAME='.$firstName.'&LASTNAME='.$lastName.'&STREET='.$address1.'&CITY='.$city.'&STATE='.$state.'&ZIP='.$zip.'&COUNTRYCODE='.$country.'&CURRENCYCODE='.$currencyCode.$nvpRecurring;
        $paypalPro = new ZC_Paypal( '', '', FALSE, FALSE,'51.0',Zend_Registry::get("admin_paypal_pro_url"));
                $resArray = $paypalPro->hash_call($methodToCall,$nvpstr,Zend_Registry::get("admin_paypal_user_name"), Zend_Registry::get("admin_paypal_password"),Zend_Registry::get("admin_paypal_pro_url"),Zend_Registry::get("admin_paypal_ssl_path"));
        $ack = strtoupper($resArray["ACK"]);

        //echo '<pre>'; print_r($resArray); exit;
        if($ack!="SUCCESS")
        {
            if(!in_array($resArray["L_LONGMESSAGE0"], array('Internal Error', 'CURL send a error during perform operation: 6')))
                echo $resArray["L_LONGMESSAGE0"];
            else
                echo 'Transaction Failed, Try again later.';
        }
        else{   
// if success .....
}

pass credential in respective variable,i am using zend, so try to use core php here...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文