简体   繁体   English

如何检索 Stripe 订阅的状态

[英]How to retrieve the status of a Stripe Subscription

I am trying to get the status of customers subscriptions using the Stripe API我正在尝试使用 Stripe API 获取客户订阅的状态

for example:例如:

print(subscriptionStatusFunctionAPI(subscriptionID))  
  
*"returns subscription status (e.g. active, past_due, canceled, unpaid, etc)"*

below is the current pseudocode下面是当前的伪代码

import stripe
stripe.api_key = 'rk_test_XXX' 

### retrieve and return customer specific Subscription object ###

retrieve_sub = stripe.Subscription.retrieve(
  "sub_1M3iv0LwptWcL8DfHusz7LZ1",
)

print(dir(retrieve_sub))

['OBJECT_NAME', 'ReprJSONEncoder', '__ class__', '__ contains__', '__ copy__', '__ deepcopy__', '__ delattr__', '__ delitem__', '__ dict__', '__ dir__', '__ doc__', '__ eq__', '__ format__', '__ ge__', '__ getattr__', '__ getattribute__', '__ getitem__', '__ gt__', '__ hash__', '__ init__', '__ init_subclass__', '__ iter__', '__ le__', '__ len__', '__ lt__', '__ module__', '__ ne__', '__ new__', '__ reduce__', '__ reduce_ex__', '__ repr__', '__ reversed__', '__ setattr__', '__ setitem__', '__ setstate__', '__ sizeof__', '__ str__', '__ subclasshook__', '__ weakref__', '_cls_cancel', '_cls_delete', '_cls_delete_discount', '_last_response', '_previous', '_request', '_request_and_refresh', '_retrieve_params', '_search', '_static_request', '_static_request_stream', '_transient_values', '_unsaved_values', 'api_base', 'api_key', 'auto_paging_iter', 'cancel', 'class_url', 'clear', 'construct_from', 'copy', 'create', 'delete', 'delete_discount', 'fromkeys', 'get', 'instance_url', 'items', 'key ['OBJECT_NAME', 'ReprJSONEncoder', '__ class__', '__ contains__', '__ copy__', '__ deepcopy__', '__ delattr__', '__ delitem__', '__ dict__', '__ dir__', ' __ doc__', '__ eq__', '__ format__', '__ ge__', '__ getattr__', '__ getattribute__', '__ getitem__', '__ gt__', '__ hash__', '__ init__', ' __ init_subclass__', '__ iter__', '__ le__', '__ len__', '__ lt__', '__ module__', '__ ne__', '__ new__', '__ reduce__', '__ reduce_ex__', ' __ repr__', '__ reversed__', '__ setattr__', '__ setitem__', '__ setstate__', '__ sizeof__', '__ str__', '__ subclasshook__', '__ weakref__', '_cls_cancel', '_cls_delete ', '_cls_delete_discount', '_last_response', '_previous', '_request', '_request_and_refresh', '_retrieve_params', '_search', '_static_request', '_static_request_stream', '_transient_values', '_unsaved_values', 'api_base', 'api_key', 'auto_paging_iter', 'cancel', 'class_url', 'clear', 'construct_from', 'copy', 'create', 'delete', 'delete_discount', 'fromkeys', 'get', 'instance_url ', '项目', '关键s', 'last_response', 'list', 'modify', 'pop', 'popitem', 'refresh', 'refresh_from', 'request', 'request_stream', 'retrieve', 'save', 'search', 'search_auto_paging_iter', 'serialize', 'setdefault', 'stripe_account', 'stripe_id', 'stripe_version', 'to_dict', 'to_dict_recursive', 'update', 'values'] s', 'last_response', 'list', 'modify', 'pop', 'popitem', 'refresh', 'refresh_from', 'request', 'request_stream', 'retrieve', 'save', 'search' , 'search_auto_paging_iter', 'serialize', 'setdefault', 'stripe_account', 'stripe_id', 'stripe_version', 'to_dict', 'to_dict_recursive', 'update', 'values']

retrieve_sub
<Subscription subscription id=sub_1M3je8LwptWcL8DfJCObejxI at 0x25183cb5c70> JSON: {
  
"application": null,  
  "application_fee_percent": null,  
  "automatic_tax": {  
    "enabled": false  
  },  
  "billing": "charge_automatically",  
  "billing_cycle_anchor": 1672333500,  
  "billing_thresholds": null,  
  "cancel_at": null,  
  "cancel_at_period_end": false,  
  "canceled_at": null,  
  "collection_method": "charge_automatically",  
  "created": 1672333500,  
  "currency": "usd",  
  "current_period_end": 1675011900,  
  "current_period_start": 1672333500,  
  "customer": "cus_MnKIJmGPujdOVd",  
  "days_until_due": null,  
  "default_payment_method": null,  
  "default_source": null,  
  "description": null,  
  "discount": null,  
  "ended_at": null,  
  "id": "sub_1M3je8LwptWcL8DfJCObejxI",  
  "invoice_customer_balance_settings": {  
    "consume_applied_balance_on_void": true  
  },  
  "items": {  
    "data": [  
      {  
        "billing_thresholds": null,  
        "created": 1672333500,  
        "id": "si_MnKIat6U3EN0O9",  
        "metadata": {},  
        "object": "subscription_item",  
        "plan": {  
          "active": true,  
          "aggregate_usage": null,
          "amount": 10000,
          "amount_decimal": "10000",
          "billing_scheme": "per_unit",
          "created": 1668352687,
          "currency": "usd",
          "id": "price_1M3hwhLwptWcL8DfUOZfUBnH",
          "interval": "month",
          "interval_count": 1,
          "livemode": false,
          "metadata": {},
          "name": "Test Sub",
          "nickname": null,
          "object": "plan",
          "product": "prod_MnIXRF3ji6h4Nq",
          "statement_descriptor": null,
          "tiers": null,
          "tiers_mode": null,
          "transform_usage": null,
          "trial_period_days": null,
          "usage_type": "licensed"
        },
        "price": {
          "active": true,
          "billing_scheme": "per_unit",
          "created": 1668352687,
          "currency": "usd",
          "custom_unit_amount": null,
          "id": "price_1M3hwhLwptWcL8DfUOZfUBnH",
          "livemode": false,
          "lookup_key": null,
          "metadata": {},
          "nickname": null,
          "object": "price",
          "product": "prod_MnIXRF3ji6h4Nq",
          "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": 10000,
          "unit_amount_decimal": "10000"
        },
        "quantity": 1,
        "subscription": "sub_1M3je8LwptWcL8DfJCObejxI",
        "tax_rates": []
      }
    ],
    "has_more": false,
    "object": "list",
    "total_count": 1,
    "url": "/v1/subscription_items?subscription=sub_1M3je8LwptWcL8DfJCObejxI"
  },
  "latest_invoice": "in_1M3je8LwptWcL8DfNbeYfB6C",
  "livemode": false,
  "metadata": {},
  "next_pending_invoice_item_invoice": null,
  "object": "subscription",
  "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,
  "plan": {
    "active": true,
    "aggregate_usage": null,
    "amount": 10000,
    "amount_decimal": "10000",
    "billing_scheme": "per_unit",
    "created": 1668352687,
    "currency": "usd",
    "id": "price_1M3hwhLwptWcL8DfUOZfUBnH",
    "interval": "month",
    "interval_count": 1,
    "livemode": false,
    "metadata": {},
    "name": "Test Sub",
    "nickname": null,
    "object": "plan",
    "product": "prod_MnIXRF3ji6h4Nq",
    "statement_descriptor": null,
    "tiers": null,
    "tiers_mode": null,
    "transform_usage": null,
    "trial_period_days": null,
    "usage_type": "licensed"
  },
  "quantity": 1,
  "schedule": null,
  "start": 1672333500,
  "start_date": 1672333500,
  "status": "active",
  "tax_percent": null,
  "test_clock": "clock_1M3jckLwptWcL8Dff89Wpff4",
  "transfer_data": null,
  "trial_end": null,
  "trial_start": null
}

The Subscription object has a status property .订阅object有一个status 属性 When you call the Retrieve Subscription API , you get back that Subscription object as a class in stripe-python.当您调用检索订阅API时,您会在 stripe-python 中将订阅 object 取回为 class。 At that point you have access to all the properties of that object directly.那时你可以直接访问 object 的所有属性。

You can access the status like this:您可以像这样访问状态:

retrieve_sub = stripe.Subscription.retrieve('sub_123')
status = retrieve_sub.status

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM