Bank transfer: Allow to refund payments without BIC

This commit is contained in:
Raphael Michel
2021-01-08 23:21:26 +01:00
parent bc8358cd97
commit cf3c4d26cb
7 changed files with 75 additions and 11 deletions

View File

@@ -11,7 +11,7 @@ from i18nfield.fields import I18nFormField, I18nTextarea
from i18nfield.forms import I18nTextInput
from i18nfield.strings import LazyI18nString
from localflavor.generic.forms import BICFormField, IBANFormField
from localflavor.generic.validators import BICValidator, IBANValidator
from localflavor.generic.validators import IBANValidator
from pretix.base.models import OrderPayment, OrderRefund
from pretix.base.payment import BasePaymentProvider
@@ -269,11 +269,10 @@ class BankTransfer(BasePaymentProvider):
return s.strip().upper().replace(" ", "")
def payment_refund_supported(self, payment: OrderPayment) -> bool:
if not all(payment.info_data.get(key) for key in ("payer", "iban", "bic")):
if not all(payment.info_data.get(key) for key in ("payer", "iban")):
return False
try:
IBANValidator()(self.norm(payment.info_data['iban']))
BICValidator()(self.norm(payment.info_data['bic']))
except ValidationError:
return False
else:
@@ -305,7 +304,7 @@ class BankTransfer(BasePaymentProvider):
refund.info_data = {
'payer': refund.payment.info_data['payer'],
'iban': self.norm(refund.payment.info_data['iban']),
'bic': self.norm(refund.payment.info_data['bic']),
'bic': self.norm(refund.payment.info_data['bic']) if refund.payment.info_data.get('bic') else None,
}
refund.save(update_fields=["info"])

View File

@@ -4,8 +4,10 @@ import io
from decimal import Decimal
from defusedcsv import csv
from django.core.exceptions import ValidationError
from django.templatetags.l10n import localize
from django.utils.translation import gettext_lazy as _
from localflavor.generic.validators import BICValidator
from pretix.plugins.banktransfer.models import RefundExport
@@ -22,10 +24,18 @@ def get_refund_export_csv(refund_export: RefundExport):
writer = csv.writer(output)
writer.writerow([_("Payer"), "IBAN", "BIC", _("Amount"), _("Currency"), _("Code")])
for row in refund_export.rows_data:
bic = ''
if row.get('bic'):
try:
BICValidator()(row['bic'])
except ValidationError:
pass
else:
bic = row['bic']
writer.writerow([
row['payer'],
row['iban'],
row['bic'],
bic,
localize(Decimal(row['amount'])),
refund_export.currency,
row['id'],
@@ -56,11 +66,18 @@ def build_sepa_xml(refund_export: RefundExport, account_holder, iban, bic):
payment = {
"name": row['payer'],
"IBAN": row["iban"],
"BIC": row["bic"],
"amount": int(Decimal(row['amount']) * 100), # in euro-cents
"execution_date": datetime.date.today(),
"description": f"{_('Refund')} {refund_export.entity_slug} {row['id']}",
}
if row.get('bic'):
try:
BICValidator()(row['bic'])
except ValidationError:
pass
else:
payment['BIC'] = row['bic']
sepa.add_payment(payment)
data = sepa.export(validate=True)

View File

@@ -649,7 +649,7 @@ class RefundExportListView(ListView):
transaction_rows.append({
"amount": refund.amount,
"id": refund.full_id,
**{key: data[key] for key in ("payer", "iban", "bic")}
**{key: data.get(key) for key in ("payer", "iban", "bic")}
})
refund.done(user=self.request.user)