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

你。 提交于 2019-12-11 17:08:52

问题


I have configured Stripe payments in my Java Spring MVC Web Application. I am able to add a Customer, create a Plan and also set up Subscriptions for the customer. Since I have recurring payments, I would like to send an email notification to Customers once an invoice is generated and also once the payment is made. From the Stripe documentations, the event types I require are invoice.upcoming., invoice.payment_succeeded and customer.subscription.trial_will_end since I have trial period for few plans.

I have added a webhook endpoint in my application as follows:

@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();
    }

I am trying to get the event type and also the customer Id so that I will be able to get the customer from my database and send the email notification based on the event. Since I have my webhook url in my localhost, I am unable to trigger actual data from Stripe. Also I was unable to find sample data from the Stripe Documents: https://stripe.com/docs/api#invoice_object. I have also tried Retrive stripe data from stripe webhook event, but unable to test it without some sample data.

Is there a way I can get the details required from the event and also test this on my localhost.


回答1:


During the development of your web application, in order to check webhooks sent to your localhost, you could use a solution like ngrok.

Once ngrok is setup and running, configure Stripe to send webhooks to the unique URL provided by ngrok, such as http://my-super-application.ngrok.io for example.

ngrok will "forward" the http requests it gets from Stripe to your local machine, exactly as if Stripe would have sent data directly to your local application.

Instead of ngrok, you can also check other solutions, search for "local tunnels" keywords.

To check data sent by Stripe webhooks from the Stripe dashboard, go to the "API" menu, then "Webhooks" tab, click on "TEST" button related to the end point you want to test.

If you click on "Send test webhook" button, Stripe will show you data sent by the webhook under "Request". (And you can check the request even if the webhook fails to get an answer from your end-point)

For example, for the invoice.upcoming event, you will get something like this:

{
    "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
      }
    }
  }



回答2:


The data object contain the customer ID as string

For invoice.upcoming and invoice.payment_succeeded the customer ID received as string in data object.

Following JSON contain the event data object for 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
}


来源:https://stackoverflow.com/questions/46131842/using-stripe-webhook-to-send-get-customer-details-for-recurring-payments-using-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!