2

我尝试将PayPal APIs Getting Started Guide to PHP 中的示例改编为:

<?php

define('PAYPAL_API_USER', '<snip>');
define('PAYPAL_API_PWD', '<snip>');
define('PAYPAL_API_SIGNATURE', '<snip>');
define('PAYPAL_API_APPLICATION_ID', 'APP-80W284485P519543T');

$endpoing = 'https://svcs.sandbox.paypal.com/AdaptiveAccounts/CreateAccount';
$headers = array(
    'X-PAYPAL-SECURITY-USERID' => PAYPAL_API_USER,
    'X-PAYPAL-SECURITY-PASSWORD' => PAYPAL_API_PWD,
    'X-PAYPAL-SECURITY-SIGNATURE' => PAYPAL_API_SIGNATURE,

    'X-PAYPAL-REQUEST-DATA-FORMAT' => 'JSON',
    'X-PAYPAL-RESPONSE-DATA-FORMAT' => 'JSON',

    'X-PAYPAL-APPLICATION-ID' => PAYPAL_API_APPLICATION_ID,

    'X-PAYPAL-DEVICE-IPADDRESS' => '192.0.2.0', // :-? Also tried with my public IP address
    'X-PAYPAL-SANDBOX-EMAIL-ADDRESS' => '<snip>', // Typed my account's e-mail
);
$payload = '{
    "sandboxEmailAddress": "Sender-emailAddress",
    "accountType": "PERSONAL",
    "name": {
        "firstName": "Lenny",
        "lastName": "Riceman"
    },
    "address": {
        "line1": "123 Main St",
        "city": "Austin",
        "state": "TX",
        "postalCode": "78759",
        "countryCode": "US"
    },
    "citizenshipCountryCode": "US",
    "contactPhoneNumber": "512-555-5555",
    "dateOfBirth": "1968-01-01Z",
    "createAccountWebOptions": {
        "returnUrl": "http://www.example.com/success.html"
    },
    "currencyCode": "USD",
    "emailAddress": "lr12345@example.com",
    "preferredLanguageCode": "en_US",
    "registrationType": "Web",
    "requestEnvelope": {
        "errorLanguage": "en_US"
    }
}';
$options = array(
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => false,
CURLOPT_POSTFIELDS => $payload,
    CURLOPT_RETURNTRANSFER => true,
);

try{
    $curl = curl_init($endpoing);
    if(!$curl){
        throw new Exception('Could not initialize curl');
    }
    if(!curl_setopt_array($curl, $options)){
        throw new Exception('Curl error:' . curl_error($curl));
    }
    $result = curl_exec($curl);
    if(!$result){
        throw new Exception('Curl error:' . curl_error($curl));
    }
    curl_close($curl);
    echo $result;
}catch(Exception $e){
    echo 'ERROR: ' . $e->getMessage() . PHP_EOL;
}

...但我总是得到这个:

<?xml version='1.0' encoding='utf-8'?>
<ns3:FaultMessage xmlns:ns3="http://svcs.paypal.com/types/common" xmlns:ns2="http://svcs.paypal.com/types/aa">
    <responseEnvelope>
        <timestamp>2013-03-20T16:33:46.309-07:00</timestamp>
        <ack>Failure</ack>
        <correlationId>7d7fa53e6a930</correlationId>
        <build>5343121</build>
    </responseEnvelope>
    <error>
        <errorId>520003</errorId>
        <domain>PLATFORM</domain>
        <subdomain>Application</subdomain>
        <severity>Error</severity>
        <category>Application</category>
        <message>Authentication failed. API credentials are incorrect.</message>
    </error>
</ns3:FaultMessage>

我已经仔细检查了 API 凭据(用户、密码和签名),它们与配置文件页面中显示的完全一样。我不完全确定X-PAYPAL-DEVICE-IPADDRESSand X-PAYPAL-SANDBOX-EMAIL-ADDRESS

我怀疑我读错了一些东西或省略了一些 curl 选项。你能发现我的代码有什么问题吗?

4

3 回答 3

6

有点无益的是,该CURLOPT_HTTPHEADER选项不采用Header=>Value 对的哈希值,仅采用字符串列表,每个字符串都必须是完整的 HTTP 标头。

所以不是$headers = array('X-PAYPAL-SECURITY-USERID' => PAYPAL_API_USER, ...)你需要$headers = array('X-PAYPAL-SECURITY-USERID: ' . PAYPAL_API_USER, ...)

于 2013-03-20T23:58:17.240 回答
2

原因是您使用了错误的 api 凭据。您需要在您的主要开发者帐户中设置一个用户。设置完成后,您将在他们的个人资料中看到他们拥有自己单独的 API 凭据。

我遇到了同样的问题,花了几个小时才解决,但现在一切正常。

于 2013-11-04T14:56:15.060 回答
0

它确实很清楚地表明沙盒的 APP-ID 始终为APP-80W284485P519543T.

当您上线时,您将拥有自己的。

于 2014-09-17T23:54:48.023 回答