2017-09-09 246 views
0

我已在Java Spring MVC Web Application中配置Stripe付款。我可以添加一个Customer,创建一个Plan并为客户设置Subscriptions。由于我有经常性付款,我希望一旦生成发票和付款后发送电子邮件通知给客户。从条纹文件中,我需要的事件类型是invoice.upcoming.,invoice.payment_succeededcustomer.subscription.trial_will_end,因为我有少数计划的试用期。使用Stripe Webhook获取使用Stripe的定期付款的客户详细信息

我已经在我的应用程序增加了一个网络挂接端点:

@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中触发实际的数据。此外,我无法从条纹文档中找到示例数据:https://stripe.com/docs/api#invoice_object。我也试过Retrive stripe data from stripe webhook event,但没有一些样本数据就无法测试它。

有没有一种方法可以从事件中获取所需的详细信息,并且还可以在本地主机上测试这些信息。

回答

2

在您的Web应用程序开发过程中,为了检查发送到本地主机的webhook,您可以使用像ngrok这样的解决方案。

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

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

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

要检查条纹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 
     } 
    } 
    } 
+0

感谢您的答复事件data对象,但我仍然是如何可以从事件对象的客户ID。当我尝试event.getUserId()时,它显示不推荐使用Event类型的getUserId()方法。我现在需要的是事件类型和客户ID,以便我可以向该客户发送电子邮件 –

+1

根据我的答案中的以前的JSON样本,如果事件类型为“invoice.upcoming”,则可以分析JSON你从webhook获得的数据并读取'data.object.customer'属性来获取客户ID。它会对你的用例起作用吗? –

+0

StripeObject stripeObject = event.getData()。getObject();会给对象。我仍然没有看到从这个对象中获取客户的方法 –

1

所述data对象包含customer ID作为string

invoice.upcoming对于和invoice.payment_succeeded中接收作为string顾客ID对象。

继JSON包含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 
}