Skip to content

Commit

Permalink
Merge pull request #208 from XaviTorello/feature/add_stripe_billing_e…
Browse files Browse the repository at this point in the history
…mail

Use `Payment.billing_email` as `email` and `receipt_email` for Stripe
  • Loading branch information
patrys authored Apr 3, 2020
2 parents 04103a6 + 226841a commit e8c981b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
25 changes: 18 additions & 7 deletions payments/stripe/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
27 changes: 25 additions & 2 deletions payments/stripe/test_stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Payment(Mock):
total = 100
captured_amount = 0
transaction_id = None
billing_email = "[email protected]"

def change_status(self, status, message=''):
self.status = status
Expand Down Expand Up @@ -95,8 +96,30 @@ def test_form_contains_stripe_script(self):
form = provider.get_form(payment)
self.assertTrue(
'<script class="stripe-button" data-amount="10000" '
'data-currency="USD" data-description="payment" data-image="" '
'data-key="%s" data-name="%s" '
'data-currency="USD" data-description="payment" data-email="[email protected]" '
'data-image="" data-key="%s" data-name="%s" '
'src="https://checkout.stripe.com/checkout.js"></script>' % (
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(
'<script class="stripe-button" data-amount="10000" '
'data-currency="USD" data-description="payment" '
'data-image="" data-key="%s" data-name="%s" '
'src="https://checkout.stripe.com/checkout.js"></script>' % (
PUBLIC_KEY, store_name)
in str(form))
Expand Down
1 change: 1 addition & 0 deletions payments/stripe/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit e8c981b

Please sign in to comment.