Handled stripe payment errors , Added invoice template, Added view to handle invoice data
This commit is contained in:
		
					parent
					
						
							
								bf334a38d4
							
						
					
				
			
			
				commit
				
					
						46ab364184
					
				
			
		
					 5 changed files with 202 additions and 16 deletions
				
			
		|  | @ -2,6 +2,54 @@ import stripe | |||
| from django.conf import settings | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| def handleStripeError(f): | ||||
|         def handleProblems(*args, **kwargs): | ||||
|             response = { | ||||
|                 'paid': False, | ||||
|                 'response_object': None, | ||||
|                 'error': None | ||||
|             } | ||||
|             common_message = "Currently its not possible to make payments." | ||||
| 
 | ||||
|             try: | ||||
|                 response_object = f(*args, **kwargs) | ||||
|                 response  = { | ||||
|                     'response_object': response_object, | ||||
|                     'error': None | ||||
|                 } | ||||
|                 return response | ||||
|             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']}) | ||||
|                 return response | ||||
|             except stripe.error.RateLimitError as e: | ||||
|                 response.update({'error':"Too many requests made to the API too quickly"}) | ||||
|                 return response | ||||
|             except stripe.error.InvalidRequestError as e: | ||||
|                 response.update({'error':"Invalid parameters"}) | ||||
|                 return response | ||||
|             except stripe.error.AuthenticationError as e: | ||||
|                 # Authentication with Stripe's API failed | ||||
|                 # (maybe you changed API keys recently) | ||||
|                 response.update({'error':common_message}) | ||||
|                 return response | ||||
|             except stripe.error.APIConnectionError as e: | ||||
|                 response.update({'error':common_message}) | ||||
|                 return response | ||||
|             except stripe.error.StripeError as e: | ||||
|                 # maybe send email | ||||
|                 response.update({'error':common_message}) | ||||
|                 return response | ||||
|             except Exception as e: | ||||
|                 # maybe send email | ||||
|                 response.update({'error':common_message}) | ||||
|                 return response | ||||
|         return handleProblems | ||||
| 
 | ||||
| 
 | ||||
| class StripeUtils(object): | ||||
| 
 | ||||
|     CURRENCY = 'chf' | ||||
|  | @ -12,18 +60,18 @@ class StripeUtils(object): | |||
|         self.stripe = stripe | ||||
|         self.stripe.api_key = settings.STRIPE_API_PRIVATE_KEY | ||||
| 
 | ||||
|     @handleStripeError | ||||
|     def create_customer(self, token, email): | ||||
|         stripe.api_key = settings.STRIPE_API_PRIVATE_KEY | ||||
|         customer = stripe.Customer.create( | ||||
|         customer = self.stripe.Customer.create( | ||||
|               source=token, | ||||
|               description='description for testing', | ||||
|               email=email | ||||
|         ) | ||||
|         return customer | ||||
| 
 | ||||
|     @handleStripeError | ||||
|     def make_charge(self, amount=None, customer=None): | ||||
|         amount = int(amount * 100)  # stripe amount unit, in cents | ||||
| 
 | ||||
|         charge = self.stripe.Charge.create( | ||||
|           amount=amount,  # in cents | ||||
|           currency=self.CURRENCY, | ||||
|  | @ -31,6 +79,7 @@ class StripeUtils(object): | |||
|         ) | ||||
|         return charge | ||||
| 
 | ||||
|     @handleStripeError | ||||
|     def create_plan(self, amount, name, id): | ||||
|         self.stripe.Plan.create( | ||||
|           amount=amount, | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue