Merge pull request #449 from pcoder/task/3687/stripe_customer_description
Added code to store customer's name in Stripe's description field.
This commit is contained in:
		
				commit
				
					
						2d7231caa0
					
				
			
		
					 3 changed files with 42 additions and 8 deletions
				
			
		|  | @ -173,7 +173,6 @@ class StripeCustomer(models.Model): | ||||||
|             Check if there is a registered stripe customer with that email |             Check if there is a registered stripe customer with that email | ||||||
|             or create a new one |             or create a new one | ||||||
|         """ |         """ | ||||||
|         stripe_customer = None |  | ||||||
|         try: |         try: | ||||||
|             stripe_utils = StripeUtils() |             stripe_utils = StripeUtils() | ||||||
|             stripe_customer = cls.objects.get(user__email=email) |             stripe_customer = cls.objects.get(user__email=email) | ||||||
|  | @ -189,7 +188,7 @@ class StripeCustomer(models.Model): | ||||||
|             user = CustomUser.objects.get(email=email) |             user = CustomUser.objects.get(email=email) | ||||||
| 
 | 
 | ||||||
|             stripe_utils = StripeUtils() |             stripe_utils = StripeUtils() | ||||||
|             stripe_data = stripe_utils.create_customer(token, email) |             stripe_data = stripe_utils.create_customer(token, email, user.name) | ||||||
|             if stripe_data.get('response_object'): |             if stripe_data.get('response_object'): | ||||||
|                 stripe_cus_id = stripe_data.get('response_object').get('id') |                 stripe_cus_id = stripe_data.get('response_object').get('id') | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -90,12 +90,12 @@ class StripeUtils(object): | ||||||
|     def check_customer(self, id, user, token): |     def check_customer(self, id, user, token): | ||||||
|         customers = self.stripe.Customer.all() |         customers = self.stripe.Customer.all() | ||||||
|         if not customers.get('data'): |         if not customers.get('data'): | ||||||
|             customer = self.create_customer(token, user.email) |             customer = self.create_customer(token, user.email, user.name) | ||||||
|         else: |         else: | ||||||
|             try: |             try: | ||||||
|                 customer = stripe.Customer.retrieve(id) |                 customer = stripe.Customer.retrieve(id) | ||||||
|             except stripe.InvalidRequestError: |             except stripe.InvalidRequestError: | ||||||
|                 customer = self.create_customer(token, user.email) |                 customer = self.create_customer(token, user.email, user.name) | ||||||
|                 user.stripecustomer.stripe_id = customer.get('response_object').get('id') |                 user.stripecustomer.stripe_id = customer.get('response_object').get('id') | ||||||
|                 user.stripecustomer.save() |                 user.stripecustomer.save() | ||||||
|         return customer |         return customer | ||||||
|  | @ -107,11 +107,12 @@ class StripeUtils(object): | ||||||
|         return customer |         return customer | ||||||
| 
 | 
 | ||||||
|     @handleStripeError |     @handleStripeError | ||||||
|     def create_customer(self, token, email): |     def create_customer(self, token, email, name=None): | ||||||
| 
 |         if name is None or name.strip() == "": | ||||||
|  |             name = email | ||||||
|         customer = self.stripe.Customer.create( |         customer = self.stripe.Customer.create( | ||||||
|             source=token, |             source=token, | ||||||
|             description='description for testing', |             description=name, | ||||||
|             email=email |             email=email | ||||||
|         ) |         ) | ||||||
|         return customer |         return customer | ||||||
|  |  | ||||||
|  | @ -3,6 +3,9 @@ from django.test import Client | ||||||
| from django.http.request import HttpRequest | from django.http.request import HttpRequest | ||||||
| 
 | 
 | ||||||
| from model_mommy import mommy | from model_mommy import mommy | ||||||
|  | from utils.stripe_utils import StripeUtils | ||||||
|  | import stripe | ||||||
|  | from django.conf import settings | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class BaseTestCase(TestCase): | class BaseTestCase(TestCase): | ||||||
|  | @ -11,7 +14,6 @@ class BaseTestCase(TestCase): | ||||||
|     """ |     """ | ||||||
| 
 | 
 | ||||||
|     def setUp(self): |     def setUp(self): | ||||||
| 
 |  | ||||||
|         # Password |         # Password | ||||||
|         self.dummy_password = 'test_password' |         self.dummy_password = 'test_password' | ||||||
| 
 | 
 | ||||||
|  | @ -83,3 +85,35 @@ class BaseTestCase(TestCase): | ||||||
|         view.kwargs = kwargs |         view.kwargs = kwargs | ||||||
|         view.config = None |         view.config = None | ||||||
|         return view |         return view | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class TestStripeCustomerDescription(TestCase): | ||||||
|  |     """ | ||||||
|  |     A class to test setting the description field of the stripe customer | ||||||
|  |     https://stripe.com/docs/api#metadata | ||||||
|  |     """ | ||||||
|  | 
 | ||||||
|  |     def setUp(self): | ||||||
|  |         self.dummy_password = 'test_password' | ||||||
|  |         self.dummy_email = 'test@ungleich.ch' | ||||||
|  |         self.customer = mommy.make('membership.CustomUser') | ||||||
|  |         self.customer.set_password(self.dummy_password) | ||||||
|  |         self.customer.email = self.dummy_email | ||||||
|  |         self.customer.save() | ||||||
|  |         stripe.api_key = settings.STRIPE_API_PRIVATE_KEY | ||||||
|  | 
 | ||||||
|  |     def test_creating_stripe_customer(self): | ||||||
|  |         test_name = "Monty Python" | ||||||
|  |         token = stripe.Token.create( | ||||||
|  |             card={ | ||||||
|  |                 "number": '4111111111111111', | ||||||
|  |                 "exp_month": 12, | ||||||
|  |                 "exp_year": 2022, | ||||||
|  |                 "cvc": '123' | ||||||
|  |             }, | ||||||
|  |         ) | ||||||
|  |         stripe_utils = StripeUtils() | ||||||
|  |         stripe_data = stripe_utils.create_customer(token.id, self.customer.email, test_name) | ||||||
|  |         self.assertEqual(stripe_data.get('error'), None) | ||||||
|  |         customer_data = stripe_data.get('response_object') | ||||||
|  |         self.assertEqual(customer_data.description, test_name) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue