我正在解析事件webhooks,对于invoice.payment_succeeded
事件,我需要获取Invoice
对象,Subscription
对象和Plan
对象:>
if (event.getType().equals("invoice.payment_succeeded")) {
Invoice invoice = (Invoice) event.getData().getObject();
Subscription subscription = invoice.getSubscriptionObject();
Plan plan = subscription.getPlan();
问题是它获取的Subscription
对象为空,所以我无法获得计划,但是在webhook json中,发票显然不为空:
<com.stripe.model.Invoice@1862723626 id=in_1EAqJIJIKy5oOMbPbbnFDfhe> JSON: {
"id": "in_1EAqJIJIKy5oOMbPbbnFDfhe",
"object": "invoice",
"amount_due": 500,
"amount_paid": 500,
"amount_remaining": 0,
"application_fee": null,
"attempt_count": 1,
"attempted": true,
"auto_advance": false,
"billing": "charge_automatically",
"billing_reason": "subscription_create",
"charge": "ch_1EAqJIJIKy5oOMbPzJWZRmkB",
"created": 1551842448,
"currency": "usd",
"custom_fields": null,
"customer": "cus_EdkAtTWX48vPlL",
"date": 1551842448,
"default_source": null,
"deleted": null,
"description": null,
"discount": null,
"due_date": null,
"ending_balance": 0,
"finalized_at": 1551842448,
"footer": null,
"hosted_invoice_url": "https://pay.stripe.com/invoice/invst_Xk5QCVu0wZGe8yDKRjNku8do3v",
"invoice_pdf": "https://pay.stripe.com/invoice/invst_Xk5QCVu0wZGe8yDKRjNku8do3v/pdf",
"lines": {
"object": "list",
"data": [
{
"id": "sli_b7542154e3401d",
"object": "line_item",
"amount": 500,
"currency": "usd",
"description": "1 ? Service (at $5.00 / month)",
"discountable": true,
"livemode": false,
"metadata": {},
"period": {
"end": 1554520848,
"start": 1551842448
},
"plan": {
"id": "plan_EYQ3ShMp3aS5RZ",
"object": "plan",
"active": true,
"amount": 500,
"billing_scheme": "per_unit",
"created": 1550523572,
"currency": "usd",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": "535281012935229482:540347090178670611",
"product": "prod_EYQ2LpVmAUc2Kh",
"tiers": null,
"tiers_mode": null,
"transform_usage": null,
"usage_type": "licensed",
"deleted": null,
"name": null,
"statement_description": null,
"statement_descriptor": null,
"trial_period_days": null
},
"proration": false,
"quantity": 1,
"subscription": "sub_Ee8abQjuYiFJXx",
"subscription_item": "si_Ee8aKEipOAG8hQ",
"type": "subscription"
}
],
"has_more": false,
"total_count": 1,
"url": "/v1/invoices/in_1EAqJIJIKy5oOMbPbbnFDfhe/lines",
"count": null,
"request_options": null,
"request_params": null
},
"livemode": false,
"metadata
答案 0 :(得分:0)
它在文档中指出,数据仅包含您的Webhook的relevant object。
在发票的情况下,它仅是发票对象。幸运的是,其中包含您的subscription
ID:
"subscription": "sub_Ee8abQjuYiFJXx",
您可以将此ID用作
的retrieve the dataSubscription subscription = Subscription.retrieve("sub_Ee8abQjuYiFJXx");
答案 1 :(得分:0)
首先,subscription
属性的值可以为null,例如在one-off invoices的情况下。
默认情况下,整个Subscription
对象不包括在Invoice
对象中,仅包括其标识符。 Invoice
对象中包含的Event
对象遵循相同的规则。因此,您将需要检索Subscription
及其标识符,如here
if (event.getType().equals("invoice.payment_succeeded")) {
Invoice invoice = (Invoice) event.getData().getObject();
Subscription subscription = Subscription.retrieve(invoice.getSubscription());
}
仅当预订包含单个计划时,才会设置plan
属性。
请注意,检索对象时可以“扩展” Stripe对象。请参阅Stripe API参考的Expanding Objects部分。