2012-12-08 16 views
6

मेरे पास एक ऐसी साइट है जो मेरे ग्राहक को पेपैल एक्सप्रेस चेकआउट पर निर्देशित करती है। मैं अतिथि को पेपैल खाता बनाने के बिना भुगतान करने का विकल्प देना चाहता हूं। मेरे पास पेपैल SetExpressCheckout API कॉल में 2 चर हैं लेकिन यह पता नहीं लगा सकता कि इसे मेरे कोड में कहां रखा जाए। क्या कोई इस में मेरी मदद कर सकता है?पेपैल समाधान प्रकार = एकमात्र

चर और मानों:

SOLUTIONTYPE = एकमात्र LandingPage = बिलिंग

कोड:

<?php 

/** 
* PayPal class 
*/ 
class PayPal 
{ 
    var $version = "64"; 

    /** 
    * Wether or not use Sandbox mode 
    */ 
    var $sandbox = false; 

    /** 
    * The API credentials 
    */ 
    var $api_username; 
    var $api_password; 
    var $api_signature; 

    /** 
    * The API endpoint and the URL for non-sandbox integration 
    */ 
    var $api_endpoint = 'https://api-3t.paypal.com/nvp'; 
    var $paypal_url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='; 

    /** 
    * Proxy settings 
    */ 
    var $use_proxy = false; 
    var $proxy_host = '127.0.0.1'; 
    var $proxy_port = 808; 

    /** 
    * BN Code is only applicable for partners 
    */ 
    var $bncode = "PP-ECWizard"; 

    /** 
    * Some private keys 
    */ 
    private $_token; 
    private $_payerId; 
    private $_currency = 'EUR'; 
    private $_paymentType = 'Sale'; 
    private $_resArray = array(); 

    /** 
    * Constructor 
    * @param string $api_username 
    * @param string $api_password 
    * @param string $api_signature 
    */ 
    public function __construct($api_username, $api_password, $api_signature) { 

     $this->api_username = $api_username; 
     $this->api_password = $api_password; 
     $this->api_signature = $api_signature; 
    } 

    /** 
    * Set Sandbox status 
    */ 
    public function useSandbox() { 

     $this->sandbox = true; 
     $this->api_endpoint = 'https://api-3t.sandbox.paypal.com/nvp'; 
     $this->paypal_url = 'https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token='; 
    } 

    /** 
    * When using a proxy server, set the host and port 
    * @param string $host 
    * @param integer $port 
    */ 
    public function useProxy($host, $port=808) { 

     if(!empty($host) && !empty($port) && is_numeric($port)) { 

      $this->use_proxy = true; 
      $this->proxy_host = $host; 
      $this->proxy_port = (integer) $port; 
     } 
    } 

    /** 
    * Sets a new BN ocde 
    */ 
    public function setBNcode($bncode=null) { 

     $this->bncode = $bncode; 
    } 

    /** 
    * Sets the currency used for the order 
    */ 
    public function setCurrency($currency='EUR') { 

     $this->_currency = $currency; 
    } 

    /** 
    * The type of payment is done here 
    */ 
    public function setPaymentType($type='Sale') { 

     $this->_paymentType = $type; 
    } 

    /** 
    * An express checkout transaction starts with a token, that 
    * identifies to PayPal your transaction 
    * In this example, when the script sees a token, the script 
    * knows that the buyer has already authorized payment through 
    * paypal. If no token was found, the action is to send the buyer 
    * to PayPal to first authorize payment 
    * 
    * Prepares the parameters for the SetExpressCheckout API Call. 
    * 
    * @param string $paymentAmount:  Total value of the shopping cart 
    * @param string $returnURL:   the page where buyers return to after they are done with the payment review on PayPal 
    * @param string $cancelURL:   the page where buyers return to when they cancel the payment review on PayPal 
    */ 
    function shortcutExpressCheckout($paymentAmount, $returnURL, $cancelURL) 
    { 
     // Construct the parameter string that describes the SetExpressCheckout API call in the shortcut implementation 
     $nvpstr = '&PAYMENTREQUEST_0_AMT='. $paymentAmount; 
     $nvpstr .= '&PAYMENTREQUEST_0_PAYMENTACTION='.$this->_paymentType; 
     $nvpstr .= '&RETURNURL='.$returnURL; 
     $nvpstr .= '&CANCELURL='.$cancelURL; 
     $nvpstr .= '&PAYMENTREQUEST_0_CURRENCYCODE='.$this->_currency; 

     // 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->_hashCall('SetExpressCheckout', $nvpstr); 
     $ack = strtoupper($resArray["ACK"]); 

     if($ack == 'SUCCESS' || $ack == 'SUCCESSWITHWARNING') { 
      $this->_token = urldecode($resArray["TOKEN"]); 
     } 
     else { 
      throw new PayPal_Exception($resArray['L_ERRORCODE0'].': '.$resArray['L_SHORTMESSAGE0'].', '.$resArray['L_LONGMESSAGE0']); 
     } 

     // save result 
     $this->_resArray = $resArray; 
    } 

    /** 
    * Sets a token for confirm purposes 
    * @param string $token 
    */ 
    public function setToken($token) { 

     if(!empty($token)) { 

      $this->_token = $token; 
     } 
    } 

    /** 
    * Returns the token set for the current payment 
    * @return string 
    */ 
    public function getToken() { 

     return $this->_token; 
    } 

    /** 
    * Sets the payer id for confirm purposes 
    * @param string $payerid 
    */ 
    public function setPayer($payerid) { 

     if(!empty($payerid)) { 

      $this->_payerId = $payerid; 
     } 
    } 

    /** 
    * Returns the result array set for the current payment 
    * @return array 
    */ 
    public function getResult() { 

     return $this->_resArray; 
    } 

    /** 
    * Prepares the parameters for the GetExpressCheckoutDetails API Call. 
    * 
    * @param string $paymentAmount: The total payment amount. 
    * @return object The NVP Collection object of the GetExpressCheckoutDetails Call Response. 
    */ 
    function confirm($paymentAmount) 
    { 
     // Format the other parameters that were stored in the session from the previous calls 
     $serverName = urlencode($_SERVER['SERVER_NAME']); 

     $nvpstr = '&TOKEN='.urlencode($this->_token); 
     $nvpstr .= '&PAYERID='.urlencode($this->_payerId); 
     $nvpstr .= '&PAYMENTREQUEST_0_PAYMENTACTION='.$this->_paymentType; 
     $nvpstr .= '&PAYMENTREQUEST_0_AMT='.$paymentAmount; 
     $nvpstr .= '&PAYMENTREQUEST_0_CURRENCYCODE='.$this->_currency; 
     $nvpstr .= '&IPADDRESS='.$serverName; 

     // Make the call to PayPal to finalize payment 
     // if an error occured, show the resulting errors 
     $resArray = $this->_hashCall('DoExpressCheckoutPayment', $nvpstr); 

     $this->_resArray = $resArray; 
    } 

    /** 
    * Function to perform the API call to PayPal using API signature 
    * @param string $methodName: is name of API method. 
    * @param string $nvpStr:  is nvp string. 
    * @return array containing the response from the server. 
    */ 
    private function _hashCall($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 set to TRUE, then only proxy will be enabled. 
     if($this->use_proxy) { 

      curl_setopt ($ch, CURLOPT_PROXY, $this->proxy_host.':'.$this->proxy_port); 
     } 

     // NVPRequest for submitting to server 
     $nvpreq = 'METHOD='.urlencode($methodName); 
     $nvpreq .= '&VERSION='.urlencode($this->version); 
     $nvpreq .= '&PWD='.urlencode($this->api_password); 
     $nvpreq .= '&USER='.urlencode($this->api_username); 
     $nvpreq .= '&SIGNATURE='.urlencode($this->api_signature); 
     $nvpreq .= $nvpStr.'&BUTTONSOURCE='.urlencode($this->bncode); 

     // 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); 

     // Execute the Error handling module to display errors. 
     if(curl_errno($ch)) { 

      $error = curl_error($ch); 
      throw new PayPal_Exception($error); 
     } 
     // closing the curl 
     else { 

      curl_close($ch); 
     } 

     return $nvpResArray; 
    } 

    /** 
    * This function will take NVPString and convert it to an Associative Array and it will decode the response. 
    * It is usefull to search for a particular key and displaying arrays. 
    * @param string $nvpstr is NVPString. 
    * @return array $nvpArray is Associative Array. 
    **/ 
    private 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; 
    } 

    /** 
    * Redirects to PayPal.com site. 
    */ 
    public function redirect() { 

     $url = $this->paypal_url.$this->_token; 
     header("Location: ".$url); 
     exit(); 
    } 
} 

class PayPal_Exception extends Exception 
{ 
    // Redefine the exception so message isn't optional 
    public function __construct($message, $code = 0) { 

     // make sure everything is assigned properly 
     parent::__construct($message, $code); 
    } 

    // custom string representation of object 
    public function __toString() { 
     return __CLASS__ . ": [{$this->code}]: {$this->message}\n"; 
    } 
} 

?> 

उत्तर

2

जो भी वर्ग पुस्तकालय इस प्रयोग कर रहे हैं बहुत यह क्या कर सकते हैं में सीमित लगता है एक्सप्रेस चेकआउट के साथ। यह मूल रूप से इसे काम करने के लिए न्यूनतम न्यूनतम विकल्पों का उपयोग कर रहा है। $paymentAmount, $returnURL, और $cancelURL:

आपका shortcutExpressCheckout() केवल 3 मानकों acccepting है। ऐसा और भी बहुत अधिक है कि इस में जा सकते हैं, विकल्प आप में पारित करने के लिए कोशिश कर रहे हैं शामिल हैं।

कि समारोह के भीतर, वे NVP स्ट्रिंग जेनरेट कर रहे हैं और $ nvpstr चर को यह बताए है। वांछित प्रभाव प्राप्त करने के लिए आपको अपने नए पैरामीटर जोड़ने की आवश्यकता है।

इस प्रयास करें ...

// Construct the parameter string that describes the SetExpressCheckout API call in the shortcut implementation 
$nvpstr = '&PAYMENTREQUEST_0_AMT='. $paymentAmount; 
$nvpstr .= '&PAYMENTREQUEST_0_PAYMENTACTION='.$this->_paymentType; 
$nvpstr .= '&RETURNURL='.$returnURL; 
$nvpstr .= '&CANCELURL='.$cancelURL; 
$nvpstr .= '&PAYMENTREQUEST_0_CURRENCYCODE='.$this->_currency; 
$nvpstr .= '&SOLUTIONTYPE=Sole&LANDINGPAGE=Billing'; 

कि यह आप के लिए क्या करना चाहिए ... सब मैंने किया था कोड के इस खंड के लिए कि अंतिम पंक्ति जोड़ने था।

भविष्य के संदर्भ के लिए, आप मेरे PHP class library for PayPal पर एक नज़र डालने में रुचि रखते हैं। आप इसका उपयोग कर रहे हैं उससे कहीं अधिक पूर्ण है, और इसका उपयोग करना बहुत आसान है।

+0

हाय एंड्रयू, मैंने आपके द्वारा सुझाए गए कार्यों को करने का प्रयास किया लेकिन जब मैं पेपैल पेज पर जाता हूं, तो भी यह मुझे पेपैल खाता बनाने के लिए कहता है। मुझे अतिथि के रूप में भुगतान नहीं करने देंगे। – user1888584

+0

यह सुनिश्चित करने की भी आवश्यकता है कि आपके पेपैल प्रोफ़ाइल में पेपैल खाता वैकल्पिक विकल्प सक्षम है। मुझे लगता है कि यह वेबसाइट भुगतान प्राथमिकताओं के तहत है। –

+2

पेपैल खाता वैकल्पिक चालू है और अभी भी काम नहीं करता है। स्क्रीन पर – user1888584

संबंधित मुद्दे