簡體   English   中英

Paypal 訂閱取消 - 翻譯 CURL 命令

[英]Paypal Subscription cancel - Translate CURL command

我想為用戶添加選項以取消我的 Web 應用程序的 Paypal 訂閱。

https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel

https://developer.paypal.com/reference/get-an-access-token/

https://developer.paypal.com/api/rest/authentication/

我知道首先我需要使用我的項目 ID 和密碼調用端點。 我是否需要在服務器上執行此操作以免泄露秘密?

然后使用收到的身份驗證數據並再次調用訂閱結束。

CURL 代碼:

 curl -v https://api-m.sandbox.paypal.com/v1/oauth2/token \ -H "Accept: 
 application/json" \ -H "Accept-Language: en_US" \ -u "client_id:secret" \ -d 
 "grant_type=client_credentials"

Postman 文檔:“

  1. 為您的環境下載 Postman。 Postman、select中的POST方法。
  2. Postman、select中的POST方法。
  3. 輸入https://api-m.sandbox.paypal.com/v1/oauth2/token請求 URL。
  4. 在授權選項卡上,select 是基本身份驗證類型。 在用戶名框中鍵入您的客戶端 ID,然后在密碼框中鍵入您的密碼。
  5. 在正文選項卡上,select x-www-form-urlencoded。 在密鑰框中鍵入 grant_type,然后在值框中鍵入 client_credentials。
  6. 點擊發送

有人可以將 CURL 代碼翻譯成獲取 API 請求嗎? 有人可以解釋一下取消 PayPal 訂閱必須采取的步驟嗎?

https://www.paypal.com/merchantapps/appcenter/acceptpayments/subscriptions

按照 PayPal 提供的和上面列出的說明,我能夠在 Postman Desktop for Mac 上成功執行身份驗證。

然后我在 Postman 中查找 JavaScript fetch 中的代碼片段,並找到了我要找的東西。

我對需要 base 64 編碼的要求( btoa() )感到有點困惑,並由 Postman 自動完成並添加到代碼片段中。

@Peter Thoeny 的評論也很有幫助。

這是我用於身份驗證和取消授權的代碼:

var myHeaders = new Headers();
myHeaders.append("Authorization", "Basic " + btoa("ClientID:Secret") );
myHeaders.append("Content-Type", "application/x-www-form-urlencoded");

var urlencoded = new URLSearchParams();
urlencoded.append("grant_type", "client_credentials");

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: urlencoded
};

fetch("https://api-m.sandbox.paypal.com/v1/oauth2/token", requestOptions)
  .then( (response) => response.json())
  .then(result => {
    console.log(result);

    var myHeaders = new Headers();
    myHeaders.append("Authorization", "Bearer " + result.access_token );
    myHeaders.append("Content-Type", "application/json");

    fetch("https://api-m.sandbox.paypal.com/v1/billing/subscriptions/" + _this.lastSubscriptionData.resourceId + "/cancel", {
      method: 'POST',
      headers : myHeaders
    })
    .then( (response) => response.text())
    .then( (result) => {
      console.log(result);
    })
    .catch( (error) => console.log('error', error));

  })
  .catch(error => console.log('error', error));

使用“axios”版本

const axios = require('axios')
const config = require('./config.json');
const getAccessToken = async () => {
    try {
        const resp = await axios.post(
            'https://api-m.sandbox.paypal.com/v1/oauth2/token',
            '',
            {
                params: {
                    'grant_type': 'client_credentials'
                },
                auth: {
                    username: config.CLIENT_ID,
                    password: config.CLIENT_SECRET
                }
            }
        );
        // console.log(resp.data);
        return Promise.resolve(resp.data.access_token);
    } catch (err) {
        // Handle Error Here
        console.error(err);
        return Promise.reject(err);
    }
};

getAccessToken()
    .then((token) => {
        console.log(token);
    })

config.json

{
    "CLIENT_ID" : "***** your Client ID *******",
    "CLIENT_SECRET" : "***** your client secret ********"
}

和 curl 版本

CLIENT_ID='***** your Client ID *******'
CLIENT_SECRET='***** your client secret ********'
CLIENT_ID_SECRET=$(echo -n $CLIENT_ID:$CLIENT_SECRET | base64 -w 0)

ACCESS_TOKEN=$(curl -v https://api-m.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-H 'Authorization: Basic '$CLIENT_ID_SECRET \
-d "grant_type=client_credentials" | jq -r '.access_token')
echo $ACCESS_TOKEN

這里我用的是axios;

const axios = require('axios');

exports.handler = async (event) => {
await axios.post(
    `${PAYPAL_API_ROOT_URL}/v1/oauth2/token`,
    new URLSearchParams({
        'grant_type': 'client_credentials'
    }),
    {
        auth: {
            username: PAYPAL_CLIENT_ID,
            password: PAYPAL_CLIENT_SECRET
        }
    }
    ).then((result) => {
        access_token = result.data.access_token
        // responseBody = result.data;
        statusCode = 200;
    }).catch((error) => {
        console.log(error)
        responseBody = error;
        statusCode = 404;
    })

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM