使用Stripe Webhook使用Stripe发送获取定期付款的客户详细信息

时间:2017-09-09 14:47:23

标签: java stripe-payments webhooks

我已在Java Spring MVC Web Application中配置Customer次付款。我可以为客户添加Plan,创建Subscriptions并设置invoice.upcoming.。由于我有定期付款,因此我想在生成发票后向客户发送电子邮件通知,并在付款后再发送。从Stripe文档中,我需要的事件类型是invoice.payment_succeededcustomer.subscription.trial_will_end@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(); } ,因为我有几个计划的试用期。

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

event type

我正在尝试获取customer Idlocalhost,以便我能够从我的数据库中获取客户并根据事件发送电子邮件通知。由于我的{{1}}中有我的webhook网址,因此我无法从Stripe中触发实际数据。此外,我无法从条带文档中找到示例数据:https://stripe.com/docs/api#invoice_object。我也尝试了Retrive stripe data from stripe webhook event,但没有一些示例数据就无法测试它。

有没有办法可以从事件中获取所需的详细信息,并在我的localhost上测试。

2 个答案:

答案 0 :(得分:3)

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

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

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

您可以检查其他解决方案,搜索"本地隧道"而不是ngrok。关键字。

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

如果您点击"发送测试webhook"按钮,Stripe将显示webhook在" Request"下发送的数据。 (即使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
      }
    }
  }

答案 1 :(得分:1)

data对象包含customer ID为string

对于invoice.upcominginvoice.payment_succeededstring对象中收到的客户ID为data

以下JSON包含data

的事件invoice.upcoming对象
{
  "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
}