Skip to content

Commit 9a09772

Browse files
authored
deps: Bump chargebee from 2.7.7 to 3.10.0 (#6788)
1 parent d90c226 commit 9a09772

File tree

10 files changed

+430
-229
lines changed

10 files changed

+430
-229
lines changed

api/organisations/chargebee/cache.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
from typing import Dict, Generator
1+
from typing import Any, Dict, Generator
22

3-
import chargebee # type: ignore[import-untyped]
4-
from chargebee.result import Result as ChargebeeResult # type: ignore[import-untyped]
53
from django.conf import settings
64
from django.core.cache import caches
75

8-
from .metadata import ChargebeeItem, ChargebeeObjMetadata
6+
from organisations.chargebee.client import chargebee_client
7+
from organisations.chargebee.metadata import ChargebeeItem, ChargebeeObjMetadata
98

109
CHARGEBEE_CACHE_KEY = "chargebee_items"
1110

@@ -50,13 +49,13 @@ def fetch_addons(self) -> dict: # type: ignore[type-arg]
5049
return addons
5150

5251

53-
def get_item_generator(item: ChargebeeItem) -> Generator[ChargebeeResult, None, None]:
52+
def get_item_generator(item: ChargebeeItem) -> Generator[Any, None, None]:
5453
next_offset = None
5554
while True:
56-
entries = getattr(chargebee, item.value).list(
57-
{"limit": 100, "offset": next_offset}
58-
)
59-
for entry in entries:
55+
resource = getattr(chargebee_client, item.value)
56+
list_params_cls = resource.ListParams
57+
entries = resource.list(list_params_cls(limit=100, offset=next_offset))
58+
for entry in entries.list:
6059
yield entry
6160

6261
if entries.next_offset:

api/organisations/chargebee/chargebee.py

Lines changed: 83 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,39 @@
33
from contextlib import suppress
44
from datetime import datetime
55

6-
import chargebee # type: ignore[import-untyped]
76
from chargebee.api_error import ( # type: ignore[import-untyped]
87
APIError as ChargebeeAPIError,
98
)
10-
from django.conf import settings
9+
from chargebee.models.hosted_page.operations import ( # type: ignore[import-untyped]
10+
HostedPage as HostedPageOps,
11+
)
12+
from chargebee.models.portal_session.operations import ( # type: ignore[import-untyped]
13+
PortalSession as PortalSessionOps,
14+
)
15+
from chargebee.models.subscription.operations import ( # type: ignore[import-untyped]
16+
Subscription as SubscriptionOps,
17+
)
18+
from chargebee.models.subscription.responses import ( # type: ignore[import-untyped]
19+
SubscriptionResponse,
20+
)
1121
from pytz import UTC
1222

13-
from ..subscriptions.constants import CHARGEBEE
14-
from ..subscriptions.exceptions import (
23+
from organisations.chargebee.cache import ChargebeeCache
24+
from organisations.chargebee.client import chargebee_client
25+
from organisations.chargebee.constants import (
26+
ADDITIONAL_API_SCALE_UP_ADDON_ID,
27+
ADDITIONAL_API_START_UP_ADDON_ID,
28+
ADDITIONAL_SEAT_ADDON_ID,
29+
)
30+
from organisations.chargebee.metadata import ChargebeeObjMetadata
31+
from organisations.subscriptions.constants import CHARGEBEE
32+
from organisations.subscriptions.exceptions import (
1533
CannotCancelChargebeeSubscription,
1634
UpgradeAPIUsageError,
1735
UpgradeAPIUsagePaymentFailure,
1836
UpgradeSeatsError,
1937
UpgradeSeatsPaymentFailure,
2038
)
21-
from .cache import ChargebeeCache
22-
from .constants import (
23-
ADDITIONAL_API_SCALE_UP_ADDON_ID,
24-
ADDITIONAL_API_START_UP_ADDON_ID,
25-
ADDITIONAL_SEAT_ADDON_ID,
26-
)
27-
from .metadata import ChargebeeObjMetadata
28-
29-
chargebee.configure(settings.CHARGEBEE_API_KEY, settings.CHARGEBEE_SITE)
3039

3140
logger = logging.getLogger(__name__)
3241

@@ -43,13 +52,13 @@
4352
def get_subscription_data_from_hosted_page(hosted_page_id): # type: ignore[no-untyped-def]
4453
hosted_page = get_hosted_page(hosted_page_id) # type: ignore[no-untyped-call]
4554
subscription = get_subscription_from_hosted_page(hosted_page) # type: ignore[no-untyped-call]
46-
plan_metadata = get_plan_meta_data(subscription.plan_id) # type: ignore[no-untyped-call]
55+
plan_metadata = get_plan_meta_data(subscription["plan_id"]) # type: ignore[no-untyped-call]
4756
if subscription:
4857
return {
49-
"subscription_id": subscription.id,
50-
"plan": subscription.plan_id,
58+
"subscription_id": subscription["id"],
59+
"plan": subscription["plan_id"],
5160
"subscription_date": datetime.fromtimestamp(
52-
subscription.created_at, tz=UTC
61+
subscription["created_at"], tz=UTC
5362
),
5463
"max_seats": get_max_seats_for_plan(plan_metadata),
5564
"max_api_calls": get_max_api_calls_for_plan(plan_metadata),
@@ -61,22 +70,20 @@ def get_subscription_data_from_hosted_page(hosted_page_id): # type: ignore[no-u
6170

6271

6372
def get_hosted_page(hosted_page_id): # type: ignore[no-untyped-def]
64-
response = chargebee.HostedPage.retrieve(hosted_page_id)
73+
response = chargebee_client.HostedPage.retrieve(hosted_page_id)
6574
return response.hosted_page
6675

6776

6877
def get_subscription_from_hosted_page(hosted_page): # type: ignore[no-untyped-def]
69-
if hasattr(hosted_page, "content"):
70-
content = hosted_page.content
71-
if hasattr(content, "subscription"):
72-
return content.subscription
78+
content = hosted_page.content
79+
if content and "subscription" in content:
80+
return content["subscription"]
7381

7482

7583
def get_customer_id_from_hosted_page(hosted_page): # type: ignore[no-untyped-def]
76-
if hasattr(hosted_page, "content"):
77-
content = hosted_page.content
78-
if hasattr(content, "customer"):
79-
return content.customer.id
84+
content = hosted_page.content
85+
if content and "customer" in content:
86+
return content["customer"]["id"]
8087

8188

8289
def get_max_seats_for_plan(meta_data: dict) -> int: # type: ignore[type-arg]
@@ -96,28 +103,39 @@ def get_plan_meta_data(plan_id): # type: ignore[no-untyped-def]
96103

97104
def get_plan_details(plan_id): # type: ignore[no-untyped-def]
98105
if plan_id:
99-
return chargebee.Plan.retrieve(plan_id)
106+
return chargebee_client.Plan.retrieve(plan_id)
100107

101108

102109
def get_portal_url(customer_id, redirect_url): # type: ignore[no-untyped-def]
103-
result = chargebee.PortalSession.create(
104-
{"redirect_url": redirect_url, "customer": {"id": customer_id}}
110+
result = chargebee_client.PortalSession.create(
111+
PortalSessionOps.CreateParams(
112+
redirect_url=redirect_url,
113+
customer=PortalSessionOps.CreateCustomerParams(
114+
id=customer_id,
115+
),
116+
)
105117
)
106118
if result and hasattr(result, "portal_session"):
107119
return result.portal_session.access_url
108120

109121

110122
def get_customer_id_from_subscription_id(subscription_id): # type: ignore[no-untyped-def]
111-
subscription_response = chargebee.Subscription.retrieve(subscription_id)
123+
subscription_response = chargebee_client.Subscription.retrieve(subscription_id)
112124
if hasattr(subscription_response, "customer"):
113125
return subscription_response.customer.id
114126

115127

116128
def get_hosted_page_url_for_subscription_upgrade(
117129
subscription_id: str, plan_id: str
118130
) -> str:
119-
params = {"subscription": {"id": subscription_id, "plan_id": plan_id}}
120-
checkout_existing_response = chargebee.HostedPage.checkout_existing(params)
131+
checkout_existing_response = chargebee_client.HostedPage.checkout_existing(
132+
HostedPageOps.CheckoutExistingParams(
133+
subscription=HostedPageOps.CheckoutExistingSubscriptionParams(
134+
id=subscription_id,
135+
plan_id=plan_id,
136+
),
137+
)
138+
)
121139
return checkout_existing_response.hosted_page.url # type: ignore[no-any-return]
122140

123141

@@ -150,7 +168,7 @@ def get_subscription_metadata_from_id( # type: ignore[return]
150168
return None
151169

152170
with suppress(ChargebeeAPIError):
153-
chargebee_result = chargebee.Subscription.retrieve(subscription_id)
171+
chargebee_result = chargebee_client.Subscription.retrieve(subscription_id)
154172
chargebee_subscription = _convert_chargebee_subscription_to_dictionary(
155173
chargebee_result.subscription
156174
)
@@ -162,7 +180,10 @@ def get_subscription_metadata_from_id( # type: ignore[return]
162180

163181
def cancel_subscription(subscription_id: str): # type: ignore[no-untyped-def]
164182
try:
165-
chargebee.Subscription.cancel(subscription_id, {"end_of_term": True})
183+
chargebee_client.Subscription.cancel(
184+
subscription_id,
185+
SubscriptionOps.CancelParams(end_of_term=True),
186+
)
166187
except ChargebeeAPIError as e:
167188
msg = "Cannot cancel CB subscription for subscription id: %s" % subscription_id
168189
logger.error(msg)
@@ -171,7 +192,9 @@ def cancel_subscription(subscription_id: str): # type: ignore[no-untyped-def]
171192

172193
def add_single_seat(subscription_id: str): # type: ignore[no-untyped-def]
173194
try:
174-
subscription = chargebee.Subscription.retrieve(subscription_id).subscription
195+
subscription = chargebee_client.Subscription.retrieve(
196+
subscription_id
197+
).subscription
175198
addons = subscription.addons or []
176199
current_seats = next(
177200
(
@@ -182,15 +205,18 @@ def add_single_seat(subscription_id: str): # type: ignore[no-untyped-def]
182205
0,
183206
)
184207

185-
chargebee.Subscription.update(
208+
chargebee_client.Subscription.update(
186209
subscription_id,
187-
{
188-
"addons": [
189-
{"id": ADDITIONAL_SEAT_ADDON_ID, "quantity": current_seats + 1}
210+
SubscriptionOps.UpdateParams(
211+
addons=[
212+
SubscriptionOps.UpdateAddonParams(
213+
id=ADDITIONAL_SEAT_ADDON_ID,
214+
quantity=current_seats + 1,
215+
)
190216
],
191-
"prorate": True,
192-
"invoice_immediately": False,
193-
},
217+
prorate=True,
218+
invoice_immediately=False,
219+
),
194220
)
195221

196222
except ChargebeeAPIError as e:
@@ -232,13 +258,18 @@ def add_100k_api_calls(
232258
if not count:
233259
return
234260
try:
235-
chargebee.Subscription.update(
261+
chargebee_client.Subscription.update(
236262
subscription_id,
237-
{
238-
"addons": [{"id": addon_id, "quantity": count}],
239-
"prorate": False,
240-
"invoice_immediately": invoice_immediately,
241-
},
263+
SubscriptionOps.UpdateParams(
264+
addons=[
265+
SubscriptionOps.UpdateAddonParams(
266+
id=addon_id,
267+
quantity=count,
268+
)
269+
],
270+
prorate=False,
271+
invoice_immediately=invoice_immediately,
272+
),
242273
)
243274

244275
except ChargebeeAPIError as e:
@@ -260,11 +291,10 @@ def add_100k_api_calls(
260291

261292

262293
def _convert_chargebee_subscription_to_dictionary(
263-
chargebee_subscription: chargebee.Subscription,
294+
chargebee_subscription: SubscriptionResponse,
264295
) -> dict: # type: ignore[type-arg]
265-
chargebee_subscription_dict = vars(chargebee_subscription)
266-
# convert the addons into a list of dictionaries since vars don't do it recursively
296+
chargebee_subscription_dict = dict(chargebee_subscription.raw_data)
267297
addons = chargebee_subscription.addons or []
268-
chargebee_subscription_dict["addons"] = [vars(addon) for addon in addons]
298+
chargebee_subscription_dict["addons"] = [dict(addon.raw_data) for addon in addons]
269299

270-
return chargebee_subscription_dict # type: ignore[no-any-return]
300+
return chargebee_subscription_dict
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from chargebee import Chargebee # type: ignore[import-untyped]
2+
from django.conf import settings
3+
4+
chargebee_client = Chargebee(
5+
api_key=settings.CHARGEBEE_API_KEY,
6+
site=settings.CHARGEBEE_SITE,
7+
)

0 commit comments

Comments
 (0)