express - 将 Stripe webhooks 与 graphql-yoga 和 Prisma 结合使用

标签 express stripe-payments graphql webhooks prisma

我在查找如何在我的应用程序中拦截 Stripe webhook 调用时遇到了一些麻烦。我使用 graphql-yoga (express) 和 prisma。

我必须监听来自 Stripe 的支付失败电话,以便我可以编辑相应的用户资料。

感谢您的帮助!

Stripe webhook 调用如下所示:

{
  "created": 1326853478,
  "id": "charge.expired_00000000000000",
  "type": "charge.expired",
  "object": "event",
  "request": null,
  "pending_webhooks": 1,
  "data": {
    "object": {
      "id": "ch_00000000000000",
      "object": "charge",
      "amount": 100,
      "captured": false,
      "created": 1537153592,
      "currency": "usd",
      "customer": null,
      "description": "My First Test Charge (created for API docs)",
      "invoice": null,
      "livemode": false,
      "on_behalf_of": null,
      "order": null,
      "outcome": null,
      "paid": true,
      "receipt_email": null,
      "receipt_number": null,
      "refunded": false,
      "review": null,
      "shipping": null,
      "source": {
        "id": "card_00000000000000",
        "object": "card",
        "address_city": null,
        "address_country": null,
        "address_line1": null,
        "address_line1_check": null,
        "address_line2": null,
        "address_state": null,
        "address_zip": "12919",
        "address_zip_check": "pass",
        "brand": "Visa",
        "country": "US",
        "customer": "cus_00000000000000",
        "cvc_check": null,
        "name": null,
        "tokenization_method": null
      },
      "statement_descriptor": null,
      "status": "succeeded",
    }
  }
}

最佳答案

由于 Stripe Webhook 返回带有 JSON 负载的通用 http POST,它不会根据 Graphql< 格式化 event 数据 语言查询。

现在,您可以做的是使用 Graphql-Yogaexpress[0]

我编写了一个可以工作的示例代码,您可以尝试一下

const { GraphQLServer } = require('graphql-yoga')
const typeDefs = `
  type Query {
    hello(name: String): String!
  }
`
const resolvers = {
  Query: {
    hello: (_, { name }) => `Hello ${name || 'World'}`,
  },
}

const server = new GraphQLServer({ typeDefs, resolvers, skipValidation: true })
server.express.use('/api/stripe/webhooks', (req, res) => {
    // Handle your callback here !!!!
    res.status(200).send();
})

server.start(() => console.log('Server is running on localhost:4000'))

如果以上内容有帮助,请告诉我。

[0] https://github.com/prisma/graphql-yoga#how-to-eject-from-the-standard-express-setup

关于express - 将 Stripe webhooks 与 graphql-yoga 和 Prisma 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52360385/

相关文章:

stripe-payments - 如何在 Stripe 结帐屏幕上添加 "Agree to terms and conditions"复选框?

c# - 如何在 asp.net webform 中获取 js POST?

amazon-web-services - 如何在 aws-amplify 中的列表查询中的字段上按日期(createdAt)排序?

javascript - Mongoose 虚拟填充返回 null

node.js - node/express 中的 "/register"和 "register"路由是否相同?

node.js - 如何使用 websockets 制作一个 crud 应用程序?

sql - 使用 Node.js 将简单表从 SQL 转换为 Redis

javascript - 如何使用路由在 nodejs 应用程序中发布表单数据?

node.js - 在 Heroku 的 Express Node 中存储 api key 的位置

java - 从 Java 在 GraphQL 查询中添加片段