2

我在查找如何在我的应用程序中拦截 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",
    }
  }
}
4

1 回答 1

5

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

现在,您可以做的是使用's [0]公开一个普通的RESTAPI 端点Graphql-Yogaexpress

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

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

于 2018-09-26T08:37:29.323 回答