使用 Stripe Webhook 发送使用 Stripe 定期付款的客户详细信息 [英] Using Stripe Webhook to send get customer details for recurring payments using Stripe
问题描述
我已经在我的 Java Spring MVC Web 应用程序
中配置了 Stripe
付款.我可以添加一个 Customer
,创建一个 Plan
并为客户设置 Subscriptions
.由于我有定期付款,因此我想在生成发票和付款后向客户发送电子邮件通知.从 Stripe 文档中,我需要的事件类型是 invoice.upcoming.
、invoice.payment_succeeded
和 customer.subscription.trial_will_end
因为我有试用少数计划的时期.
我在我的应用程序中添加了一个 webhook 端点,如下所示:
@ResponseBody@RequestMapping(consumes="application/json", products="application/json", method=RequestMethod.POST, value="/webhook-endpoint")公共响应 stripeWebhookEndpoint(@RequestBody String stripeJsonEvent){事件事件 = Event.GSON.fromJson(stripeJsonEvent, Event.class);字符串类型 = event.getType();StripeObject stripeObject = event.getData().getObject();返回 Response.status(Response.Status.OK).build();}
我正在尝试获取 event type
以及 customer Id
以便我能够从我的数据库中获取客户并根据以下内容发送电子邮件通知事件.由于我的 localhost
中有我的 webhook url,我无法从 Stripe 触发实际数据.我也无法从 Stripe 文档中找到示例数据:https://stripe.com/docs/api#invoice_object.我也尝试过从条纹 webhook 事件中检索条纹数据,但无法在没有一些样本数据的情况下对其进行测试.
有没有办法可以从事件中获取所需的详细信息,并在我的本地主机上进行测试.
在开发 Web 应用程序期间,为了检查发送到 localhost 的 Webhook,您可以使用类似 ngrok.
ngrok 设置并运行后,配置 Stripe 将 webhook 发送到 ngrok 提供的唯一 URL,例如 http://my-super-application.ngrok.io
.>
ngrok 会将它从 Stripe 获取的 http 请求转发"到您的本地计算机,就像 Stripe 将数据直接发送到您的本地应用程序一样.
除了ngrok,你还可以查看其他解决方案,搜索本地隧道"关键字.
要从 Stripe 仪表板检查 Stripe webhooks 发送的数据,请转到API"菜单,然后转到Webhooks"选项卡,单击与要测试的端点相关的TEST"按钮.
如果您单击发送测试 webhook"按钮,Stripe 将在请求"下显示 webhook 发送的数据.(即使 webhook 无法从您的端点获得答案,您也可以检查请求)
例如,对于 invoice.upcoming
事件,您将得到如下内容:
<代码>{创建":1326853478,实时模式":假,"id": "evt_00000000000000","type": "invoice.upcoming",对象":事件",请求":空,"pending_webhooks": 1,"api_version": "2017-06-05",数据": {目的": {id":空,"object": "发票","amount_due": 0,application_fee":空,尝试计数":0,尝试":真的,收费":空,关闭":真的,"货币": "日元","客户": "cus_00000000000000",日期":1503541536,描述":空,折扣":空,结束平衡":0,原谅":假的,行":{数据": [{"id": "sub_BN5yNiTkAlQOye","object": "line_item",金额":500,"货币": "日元",描述":空,折扣":真的,实时模式":真,元数据":{},时期": {开始":1507604796,结束":1510283196},计划": {"id": "bplan",对象":计划",金额":500,创建":1504352393,"货币": "日元","间隔": "月",间隔计数":1,实时模式":假,元数据":{},"name": "B 计划",statement_descriptor":空,trial_period_days":空},按比例":假,数量":1,订阅":空,"subscription_item": "si_1B0LmKE9P3qCpf5erqbpMxkI",类型":订阅"}],"total_count": 1,对象":列表","url": "/v1/invoices/in_1AuB2KE9P3qCpf5ekFh7qpAi/lines"},实时模式":假,元数据":{},next_payment_attempt":空,付费":真实,期间结束":1503541536,期间开始":1503541536,receipt_number":空,起始平衡":0,statement_descriptor":空,"订阅": "sub_00000000000000",小计":0,税":空,tax_percent":空,总":0,webhooks_delivered_at":1503541537}}}
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.
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
}
}
}
这篇关于使用 Stripe Webhook 发送使用 Stripe 定期付款的客户详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!