从 Stripe 中的 invoice.payment_failed webhook 获取订阅 ID

Get subscription id from invoice.payment_failed webhook in Stripe

在我的应用程序中,我有一些客户可以有多个订阅。当某些订阅付款失败时,我通过 stripe webhooks 将此参数发送到我的服务器:

{
  "created"=>1326853478,
  "livemode"=>false,
  "id"=>"evt_00000000000000",
  "type"=>"invoice.payment_failed",
  "object"=>"event",
  "request"=>nil,
  "pending_webhooks"=>1,
  "api_version"=>"2015-02-18",
  "data"=>{
    "object"=>{
      "date"=>1426238137,
      "id"=>"in_00000000000000",
      "period_start"=>1426232774,
      "period_end"=>1426238137,
      "lines"=>{
        "data"=>[
          {
            "id"=>"sub_5rZCR6ApQpG5fJ",
            "object"=>"line_item",
            "type"=>"subscription",
            "livemode"=>true,
            "amount"=>2500,
            "currency"=>"usd",
            "proration"=>false,
            "period"=>{
              "start"=>1428921062,
              "end"=>1431513062
            },
            "subscription"=>nil,
            "quantity"=>1,
            "plan"=>{
              "interval"=>"month",
              "name"=>"Monthly device subscription",
              "created"=>1426232489,
              "amount"=>2500,
              "currency"=>"usd",
              "id"=>"monthly-device",
              "object"=>"plan",
              "livemode"=>false,
              "interval_count"=>1,
              "trial_period_days"=>nil,
              "metadata"=>{

              },
              "statement_descriptor"=>nil
            },
            "description"=>nil,
            "metadata"=>{

            }
          }
        ],
        "total_count"=>1,
        "object"=>"list",
        "url"=>"/v1/invoices/in_15fotJEFda5OVrS251rpXd9l/lines"
      },
      "subtotal"=>2500,
      "total"=>2500,
      "customer"=>"cus_00000000000000",
      "object"=>"invoice",
      "attempted"=>true,
      "closed"=>false,
      "forgiven"=>false,
      "paid"=>false,
      "livemode"=>false,
      "attempt_count"=>1,
      "amount_due"=>2500,
      "currency"=>"usd",
      "starting_balance"=>0,
      "ending_balance"=>0,
      "next_payment_attempt"=>nil,
      "webhooks_delivered_at"=>1426238137,
      "charge"=>"ch_00000000000000",
      "discount"=>nil,
      "application_fee"=>nil,
      "subscription"=>"sub_00000000000000",
      "tax_percent"=>nil,
      "tax"=>nil,
      "metadata"=>{

      },
      "statement_descriptor"=>nil,
      "description"=>nil,
      "receipt_number"=>nil
    }
  },
  "webhook"=>{
    "created"=>1326853478,
    "livemode"=>false,
    "id"=>"evt_00000000000000",
    "type"=>"invoice.payment_failed",
    "object"=>"event",
    "request"=>nil,
    "pending_webhooks"=>1,
    "api_version"=>"2015-02-18",
    "data"=>{
      "object"=>{
        "date"=>1426238137,
        "id"=>"in_00000000000000",
        "period_start"=>1426232774,
        "period_end"=>1426238137,
        "lines"=>{
          "data"=>[
            {
              "id"=>"sub_5rZCR6ApQpG5fJ",
              "object"=>"line_item",
              "type"=>"subscription",
              "livemode"=>true,
              "amount"=>2500,
              "currency"=>"usd",
              "proration"=>false,
              "period"=>{
                "start"=>1428921062,
                "end"=>1431513062
              },
              "subscription"=>nil,
              "quantity"=>1,
              "plan"=>{
                "interval"=>"month",
                "name"=>"Monthly device subscription",
                "created"=>1426232489,
                "amount"=>2500,
                "currency"=>"usd",
                "id"=>"monthly-device",
                "object"=>"plan",
                "livemode"=>false,
                "interval_count"=>1,
                "trial_period_days"=>nil,
                "metadata"=>{

                },
                "statement_descriptor"=>nil
              },
              "description"=>nil,
              "metadata"=>{

              }
            }
          ],
          "total_count"=>1,
          "object"=>"list",
          "url"=>"/v1/invoices/in_15fotJEFda5OVrS251rpXd9l/lines"
        },
        "subtotal"=>2500,
        "total"=>2500,
        "customer"=>"cus_00000000000000",
        "object"=>"invoice",
        "attempted"=>true,
        "closed"=>false,
        "forgiven"=>false,
        "paid"=>false,
        "livemode"=>false,
        "attempt_count"=>1,
        "amount_due"=>2500,
        "currency"=>"usd",
        "starting_balance"=>0,
        "ending_balance"=>0,
        "next_payment_attempt"=>nil,
        "webhooks_delivered_at"=>1426238137,
        "charge"=>"ch_00000000000000",
        "discount"=>nil,
        "application_fee"=>nil,
        "subscription"=>"sub_00000000000000",
        "tax_percent"=>nil,
        "tax"=>nil,
        "metadata"=>{

        },
        "statement_descriptor"=>nil,
        "description"=>nil,
        "receipt_number"=>nil
      }
    }
  }
}

现在我想从这个参数条带中获取 subscription_id。但我不知道哪个是正确的,因为我们在这个参数中有以下数据:

"id"=>"sub_5rZCR6ApQpG5fJ" - 第 19 行 - 始终条带化订阅开始

with sub 所以也许这是 subscription_id?

"subscription"=>"sub_00000000000000" - 第 77 行 - 也许是这个?

请帮助我在 stripe 文档中找不到任何关于此的信息...

当您获得事件 invoice.payment_failed 时,您可以在 data 散列的 subscription 键中检索相应的订阅 ID。

在您的示例中,它是 "subscription"=>"sub_00000000000000",因为您使用的 "Send Test Webhook..." 发送虚假数据,而不是基于您自己帐户的数据。

如果您想使用真实数据进行测试,最简单的解决方案是为没有卡的客户创建订阅。您将使用 API 并将 trial_end 设置为未来几分钟。这样,第一个 0 美元的发票将成功收取,但几分钟后计划价格的下一个发票将按预期收取失败。