From 3be7fbe2eee8e8e98c8734b3f518f33edfabd478 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 15 Aug 2017 16:05:52 +0530 Subject: [PATCH 1/3] Added name parameter for creating StripeCustomer and set description to this --- membership/models.py | 3 +-- utils/stripe_utils.py | 11 ++++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/membership/models.py b/membership/models.py index 6b6f4413..72b8073e 100644 --- a/membership/models.py +++ b/membership/models.py @@ -173,7 +173,6 @@ class StripeCustomer(models.Model): Check if there is a registered stripe customer with that email or create a new one """ - stripe_customer = None try: stripe_utils = StripeUtils() stripe_customer = cls.objects.get(user__email=email) @@ -189,7 +188,7 @@ class StripeCustomer(models.Model): user = CustomUser.objects.get(email=email) 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'): stripe_cus_id = stripe_data.get('response_object').get('id') diff --git a/utils/stripe_utils.py b/utils/stripe_utils.py index d46cf54d..aaf3d9e9 100644 --- a/utils/stripe_utils.py +++ b/utils/stripe_utils.py @@ -90,12 +90,12 @@ class StripeUtils(object): def check_customer(self, id, user, token): customers = self.stripe.Customer.all() if not customers.get('data'): - customer = self.create_customer(token, user.email) + customer = self.create_customer(token, user.email, user.name) else: try: customer = stripe.Customer.retrieve(id) 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.save() return customer @@ -107,11 +107,12 @@ class StripeUtils(object): return customer @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( source=token, - description='description for testing', + description=name, email=email ) return customer From 492f7553d83e86dce18618e1570fd920dc28e611 Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 15 Aug 2017 16:12:08 +0530 Subject: [PATCH 2/3] Added test TestStripeCustomerDescription --- utils/tests.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/utils/tests.py b/utils/tests.py index d40f2c40..e9b9e1d6 100644 --- a/utils/tests.py +++ b/utils/tests.py @@ -3,6 +3,9 @@ from django.test import Client from django.http.request import HttpRequest from model_mommy import mommy +from utils.stripe_utils import StripeUtils +import stripe +from django.conf import settings class BaseTestCase(TestCase): @@ -11,7 +14,6 @@ class BaseTestCase(TestCase): """ def setUp(self): - # Password self.dummy_password = 'test_password' @@ -83,3 +85,36 @@ class BaseTestCase(TestCase): view.kwargs = kwargs view.config = None 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): + # Password + 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) From 999f5d8e2d51a3a267387f51003273b37274fc3c Mon Sep 17 00:00:00 2001 From: PCoder Date: Tue, 15 Aug 2017 18:48:21 +0530 Subject: [PATCH 3/3] Cleaned up a comment --- utils/tests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/utils/tests.py b/utils/tests.py index e9b9e1d6..4a29fa60 100644 --- a/utils/tests.py +++ b/utils/tests.py @@ -94,7 +94,6 @@ class TestStripeCustomerDescription(TestCase): """ def setUp(self): - # Password self.dummy_password = 'test_password' self.dummy_email = 'test@ungleich.ch' self.customer = mommy.make('membership.CustomUser')