Как назначить дочернее значение объекта переменной? - PullRequest
0 голосов
/ 14 марта 2019

Я создаю нового клиента и подписку, используя Stripe. Когда объекты клиента и подписки возвращаются, они выглядят так:

Stripe\Customer Object
(
[id] => cus_EhMbQTp34KxvmO
[object] => customer
[account_balance] => 0
[created] => 1552586480
[currency] => 
[default_source] => card_1EDxrkKBfYBcUnN2WtT65hA3
[delinquent] => 
[description] => 
[discount] => 
[email] => alanpollenz@hotmail.com
[invoice_prefix] => FFD0DC3
[invoice_settings] => Stripe\StripeObject Object
    (
        [custom_fields] => 
        [footer] => 
    )

[livemode] => 
[metadata] => Stripe\StripeObject Object
    (
    )

[shipping] => 
[sources] => Stripe\Collection Object
    (
        [object] => list
        [data] => Array
            (
                [0] => Stripe\Card Object
                    (
                        [id] => card_1EDxrkKBfYBcUnN2WtT65hA3
                        [object] => card
                        [address_city] => 
                        [address_country] => 
                        [address_line1] => 
                        [address_line1_check] => 
                        [address_line2] => 
                        [address_state] => 
                        [address_zip] => 
                        [address_zip_check] => 
                        [brand] => Visa
                        [country] => US
                        [customer] => cus_EhMbQTp34KxvmO
                        [cvc_check] => pass
                        [dynamic_last4] => 
                        [exp_month] => 11
                        [exp_year] => 2021
                        [fingerprint] => CNlYQ7G0V42HfZwK
                        [funding] => credit
                        [last4] => 4242
                        [metadata] => Stripe\StripeObject Object
                            (
                            )

                        [name] => alanpollenz@hotmail.com
                        [tokenization_method] => 
                    )

            )

        [has_more] => 
        [total_count] => 1
        [url] => /v1/customers/cus_EhMbQTp34KxvmO/sources
    )

[subscriptions] => Stripe\Collection Object
    (
        [object] => list
        [data] => Array
            (
            )

        [has_more] => 
        [total_count] => 0
        [url] => /v1/customers/cus_EhMbQTp34KxvmO/subscriptions
    )

[tax_info] => 
[tax_info_verification] => 
)


Stripe\Subscription Object
(
[id] => sub_EhMbwAwXReRUo9
[object] => subscription
[application_fee_percent] => 
[billing] => charge_automatically
[billing_cycle_anchor] => 1553882480
[billing_thresholds] => 
[cancel_at] => 
[cancel_at_period_end] => 
[canceled_at] => 
[created] => 1552586480
[current_period_end] => 1553882480
[current_period_start] => 1552586480
[customer] => cus_EhMbQTp34KxvmO
[days_until_due] => 
[default_source] => 
[discount] => 
[ended_at] => 
[items] => Stripe\Collection Object
    (
        [object] => list
        [data] => Array
            (
                [0] => Stripe\SubscriptionItem Object
                    (
                        [id] => si_EhMbNyhkOmti8x
                        [object] => subscription_item
                        [billing_thresholds] => 
                        [created] => 1552586481
                        [metadata] => Stripe\StripeObject Object
                            (
                            )

                        [plan] => Stripe\Plan Object
                            (
                                [id] => 00030
                                [object] => plan
                                [active] => 1
                                [aggregate_usage] => 
                                [amount] => 999
                                [billing_scheme] => per_unit
                                [created] => 1552107233
                                [currency] => usd
                                [interval] => month
                                [interval_count] => 1
                                [livemode] => 
                                [metadata] => Stripe\StripeObject Object
                                    (
                                    )

                                [nickname] => Monthly
                                [product] => prod_EfHkC7lwkSS1l3
                                [tiers] => 
                                [tiers_mode] => 
                                [transform_usage] => 
                                [trial_period_days] => 15
                                [usage_type] => licensed
                            )

                        [quantity] => 1
                        [subscription] => sub_EhMbwAwXReRUo9
                    )

            )

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

[latest_invoice] => in_1EDxroKBfYBcUnN2AkQjePZX
[livemode] => 
[metadata] => Stripe\StripeObject Object
    (
    )

[plan] => Stripe\Plan Object
    (
        [id] => 00030
        [object] => plan
        [active] => 1
        [aggregate_usage] => 
        [amount] => 999
        [billing_scheme] => per_unit
        [created] => 1552107233
        [currency] => usd
        [interval] => month
        [interval_count] => 1
        [livemode] => 
        [metadata] => Stripe\StripeObject Object
            (
            )

        [nickname] => Monthly
        [product] => prod_EfHkC7lwkSS1l3
        [tiers] => 
        [tiers_mode] => 
        [transform_usage] => 
        [trial_period_days] => 15
        [usage_type] => licensed
    )

[quantity] => 1
[schedule] => 
[start] => 1552586480
[status] => trialing
[tax_percent] => 
[trial_end] => 1553882480
[trial_start] => 1552586480
)

Я новичок с oop, но мне удалось установить некоторые переменные для вставки в таблицу mySQL, такие как

$customerID = $customer->id;

Но как бы получить значение псевдонима подписки (примерно на 15 строк снизу вверх). Я пробовал разные итерации, например:

$nickname = $subscription->plan->nickname;

но, похоже, ничего не работает.

Есть идеи?

Спасибо.

Alan

Редактировать на основе комментариев Патрика:

var_dump($subscription->plan->nickname);

выходы: * * один тысяча двадцать-одна

string(7) "Monthly

и затем следующее:

$output .= "Subscription Plan ID = $subscription->id<br />";
$output .= "Subscription Plan Nickname = $subscription->plan->nickname<br />";
echo $output;

выходы:

Subscription Plan ID = sub_EhN9GgJfiFYmsV Subscription Plan

Nickname = Stripe\Plan JSON: { "id": "00030", "object": "plan", "active": true, "aggregate_usage": null, "amount": 999, "billing_scheme": "per_unit", "created": 1552107233, "currency": "usd", "interval": "month", "interval_count": 1, "livemode": false, "metadata": [], "nickname": "Monthly", "product": "prod_EfHkC7lwkSS1l3", "tiers": null, "tiers_mode": null, "transform_usage": null, "trial_period_days": 15, "usage_type": "licensed" }->nickname

Ответы [ 2 ]

0 голосов
/ 14 марта 2019

Поскольку вы пытаетесь получить доступ к вложенным массивам, вам нужно либо сначала присвоить требуемое значение переменной, а затем вставить его в строку, либо вам нужно заключить бит доступа к объекту в фигурные скобки.

Так что вы можете сделать:

$planNickname = $subscription->plan->nickname;
$output .= "Subscription Plan ID = $subscription->id<br />";
$output .= "Subscription Plan Nickname = $planNickname<br />";
echo $output;

Или вы можете сделать:

$output .= "Subscription Plan ID = $subscription->id<br />";
$output .= "Subscription Plan Nickname = {$subscription->plan->nickname}<br />";
echo $output;
0 голосов
/ 14 марта 2019

Благодаря Патрику я смог придумать следующее, что, кажется, работает:

$nickname = print_r($subscription->plan->nickname, true);
$output .= "Subscription Plan Nickname = $nickname<br />";
echo $output;

Результат:

Subscription Plan Nickname = Monthly
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...