538*_*MEO 4 javascript paypal webhooks node.js paypal-rest-sdk
我正在尝试实现PayPal 订阅流程,其中用户单击我通过仪表板创建的PayPal 订阅按钮。
在后端,我监听PAYMENT.SALE.COMPLETED订阅计费成功时触发的 Webhook。不幸的是,网络钩子没有向我发送太多信息,以便我可以检索链接到刚刚计费的订阅的数据库中的用户和项目。
这将使我能够安全地向该用户显示私人内容。
以下是payPal 发送的webhook 内容(抱歉太长了):
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'
}
],
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试过GET,/v1/payments/sale/:id但它没有给我带来太多信息。
我还检查了有关该主题的其他堆栈溢出线程,但没有任何帮助。我也不想使用前端 SDK 中提供的成功回调,因为它们不像 Webhook 那样安全(连接可以在触发回调之前关闭,请参阅此 gitlab 问题)
我如何知道用户已为其订阅付费?
我们终于找到了一个解决方法,让我们的后端检索买家和商品。
在订阅按钮代码上,经过多次试验/错误,我们注意到该createSubscription方法接受承诺,并且我们可以使用它在付款继续之前将 subscriptionId 发送到后端:
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');
Run Code Online (Sandbox Code Playgroud)
后端等待webhookResponse.resource.billing_agreement_id 订阅 ID所在的确认 Webhook ,并允许验证之前创建的订阅。我不太明白为什么billing_agreement_id不命名subscrition_id...
如果还不够清楚,请告诉我。我以此作为答案,直到有更好的方法来做到这一点:)