Skip to content

Commit

Permalink
Import InvalidRequestError from stripe-python directly
Browse files Browse the repository at this point in the history
Fixes issues on stripe>=2.0
  • Loading branch information
jleclanche committed Jul 13, 2018
1 parent 9bb64e5 commit cf70c05
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions djstripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from django.utils import dateformat, timezone
from django.utils.encoding import smart_text
from django.utils.functional import cached_property
from stripe.error import InvalidRequestError

from . import enums
from . import settings as djstripe_settings
Expand Down Expand Up @@ -1103,7 +1104,7 @@ def add_card(self, source, set_default=True):
def purge(self):
try:
self._api_delete()
except stripe.InvalidRequestError as exc:
except InvalidRequestError as exc:
if "No such customer:" in str(exc):
# The exception was thrown because the stripe customer was already
# deleted on the stripe side, ignore the exception
Expand Down Expand Up @@ -1227,7 +1228,7 @@ def send_invoice(self):
invoice = Invoice._api_create(customer=self.stripe_id)
invoice.pay()
return True
except stripe.InvalidRequestError: # TODO: Check this for a more specific error message.
except InvalidRequestError: # TODO: Check this for a more specific error message.
return False # There was nothing to invoice

def retry_unpaid_invoices(self):
Expand All @@ -1237,7 +1238,7 @@ def retry_unpaid_invoices(self):
for invoice in self.invoices.filter(paid=False, closed=False):
try:
invoice.retry() # Always retry unpaid invoices
except stripe.InvalidRequestError as exc:
except InvalidRequestError as exc:
if str(exc) != "Invoice is already paid":
raise

Expand Down Expand Up @@ -1750,7 +1751,7 @@ def remove(self):

try:
self._api_delete()
except stripe.InvalidRequestError as exc:
except InvalidRequestError as exc:
if "No such source:" in str(exc) or "No such customer:" in str(exc):
# The exception was thrown because the stripe customer or card was already
# deleted on the stripe side, ignore the exception
Expand All @@ -1773,7 +1774,7 @@ def api_retrieve(self, api_key=None):
# eg. {"id": "cus_XXXXXXXX", "deleted": True}
if "sources" not in customer:
# We fake a native stripe InvalidRequestError so that it's caught like an invalid ID error.
raise stripe.InvalidRequestError("No such source: %s" % (self.stripe_id), "id")
raise InvalidRequestError("No such source: %s" % (self.stripe_id), "id")

return customer.sources.retrieve(self.stripe_id, expand=self.expand_fields)

Expand Down Expand Up @@ -2263,7 +2264,7 @@ def upcoming(
subscription_proration_date=subscription_proration_date,
subscription_quantity=subscription_quantity,
subscription_trial_end=subscription_trial_end, **kwargs)
except stripe.InvalidRequestError as exc:
except InvalidRequestError as exc:
if str(exc) != "Nothing to invoice for customer":
raise
return
Expand Down Expand Up @@ -2972,7 +2973,7 @@ def cancel(self, at_period_end=djstripe_settings.CANCELLATION_AT_PERIOD_END):

try:
stripe_subscription = self._api_delete(at_period_end=at_period_end)
except stripe.InvalidRequestError as exc:
except InvalidRequestError as exc:
if "No such subscription:" in str(exc):
# cancel() works by deleting the subscription. The object still
# exists in Stripe however, and can still be retrieved.
Expand Down

0 comments on commit cf70c05

Please sign in to comment.