我正在try 在我的应用程序中将条纹订阅计费与分级定价模式集成在一起,根据我的理解,我需要做两件事:

  1. 允许我的新用户还创建条带客户帐户(通过集成)
  2. 在客户订阅付款处于活动状态时,监控条带WebHook‘Events’以提供访问权限

我的用户流如下:

  • 在我的应用程序中创建配置文件(保存到数据库)-&>;重定向到条带 checkout 门户以获取账单信息(保存到条带数据库)->attempt将条带客户ID保存到我的数据库,以便我可以监控订阅状态

然而,我不知道如何在我的应用程序中保存CustomerID信息,因为req.userreq.params是空的,因为用户从条带计费门户发送back到我的应用程序

Controller function

module.exports.stripeWebhook = async (req, res) => {
    let data;
    const webhookSecret = stripeWebhookSecret;
         if (webhookSecret) {
      let event;
      let signature = req.headers["stripe-signature"];

      try {
        event = stripe.webhooks.constructEvent(
          req.body,
          signature,
          webhookSecret
        );
      } catch (err) {
        console.log(`⚠️  Webhook signature verification failed.`);
        return res.sendStatus(400);
      }
      data = event.data;
      eventType = event.type;
    } else {
      // retrieve the event data directly from the request body.
      data = req.body.data;
      eventType = req.body.type;
    }

    switch (eventType) {
        case 'payment_intent.succeeded': {
          console.log('PaymentIntent was successful!');
          break;
      }
    case 'checkout.session.completed':
          // Payment is successful and the subscription is created.
          // You should provision the subscription and save the customer ID to your database.
          console.log(data.object.customer);  <- works
          const user = await User.findById(req.user.id); <- comes back empty so my next two lines of code don't work
          user.stripeId.push(data.object.customer);
          await user.save();
      break;
        default:
      }
      res.sendStatus(200);
  };

App.js

app.use(bodyParser.raw({type: "application/json"}));
app.use(express.json({ limit: '1mb' }));
app.use(express.urlencoded({ extended: true }));

我之所以包含app.js代码,是因为bodyparser.raw对我的控制器函数中主体的实现方式有影响.

我指望req.user或req.pars在我的数据库中找到该用户,但它不起作用.如何像条带注释所建议的那样将客户ID保存到我的数据库中?

推荐答案

在为客户创建 checkout 会话之前,您应该创建条纹客户帐户.

  1. 判断客户是否已拥有stripe_customer帐户(条纹客户帐户).如果是,请使用那个.如果没有,为他创建一个并将其保存在数据库中.

  2. 将 checkout 会话设置为stripe_customer,这样客户将在条带 checkout 时自动进行身份验证

  3. 您可以有 Select 地将用户的_id放入条纹 checkout 会话的元数据中,这样您就可以在稍后的Web挂钩中访问该数据.

您应该为每种货币创建一个stripe_customer帐户.因此,一个用户可以拥有多个stripe_customer个账户,每个账户对应一种货币.

router.post('/create-checkout-session', authenticate.verifyUser, async (req, res) => {
    const { currency } = req.body;

    ...

    // If user does not have stripe customer for order currency, create a new one.
    if (!(req.user.stripe_customer && req.user.stripe_customer[currency])) {
      const new_stripe_customer = await stripe.customers.create({
        email: req.user.email,
        metadata: {
          user_id: req.user._id.toString(),
        },
      });

      let update = {};
      update[`stripe_customer.${currency}`] = new_stripe_customer.id;
      await Users.findByIdAndUpdate(req.user._id, update);

      if (!req.user.stripe_customer) {
        req.user.stripe_customer = {};
        req.user.stripe_customer[currency] = new_stripe_customer.id;
      } else {
        req.user.stripe_customer[currency] = new_stripe_customer.id;
      }
    }

    ...

    // Set `stripe_customer` for checkout session.
    const session = await stripe.checkout.sessions.create({
      customer: req.user.stripe_customer[currency],
      ...
    });
 
    ...

}

Javascript相关问答推荐

如何用显示网格平滑地将元素从一个地方移动到另一个地方?

如何在Connect 4游戏中 for each 玩家使用位板寻找7形状?

Snowflake JavaScript存储过程返回成功,尽管预期失败

康威的生活游戏规则在我的Javascript版本中不起作用.''我做错了什么?

如何从URL获取令牌?

无法访问Vue 3深度监视器中对象数组的特定对象值'

XSLT处理器未运行

JavaScript:如果字符串不是A或B,则

如何使用Astro优化大图像?

如何组合Multer上传?

如何根据查询结果重新排列日期

如何修复错误&语法错误:不能在纯react 项目中JEST引发的模块&之外使用导入语句?

不协调嵌入图片

如何让SVG图标在被点击和访问后改变 colored颜色 ,并在被访问后取消点击时恢复到原来的 colored颜色 ?

使用createBrowserRoutVS BrowserRouter的Reaction路由

是否设置以JavaScript为背景的画布元素?

如何在每隔2分钟刷新OKTA令牌后停止页面刷新

无法向甜甜圈图表上的ChartJSImage添加可见标签

当一条路由在Reaction路由中命中时,如何有条件地渲染两个组件或更改两个插座?

如何在HTML中使用rxjs显示动态更新