From ca39fcb46e342815b9de0d5fb8fd9488aedfeb8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Fleschenberg?= Date: Mon, 22 Dec 2014 15:19:18 +0100 Subject: [PATCH] Payment view: error handling, coding style Throw ImproperlyConfigured if a required setting is missing. Improve code readability a little. --- shop_stripe/offsite_stripe.py | 40 +++++++++++++++++------------------ 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/shop_stripe/offsite_stripe.py b/shop_stripe/offsite_stripe.py index 35a70a9..eec67d8 100644 --- a/shop_stripe/offsite_stripe.py +++ b/shop_stripe/offsite_stripe.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +from django.core.exceptions import ImproperlyConfigured from django.conf import settings from django.conf.urls import patterns, url from django.http import HttpResponseRedirect @@ -42,6 +43,16 @@ def get_urls(self): def stripe_payment_view(self, request): if request.POST: + try: + stripe.api_key = settings.SHOP_STRIPE_PRIVATE_KEY + pub_key = settings.SHOP_STRIPE_PUBLISHABLE_KEY + except AttributeError: + raise ImproperlyConfigured( + 'You must define the SHOP_STRIPE_PRIVATE_KEY' + ' and SHIP_STRIPE_PUBLISHABLE_KEY settings' + ) + currency = getattr(settings, 'SHOP_STRIPE_CURRENCY', 'usd') + card_token = request.POST['stripeToken'] order = self.shop.get_order(request) order_id = self.shop.get_order_unique_id(order) @@ -52,34 +63,21 @@ def stripe_payment_view(self, request): else: description = 'guest customer' - # Default to good ol' U.S. Dollar - currency = getattr(settings, 'SHOP_STRIPE_CURRENCY', 'usd') - - if hasattr(settings, 'SHOP_STRIPE_PRIVATE_KEY'): - stripe.api_key = settings.SHOP_STRIPE_PRIVATE_KEY - else: - raise ConfigError( - 'You must set SHOP_STRIPE_PRIVATE_KEY ' - 'in your configuration file.' - ) - stripe_dict = { 'amount': amount, 'currency': currency, 'card': card_token, - 'description': description, } + 'description': description, + } stripe_result = stripe.Charge.create(**stripe_dict) - self.shop.confirm_payment(self.shop.get_order_for_id( - order_id), amount, stripe_result['id'], self.backend_name) - - if hasattr(settings, 'SHOP_STRIPE_PUBLISHABLE_KEY'): - pub_key = settings.SHOP_STRIPE_PUBLISHABLE_KEY - else: - raise ConfigError( - 'You must set SHOP_STRIPE_PUBLISHABLE_KEY ' - 'in your configuration file.' + self.shop.confirm_payment( + self.shop.get_order_for_id(order_id), + amount, + stripe_result['id'], + self.backend_name ) + form = CardForm context = RequestContext( request, {'form': form, 'STRIPE_PUBLISHABLE_KEY': pub_key})