[英]Paypal Subscription cancel - Translate CURL command
I want to add the option for users to cancel a Paypal Subscription for my Web App.我想为用户添加选项以取消我的 Web 应用程序的 Paypal 订阅。
https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel
https://developer.paypal.com/reference/get-an-access-token/ https://developer.paypal.com/reference/get-an-access-token/
https://developer.paypal.com/api/rest/authentication/ https://developer.paypal.com/api/rest/authentication/
I understand that first I need to call a endpoint with my project ID and secret.我知道首先我需要使用我的项目 ID 和密码调用端点。 Do I need to do this on the server so the secret is not exposed?我是否需要在服务器上执行此操作以免泄露秘密?
Then use the authentication data received and make another call for the subscription ending.然后使用收到的身份验证数据并再次调用订阅结束。
CURL code: 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"
The Postman documentation: " Postman 文档:“
" “
Can someone please translate the CURL code into a fetch API request?有人可以将 CURL 代码翻译成获取 API 请求吗? Can someone please explain the steps that I have to take to cancel a PayPal subscription?有人可以解释一下取消 PayPal 订阅必须采取的步骤吗?
https://www.paypal.com/merchantapps/appcenter/acceptpayments/subscriptions https://www.paypal.com/merchantapps/appcenter/acceptpayments/subscriptions
I was able to perform a successful authentication on Postman Desktop for Mac following the instructions provided by PayPal and listed above.按照 PayPal 提供的和上面列出的说明,我能够在 Postman Desktop for Mac 上成功执行身份验证。
Then I looked in Postman for Code Snippet in JavaScript fetch and found out what I was looking for.然后我在 Postman 中查找 JavaScript fetch 中的代码片段,并找到了我要找的东西。
I was a little confused by the requirement of base 64 encoding required( btoa()
) and automatically done by Postman and added in the code snippet.我对需要 base 64 编码的要求( btoa()
)感到有点困惑,并由 Postman 自动完成并添加到代码片段中。
The comment from @Peter Thoeny was also helpful. @Peter Thoeny 的评论也很有帮助。
This is the code that I used for authentication and cancel authorization:这是我用于身份验证和取消授权的代码:
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));
Using 'axios` version使用“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 ********"
}
And curl version和 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
Here I used axios;这里我用的是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.