【发布时间】:2019-06-26 21:45:53
【问题描述】:
我正在使用 PayPal API 在我的网站上设置订阅付款。我知道有 PayPal 节点 SDK,但不幸的是,他们没有相应地更新它,并且在订阅方面已经过时了。
我已经设法获取访问令牌并使用它来进行调用等。但是每次我调用他们的 API 时都获取访问令牌是没有意义的。存储访问令牌本身的最理想方式是什么,令牌的到期时间和创建令牌的时间。将使用过期时间和创建令牌的时间来检查令牌是否已过期,然后再尝试获取新令牌。
我创建了下面的 js 文件,将通过 await paypal.getToken() 使用
async function getToken(){
return new Promise((resolve, reject) => {
if(//Token expired) {
request.post({
uri: "https://api.sandbox.paypal.com/v1/oauth2/token",
headers: {
"Accept": "application/json",
"Accept-Language": "en_US",
"content-type": "application/x-www-form-urlencoded"
},
auth: {
'user': clientID,
'pass': clientSecret,
},
form: {
"grant_type": "client_credentials"
}
}, function(error, response, body) {
if(error)
reject(new Error(error))
else {
//Set global variable or whats best option with the access token that was obtained - JSON.parse(body).access_token
//Set global variable or whats best option with the expiry time - SON.parse(body).expires_in
//Set global vairable or whats best option with the current time in MS - Date.now()
resolve(JSON.parse(body).access_token);
}
});
}else {
// resolve(config.paypal_access_token); //Retrieve access_token from global vairable or whats best option
}
});
}
module.exports.getToken = getToken;
【问题讨论】: