json数据条纹webhook

时间:2017-09-05 23:43:57

标签: ruby-on-rails json stripe-payments webhooks

我试图在我的rails应用程序中实现条带webhooks。 webhook发送此json数据。

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

webhook方法

 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 [' lines'] [' id']

似乎无法正常工作我做错了什么?我确定它只是一种语法,我不理解。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

计算出数据在哈希中的行数,因此你必须迭代它。这就是我为遇到这种情况的人所做的。

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