如何解决我不明白如何获取令牌以进行 API 调用
我正在使用 Payouts API,为此我必须在调用 https://api-m.sandbox.paypal.com/v1/payments/payouts
时指定访问令牌
要获得访问令牌,as specified here 我必须使用 Postman 向 https://developer.paypal.com/docs/api/overview/#get-an-access-token 发送 POST 请求,它在响应中返回令牌
我的问题是这个令牌有一个过期时间,这是一个问题,因为我不能每 15 分钟进入我的应用程序来更改授权标头中的令牌。如何获得“永久”或“自动刷新”令牌?
我试图从我的应用程序而不是 Postman 拨打电话,但它似乎不起作用,它说我的凭据无效
这是我的代码,以防它有用(我从前端管理所有内容):
window.paypal
.Buttons({
createOrder: (data,actions) => {
return actions.order.create({
purchase_units: [
{
description: this.product.description,amount: {
value: this.product.price
}
}
]
});
},onApprove: (data,actions) => {
const timestamp = new Date().getUTCMilliseconds();
const id = timestamp;
return actions.order.capture().then(details => {
axios
.post(
"https://api-m.sandbox.paypal.com/v1/payments/payouts",{
sender_batch_header: {
sender_batch_id: id,email_subject: "You have a payout!",email_message:
"You have received a payout! Thanks for using our service!",recipient_type: "EMAIL"
},items: [
{
amount: {
value: this.product.price,currency: "USD"
},note: "congrats someone bought your stuff",sender_item_id: id,receiver: "john@doe.com"
}
]
},{
headers: {
Authorization:
"Bearer <my token that I get from postman>"
}
}
)
.then(() => {
alert(
"Transaction completed by " + details.payer.name.given_name
);
})
.catch(err => console.log(err));
});
}
})
.render(".paypal");
}
},
解决方法
更新
再次阅读代码并注意到您的付款与原始付款的 this.product.price
相同,这毫无意义。对于此用例,根本不要使用付款。全额通常会转到您的帐户,但如果您希望全额转到其他地方,请为此结帐设置 payee
变量:https://developer.paypal.com/docs/checkout/integration-features/pay-another-account/
(下面的原始答案)
从客户端使用 Payouts 绝对是一个糟糕的主意,任何拥有您的客户凭据的人都可以从您的帐户向他们想要的任何地方汇款
Payouts API 只能在您的服务器上使用。您的服务器需要执行 API 调用来获取访问令牌,类似于您提到的邮递员——除了使用您拥有的任何后端环境。您可以集成直接 HTTPS 调用,也可以使用 Payouts SDKs available 来自动处理获取访问令牌。
至于如何在捕获付款时触发后端操作,请使用适当的服务器集成:
创建两条路由,一条用于“创建订单”,一条用于“捕获订单”,documented here. 这些路由应返回 JSON 数据。在返回 JSON 数据之前,最后一条路线(捕获路线)应该在您的数据库中记录成功的交易并触发您想要的任何支付逻辑。
将这两条路线与以下审批流程配对:https://developer.paypal.com/demo/checkout/#/pattern/server
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。