结账后使用 Mongoose 将 Stripe 客户 ID 保存在我的数据库中

2024-01-19

我使用客户门户设置了 Stripe Checkout,并且希望能够检索客户 ID 以让用户访问他的门户。 为了:

  1. 我想检索客户 ID
  2. 将其保存在我的数据库中(用户已登录)。

结账表单效果很好,重定向也很好。但我无法检索客户 ID(当我console.log() it.

我的快递代码:

  router.post("/create-checkout-session", ensureAuthenticated, async (req, res) => {
    const { priceId } = req.body;
    try {
      const session = await stripe.checkout.sessions.create({
        mode: "subscription",
        payment_method_types: ["card"],
        line_items: [
          {
            price: priceId,
            quantity: 1,
          },
        ],
        
        success_url: 'http://localhost:3000/fr/premiereconnexion?session_id={CHECKOUT_SESSION_ID}'
      });
    
      res.send({
        sessionId: session.id,
      });
    }
    catch (e) {
    res.status(400);
    return res.send({
      error: {
        message: e.message,
      }
    });
  }
});

router.post('/premiereconnexion', ensureAuthenticated, async (req, res) => {
  const session = await stripe.checkout.sessions.retrieve(req.query.session_id);
  const customerId = await stripe.customers.retrieve(session.customer.id);
  console.log(customerId);
  
  req.user.stripeCustomer = customerId
  req.user.save()
});
router.get('/premiereconnexion', ensureAuthenticated, (req, res) => {
  res.render('users/fr/endpayment', {
    user: req.user
  })
})

我的用户模型:

const User = new Schema({
    firstName: {
        type: String,
        required: true
    },
    lastName: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true,
        unique: true,
        required: true
    },
    stripeCustomer: {
        type: String,
        default: null
    }
});

第一个问题是能够调用客户ID。我什至不知道我该怎么做。


const customerId = await stripe.customers.retrieve(session.customer.id)

Session 对象上的客户只是一个ID https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-customer— 这是 ID 本身。你可以expand https://stripe.com/docs/expand但你似乎没有。它不是一个具有id场地。所以 customer.id 为空。

尝试做只是customerId = session.customer反而。

另请注意,您不应该真正在页面上执行此逻辑success_url。客户可能不会访问该页面,因为他们可能会在付款后立即关闭浏览器。您应该使用网络钩子。https://stripe.com/docs/ payments/checkout/fulfill-orders https://stripe.com/docs/payments/checkout/fulfill-orders

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

结账后使用 Mongoose 将 Stripe 客户 ID 保存在我的数据库中 的相关文章

  • NodeJS 找不到模块“grpc”

    我尝试在我的树莓派 3 上运行 JS 脚本 但我不断返回一个似乎是 grpc 模块的问题 我已经尝试重新安装和重建 npm 以下https github com firebase firebase tools issues 442 http
  • 将jquery与nodejs一起使用时的基本错误

    我正在尝试在我的项目中使用一些jquery 当我尝试使用它时 我在复制的代码中遇到了错误 并且无法获得任何有关它的谷歌帮助 var jquery require jquery var jquery create TypeError Obje
  • ER_NOT_SUPPORTED_AUTH_MODE:客户端不支持服务器请求的身份验证协议;考虑升级MySQL客户端

    Nodejs中使用mysql的问题 const mysql require mysql var connection mysql createConnection host localhost user root password 1234
  • 使用 Bluebird.js 和 Twitter 流的 Promise 和流

    我对 Promises 和 Node 非常陌生 并且对在流中使用 Promise 感到好奇 我可以承诺直播吗 使用 Bluebirdjs 和 Twit 模块 我有以下内容 var Twit require twit var Promise
  • 谷歌应用程序引擎nodejs本地开发

    有没有办法在本地运行我的nodejs应用程序 以便我可以模拟在生产App Engine中运行的应用程序而无需部署它 由于某种原因 我的应用程序在 Google App Engine 中的行为与我的本地主机不同 我厌倦了每次为了查看是否存在错
  • JavaScript - 类根据条件扩展

    事情是这样的 我有一个名为 A 的主课 我希望这个班级能够扩展 B 级 class A extends B 但事实上 我希望 B 类在特定条件下扩展 C D 或 E class B extends B1 or class B extends
  • ExpressJS - 提供通用 Nuxt 应用程序和 AngularJS SPA

    我有一个具有以下结构的博客项目 服务器 用 Node Express 编写 管理员 AngularJS SPA public AngularJS SPA 目前 管理部分和公共部分具有相同的域 但管理部分使用不同的子域 这允许我在 Expre
  • 已安装全局 NPM 包但未找到命令

    我已经全局安装了两个 npm 包 下载 https www npmjs com package download and 谜虚拟盒 https www npmjs com package enigmavirtualbox通过命令行 npm
  • mongoDB白名单IP

    我看到类似的帖子 但没有一个能帮助我解决我的问题 在学习了从头开始构建 MERN 应用程序的 Udemy 教程后 我陷入了 mongoose 连接的困境 这是我的 index js 代码 const express require expr
  • 如何在 inversify 中注入异步依赖?

    我有 TypeScript 应用程序并且正在使用反转 http inversify io 对于国际奥委会 我有一个连接类 use strict import injectable from inversify import createCo
  • 使用 node.js 获取正在运行的进程的 stdin/stdout

    我正在从节点启动一个进程child process spawn http nodejs org docs v0 6 1 api child processes html child process spawn处理 process stdou
  • nodeJS exec 不适用于“cd”shell cmd

    var sys require sys exec require child process exec exec cd home ubuntu distro function err stdout stderr console log cd
  • 在 TypeScript 中将 Chai 自定义插件声明为 NodeJS 全局变量

    这是我之前的问题 https stackoverflow com questions 61676032 declare nodejs global variables in before hook in webdriverio using
  • Windows 上的 Node.js 和 Express

    今晚 我决定尝试在我的 Windows 7 计算机上使用 Express 构建一个简单的 Node js 应用程序 安装过程还算顺利 但 Express 拒绝配合 以下是我已采取的步骤 使用以下位置提供的 MSI 安装 Node jshtt
  • 如何使用特定版本的NPM?

    如何切换我正在使用的 npm 版本 现在 npm v 1 1 65 但我需要 1 0 x 我尝试过 但出现错误 npm version 1 0 npm ERR version No package json found 有人知道如何使用不同
  • npm install 没有拉入 devDependency

    我有 npm v 1 2 32 当我跑步时 npm 安装 mongo migrate 它不安装 mongodb 这是一个 devDependency 我究竟做错了什么 虽然与这个问题没有直接关系 但有些人可能有兴趣知道如果环境变量NODE
  • npm install 的 --save 选项是什么?

    我看到一些教程 其中命令是 npm install save 什么是 save选项是什么意思 更新 npm 5 As of npm 5 0 0 http blog npmjs org post 161081169345 v500 安装的模块
  • Socket.io 与服务器离线连接

    如何检测服务器是否离线或由于其他原因无法连接 我的代码看起来像这样 this socket io connect connectionInfo reconnect false 它不会抛出任何错误 因此 try catch 子句不起作用 Us
  • node-mongodb-native的插入性能

    我正在使用 MongoDB 测试 Node js 的性能 我知道其中每一个都很好 彼此独立 但我正在尝试一些测试来感受它们 我遇到了这个问题 但无法确定来源 问题 我正在尝试在单个 Node js 程序中插入 1 000 000 条记录 它
  • Electron — 无法显示自定义图标

    我在以两种不同的方式设置 Electron 应用程序的图标时遇到问题 非打包 通过终端运行应用程序 我的 main js 确实指定了一个 图标 值 指向图标文件 但它不适用 包装 使用电子包装器 我的 package json 文件指定了

随机推荐