我正在使用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;
答案 0 :(得分:0)
我认为将令牌存储在数据库以及像Redis这样的数据存储中的最佳方法。这是我的主意