Revamp stripe error handling

This commit is contained in:
fnux 2020-03-05 11:03:47 +01:00
commit 80fe28588e
4 changed files with 104 additions and 63 deletions

View file

@ -139,35 +139,40 @@ class PaymentMethod(models.Model):
return False
def charge(self, amount):
if amount > 0: # Make sure we don't charge negative amount by errors...
if self.source == 'stripe':
stripe_customer = StripeCustomer.objects.get(owner=self.owner).stripe_id
charge_request = uncloud_pay.stripe.charge_customer(
amount, stripe_customer, self.stripe_payment_method_id)
if charge_request['error'] == None:
payment = Payment(owner=self.owner, source=self.source, amount=amount)
payment.save() # TODO: Check return status
if not self.active:
raise Exception('This payment method is inactive.')
return payment
else:
raise Exception('Stripe error: {}'.format(charge_request['error']))
else:
raise Exception('This payment method is unsupported/cannot be charged.')
else:
if amount > 0: # Make sure we don't charge negative amount by errors...
raise Exception('Cannot charge negative amount.')
if self.source == 'stripe':
stripe_customer = StripeCustomer.objects.get(owner=self.owner).stripe_id
stripe_payment = uncloud_pay.stripe.charge_customer(
amount, stripe_customer, self.stripe_payment_method_id)
if stripe_payment['paid']:
payment = Payment(owner=self.owner, source=self.source, amount=amount)
payment.save() # TODO: Check return status
return payment
else:
raise Exception(stripe_payment['error'])
else:
raise Exception('This payment method is unsupported/cannot be charged.')
def get_primary_for(user):
methods = PaymentMethod.objects.filter(owner=user)
for method in methods:
# Do we want to do something with non-primary method?
if method.primary:
if method.active and method.primary:
return method
return None
class Meta:
#API_keyunique_together = [['owner', 'primary']]
# TODO: limit to one primary method per user.
# unique_together is no good since it won't allow more than one
# non-primary method.
pass
###