Only allow restricting payment countries if invoice address is obligatory

This commit is contained in:
Raphael Michel
2018-10-31 15:21:07 +01:00
parent 863165caaa
commit b8669503fa
2 changed files with 38 additions and 9 deletions

View File

@@ -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

View File

@@ -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(