如何从Stripe的invoice.payment_succeeded webhook事件中检索Rails中的计划ID?
我已尝试过以下代码,但继续获取计划是否为错误。
plan_id = event.data.object.lines.data[1].plan.id
这是web hook事件。
{
"id"=>"evt_111111111",
"created"=>1207343135,
"livemode"=>true,
"type"=>"invoice.payment_succeeded",
"data"=>{
"object"=>{
"date"=>2437418246,
"id"=>"in_......",
"period_start"=>1406044997,
"period_end"=>1437238559,
"lines"=>{
"object"=>"list",
"total_count"=>1,
"has_more"=>false,
"url"=>"/v1/invoices/in_...../lines",
"data"=>[
{
"id"=>"sub_.....",
"object"=>"line_item",
"type"=>"subscription",
"livemode"=>true,
"amount"=>29900,
"currency"=>"cad",
"proration"=>false,
"period"=>{
"start"=>2437418246,
"end"=>2437418246
},
"quantity"=>1,
"plan"=>{
"interval"=>"month",
"name"=>"Business",
"created"=>2437446,
"amount"=>19900,
"currency"=>"cad",
"id"=>"P3",
"object"=>"plan",
"livemode"=>true,
"interval_count"=>1,
"trial_period_days"=>7,
"metadata"=>{},
"statement_description"=>nil
},
"description"=>nil,
"metadata"=>{}
}
],
"count"=>1
},
"subtotal"=>19900,
"total"=>19900,
"customer"=>"cus_.....",
"object"=>"invoice",
"attempted"=>true,
"closed"=>true,
"forgiven"=>false,
"paid"=>true,
"livemode"=>true,
"attempt_count"=>1,
"amount_due"=>19900,
"currency"=>"cad",
"starting_balance"=>0,
"ending_balance"=>0,
"next_payment_attempt"=>nil,
"webhooks_delivered_at"=>14022ert61,
"charge"=>"ch_......",
"discount"=>nil,
"application_fee"=>nil,
"subscription"=>"sub_......",
"metadata"=>{},
"statement_description"=>nil,
"description"=>nil
}
},
"object"=>"event",
"pending_webhooks"=>1,
"request"=>nil,
"webhook"=>{
"id"=>"evt_....."
}
}
答案 0 :(得分:0)
您似乎在data
列表中只有一个项目。
请注意,Ruby中的数组从索引0
开始。
我相信您需要将data[1]
更改为data[0]
。
注意,如果data
列表可能为空,这仍然会中断。