From b8669503fa96205eb2474f31514caedb1e6fe4db Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Wed, 31 Oct 2018 15:21:07 +0100 Subject: [PATCH] Only allow restricting payment countries if invoice address is obligatory --- src/pretix/base/payment.py | 17 ++++++++++------- src/tests/presale/test_checkout.py | 30 ++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/pretix/base/payment.py b/src/pretix/base/payment.py index 65ceee603b..87ca7bb08c 100644 --- a/src/pretix/base/payment.py +++ b/src/pretix/base/payment.py @@ -257,11 +257,13 @@ class BasePaymentProvider: label=_('Restrict to countries'), choices=Countries(), help_text=_('Only allow choosing this payment provider for invoice addresses in the selected ' - 'countries. If you don\'t select any country, all countries are allowed.'), + 'countries. If you don\'t select any country, all countries are allowed. This is only ' + 'enabled if the invoice address is required.'), widget=forms.CheckboxSelectMultiple( attrs={'class': 'scrolling-multiple-choice'} ), - required=False + required=False, + disabled=not self.event.settings.invoice_address_required )), ]) d['_restricted_countries']._as_type = list @@ -409,11 +411,12 @@ class BasePaymentProvider: request._checkout_flow_invoice_address = InvoiceAddress() return request._checkout_flow_invoice_address - restricted_countries = self.settings.get('_restricted_countries', as_type=list) - if restricted_countries: - ia = get_invoice_address() - if str(ia.country) not in restricted_countries: - return False + if self.event.settings.invoice_address_required: + restricted_countries = self.settings.get('_restricted_countries', as_type=list) + if restricted_countries: + ia = get_invoice_address() + if str(ia.country) not in restricted_countries: + return False return timing and pricing diff --git a/src/tests/presale/test_checkout.py b/src/tests/presale/test_checkout.py index 9ed2594001..0469e517d8 100644 --- a/src/tests/presale/test_checkout.py +++ b/src/tests/presale/test_checkout.py @@ -731,13 +731,38 @@ class CheckoutTestCase(TestCase): doc = BeautifulSoup(response.rendered_content, "lxml") assert doc.select(".alert-danger") + def test_payment_country_ignored_without_invoice_address_required(self): + self.event.settings.set('payment_stripe__enabled', True) + self.event.settings.set('payment_banktransfer__restricted_countries', ['DE', 'AT']) + self.event.settings.set('payment_banktransfer__enabled', True) + self.event.settings.set('invoice_address_required', False) + ia = InvoiceAddress.objects.create( + is_business=True, vat_id='ATU1234567', vat_id_validated=True, + country=Country('CH') + ) + self._set_session('invoice_address', ia.pk) + CartPosition.objects.create( + event=self.event, cart_id=self.session_key, item=self.ticket, + price=23, expires=now() + timedelta(minutes=10) + ) + response = self.client.get('/%s/%s/checkout/payment/' % (self.orga.slug, self.event.slug), follow=True) + doc = BeautifulSoup(response.rendered_content, "lxml") + self.assertEqual(len(doc.select('input[name=payment]')), 2) + response = self.client.post('/%s/%s/checkout/payment/' % (self.orga.slug, self.event.slug), { + 'payment': 'banktransfer' + }, follow=True) + self.assertEqual(response.status_code, 200) + doc = BeautifulSoup(response.rendered_content, "lxml") + assert not doc.select(".alert-danger") + def test_payment_country_allowed(self): self.event.settings.set('payment_stripe__enabled', True) self.event.settings.set('payment_banktransfer__restricted_countries', ['DE', 'AT']) self.event.settings.set('payment_banktransfer__enabled', True) + self.event.settings.set('invoice_address_required', True) ia = InvoiceAddress.objects.create( is_business=True, vat_id='ATU1234567', vat_id_validated=True, - country=Country('DE') + country=Country('DE'), name='Foo', street='Foo' ) self._set_session('invoice_address', ia.pk) CartPosition.objects.create( @@ -758,9 +783,10 @@ class CheckoutTestCase(TestCase): self.event.settings.set('payment_stripe__enabled', True) self.event.settings.set('payment_banktransfer__restricted_countries', ['DE', 'AT']) self.event.settings.set('payment_banktransfer__enabled', True) + self.event.settings.set('invoice_address_required', True) ia = InvoiceAddress.objects.create( is_business=True, vat_id='ATU1234567', vat_id_validated=True, - country=Country('CH') + country=Country('CH'), name='Foo', street='Foo' ) self._set_session('invoice_address', ia.pk) CartPosition.objects.create(