Skip to content

Commit

Permalink
feat(subscription): Add support for trial_end and cancel_at_period_end
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmuscnielsen authored and adrienverge committed Nov 19, 2019
1 parent 574ddf6 commit 55ca7a4
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions localstripe/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 55ca7a4

Please sign in to comment.