【问题标题】:json data stripe webhookjson 数据条带 webhook
【发布时间】:2018-02-14 07:51:34
【问题描述】:

我正在尝试在我的 rails 应用程序中实现条带 webhook。 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']

它似乎没有工作我做错了什么?我确定这只是我不理解的语法。

感谢大家的帮助!

【问题讨论】:

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


    【解决方案1】:

    发现行数据在散列中,因此您必须对其进行迭代。这就是我为遇到这种情况的任何人所做的。

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

    【讨论】:

      猜你喜欢
      • 2017-01-13
      • 2018-11-29
      • 2018-11-18
      • 2018-02-10
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 2017-06-08
      相关资源
      最近更新 更多