Skip to content

Commit

Permalink
payments: relocate shared EPC QR generation code
Browse files Browse the repository at this point in the history
  • Loading branch information
jayaddison authored and russss committed Dec 22, 2023
1 parent 81c15e6 commit 177ad55
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
37 changes: 37 additions & 0 deletions apps/common/epc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from io import BytesIO

from markupsafe import Markup
from segno import helpers


def make_epc_qrfile(payment, **kwargs):
qrfile = BytesIO()
# TODO: this isn't currently used. Need to fetch IBAN from payment.recommended_destination
# and name from somewhere - maybe config rather than hard-coding.
qr = helpers.make_epc_qr(
name="EMF Festivals Ltd",
iban=payment.recommended_destination.iban,
amount=payment.amount,
reference=payment.bankref,
encoding=1,
)
qr.save(qrfile, **kwargs)
qrfile.seek(0)
return qrfile


def qrfile_to_svg(qrfile):
return Markup(qrfile.getvalue().decode("utf-8"))


def format_inline_epc_qr(payment):
qrfile = make_epc_qrfile(
payment,
kind="svg",
svgclass=None,
omitsize=True,
xmldecl=False,
svgns=False,
nl=False,
)
return qrfile_to_svg(qrfile)
37 changes: 1 addition & 36 deletions apps/payments/invoice.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from decimal import Decimal
from io import BytesIO
import logging
import shutil
import os.path
Expand All @@ -15,55 +14,21 @@
send_file,
)
from flask_login import login_required, current_user
from markupsafe import Markup
from segno import helpers
from sqlalchemy.sql.functions import func
from wtforms import TextAreaField, SubmitField

from main import external_url, db
from ..common.receipt import render_pdf
from models.product import Product, PriceTier
from models.purchase import Purchase
from ..common.epc import format_inline_epc_qr
from ..common.forms import Form
from . import get_user_payment_or_abort
from . import payments

logger = logging.getLogger(__name__)


def make_epc_qrfile(payment, **kwargs):
qrfile = BytesIO()
# TODO: this isn't currently used. Need to fetch IBAN from payment.recommended_destination
# and name from somewhere - maybe config rather than hard-coding.
qr = helpers.make_epc_qr(
name="EMF Festivals Ltd",
iban=payment.recommended_destination.iban,
amount=payment.amount,
reference=payment.bankref,
encoding=1,
)
qr.save(qrfile, **kwargs)
qrfile.seek(0)
return qrfile


def qrfile_to_svg(qrfile):
return Markup(qrfile.getvalue().decode("utf-8"))


def format_inline_epc_qr(payment):
qrfile = make_epc_qrfile(
payment,
kind="svg",
svgclass=None,
omitsize=True,
xmldecl=False,
svgns=False,
nl=False,
)
return qrfile_to_svg(qrfile)


class InvoiceForm(Form):
company = TextAreaField("Company name")
update = SubmitField("Update")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_invoice.py → tests/test_epc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pyzbar.pyzbar import decode

from apps.payments.invoice import format_inline_epc_qr
from apps.common.epc import format_inline_epc_qr
from models.payment import BankPayment

from tests._utils import render_svg
Expand Down

0 comments on commit 177ad55

Please sign in to comment.