【问题标题】:Using Stripe Webhook to send get customer details for recurring payments using Stripe使用 Stripe Webhook 发送获取客户详细信息以使用 Stripe 进行定期付款
【发布时间】:2018-02-18 06:53:22
【问题描述】:

我在Java Spring MVC Web Application 中配置了Stripe 付款。我可以添加Customer,创建Plan 并为客户设置Subscriptions。由于我有定期付款,我想在生成发票和付款后向客户发送电子邮件通知。从 Stripe 文档中,我需要的事件类型是 invoice.upcoming.invoice.payment_succeededcustomer.subscription.trial_will_end,因为我有几个计划的试用期。

我在我的应用程序中添加了一个 webhook 端点,如下所示:

@ResponseBody
    @RequestMapping(consumes="application/json", produces="application/json", method=RequestMethod.POST, value="/webhook-endpoint")
    public Response  stripeWebhookEndpoint(@RequestBody String stripeJsonEvent)
    {
        Event event = Event.GSON.fromJson(stripeJsonEvent, Event.class);
        String type = event.getType();
        StripeObject stripeObject = event.getData().getObject();
        return Response.status(Response.Status.OK).build();
    }

我正在尝试获取event typecustomer Id,以便能够从我的数据库中获取客户并根据事件发送电子邮件通知。因为我的 localhost 中有我的 webhook url,所以我无法从 Stripe 触发实际数据。我也无法从 Stripe 文档中找到示例数据:https://stripe.com/docs/api#invoice_object。我也试过Retrive stripe data from stripe webhook event,但没有一些样本数据就无法测试。

有什么方法可以让我从活动中获取所需的详细信息,并在我的本地主机上进行测试。

【问题讨论】:

    标签: java stripe-payments webhooks


    【解决方案1】:

    在开发 Web 应用程序期间,为了检查发送到本地主机的 webhook,您可以使用类似ngrok 的解决方案。

    ngrok 设置并运行后,配置 Stripe 以将 webhook 发送到 ngrok 提供的唯一 URL,例如 http://my-super-application.ngrok.io

    ngrok 会将它从 Stripe 获得的 http 请求“转发”到您的本地计算机,就像 Stripe 将数据直接发送到您的本地应用程序一样。

    除了ngrok,你也可以查看其他解决方案,搜索“本地隧道”关键字。

    要从 Stripe 仪表板检查 Stripe webhooks 发送的数据,请转到“API”菜单,然后转到“Webhooks”选项卡,单击与您要测试的端点相关的“TEST”按钮。

    如果您点击“Send test webhook”按钮,Stripe 将在“Request”下显示 webhook 发送的数据。 (即使 webhook 无法从您的端点获得答案,您也可以检查请求)

    例如,对于invoice.upcoming 事件,您将得到如下内容:

    {
        "created": 1326853478,
        "livemode": false,
        "id": "evt_00000000000000",
        "type": "invoice.upcoming",
        "object": "event",
        "request": null,
        "pending_webhooks": 1,
        "api_version": "2017-06-05",
        "data": {
          "object": {
            "id": null,
            "object": "invoice",
            "amount_due": 0,
            "application_fee": null,
            "attempt_count": 0,
            "attempted": true,
            "charge": null,
            "closed": true,
            "currency": "jpy",
            "customer": "cus_00000000000000",
            "date": 1503541536,
            "description": null,
            "discount": null,
            "ending_balance": 0,
            "forgiven": false,
            "lines": {
              "data": [
                {
                  "id": "sub_BN5yNiTkAlQOye",
                  "object": "line_item",
                  "amount": 500,
                  "currency": "jpy",
                  "description": null,
                  "discountable": true,
                  "livemode": true,
                  "metadata": {
                  },
                  "period": {
                    "start": 1507604796,
                    "end": 1510283196
                  },
                  "plan": {
                    "id": "bplan",
                    "object": "plan",
                    "amount": 500,
                    "created": 1504352393,
                    "currency": "jpy",
                    "interval": "month",
                    "interval_count": 1,
                    "livemode": false,
                    "metadata": {
                    },
                    "name": "B plan",
                    "statement_descriptor": null,
                    "trial_period_days": null
                  },
                  "proration": false,
                  "quantity": 1,
                  "subscription": null,
                  "subscription_item": "si_1B0LmKE9P3qCpf5erqbpMxkI",
                  "type": "subscription"
                }
              ],
              "total_count": 1,
              "object": "list",
              "url": "/v1/invoices/in_1AuB2KE9P3qCpf5ekFh7qpAi/lines"
            },
            "livemode": false,
            "metadata": {
            },
            "next_payment_attempt": null,
            "paid": true,
            "period_end": 1503541536,
            "period_start": 1503541536,
            "receipt_number": null,
            "starting_balance": 0,
            "statement_descriptor": null,
            "subscription": "sub_00000000000000",
            "subtotal": 0,
            "tax": null,
            "tax_percent": null,
            "total": 0,
            "webhooks_delivered_at": 1503541537
          }
        }
      }
    

    【讨论】:

    • 感谢您的回复,但我仍然如何从 Event 对象中获取客户 ID。当我尝试 event.getUserId() 时,它显示 Event 类型的方法 getUserId() 已弃用。我现在只需要事件类型和客户 ID,以便我可以向该客户发送电子邮件
    • 根据我之前回答的 JSON 示例,如果事件类型为 invoice.upcoming,则可以解析从 webhook 获取的 JSON 数据并读取 data.object.customer 属性以获取客户 ID。它适用于您的用例吗?
    • StripeObject stripeObject = event.getData().getObject();会给对象。我仍然没有看到从这个对象获取客户的方法
    • 如果 webhook 类型是 invoice.upcoming,你得到的 stripeObject 应该被转换为 Invoice。根据文档github.com/stripe/stripe-java/blob/master/src/main/java/com/…,Invoice 对象应该有一个getCustomer() 方法。但它不适合你?
    • @Michael Rambeau,它成功了,谢谢,你知道我们如何增加 webhook 的超时时间,以便我可以在超时时间内处理数据。
    【解决方案2】:

    data 对象包含customer ID 作为string

    对于invoice.upcominginvoice.payment_succeeded,在data 对象中作为string 收到的客户ID。

    以下 JSON 包含 invoice.upcoming 的事件 data 对象

    {
      "object": {
        "object": "invoice",
        "amount_due": 30000,
        "application_fee": null,
        "attempt_count": 0,
        "attempted": false,
        "charge": null,
        "closed": false,
        "currency": "gbp",
        "customer": "cus_ATtwlQqRx75cxxx",
        "date": 1505559958,
        "description": null,
        "discount": null,
        "ending_balance": null,
        "forgiven": false,
        "lines": {
          "object": "list",
          "data": [
            {
              "id": "sub_AU9VONtkvz9xxx",
              "object": "line_item",
              "amount": 30000,
              "currency": "gbp",
              "description": null,
              "discountable": true,
              "livemode": false,
              "metadata": {
              },
              "period": {
                "start": 1505559958,
                "end": 1508151958
              },
              "plan": {
                "id": "package_1",
                "object": "plan",
                "amount": 30000,
                "created": 1492282426,
                "currency": "gbp",
                "interval": "month",
                "interval_count": 1,
                "livemode": false,
                "metadata": {
                },
                "name": "Package 1",
                "statement_descriptor": null,
                "trial_period_days": null
              },
              "proration": false,
              "quantity": 1,
              "subscription": null,
              "subscription_item": "si_1A9BCcJ7IsZfBU9bw4Cxxx",
              "type": "subscription"
            }
          ],
          "has_more": false,
          "total_count": 1,
          "url": "/v1/invoices/in_xxxxxnV9RmPcl/lines"
        },
        "livemode": false,
        "metadata": {
        },
        "next_payment_attempt": 1505563558,
        "paid": false,
        "period_end": 1505559958,
        "period_start": 1502881558,
        "receipt_number": null,
        "starting_balance": 0,
        "statement_descriptor": null,
        "subscription": "sub_AU9VONtkvz9xxx",
        "subtotal": 30000,
        "tax": null,
        "tax_percent": null,
        "total": 30000,
        "webhooks_delivered_at": null
      },
      "previous_attributes": null
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-22
      • 2015-06-13
      • 2017-03-06
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      • 2015-09-23
      相关资源
      最近更新 更多