贝宝-订阅-创建产品

时间:2019-08-01 11:18:11

标签: php curl paypal paypal-sandbox paypal-subscriptions

我正在尝试与Paypal集成订阅。 我正在按照此处的说明进行操作: https://developer.paypal.com/docs/subscriptions/integrate/#

我在步骤2(创建产品)中遇到问题。 我正在使用php进行curl调用,但出现错误,无法解决。 curl链接为:https://api.sandbox.paypal.com/v1/catalogs/products

我得到的答复是:

{
    "name": "NOT_AUTHORIZED",
    "message": "Authorization failed due to insufficient permissions.",
    "debug_id": "7de3b61dcde85",
    "details": [
        {
            "issue": "PERMISSION_DENIED",
            "description": "You do not have permission to access or perform operations on this resource"
        }
    ],
    "links": [
        {
            "href": "https://developer.paypal.com/docs/api/v1/billing/subscriptions#NOT_AUTHORIZED",
            "rel": "information_link",
            "method": "GET"
        }
    ]
}

有人知道我该如何解决?如何添加权限以便创建产品?

1 个答案:

答案 0 :(得分:0)

首先,您必须访问令牌以授权权限

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/oauth2/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD,  'your client id' .':'. 'your secret Key');
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Accept-Language: en_US';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$accessToken json_decode($result);

获取访问令牌后,您发送另一个匹配项来创建产品

$ch = curl_init();curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/catalogs/products');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"name": "Test Recurring","type": "SERVICE}');
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: '.$accessToken->token_type.' '.$accessToken->access_token.'';
$headers[] = 'Paypal-Request-Id: <your client id>';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$createProduct = json_decode($result);

您的产品创建完毕,并且您获得了产品ID

Ref1:getAccessToken

Ref2:CreateProduct