2012-02-19 105 views
3

我和另一位程序員已經在這幾天沒有快樂。我們現在所要做的就是收到PayPal的成功迴應。不幸的是,PayPal API被記錄在案。PHP - PayPal集成 - 支付多個賬戶

這裏是我們的代碼:

<?php 
    class Paypal { 
      /** 
      * Last error message(s) 
      * @var array 
      */ 
      protected $_errors = array(); 

      /** 
      * API Credentials 
      * Use the correct credentials for the environment in use (Live/Sandbox) 
      * @var array 
      */ 
      protected $_credentials = array(
        'USER' => '*************************', 
        'PWD' => '****************', 
        'SIGNATURE' => '************************************************', 
      ); 

      /** 
      * API endpoint 
      * Live - https://api-3t.paypal.com/nvp 
      * Sandbox - https://api-3t.sandbox.paypal.com/nvp 
      * @var string 
      */ 
      protected $_endPoint = 'https://svcs.sandbox.paypal.com/AdaptivePayments/Pay'; 

      /** 
      * API Version 
      * @var string 
      */ 
      protected $_version = '74.0'; 


      public function newstartpayment() { 
        //set PayPal Endpoint to sandbox 
        $url = trim("https://svcs.sandbox.paypal.com/AdaptivePayments/Pay"); 

      //PayPal API Credentials 
      $API_UserName = $_credentials['USER']; 
      $API_Password = $_credentials['PWD']; 
      $API_Signature = $_credentials['SIGNATURE']; 

      //Default App ID for Sandbox  
      $API_AppID = "******************"; 

      $API_RequestFormat = "NV"; 
      $API_ResponseFormat = "NV"; 

      //Create request payload with minimum required parameters 
      $bodyparams = array (
        "requestEnvelope.errorLanguage" => "en_US", 
        "actionType" => "PAY", 
        "currencyCode" => "USD", 
        "cancelUrl" => "http://www.paypal.com", 
        "returnUrl" => "http://www.paypal.com", 
        "receiverList.receiver(0).email" => "************@paypal.com", //TODO 
        "receiverList.receiver(0).amount" => "40", //TODO 
        "receiverList.receiver(0).primary" => "true", //TODO 
        "receiverList.receiver(1).email" => "****************@paypal.com", //TODO 
        "receiverList.receiver(1).amount" => "30", //TODO 
        "receiverList.receiver(1).primary" => "false", //TODO 
        'USER' => '************************', 
        'PWD' => '***********', 
        'SIGNATURE' => '*************************************' 
      ); 

      // convert payload array into url encoded query string 
      $body_data = http_build_query($bodyparams, "", chr(38)); 

      try 
      { 
        //create request and add headers 
        $params = array("http" => array( 
        "method" => "POST", 
        "content" => $body_data, 
        "header" => "X-PAYPAL-SECURITY-USERID: " . $API_UserName . "\r\n" . 
        "X-PAYPAL-SECURITY-SIGNATURE: " . $API_Signature . "\r\n" . 
        "X-PAYPAL-SECURITY-PASSWORD: " . $API_Password . "\r\n" . 
        "X-PAYPAL-APPLICATION-ID: " . $API_AppID . "\r\n" . 
        "X-PAYPAL-REQUEST-DATA-FORMAT: " . $API_RequestFormat . "\r\n" . 
        "X-PAYPAL-RESPONSE-DATA-FORMAT: " . $API_ResponseFormat . "\r\n" 
        )); 

        //create stream context 
        $ctx = stream_context_create($params); 

        //open the stream and send request 
        $fp = @fopen($url, "r", false, $ctx); 

        //get response 
        $response = stream_get_contents($fp); 

        //check to see if stream is open 
        if ($response === false) { 
          throw new Exception("php error message = " . $php_errormsg); 
        } 

        //close the stream 
        fclose($fp); 

        return $response; 
      } 
        catch (Exception $e) { 
          error_log($e->getMessage()); 
          print_r($e); 
          return false; 
        } 
    } 

    $paypal = new Paypal(); 

    ?> 

OK,現在執行該代碼時,我們得到以下錯誤:

Paypal returned: 2012-02-19T15:07:01.883-08:00Failureff3b99b56fcb72486531520003PLATFORMApplicationErrorApplicationAuthentication failed. API credentials are incorrect.

我們是在我們的繩索結束,請爲愛上帝幫助..

+0

「API憑據不正確」,錯誤似乎很明顯。 – 2012-02-19 23:33:33

+1

API憑證徹底檢查,1000年的時間,他們是正確的。 – 2012-02-20 00:23:29

+0

他們是來自真實賬戶的憑證嗎?這對Sandbox端點不起作用,反之亦然。通過https://developer.paypal.com/>測試帳戶>預先配置並使用這些API憑證來獲取測試賣家帳戶。 – Robert 2012-02-20 01:34:23

回答

0

您正在使用pypal沙盒或現場paypal API憑據。

+0

沙箱。我們正在沙箱中測試。所以帳戶是沙箱 – 2012-03-23 02:08:41