用PHP获取Stripe订阅状态遇异常:返回对象不符预期求排查
问题:Stripe订阅查询返回客户端对象而非订阅详情
我已成功创建Stripe结账会话与Webhook,且已存储$SubscriptionID,想要查询订阅状态。使用以下代码执行查询:
$stripe = new \Stripe\StripeClient($secretKey); $stripe->subscriptions->retrieve($SubscriptionID, []);
但返回的是Stripe客户端递归对象,而非预期的订阅详情:
Stripe\StripeClient Object ( [coreServiceFactory:Stripe\StripeClient:private] => Stripe\Service\CoreServiceFactory Object ( [client:Stripe\Service\AbstractServiceFactory:private] => Stripe\StripeClient Object *RECURSION* [services:Stripe\Service\AbstractServiceFactory:private] => Array ( [subscriptions] => Stripe\Service\SubscriptionService Object ( [client:protected] => Stripe\StripeClient Object *RECURSION* [streamingClient:protected] => Stripe\StripeClient Object *RECURSION* ) ) ) [config:Stripe\BaseStripeClient:private] => Array ( [api_key] => (key) [client_id] => [stripe_account] => [stripe_version] => 2023-08-16 [api_base] => https://api.stripe.com [connect_base] => https://connect.stripe.com [files_base] => https://files.stripe.com ) [defaultOpts:Stripe\BaseStripeClient:private] => Stripe\Util\RequestOptions Object ( [apiKey] => [headers] => Array ( [Stripe-Account] => [Stripe-Version] => 2023-08-16 ) [apiBase] => ) [preview] => Stripe\Preview Object ( [client:protected] => Stripe\StripeClient Object *RECURSION* ) )
预期应返回包含status字段的订阅对象,示例如下:
{ "id": "sub_1MowQVLkdIwHu7ixeRlqHVzs", "object": "subscription", "application": null, "application_fee_percent": null, "automatic_tax": { "enabled": false }, "billing_cycle_anchor": 1679609767, "billing_thresholds": null, "cancel_at": null, "cancel_at_period_end": false, "canceled_at": null, "cancellation_details": { "comment": null, "feedback": null, "reason": null }, "collection_method": "charge_automatically", "created": 1679609767, "currency": "usd", "current_period_end": 1682288167, "current_period_start": 1679609767, "customer": "cus_Na6dX7aXxi11N4", "days_until_due": null, "default_payment_method": null, "default_source": null, "default_tax_rates": [], "description": null, "discount": null, "ended_at": null, "items": { "object": "list", "data": [ { "id": "si_Na6dzxczY5fwHx", "object": "subscription_item", "billing_thresholds": null, "created": 1679609768, "metadata": {}, "plan": { "id": "price_1MowQULkdIwHu7ixraBm864M", "object": "plan", "active": true, "aggregate_usage": null, "amount": 1000, "amount_decimal": "1000", "billing_scheme": "per_unit", "created": 1679609766, "currency": "usd", "interval": "month", "interval_count": 1, "livemode": false, "metadata": {}, "nickname": null, "product": "prod_Na6dGcTsmU0I4R", "tiers_mode": null, "transform_usage": null, "trial_period_days": null, "usage_type": "licensed" }, "price": { "id": "price_1MowQULkdIwHu7ixraBm864M", "object": "price", "active": true, "billing_scheme": "per_unit", "created": 1679609766, "currency": "usd", "custom_unit_amount": null, "livemode": false, "lookup_key": null, "metadata": {}, "nickname": null, "product": "prod_Na6dGcTsmU0I4R", "recurring": { "aggregate_usage": null, "interval": "month", "interval_count": 1, "trial_period_days": null, "usage_type": "licensed" }, "tax_behavior": "unspecified", "tiers_mode": null, "transform_quantity": null, "type": "recurring", "unit_amount": 1000, "unit_amount_decimal": "1000" }, "quantity": 1, "subscription": "sub_1MowQVLkdIwHu7ixeRlqHVzs", "tax_rates": [] } ], "has_more": false, "total_count": 1, "url": "/v1/subscription_items?subscription=sub_1MowQVLkdIwHu7ixeRlqHVzs" }, "latest_invoice": "in_1MowQWLkdIwHu7ixuzkSPfKd", "livemode": false, "metadata": {}, "next_pending_invoice_item_invoice": null, "on_behalf_of": null, "pause_collection": null, "payment_settings": { "payment_method_options": null, "payment_method_types": null, "save_default_payment_method": "off" }, "pending_invoice_item_interval": null, "pending_setup_intent": null, "pending_update": null, "quantity": 1, "schedule": null, "start_date": 1679609767, "status": "active", "test_clock": null, "transfer_data": null, "trial_end": null, "trial_settings": { "end_behavior": { "missing_payment_method": "create_invoice" } }, "trial_start": null }
问题原因与解决方法
你没有接收并使用retrieve()方法返回的订阅对象,而是直接输出了Stripe客户端实例(或未对返回值做处理,导致输出客户端本身)。
修正代码:将retrieve()的返回值赋值给变量,再操作该变量:
$stripe = new \Stripe\StripeClient($secretKey); // 将返回的订阅对象赋值给变量 $subscription = $stripe->subscriptions->retrieve($SubscriptionID, []); // 获取订阅状态 $status = $subscription->status; // 或输出完整订阅对象 print_r($subscription);
补充说明
Stripe PHP SDK的retrieve()方法会返回对应的订阅对象实例,而非修改客户端本身。直接输出$stripe变量只会得到客户端的递归结构,只有接收返回值才能拿到包含status等字段的订阅详情。
内容的提问来源于stack exchange,提问作者Stephanie
相关产品推荐
相关产品推荐

