使用条纹订阅Node.js进行身份验证

时间:2020-07-02 06:32:11

标签: node.js mongodb mongoose jwt stripe-payments

我无法确定用Stripe攻击订阅身份验证的最佳方法。我有一个预先存在的MERN堆栈,该堆栈使用JWT进行身份验证,并存储订阅ID。但是,要检测诸如取消,发票不完整等更改,我想不出没有套接字的解决方案。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

在更新JWT之前,只需检查订阅的状态-并确保有效期足够短以使您在无付费访问窗口方面感到舒适。

您还可以使用一些中间件来为每个ajax请求(或至少一个常见/常见ajax请求)执行此操作,如果不再将订阅视为“付费”,则该中间件将返回402并使JWT无效。 / p>

答案 1 :(得分:0)

看看用于接收事件通知的条纹webhooks。

https://stripe.com/docs/webhooks

收听Stripe帐户上的事件,以便您的集成可以自动触发反应。

Stripe使用webhook来在您的帐户中发生事件时通知您的应用程序。 Webhooks对于异步事件特别有用,例如当客户的银行确认付款,客户对费用提出异议或定期付款成功时,

只需三个步骤即可开始将Webhooks与Stripe集成使用:

1。在服务器上创建一个Webhook端点。

2。使用Stripe CLI测试端点是否正常工作。

3。向Stripe注册端点以启用该功能。

创建Webhook的基本示例

// This example uses Express to receive webhooks
const app = require('express')();

// Use body-parser to retrieve the raw body as a buffer
const bodyParser = require('body-parser');

// Match the raw body to content type application/json
app.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {
  let event;

  try {
    event = JSON.parse(request.body);
  } catch (err) {
    response.status(400).send(`Webhook Error: ${err.message}`);
  }

  // Handle the event
  switch (event.type) {
    case 'payment_intent.succeeded':
      const paymentIntent = event.data.object;
      // Then define and call a method to handle the successful payment intent.
      // handlePaymentIntentSucceeded(paymentIntent);
      break;
    case 'payment_method.attached':
      const paymentMethod = event.data.object;
      // Then define and call a method to handle the successful attachment of a PaymentMethod.
      // handlePaymentMethodAttached(paymentMethod);
      break;
    // ... handle other event types
    default:
      // Unexpected event type
      return response.status(400).end();
  }

  // Return a response to acknowledge receipt of the event
  response.json({received: true});
});

app.listen(8000, () => console.log('Running on port 8000'));