2017-09-09 249 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 
}