2017-09-05 63 views
0

我試圖在我的rails應用程序中實現條紋webhooks。 webhook發送這個json數據。json data stripe webhook

{ "created": 1326853478, "livemode": false, "id": "evt_00000000000000", "type": "invoice.payment_succeeded", "object": "event", "request": null, "pending_webhooks": 1, "api_version": "2017-06-05", "data": { 
    "object": { 
     "id": "in_00000000000000", 
     "object": "invoice", 
     "amount_due": 500, 
     "application_fee": null, 
     "attempt_count": 1, 
     "attempted": true, 
     "charge": "_00000000000000", 
     "closed": true, 
     "currency": "usd", 
     "customer": "cus_00000000000000", 
     "date": 1501950408, 
     "description": null, 
     "discount": null, 
     "ending_balance": 0, 
     "forgiven": false, 
     "lines": { 
     "data": [ 
      { 
      "id": "sub_BLXcTSekdHflq5", 
      "object": "line_item", 
      "amount": 500, 
      "currency": "usd", 
      "description": null, 
      "discountable": true, 
      "livemode": true, 
      "metadata": { 
      }, 
      "period": { 
       "start": 1507246341, 
       "end": 1509924741 
      }, 
      "plan": { 
       "id": "Subscriber", 
       "object": "plan", 
       "amount": 500, 
       "created": 1501901993, 
       "currency": "usd", 
       "interval": "month", 
       "interval_count": 1, 
       "livemode": false, 
       "metadata": { 
       }, 
       "name": "Subscriber", 
       "statement_descriptor": "WatchBuddie Stream sub", 
       "trial_period_days": null 
      }, 
      "proration": false, 
      "quantity": 1, 
      "subscription": null, 
      "subscription_item": "si_1AyqWnFr5iCt1Tv7n23zDLOM", 
      "type": "subscription" 
      } 
     ], 
     "total_count": 1, 
     "object": "list", 
     "url": "/v1/invoices/in_1AnV6yFr5iCt1Tv7PnqZ0EUA/lines" 
     }, 
     "livemode": false, 
     "metadata": { 
     }, 
     "next_payment_attempt": null, 
     "paid": true, 
     "period_end": 1501950408, 
     "period_start": 1501950408, 
     "receipt_number": null, 
     "starting_balance": 0, 
     "statement_descriptor": null, 
     "subscription": "sub_00000000000000", 
     "subtotal": 500, 
     "tax": null, 
     "tax_percent": null, 
     "total": 500, 
     "webhooks_delivered_at": 1501950409 
    } } } 

網絡掛接方法

def webhooks 
    begin 
     event_json = JSON.parse(request.body.read) 
     event_object = event_json['data']['object'] 
     #refer event types here https://stripe.com/docs/api#event_types 
     case event_json['type'] 
     when 'invoice.payment_succeeded' 
      #Update the total subscription total 
      #Send in email to the user telling them that they resubbed 

      logger.debug event_object['lines']['id'] 
    end 

我的問題是如何得到的 「ID」: 「sub_BLXcTSekdHflq5」 我試圖

logger.debug event_object [ '行'] ['id']

它似乎不是我我在做什麼錯了?我確定它只是一個我不瞭解的語法。

感謝您的幫助!

回答

0

把它的數據看成一個散列,所以你必須迭代它。這是我爲遇到此問題的任何人所做的。

event_object['lines']['data'].each{ |i| 
    sub_id = i['id'] 
    logger.debug sub_id 
}