2013-02-04 554 views
3

(這是我第一次與貝寶API的工作如此忍受我。)貝寶「授權失敗」

我試圖創建使用BMCreateButton API飛加密按鍵,稍微用修改後的代碼來自this page。不幸的是,我似乎無法得到這個工作,因爲我在PHP錯誤日誌中收到「驗證/授權失敗」消息。 (我也問過這個的PayPal開發者論壇,但沒有得到答覆。)

此代碼:

function createButton($name, $price) { 
    // Check to make sure that our version of PHP supports SOAP. 
    if((PHP_VERSION_ID < 50101) || !in_array("soap", get_loaded_extensions())) { 
     error_log("PHP not running with SOAP"); 
    } else { 
     include_once("config.php"); //this file holds the credentials and whether it's running in sandbox mode or not 
     //basic variable validation 
     $price = floatval($price); 

     // Import the PayPal WSDL. 
     $location = $sandbox ? "https://api-3t.sandbox.paypal.com/2.0/" : "https://api-3t.paypal.com/2.0/"; 
     $soapClient = new SoapClient("https://www.paypalobjects.com/wsdl/PayPalSvc.wsdl", array(
      location => $location 
     )); 

     // Set our API credentials. 
     $credentials->Credentials->Username = $username; 
     $credentials->Credentials->Password = $password; 
     $credentials->Credentials->Signature = $signature; 
     $soapClient->__setSoapHeaders(new SoapHeader("urn:ebay:api:PayPalAPI", "RequesterCredentials", $credentials)); 

     // Start creating our BMCreateButtonReq object. For full details about this object, see 
     // http://tinyurl.com/3mxau6j . 
     $BMCreateButtonReq->BMCreateButtonRequest->Version = "71.0"; 

     $BMCreateButtonReq->BMCreateButtonRequest->ButtonCode = "ENCRYPTED"; 

     // What type of button are we creating? 
     $BMCreateButtonReq->BMCreateButtonRequest->ButtonType = "CART"; 

     // Specific variables for this button. For the full list of options, see 
     // http://tinyurl.com/6qyanl . For a basic payment, these two should be enough. 

     $BMCreateButtonReq->BMCreateButtonRequest->ButtonVar[] = "amount=$price"; 
     $BMCreateButtonReq->BMCreateButtonRequest->ButtonVar[] = "item_name=$name"; 

     // Run the API call. 
     $response = $soapClient->BMCreateButton($BMCreateButtonReq); 

     if($response->Ack == "Success" || $response->Ack == "SuccessWithWarning") { 
      // If successful, the button code will be in the Website element of $response. 
      // It'll be a full HTML form, so don't wrap it in any <form> tags -- just 
      // display it as-is. 
      echo $response->Website; 
     } else { 
      error_log("Could not create PayPal button. " . serialize($response->Errors)); 
      echo "<p class=\"note\">Cannot create button</p>"; 

     } 
    } 
} 

給出了這樣的錯誤日誌:

Could not create PayPal button. O:8:"stdClass":4:{s:12:"ShortMessage";s:35:"Authentication/Authorization Failed";s:11:"LongMessage";s:49:"You do not have permissions to make this API call";s:9:"ErrorCode";s:5:"10002";s:12:"SeverityCode";s:5:"Error";} 

我已經檢查了憑據,他們是正確的,並且沙箱以及實時API都失敗了。我錯過了一些基本的東西,還是在我可能忽略的代碼中存在錯誤?

+0

不是這樣的答案,但我會試圖給github上的paypal sdks一個嘗試..他們傾向於比在文檔中踢一些代碼更好的工作,並可能爲您節省一兩個頭痛!這裏有一個按鈕管理器api:https://github.com/paypal/buttonmanager-sdk-php – John

回答

1

我記得這一個!

$username,$password$signature裏面定義的函數嗎?如果不是,則需要在該函數的頂部添加global $username, $password, $signature

+0

啊,哎呀,這可能是問題... – pcuser42

+0

是的,這個問題得到解決。現在得到'O:8:「stdClass」:4:{s:12:「ShortMessage」; s:14:「安全錯誤」; s:11:「LongMessage」; s:28:「安全標頭無效」 ; s:9:「ErrorCode」; s:5:「10002」; s:12:「SeverityCode」; s:5:「Error」;}'在錯誤日誌中。 – pcuser42

+0

這個問題是'$ signature'變量中的一個雜散空間。按鈕現在出現! – pcuser42