Attach receipt PDF to "payment successful" email #96850

Closed
Anna Sirota wants to merge 5 commits from attach-pdf-to-email into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
3 changed files with 73 additions and 47 deletions
Showing only changes of commit bf859d608c - Show all commits

View File

@ -14,7 +14,7 @@ from looper.tests import AbstractLooperTestCase
from looper.tests.factories import SubscriptionFactory, create_customer_with_billing_address
import looper.exceptions
from blender_fund_main.tests.utils import extract_text_from_pdf
from blender_fund_main.tests.utils import extract_text_from_pdf_bytes, expected_pdf_text_tmpl
from blender_fund_main.utils import html_to_text
import blender_fund_main.tasks as tasks
@ -373,18 +373,24 @@ class TestClockEmails(AbstractLooperTestCase):
expected_payment_received_email_body,
html_to_text(email.alternatives[0][0]),
)
self.assertEqual(len(email.attachments), 1)
self.assertEqual(email.attachments[0][1], 'application/pdf')
pdf_text = extract_text_from_pdf(email.attachments[0][0])
# Check that receipt PDF is attached to the email
self.assertEqual(len(email.attachments), 1)
title, content, mime = email.attachments[0]
self.assertEqual(title, f'blender-development-fund-receipt-{new_order.pk}.pdf')
self.assertEqual(mime, 'application/pdf')
pdf_text = extract_text_from_pdf_bytes(content)
self.assertEqual(
pdf_text,
expected_text_tmpl.format(
order=order,
expected_address=' -',
expected_currency_symbol='$',
expected_payment_method='PayPal account test@example.com',
expected_total='14.80',
expected_pdf_text_tmpl.format(
order=new_order,
expected_address=' Jane Doe\nNetherlands',
expected_currency_symbol='',
expected_date=new_order.paid_at.strftime('%B %-d, %Y'),
expected_email='jane.doe+billing@example.com',
expected_level='Silver',
expected_payment_method='Test payment method',
expected_total='10',
),
pdf_text,
)

View File

@ -14,40 +14,15 @@ from looper.tests.factories import (
create_customer_with_billing_address,
)
from blender_fund_main.tests.utils import extract_text_from_pdf
from blender_fund_main.tests.utils import (
extract_text_from_pdf,
expected_pdf_text_tmpl,
expected_pdf_text_bank_details,
)
production_storage = settings.STATICFILES_STORAGE
expected_text_tmpl = '''Stichting Blender Foundation
Buikslotermeerplein 161
1025 ET Amsterdam, the Netherlands
Tax number NL 8111.66.223
Blender Development Fund Donation Receipt
Receipt ID
{order.pk}
Payment received on
July 4, 2024
Membership level
Bronze
Billing Address
Address
{expected_address}
E-mail
billing@example.com
Payment Information
Amount paid
{expected_currency_symbol} {expected_total}
Payment method
{expected_payment_method}
'''
expected_text_bank_details = ''' Bank details
Bank: ING Bank, P/O Box 1800, Amsterdam, the Netherlands
BIC/Swift code: INGB NL 2A (international code)
IBAN: NL45 INGB 0009356121 (for euro accounts)
Account #: 93 56 121
'''
# This test needs to use the static file storage we also use in production,
# regardless of what the superclass overrides. There was an issue finding
@ -175,7 +150,7 @@ class TestReceiptPDFContent(AbstractLooperTestCase):
pdf_text = extract_text_from_pdf(response)
self.assertEqual(
pdf_text,
expected_text_tmpl.format(
expected_pdf_text_tmpl.format(
order=self.paid_order,
expected_address=(
' Main st. 123\n'
@ -185,6 +160,9 @@ class TestReceiptPDFContent(AbstractLooperTestCase):
'Netherlands'
),
expected_currency_symbol='',
expected_date=self.paid_order.paid_at.strftime('%B %-d, %Y'),
expected_email='billing@example.com',
expected_level='Bronze',
expected_payment_method='PayPal account test@example.com',
expected_total='9.90',
),
@ -212,10 +190,13 @@ class TestReceiptPDFContent(AbstractLooperTestCase):
pdf_text = extract_text_from_pdf(response)
self.assertEqual(
pdf_text,
expected_text_tmpl.format(
expected_pdf_text_tmpl.format(
order=order,
expected_address=' -',
expected_currency_symbol='',
expected_date=order.paid_at.strftime('%B %-d, %Y'),
expected_email='billing@example.com',
expected_level='Bronze',
expected_payment_method='PayPal account test@example.com',
expected_total='9.90',
),
@ -246,13 +227,16 @@ class TestReceiptPDFContent(AbstractLooperTestCase):
pdf_text = extract_text_from_pdf(response)
self.assertEqual(
pdf_text,
expected_text_tmpl.format(
expected_pdf_text_tmpl.format(
order=order,
expected_address=' -',
expected_currency_symbol='',
expected_date=order.paid_at.strftime('%B %-d, %Y'),
expected_email='billing@example.com',
expected_level='Bronze',
expected_payment_method='Bank Transfer',
expected_total='9.90',
) + expected_text_bank_details,
) + expected_pdf_text_bank_details,
pdf_text,
)
@ -278,10 +262,13 @@ class TestReceiptPDFContent(AbstractLooperTestCase):
pdf_text = extract_text_from_pdf(response)
self.assertEqual(
pdf_text,
expected_text_tmpl.format(
expected_pdf_text_tmpl.format(
order=order,
expected_address=' -',
expected_currency_symbol='$',
expected_date=order.paid_at.strftime('%B %-d, %Y'),
expected_email='billing@example.com',
expected_level='Bronze',
expected_payment_method='PayPal account test@example.com',
expected_total='14.80',
),

View File

@ -2,9 +2,42 @@ from io import BytesIO
from pypdf import PdfReader
expected_pdf_text_tmpl = '''Stichting Blender Foundation
Buikslotermeerplein 161
1025 ET Amsterdam, the Netherlands
Tax number NL 8111.66.223
Blender Development Fund Donation Receipt
Receipt ID
{order.pk}
Payment received on
{expected_date}
Membership level
{expected_level}
Billing Address
Address
{expected_address}
E-mail
{expected_email}
Payment Information
Amount paid
{expected_currency_symbol} {expected_total}
Payment method
{expected_payment_method}
'''
expected_pdf_text_bank_details = ''' Bank details
Bank: ING Bank, P/O Box 1800, Amsterdam, the Netherlands
BIC/Swift code: INGB NL 2A (international code)
IBAN: NL45 INGB 0009356121 (for euro accounts)
Account #: 93 56 121
'''
def extract_text_from_pdf(response):
pdf = PdfReader(BytesIO(response.content))
def extract_text_from_pdf_bytes(content: bytes):
pdf = PdfReader(BytesIO(content))
assert 1 == len(pdf.pages)
pdf_page = pdf.pages[0]
return pdf_page.extract_text()
def extract_text_from_pdf(response):
return extract_text_from_pdf_bytes(response.content)