【问题标题】:Using Stripe webhooks with graphql-yoga and Prisma将 Stripe webhook 与 graphql-yoga 和 Prisma 一起使用
【发布时间】:2019-02-20 22:39:31
【问题描述】:

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

我必须听取来自 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",
    }
  }
}

【问题讨论】:

    标签: express stripe-payments graphql webhooks prisma


    【解决方案1】:

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

    目前,您可以做的是使用Graphql-Yogaexpress[0] 公开一个普通的REST API 端点

    我已经编写了一个工作示例代码,你可以试试看

    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

    【讨论】:

    • 感谢您的帮助!这肯定是一个很好的解决方案。我唯一不喜欢使用服务器来监听 webhook 的地方是,在停机的情况下,事情可能会变得非常混乱。我正在考虑可能在“无服务器”解决方案或类似的解决方案上实现它
    • 实际上,Stripe webhook 有一个重试机制,以防您的服务器出现故障。如果您的服务器停机(希望您的服务器没有停机那么久:)),它将重试长达 72 小时(stripe.com/docs/webhooks#responding-to-a-webhook)。但绝对可以使用serverless 方法,解析响应并将webhook 代理到与架构匹配的Graphql 端点
    • 很好,但是如何访问 express 服务器内部的 'ctx' 变量?
    猜你喜欢
    • 2019-06-11
    • 2019-09-06
    • 2019-03-13
    • 2019-12-18
    • 2019-04-24
    • 2019-08-03
    • 2019-08-03
    • 2021-01-13
    • 2022-01-28
    相关资源
    最近更新 更多