From b8862f825323c8a555ba24959f812afd3677b36f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavi=20Torell=C3=B3?= Date: Fri, 10 May 2019 12:48:17 +0200 Subject: [PATCH 1/5] Inject payment.billing_email into Stripe widget form if exists To provide a better UX, if the billing_email is defined in our Payment, use it to auto-render it in the Stripe's payment form. Fix #57 --- payments/stripe/widgets.py | 1 + 1 file changed, 1 insertion(+) diff --git a/payments/stripe/widgets.py b/payments/stripe/widgets.py index b9989d07d..b4967d9bb 100644 --- a/payments/stripe/widgets.py +++ b/payments/stripe/widgets.py @@ -20,6 +20,7 @@ def __init__(self, provider, payment, *args, **kwargs): 'data-key': provider.public_key, 'data-image': provider.image, 'data-name': provider.name, + 'data-email': payment.billing_email, 'data-description': payment.description or _('Total payment'), # Stripe accepts cents 'data-amount': int(payment.total * 100), From 1dd7fd0e3702cc13b24e077e448fd35bc499d696 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavi=20Torell=C3=B3?= Date: Fri, 10 May 2019 12:51:20 +0200 Subject: [PATCH 2/5] Patch Stripe Charge with receipt_email if Payment.billing_email exists It integrates the `receipt_email` property if `Payment.billing_email` exists. This will improve the Stripe payment flow ensuring that an email is ready to be sended to the payment creator if a receipt is requested (due to the automatic Stripe payment notifications flow, or due to a receipt request using the Dashboard) Fix #185 --- payments/stripe/forms.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/payments/stripe/forms.py b/payments/stripe/forms.py index 688cbe76f..4d4e23b6c 100644 --- a/payments/stripe/forms.py +++ b/payments/stripe/forms.py @@ -31,14 +31,25 @@ def clean(self): if not self.payment.transaction_id: stripe.api_key = self.provider.secret_key try: - self.charge = stripe.Charge.create( - capture=False, - amount=int(self.payment.total * 100), - currency=self.payment.currency, - card=data['stripeToken'], - description='%s %s' % ( + charge_data = { + "capture": False, + "amount": int(self.payment.total * 100), + "currency": self.payment.currency, + "card": data['stripeToken'], + "description": '%s %s' % ( self.payment.billing_last_name, - self.payment.billing_first_name)) + self.payment.billing_first_name + ), + } + + # Patch charge with billing email if exists + if self.payment.billing_email: + charge_data.update({ + "receipt_email": self.payment.billing_email, + }) + + self.charge = stripe.Charge.create(**charge_data) + except stripe.error.CardError as e: # Making sure we retrieve the charge charge_id = e.json_body['error']['charge'] From 81c1e2416408a8bec50da8c1540c3ef39500e703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavi=20Torell=C3=B3?= Date: Fri, 10 May 2019 17:55:15 +0200 Subject: [PATCH 3/5] Add `billing_email` to our mocked Payment --- payments/stripe/test_stripe.py | 1 + 1 file changed, 1 insertion(+) diff --git a/payments/stripe/test_stripe.py b/payments/stripe/test_stripe.py index 6ddf8753b..6ad02b138 100644 --- a/payments/stripe/test_stripe.py +++ b/payments/stripe/test_stripe.py @@ -26,6 +26,7 @@ class Payment(Mock): total = 100 captured_amount = 0 transaction_id = None + billing_email = "john@doe.com" def change_status(self, status, message=''): self.status = status From 81019c89278ca86c9bfca37fde0f92adbfad5cb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavi=20Torell=C3=B3?= Date: Fri, 10 May 2019 17:55:57 +0200 Subject: [PATCH 4/5] Update expected form render with payment.billing_email --- payments/stripe/test_stripe.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/payments/stripe/test_stripe.py b/payments/stripe/test_stripe.py index 6ad02b138..52c23a47b 100644 --- a/payments/stripe/test_stripe.py +++ b/payments/stripe/test_stripe.py @@ -96,8 +96,8 @@ def test_form_contains_stripe_script(self): form = provider.get_form(payment) self.assertTrue( '' % ( PUBLIC_KEY, store_name) in str(form)) From 226841a9f58f6d5ebb6a3219daf6444c39638ddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavi=20Torell=C3=B3?= Date: Tue, 14 May 2019 20:11:50 +0200 Subject: [PATCH 5/5] Validate script generation if billing email is not set It should generate the script as expected --- payments/stripe/test_stripe.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/payments/stripe/test_stripe.py b/payments/stripe/test_stripe.py index 52c23a47b..fbc091406 100644 --- a/payments/stripe/test_stripe.py +++ b/payments/stripe/test_stripe.py @@ -102,6 +102,28 @@ def test_form_contains_stripe_script(self): PUBLIC_KEY, store_name) in str(form)) + def test_form_contains_stripe_script_withou_billing_email(self): + """ + If billing email is not set, it should generate the script as expected + """ + payment = Payment() + store_name = 'Test store' + provider = StripeProvider( + name=store_name, + secret_key=SECRET_KEY, public_key=PUBLIC_KEY) + + form = provider.get_form(payment) + + payment.billing_email = None + form = provider.get_form(payment) + self.assertTrue( + '' % ( + PUBLIC_KEY, store_name) + in str(form)) + def test_provider_raises_redirect_needed_when_token_does_not_exist(self): payment = Payment() provider = StripeProvider(