Error
+{{ error }}
+diff --git a/uncloud/uncloud_pay/models.py b/uncloud/uncloud_pay/models.py index 1650e22..20e357a 100644 --- a/uncloud/uncloud_pay/models.py +++ b/uncloud/uncloud_pay/models.py @@ -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 ### diff --git a/uncloud/uncloud_pay/stripe.py b/uncloud/uncloud_pay/stripe.py index 1d745ef..7dc53c6 100644 --- a/uncloud/uncloud_pay/stripe.py +++ b/uncloud/uncloud_pay/stripe.py @@ -17,6 +17,7 @@ CURRENCY = 'chf' stripe.api_key = uncloud.secrets.STRIPE_KEY # Helper (decorator) used to catch errors raised by stripe logic. +# Catch errors that should not be displayed to the end user, raise again. def handle_stripe_error(f): def handle_problems(*args, **kwargs): response = { @@ -25,49 +26,38 @@ def handle_stripe_error(f): 'error': None } - common_message = "Currently it is not possible to make payments." + common_message = "Currently it is not possible to make payments. Please try agin later." try: response_object = f(*args, **kwargs) - response = { - 'response_object': response_object, - 'error': None - } - return response + return response_object except stripe.error.CardError as e: # Since it's a decline, stripe.error.CardError will be caught body = e.json_body - err = body['error'] - response.update({'error': err['message']}) logging.error(str(e)) - return response + + raise e # For error handling. except stripe.error.RateLimitError: - response.update( - {'error': "Too many requests made to the API too quickly"}) - return response + logging.error("Too many requests made to the API too quickly.") + raise Exception(common_message) except stripe.error.InvalidRequestError as e: logging.error(str(e)) - response.update({'error': "Invalid parameters"}) - return response + raise Exception('Invalid parameters.') except stripe.error.AuthenticationError as e: # Authentication with Stripe's API failed # (maybe you changed API keys recently) logging.error(str(e)) - response.update({'error': common_message}) - return response + raise Exception(common_message) except stripe.error.APIConnectionError as e: logging.error(str(e)) - response.update({'error': common_message}) - return response + raise Exception(common_message) except stripe.error.StripeError as e: - # maybe send email + # XXX: maybe send email logging.error(str(e)) - response.update({'error': common_message}) - return response + raise Exception(common_message) except Exception as e: # maybe send email logging.error(str(e)) - response.update({'error': common_message}) - return response + raise Exception(common_message) return handle_problems @@ -82,14 +72,14 @@ def get_customer_id_for(user): return uncloud_pay.models.StripeCustomer.objects.get(owner=user).stripe_id except ObjectDoesNotExist: # No entry yet - making a new one. - customer_request = create_customer(user.username, user.email) - if customer_request['error'] == None: - mapping = uncloud_pay.models.StripeCustomer.objects.create( + try: + customer = create_customer(user.username, user.email) + uncloud_stripe_mapping = uncloud_pay.models.StripeCustomer.objects.create( owner=user, stripe_id=customer_request['response_object']['id'] ) - return mapping.stripe_id - else: + return uncloud_stripe_mapping.stripe_id + except Exception as e: return None @handle_stripe_error diff --git a/uncloud/uncloud_pay/templates/error.html.j2 b/uncloud/uncloud_pay/templates/error.html.j2 new file mode 100644 index 0000000..ba9209c --- /dev/null +++ b/uncloud/uncloud_pay/templates/error.html.j2 @@ -0,0 +1,18 @@ + + +
+{{ error }}
+