我想为我的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 -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中,选择POST方法。
- 在Postman中,选择POST方法
- 输入https://api-m.sandbox.paypal.com/v1/oauth2/token请求URL
- 在"授权"页签中选择"基本授权"类型。输入您的在"用户名"框中输入客户端ID,并在"密码"中输入您的秘密盒子。
- 在Body选项卡上,选择x-www-form-urlencoded。在密钥框中输入grant_type,在值框中输入client_credentials。
- 单击Send
,
谁能把CURL代码翻译成fetch API请求?谁能告诉我取消PayPal订阅的步骤吗?
https://www.paypal.com/merchantapps/appcenter/acceptpayments/subscriptions
我能够按照上面列出的PayPal提供的说明在Mac上成功执行Postman Desktop认证。
然后我在Postman中查找JavaScript fetch中的代码片段,并找到了我正在寻找的内容。
我对base 64 encoding required(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;
})
}