【问题标题】:Paypal - Subscriptions - Create a productPaypal - 订阅 - 创建产品
【发布时间】:2019-12-09 23:34:13
【问题描述】:

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

我在第 2 步(创建产品)中遇到问题。 我正在使用 php 进行 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"
        }
    ]
}

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

【问题讨论】:

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


    【解决方案1】:

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

    $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

    参考1:getAccessToken

    Ref2:CreateProduct

    【讨论】:

    • 这不起作用。我认为 $accessToken 是一个字符串,所以 $accessToken->token_type 什么都不返回。产品不是在这里创建的。
    猜你喜欢
    • 2020-05-30
    • 2017-06-18
    • 2021-01-25
    • 2019-10-14
    • 2020-06-16
    • 1970-01-01
    • 2021-10-02
    • 2021-11-20
    • 1970-01-01
    相关资源
    最近更新 更多