Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _api_delete(cls, id):
obj = Subscription._api_retrieve(id)
obj.ended_at = int(time.time())
obj.status = 'canceled'
schedule_webhook(Event('customer.subscription.deleted', obj))
return obj
def _api_void_invoice(cls, id):
obj = Invoice._api_retrieve(id)
if obj.status not in ('draft', 'open'):
raise UserError(400, 'Bad request')
PaymentIntent._api_cancel(obj.payment_intent)
obj._draft = False
obj._voided = True
obj.status_transitions['voided_at'] = int(time.time())
if obj.subscription:
sub = Subscription._api_retrieve(obj.subscription)
sub._on_initial_payment_voided(obj)
return obj
def _on_recurring_payment_failure(self, invoice):
# If source is SEPA, any payment failure at creation or upgrade cancels
# the subscription:
if (invoice.charge and PaymentMethod._api_retrieve(
invoice.charge.payment_method).type == 'sepa_debit'):
return Subscription._api_delete(self.id)
self.status = 'past_due'
def _api_add_subscription(cls, id, **data):
return Subscription._api_create(customer=id, **data)
def subscriptions(self):
return Subscription._api_list_all(
'/v1/customers/' + self.id + '/subscriptions', customer=self.id)
def _api_list_all(cls, url, customer=None, status=None, limit=None):
try:
if customer is not None:
assert type(customer) is str and customer.startswith('cus_')
if status is not None:
assert status in ('all', 'incomplete', 'incomplete_expired',
'trialing', 'active', 'past_due', 'unpaid',
'canceled')
except AssertionError:
raise UserError(400, 'Bad request')
li = super(Subscription, cls)._api_list_all(url, limit=limit)
if status is None:
li._list = [sub for sub in li._list if sub.status not in
('canceled', 'incomplete_expired')]
elif status != 'all':
li._list = [sub for sub in li._list if sub.status == status]
if customer is not None:
Customer._api_retrieve(customer) # to return 404 if not existant
li._list = [sub for sub in li._list if sub.customer == customer]
return li
def _api_update_subscription(cls, id, subscription_id, **data):
obj = Subscription._api_retrieve(subscription_id)
if obj.customer != id:
raise UserError(404, 'Customer ' + id + ' does not have a '
'subscription with ID ' + subscription_id)
return Subscription._api_update(subscription_id, **data)