| 
									
										
										
										
											2016-04-23 02:22:44 -05:00
										 |  |  | import stripe | 
					
						
							|  |  |  | from django.conf import settings | 
					
						
							| 
									
										
										
										
											2016-05-01 14:13:12 +02:00
										 |  |  | stripe.api_key = settings.STRIPE_API_PRIVATE_KEY | 
					
						
							| 
									
										
										
										
											2016-04-23 02:22:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 01:54:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | def handleStripeError(f): | 
					
						
							| 
									
										
										
										
											2016-05-04 17:36:54 +02:00
										 |  |  |     def handleProblems(*args, **kwargs): | 
					
						
							|  |  |  |         response = { | 
					
						
							|  |  |  |             'paid': False, | 
					
						
							|  |  |  |             'response_object': None, | 
					
						
							|  |  |  |             'error': None | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-06-04 17:05:48 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-04 17:36:54 +02:00
										 |  |  |         common_message = "Currently its not possible to make payments." | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             response_object = f(*args, **kwargs) | 
					
						
							| 
									
										
										
										
											2016-04-27 01:54:15 -05:00
										 |  |  |             response = { | 
					
						
							| 
									
										
										
										
											2016-05-04 17:36:54 +02:00
										 |  |  |                 'response_object': response_object, | 
					
						
							| 
									
										
										
										
											2016-04-27 01:54:15 -05:00
										 |  |  |                 'error': None | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-05-04 17:36:54 +02:00
										 |  |  |             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 | 
					
						
							| 
									
										
										
										
											2016-04-27 01:54:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-04 17:36:54 +02:00
										 |  |  |     return handleProblems | 
					
						
							| 
									
										
										
										
											2016-04-27 01:54:15 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-01 14:13:12 +02:00
										 |  |  | class StripeUtils(object): | 
					
						
							| 
									
										
										
										
											2016-04-23 02:22:44 -05:00
										 |  |  |     CURRENCY = 'chf' | 
					
						
							|  |  |  |     INTERVAL = 'month' | 
					
						
							|  |  |  |     SUCCEEDED_STATUS = 'succeeded' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self): | 
					
						
							|  |  |  |         self.stripe = stripe | 
					
						
							| 
									
										
										
										
											2016-05-01 14:13:12 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-19 09:33:15 -05:00
										 |  |  |     def update_customer_token(self, customer, token): | 
					
						
							| 
									
										
										
										
											2017-01-20 11:33:25 -05:00
										 |  |  |         customer.source = token | 
					
						
							|  |  |  |         customer.save() | 
					
						
							| 
									
										
										
										
											2016-12-19 09:33:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-20 11:33:25 -05:00
										 |  |  |     @handleStripeError | 
					
						
							|  |  |  |     def update_customer_card(self, customer_id, token): | 
					
						
							|  |  |  |         customer = stripe.Customer.retrieve(customer_id) | 
					
						
							|  |  |  |         current_card_token = customer.default_source | 
					
						
							|  |  |  |         customer.sources.retrieve(current_card_token).delete() | 
					
						
							| 
									
										
										
										
											2016-12-19 09:33:15 -05:00
										 |  |  |         customer.source = token | 
					
						
							|  |  |  |         customer.save() | 
					
						
							| 
									
										
										
										
											2017-01-20 11:33:25 -05:00
										 |  |  |         credit_card_raw_data = customer.sources.data.pop() | 
					
						
							|  |  |  |         new_card_data = { | 
					
						
							|  |  |  |             'last4': credit_card_raw_data.last4, | 
					
						
							|  |  |  |             'brand': credit_card_raw_data.brand | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return new_card_data | 
					
						
							| 
									
										
										
										
											2016-12-19 09:33:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-30 11:08:00 +05:30
										 |  |  |     @handleStripeError | 
					
						
							|  |  |  |     def get_card_details(self, customer_id, token): | 
					
						
							|  |  |  |         customer = stripe.Customer.retrieve(customer_id) | 
					
						
							|  |  |  |         credit_card_raw_data = customer.sources.data.pop() | 
					
						
							|  |  |  |         card_details = { | 
					
						
							|  |  |  |             'last4': credit_card_raw_data.last4, | 
					
						
							|  |  |  |             'brand': credit_card_raw_data.brand | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return card_details | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-04 17:36:54 +02:00
										 |  |  |     def check_customer(self, id, user, token): | 
					
						
							| 
									
										
										
										
											2016-05-01 14:13:12 +02:00
										 |  |  |         customers = self.stripe.Customer.all() | 
					
						
							|  |  |  |         if not customers.get('data'): | 
					
						
							| 
									
										
										
										
											2016-05-04 17:36:54 +02:00
										 |  |  |             customer = self.create_customer(token, user.email) | 
					
						
							| 
									
										
										
										
											2016-05-01 14:13:12 +02:00
										 |  |  |         else: | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 customer = stripe.Customer.retrieve(id) | 
					
						
							|  |  |  |             except stripe.InvalidRequestError: | 
					
						
							| 
									
										
										
										
											2016-05-04 17:36:54 +02:00
										 |  |  |                 customer = self.create_customer(token, user.email) | 
					
						
							| 
									
										
										
										
											2016-05-05 01:03:35 -05:00
										 |  |  |                 user.stripecustomer.stripe_id = customer.get('response_object').get('id') | 
					
						
							| 
									
										
										
										
											2016-05-01 14:13:12 +02:00
										 |  |  |                 user.stripecustomer.save() | 
					
						
							|  |  |  |         return customer | 
					
						
							| 
									
										
										
										
											2016-04-23 02:22:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-20 11:33:25 -05:00
										 |  |  |     @handleStripeError | 
					
						
							|  |  |  |     def get_customer(self, id): | 
					
						
							|  |  |  |         customer = stripe.Customer.retrieve(id) | 
					
						
							|  |  |  |         # data = customer.get('response_object') | 
					
						
							|  |  |  |         return customer | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 01:54:15 -05:00
										 |  |  |     @handleStripeError | 
					
						
							| 
									
										
										
										
											2016-04-26 01:16:03 -05:00
										 |  |  |     def create_customer(self, token, email): | 
					
						
							| 
									
										
										
										
											2016-05-04 17:36:54 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 01:54:15 -05:00
										 |  |  |         customer = self.stripe.Customer.create( | 
					
						
							| 
									
										
										
										
											2016-05-01 14:13:12 +02:00
										 |  |  |             source=token, | 
					
						
							|  |  |  |             description='description for testing', | 
					
						
							|  |  |  |             email=email | 
					
						
							| 
									
										
										
										
											2016-04-26 01:16:03 -05:00
										 |  |  |         ) | 
					
						
							|  |  |  |         return customer | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 01:54:15 -05:00
										 |  |  |     @handleStripeError | 
					
						
							| 
									
										
										
										
											2016-04-26 01:16:03 -05:00
										 |  |  |     def make_charge(self, amount=None, customer=None): | 
					
						
							| 
									
										
										
										
											2017-05-25 13:04:29 -05:00
										 |  |  |         _amount = float(amount) | 
					
						
							|  |  |  |         amount = int(_amount * 100)  # stripe amount unit, in cents | 
					
						
							| 
									
										
										
										
											2016-04-26 01:16:03 -05:00
										 |  |  |         charge = self.stripe.Charge.create( | 
					
						
							| 
									
										
										
										
											2016-05-04 17:36:54 +02:00
										 |  |  |             amount=amount,  # in cents | 
					
						
							|  |  |  |             currency=self.CURRENCY, | 
					
						
							|  |  |  |             customer=customer | 
					
						
							| 
									
										
										
										
											2016-04-26 01:16:03 -05:00
										 |  |  |         ) | 
					
						
							|  |  |  |         return charge | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-27 01:54:15 -05:00
										 |  |  |     @handleStripeError | 
					
						
							| 
									
										
										
										
											2016-04-23 02:22:44 -05:00
										 |  |  |     def create_plan(self, amount, name, id): | 
					
						
							|  |  |  |         self.stripe.Plan.create( | 
					
						
							| 
									
										
										
										
											2016-05-04 17:36:54 +02:00
										 |  |  |             amount=amount, | 
					
						
							|  |  |  |             interval=self.INTERVAL, | 
					
						
							|  |  |  |             name=name, | 
					
						
							|  |  |  |             currency=self.CURRENCY, | 
					
						
							|  |  |  |             id=id) | 
					
						
							| 
									
										
										
										
											2016-04-23 02:22:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-04 17:36:54 +02:00
										 |  |  |     @handleStripeError | 
					
						
							| 
									
										
										
										
											2017-06-29 17:34:40 +03:00
										 |  |  |     def make_payment(self, customer, amount, token): | 
					
						
							| 
									
										
										
										
											2016-05-04 17:36:54 +02:00
										 |  |  |         charge = self.stripe.Charge.create( | 
					
						
							|  |  |  |             amount=amount,  # in cents | 
					
						
							|  |  |  |             currency=self.CURRENCY, | 
					
						
							|  |  |  |             customer=customer | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         return charge |