PayPal "授权失败"

3

这是我第一次使用PayPal API,所以请耐心等待。

我正在尝试使用BMCreateButton API动态创建加密按钮,使用了稍微修改过的代码来自这个页面。不幸的是,我似乎无法让它工作,因为在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中都失败了。我是否遗漏了某些基本知识,或者可能忽略了代码中的错误?请帮我看下。

虽然不是一个确切的答案,但我会建议你尝试一下GitHub上的PayPal SDK。相对于文档中的一些代码,它们往往工作得更好,并且可能能够帮你避免一些麻烦!这里有一个用于按钮管理API的SDK:https://github.com/paypal/buttonmanager-sdk-php - John
1个回答

1
我记得这个!
如果函数内没有定义$username,$password和$signature,那么你需要在函数顶部添加"global $username, $password, $signature"。

好的,问题已经解决。现在在错误日志中出现了O:8:"stdClass":4:{s:12:"ShortMessage";s:14:"Security error";s:11:"LongMessage";s:28:"Security header is not valid";s:9:"ErrorCode";s:5:"10002";s:12:"SeverityCode";s:5:"Error";} - pcuser42
那个问题是 $signature 变量中的一个杂项空格。现在按钮已经出现了! - pcuser42

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接