繁体   English   中英

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

[英]Using Stripe Webhook to send get customer details for recurring payments using Stripe

我已经在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 type以及customer Id以便能够从数据库中获取客户并根据事件发送电子邮件通知。 由于我在localhost有webhook网址,因此无法从Stripe触发实际数据。 我也无法从Stripe文档中找到示例数据: https : //stripe.com/docs/api#invoice_object 我还尝试了从stripe webhook事件中检索条纹数据 ,但是如果没有一些示例数据就无法测试它。

有没有办法我可以从事件中获取所需的详细信息,也可以在本地主机上对其进行测试。

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

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

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

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

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

如果单击“发送测试Webhook”按钮,Stripe将在“请求”下显示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
      }
    }
  }

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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM