简体   繁体   中英

Paypal - Subscriptions - Create a product

I am trying to make subscriptions integration with paypal. I am following the instructions from here: https://developer.paypal.com/docs/subscriptions/integrate/#

I have a problem in step number 2 (Create a product). I am using php to make curl call but I get error and can't solve it. The curl link is: https://api.sandbox.paypal.com/v1/catalogs/products

The response I got is:

{
    "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"
        }
    ]
}

Someone know please how can I fix it? How I can add permission so I can create a product?

First, you have to access token to authorize the permission

$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);

after getting access token, you send another hit to create a product

$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);

here your product creation is complete and you get a product id

Ref1: getAccessToken

Ref2: CreateProduct

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM