从订阅对象中检索计划 ID

Retrieving the plan Id from the subscription object

我有一个通过结帐会话创建的条带订阅对象。这是我在创建会话时指定的内容:

            'mode' => 'subscription',
            'line_items' => [[
                'price' => $planId,
                'quantity' => 1,
            ]],

我的问题是,由于数量 = 1,我的情况下是否始终具有以下状态?

$subscription_obj->items->data['0']->plan->id== $subscription_obj->plan->id

因为我的代码每当我需要计划 ID 时,我有时使用左侧表达式,有时使用右侧表达式。所以我只想确保这些在我的情况下是相同的并且不会有任何: $subscription_obj->items->data['1']->plan->id ...

Here's my subscription object for a plan of 12 euros/month:
[id] => sub_J75FZOObndggWX
    [object] => subscription
    [application_fee_percent] => 
    [billing_cycle_anchor] => 1615717329
    [billing_thresholds] => 
    [cancel_at] => 1620984129
    [cancel_at_period_end] => 
    [canceled_at] => 1615717335
    [collection_method] => charge_automatically
    [created] => 1615717329
    [current_period_end] => 1618395729
    [current_period_start] => 1615717329
    [customer] => cus_J75Fs7RnQtMBTX
    [days_until_due] => 
    [default_payment_method] => pm_1IUr5IHEWYmoXN4GjeSdn3aK
    [default_source] => 
    [default_tax_rates] => Array
        (
        )

    [discount] => 
    [ended_at] => 
    [items] => Stripe\Collection Object
        (
            [object] => list
            [data] => Array
                (
                    [0] => Stripe\SubscriptionItem Object
                        (
                            [id] => si_J75F5QEL5Gnzqf
                            [object] => subscription_item
                            [billing_thresholds] => 
                            [created] => 1615717329
                            [metadata] => Stripe\StripeObject Object
                                (
                                )

                            [plan] => Stripe\Plan Object
                                (
                                    [id] => price_1IJes9HEWYmoXN4GiFeNywqa
                                    [object] => plan
                                    [active] => 1
                                    [aggregate_usage] => 
                                    [amount] => 1200
                                    [amount_decimal] => 1200
                                    [billing_scheme] => per_unit
                                    [created] => 1613048777
                                    [currency] => eur
                                    [interval] => month
                                    [interval_count] => 1
                                    [livemode] => 
                                    [metadata] => Stripe\StripeObject Object
                                        (
                                        )

                                    [nickname] => 
                                    [product] => prod_IvVt5EemrpxKcf
                                    [tiers_mode] => 
                                    [transform_usage] => 
                                    [trial_period_days] => 
                                    [usage_type] => licensed
                                )

                            [price] => Stripe\Price Object
                                (
                                    [id] => price_1IJes9HEWYmoXN4GiFeNywqa
                                    [object] => price
                                    [active] => 1
                                    [billing_scheme] => per_unit
                                    [created] => 1613048777
                                    [currency] => eur
                                    [livemode] => 
                                    [lookup_key] => 
                                    [metadata] => Stripe\StripeObject Object
                                        (
                                        )

                                    [nickname] => 
                                    [product] => prod_IvVt5EemrpxKcf
                                    [recurring] => Stripe\StripeObject Object
                                        (
                                            [aggregate_usage] => 
                                            [interval] => month
                                            [interval_count] => 1
                                            [trial_period_days] => 
                                            [usage_type] => licensed
                                        )

                                    [tiers_mode] => 
                                    [transform_quantity] => 
                                    [type] => recurring
                                    [unit_amount] => 1200
                                    [unit_amount_decimal] => 1200
                                )

                            [quantity] => 1
                            [subscription] => sub_J75FZOObndggWX
                            [tax_rates] => Array
                                (
                                )

                        )

                )

            [has_more] => 
            [total_count] => 1
            [url] => /v1/subscription_items?subscription=sub_J75FZOObndggWX
        )

    [latest_invoice] => in_1IUr5JHEWYmoXN4GTmJ3pJUZ
    [livemode] => 
    [metadata] => Stripe\StripeObject Object
        (
            [myID] => 1
        )

    [next_pending_invoice_item_invoice] => 
    [pause_collection] => 
    [pending_invoice_item_interval] => 
    [pending_setup_intent] => 
    [pending_update] => 
    [plan] => Stripe\Plan Object
        (
            [id] => price_1IJes9HEWYmoXN4GiFeNywqa
            [object] => plan
            [active] => 1
            [aggregate_usage] => 
            [amount] => 1200
            [amount_decimal] => 1200
            [billing_scheme] => per_unit
            [created] => 1613048777
            [currency] => eur
            [interval] => month
            [interval_count] => 1
            [livemode] => 
            [metadata] => Stripe\StripeObject Object
                (
                )

            [nickname] => 
            [product] => prod_IvVt5EemrpxKcf
            [tiers_mode] => 
            [transform_usage] => 
            [trial_period_days] => 
            [usage_type] => licensed
        )

    [quantity] => 1
    [schedule] => 
    [start_date] => 1615717329
    [status] => active
    [transfer_data] => 
    [trial_end] => 
    [trial_start] => 
)
```

    

是的,你是对的。您还可以添加一个保护子句来检查 data 的大小是否为 1,如果这是您一直期望的那样。 quantity 对您的情况没有帮助,它指的是您的 Customer 订阅的特定 Plan 的数量,并不意味着 data 的大小.