QR Code Integration¶
Install: pip install "instapay-eg[qrcode]"
Function Reference¶
See the QR Codes user guide for usage examples.
instapay_eg.integrations.qrcode
¶
QR code generation integration for instapay-eg.
Generates QR codes for InstaPay payment links using the segno library.
Supports PNG, SVG, and base64-encoded output - covering every common use case
from saving to disk, embedding in web pages, and returning from JSON APIs.
Usage::
from instapay_eg.integrations.qrcode import qr_as_base64, save_qr
# For a JSON API response:
b64 = qr_as_base64("https://ipn.eg/S/alice/instapay/2DcFGv")
# For saving to disk:
save_qr("https://ipn.eg/S/alice/instapay/2DcFGv", "alice_payment.png")
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
generate_qr
¶
Generate a segno.QRCode object for link.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
link
|
str
|
A validated |
required |
error
|
str
|
QR error correction level. One of |
'm'
|
Returns:
| Name | Type | Description |
|---|---|---|
A |
QRCode
|
class: |
QRCode
|
or any other segno method on it directly. |
Example
qr = generate_qr("https://ipn.eg/S/alice/instapay/2DcFGv") qr.save("payment.png", scale=10)
Source code in src/instapay_eg/integrations/qrcode.py
save_qr
¶
save_qr(
link: str,
path: str | Path,
*,
scale: int = 10,
dark: str = "#1a1a2e",
light: str = "#ffffff",
file_format: str | None = None,
) -> None
Save a QR code image for link to path on disk.
The output format is inferred from the file extension of path (e.g.
payment.png → PNG, payment.svg → SVG) unless file_format is
given explicitly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
link
|
str
|
A validated |
required |
path
|
str | Path
|
Destination file path. Parent directories must already exist. |
required |
scale
|
int
|
Pixel size of each QR module. |
10
|
dark
|
str
|
Hex colour for the dark modules (default: deep navy |
'#1a1a2e'
|
light
|
str
|
Hex colour for the light modules (default: white |
'#ffffff'
|
file_format
|
str | None
|
Override the output format (e.g. |
None
|
Example
save_qr( ... "https://ipn.eg/S/alice/instapay/2DcFGv", ... "alice_qr.png", ... scale=12, ... dark="#005f5f", ... )
Source code in src/instapay_eg/integrations/qrcode.py
qr_as_bytes
¶
qr_as_bytes(
link: str,
*,
file_format: str = "png",
scale: int = 10,
dark: str = "#1a1a2e",
light: str = "#ffffff",
) -> bytes
Return the QR code image for link as raw bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
link
|
str
|
A validated |
required |
file_format
|
str
|
Image format. |
'png'
|
scale
|
int
|
Pixel size of each QR module. |
10
|
dark
|
str
|
Hex colour for the dark modules. |
'#1a1a2e'
|
light
|
str
|
Hex colour for the light modules. |
'#ffffff'
|
Returns:
| Type | Description |
|---|---|
bytes
|
Raw image bytes in the requested format. |
Example
png_bytes = qr_as_bytes("https://ipn.eg/S/alice/instapay/2DcFGv") with open("payment.png", "wb") as f: ... f.write(png_bytes)
Source code in src/instapay_eg/integrations/qrcode.py
qr_as_base64
¶
qr_as_base64(
link: str,
*,
file_format: str = "png",
scale: int = 10,
dark: str = "#1a1a2e",
light: str = "#ffffff",
) -> str
Return the QR code as a base64-encoded string, ready for a JSON API.
The returned string can be embedded directly in an HTML <img> tag or
returned from a REST endpoint without needing to write a file to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
link
|
str
|
A validated |
required |
file_format
|
str
|
Image format used for encoding. |
'png'
|
scale
|
int
|
Pixel size of each QR module. |
10
|
dark
|
str
|
Hex colour for the dark modules. |
'#1a1a2e'
|
light
|
str
|
Hex colour for the light modules. |
'#ffffff'
|
Returns:
| Type | Description |
|---|---|
str
|
A base64-encoded string of the QR code image (no |
Example
b64 = qr_as_base64("https://ipn.eg/S/alice/instapay/2DcFGv") html = f'
'
Source code in src/instapay_eg/integrations/qrcode.py
qr_as_svg_string
¶
qr_as_svg_string(
link: str,
*,
scale: int = 10,
dark: str = "#1a1a2e",
light: str = "#ffffff",
) -> str
Return the QR code as an inline SVG string.
The returned string can be injected directly into an HTML document without a separate HTTP request, and scales perfectly at any resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
link
|
str
|
A validated |
required |
scale
|
int
|
Size multiplier for the SVG viewport. |
10
|
dark
|
str
|
Hex colour for the dark modules. |
'#1a1a2e'
|
light
|
str
|
Hex colour for the light modules. |
'#ffffff'
|
Returns:
| Type | Description |
|---|---|
str
|
A complete |
Example
svg = qr_as_svg_string("https://ipn.eg/S/alice/instapay/2DcFGv") html_response = f"
{svg}"