From 55ca7a4c38169469eacc36aec6d2187655888dc0 Mon Sep 17 00:00:00 2001 From: Rasmus Nielsen Date: Tue, 24 Sep 2019 14:30:24 +0200 Subject: [PATCH] feat(subscription): Add support for trial_end and cancel_at_period_end --- localstripe/resources.py | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/localstripe/resources.py b/localstripe/resources.py index 04b6372f..607fc574 100644 --- a/localstripe/resources.py +++ b/localstripe/resources.py @@ -2101,20 +2101,27 @@ class Subscription(StripeObject): _id_prefix = 'sub_' def __init__(self, customer=None, metadata=None, items=None, + trial_end=None, default_tax_rates=None, tax_percent=None, # deprecated - default_tax_rates=None, enable_incomplete_payments=True, # legacy support payment_behavior='allow_incomplete', trial_period_days=None, **kwargs): if kwargs: raise UserError(400, 'Unexpected ' + ', '.join(kwargs.keys())) + trial_end = try_convert_to_int(trial_end) tax_percent = try_convert_to_float(tax_percent) enable_incomplete_payments = try_convert_to_bool( enable_incomplete_payments) trial_period_days = try_convert_to_int(trial_period_days) + try: assert type(customer) is str and customer.startswith('cus_') + if trial_end is not None: + if trial_end == 'now': + trial_end = int(time.time()) + assert type(trial_end) is int + assert trial_end > 1500000000 if tax_percent is not None: assert default_tax_rates is None assert type(tax_percent) is float @@ -2300,14 +2307,23 @@ def _on_recurring_payment_failure(self, invoice): self.status = 'past_due' - def _update(self, metadata=None, items=None, tax_percent=None, - default_tax_rates=None, - proration_date=None, + def _update(self, metadata=None, items=None, trial_end=None, + default_tax_rates=None, tax_percent=None, + prorate=None, proration_date=None, cancel_at_period_end=None, # Currently unimplemented, only False works as expected: enable_incomplete_payments=False): + trial_end = try_convert_to_int(trial_end) tax_percent = try_convert_to_float(tax_percent) + prorate = try_convert_to_bool(prorate) proration_date = try_convert_to_int(proration_date) + cancel_at_period_end = try_convert_to_bool(cancel_at_period_end) + try: + if trial_end is not None: + if trial_end == 'now': + trial_end = int(time.time()) + assert type(trial_end) is int + assert trial_end > 1500000000 if tax_percent is not None: assert default_tax_rates is None assert type(tax_percent) is float @@ -2317,9 +2333,13 @@ def _update(self, metadata=None, items=None, tax_percent=None, assert type(default_tax_rates) is list assert all(type(txr) is str and txr.startswith('txr_') for txr in default_tax_rates) + if prorate is not None: + assert type(prorate) is bool if proration_date is not None: assert type(proration_date) is int assert proration_date > 1500000000 + if cancel_at_period_end is not None: + assert type(cancel_at_period_end) is bool if items is not None: assert type(items) is list for item in items: @@ -2358,6 +2378,12 @@ def _update(self, metadata=None, items=None, tax_percent=None, self.quantity = items[0]['quantity'] + if trial_end is not None: + self.trial_end = trial_end + + if cancel_at_period_end is not None: + self.cancel_at_period_end = cancel_at_period_end + # If the subscription is updated to a more expensive plan, an invoice # is not automatically generated. To achieve that, an invoice has to # be manually created using the POST /invoices route.