gpt4 book ai didi

javascript - 如何通过 webhook 在 PayPal API 中确认每月订阅的计费?

转载 作者:行者123 更新时间:2023-12-05 00:36:28 24 4
gpt4 key购买 nike

我正在尝试实现 Paypal 订阅流程用户点击 Paypal 订阅按钮我通过仪表板创建的。
在后端,我听PAYMENT.SALE.COMPLETED订阅计费成功时触发的 webhook。不幸的是,webhook 并没有向我发送太多信息,因此我可以在我的数据库中检索与刚刚计费的订阅相关联的用户和项目。
这将允许我 安全地显示私有(private)内容 给那个用户。
这里是 webhook 内容 由 payPal 发送(对不起,长度):

const response = {
id: 'WH-4W487015EX264720U-32N35125TV248784B',
event_version: '1.0',
create_time: '2021-04-26T08:24:41.436Z',
resource_type: 'sale',
event_type: 'PAYMENT.SALE.COMPLETED',
summary: 'Payment completed for EUR 6.9 EUR',
resource: {
billing_agreement_id: 'I-T2HP99MJTS1T',
amount: {
total: '6.90',
currency: 'EUR',
details: {
subtotal: '6.90'
}
},
payment_mode: 'INSTANT_TRANSFER',
update_time: '2021-04-26T08:23:59Z',
create_time: '2021-04-26T08:23:59Z',
protection_eligibility_type: 'ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE',
transaction_fee: {
currency: 'EUR',
value: '0.48'
},
protection_eligibility: 'ELIGIBLE',
links: [
{
method: 'GET',
rel: 'self',
href: 'https://api.sandbox.paypal.com/v1/payments/sale/6R7481343K8159132'
},
{
method: 'POST',
rel: 'refund',
href: 'https://api.sandbox.paypal.com/v1/payments/sale/6R7481343K8159132/refund'
}
],
id: '6R7481343K8159132',
state: 'completed',
invoice_number: ''
},
links: [
{
href: 'https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-4W487015EX264720U-32N35125TV248784B',
rel: 'self',
method: 'GET'
},
{
href: 'https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-4W487015EX264720U-32N35125TV248784B/resend',
rel: 'resend',
method: 'POST'
}
],
}
我试过 GET /v1/payments/sale/:id但它并没有给我带来太多信息。
我还检查了有关该主题的其他堆栈溢出线程,但没有任何帮助。
我也不想使用前端 SDK 中提供的成功回调,因为它们不如 webhook 安全(连接可以在触发回调 see this gitlab issue 之前关闭)
我如何知道用户已为其订阅付费?

最佳答案

我们终于找到了解决方法 让我们的后端检索买家和商品。
前端
在订阅按钮代码上,经过大量试验/错误后,我们注意到 createSubscription方法接受 promise ,我们可以使用它在付款继续之前将订阅 ID 发送到后端:

paypal.Buttons({
style: {...},
createSubscription: function (data, actions) {
return actions.subscription.create({
/* Creates the subscription */
plan_id: 'P-26J60279VA924454WMCBPBSA',
}).then(subscriptionId => { // subscriptionId == I-9DH5L3A3JAEB
return new Promise((res, rej) => {
// here we send the subscriptionId to the back-end
// and create a pending subscription
const body = {subscriptionId, userId, itemId};
apiCall('POST', '/subscription', body,() => {
// allow to return subscriptionId to paypal
resolve(subscriptionId);
})
});
});
},
onApprove: function (data, actions) {
// this function was of NO USE
// it is not safe to call your backend here
// as connexion can close and paypal doesn't
// wait after this function to capture payment
// thus leading to orphaned subscriptions
// (paid but not linked to your backend)
},
}).render('#paypal-button');
后端(webhook 处理程序)
后端等待确认 webhook 其中 webhookResponse.resource.billing_agreement_id 是订阅 ID 并允许验证先前创建的订阅。我不太明白为什么 billing_agreement_id未命名 subscrition_id ...
如果不清楚,请告诉我。我让它作为答案,直到有更好的方法来做到这一点:)

关于javascript - 如何通过 webhook 在 PayPal API 中确认每月订阅的计费?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67270378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com