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

时间:2015-03-13 10:56:11

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

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

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

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

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

使用sub也许这是subscription_id?

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

请帮助我在条纹文档中找不到任何相关信息...

1 个答案:

答案 0 :(得分:4)

当您收到事件invoice.payment_failed时,您可以在subscription哈希中的data密钥中检索相应的订阅ID。

在您的示例中,它是"subscription"=>"sub_00000000000000",因为您正在使用"发送测试Webhook ..."它会根据您自己的帐户发送虚假数据而非数据。

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