
In the previous tutorial, we created a sales invoice using Python and ReportLab. The invoice included details such as customer information, line items, totals, and formatting. In this tutorial, we will enhance the invoice by adding a Stripe payment button directly inside the PDF. This functionality means that when a customer opens the invoice, they can click the "Pay Now" button, which redirects them to a Stripe checkout page to complete the payment.
Prerequisite:
This tutorial is part of the Automated Sales Invoice Series.
📚 View the Complete Automated Sales Invoice Series
Preliminary
Before I begin, please activate the virtual environment and install the required dependencies.
python -m venv venv
venv\Scripts\activate
pip install reportlab# Import module from the ReportLab
from reportlab.lib.units import inch
from reportlab.lib.colors import red
def stripe_payment(c):
# Stripe Checkout payment link from step 2 above
# This is the URL that customers will be redirected to when they click the button
stripe_url = "https://buy.stripe.com/test_28EeVdecq3ME7n92OvgMw05"
# ---------------------------------------------------------
# Button Position and Size
# ---------------------------------------------------------
# the x, y coordination, width and height
x = 2.8 * inch
y = 0.5 * inch
width = 2 * inch
height = 0.5 * inch
# ---------------------------------------------------------
# Draw Button Background
# ---------------------------------------------------------
# Define the rectangle area of the button
rect = (x, y, x + width, y + height)
# Set the fill colour of the button
c.setFillColor(red)
# ---------------------------------------------------------
# Add Button Text
# ---------------------------------------------------------
# Set the font style and size
c.setFont("Roboto-Bold", 14)
# Adjust text position slightly to centre it visually
offset_x = -69 # negative = left
offset_y = -76 # negative = down
# Calculate text position based on button centre
text_x = x + width/2 + offset_x
text_y = y + height/2 + offset_y
# Button label
text = "Pay Now"
# Draw the text centred at the calculated position
c.drawCentredString(text_x, text_y, text)
# ---------------------------------------------------------
# Add Clickable Stripe Link
# ---------------------------------------------------------
# When the user clicks the rectangle area, it opens the Stripe payment page
c.linkURL(
stripe_url,
rect,
relative=0,
thickness=2,
color=red
)
# Return the updated canvas object
return c
When it prompts a security warning, just click to allow the process to proceed.# import from the payment.py
from payment import stripe_payment
# Set the output PDF file path
my_path = 'sales_invoice/my_invoice.pdf'
# Create a new PDF canvas object
c = canvas.Canvas(my_path, pagesize=letter)
# Stripe payment button at the bottom
c = stripe_payment(c)
When I run the app, it will generate a my_invoice.pdf file in the sales invoice folder. Then, I click PDF to open the file.(A) Before payment
After I click the 'Pay Now' button, it will direct me to the Stripe payment link (the link in Step 2), as shown in the diagram below.
Before I proceed, I need to fill out my email address and then complete the payment details.
The card number for testing purposes is as follows:Once I have filled out the card details and clicked the subscribe button, the diagram below displays the payment status; the tick mark indicates the payment was successful.
To confirm the payment was actually received, I need to return to the Stripe platform again. When I click the left sidebar menu on transactions to view the full list of transactions, I notice the successful payment details are shown in the table below.
Final wrap-up.
About the Author
Kelvin Loh is a Python developer focused on Flask, desktop applications, and business automation solutions. He shares practical tutorials and real-world coding projects to help developers and small businesses build useful applications.







.png)
Comments
Post a Comment