簡體   English   中英

從Stripe中的invoice.payment_failed Webhook獲取訂閱ID

[英]Get subscription id from invoice.payment_failed webhook in 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行-始終開始預訂訂閱

與子,所以也許這是subscription_id?

"subscription"=>"sub_00000000000000" -第77行-也許這一個?

請幫助我在條紋文檔中找不到關於此的任何信息...

當您收到事件invoice.payment_failed ,可以在data哈希中的subscription密鑰中檢索相應的訂閱ID。

在您的示例中,它是"subscription"=>"sub_00000000000000"因為您使用的是“發送測試Webhook ...”,它發送假數據,而不是基於您自己的帳戶的數據。

如果要使用真實數據進行測試,最簡單的解決方案是為沒有卡的客戶創建訂閱。 您將使用該API並將trial_end設置為幾分鍾。 這樣,將成功收取第一筆0美元的發票,但幾分鍾后的下一張計划價格發票將按預期失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM