├── MANIFEST.in ├── requirements.txt ├── assets └── example.png ├── thaiqrpayment ├── assets │ ├── logo.png │ └── template.png └── __init__.py ├── README.md ├── setup.py ├── LICENSE └── .gitignore /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include thaiqrpayment/assets/*.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | qrcode 2 | pillow 3 | crc16 4 | -------------------------------------------------------------------------------- /assets/example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittinan/thai-qr-payment/HEAD/assets/example.png -------------------------------------------------------------------------------- /thaiqrpayment/assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittinan/thai-qr-payment/HEAD/thaiqrpayment/assets/logo.png -------------------------------------------------------------------------------- /thaiqrpayment/assets/template.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kittinan/thai-qr-payment/HEAD/thaiqrpayment/assets/template.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # thai-qr-payment 2 | Generate QR Code image in the format of `Thai QR Payment` 3 | 4 | - [KBANK](https://apiportal.kasikornbank.com/product/public/LandingPage/QR%20Payment/Introduction/3) 5 | 6 | 7 | ## Installation 8 | 9 | available on pip https://pypi.org/project/thaiqrpayment/ 10 | 11 | ```bash 12 | pip install thaiqrpayment 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```python 18 | import thaiqrpayment 19 | 20 | # Your code from Bank 21 | code = "01" * 100 22 | 23 | # Save to image file 24 | thaiqrpayment.save(code, "/tmp/qr.png") 25 | 26 | # base64 format 27 | base64_str = thaiqrpayment.to_base64(code) 28 | 29 | # PIL image 30 | pil_image = thaiqrpayment.generate(code) 31 | 32 | ``` 33 | 34 | ## Example Image 35 | 36 | ![Thai QR Payment example](https://github.com/kittinan/thai-qr-payment/raw/main/assets/example.png?raw=true) -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | import setuptools 2 | 3 | import thaiqrpayment 4 | 5 | with open("README.md", "r") as fh: 6 | long_description = fh.read() 7 | 8 | setuptools.setup( 9 | name="thaiqrpayment", 10 | version=thaiqrpayment.__VERSION__, 11 | author="Kittinan Srithaworn", 12 | description="Thai QR Payment Generator", 13 | long_description=long_description, 14 | long_description_content_type="text/markdown", 15 | license="MIT", 16 | install_requires=["qrcode", "pillow"], 17 | keywords="Thai QR Payment Generator", 18 | url="https://github.com/kittinan/thai-qr-payment", 19 | packages=setuptools.find_packages(), 20 | include_package_data=True, 21 | classifiers=[ 22 | "Programming Language :: Python :: 3", 23 | "License :: OSI Approved :: MIT License", 24 | "Operating System :: OS Independent", 25 | ], 26 | project_urls={ 27 | "Bug Reports": "https://github.com/kittinan/thai-qr-payment/issues", 28 | "Source": "https://github.com/kittinan/thai-qr-payment", 29 | }, 30 | python_requires=">=3.6", 31 | ) 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Kittinan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | 131 | venv -------------------------------------------------------------------------------- /thaiqrpayment/__init__.py: -------------------------------------------------------------------------------- 1 | from io import BytesIO 2 | from PIL import Image 3 | 4 | import base64 5 | import qrcode 6 | import os 7 | import re 8 | from decimal import Decimal 9 | import crc16 10 | import math 11 | 12 | __VERSION__ = "0.1.0" 13 | 14 | SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__)) 15 | 16 | ID_PAYLOAD_FORMAT = "00" 17 | ID_POI_METHOD = "01" 18 | ID_MERCHANT_INFORMATION_BOT = "29" 19 | ID_TRANSACTION_CURRENCY = "53" 20 | ID_TRANSACTION_AMOUNT = "54" 21 | ID_COUNTRY_CODE = "58" 22 | ID_CRC = "63" 23 | 24 | PAYLOAD_FORMAT_EMV_QRCPS_MERCHANT_PRESENTED_MODE = "01" 25 | POI_METHOD_STATIC = "11" 26 | POI_METHOD_DYNAMIC = "12" 27 | MERCHANT_INFORMATION_TEMPLATE_ID_GUID = "00" 28 | BOT_ID_MERCHANT_PHONE_NUMBER = "01" 29 | BOT_ID_MERCHANT_TAX_ID = "02" 30 | BOT_ID_MERCHANT_EWALLET_ID = "03" 31 | GUID_PROMPTPAY = "A000000677010111" 32 | TRANSACTION_CURRENCY_THB = "764" 33 | COUNTRY_CODE_TH = "TH" 34 | 35 | 36 | def generate(code): 37 | 38 | # TODO: mode: color or black-white 39 | 40 | qr = qrcode.QRCode( 41 | version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=1 42 | ) 43 | qr.add_data(code) 44 | qr.make(fit=True) 45 | 46 | qr_img = qr.make_image(fill_color="black", back_color="white") 47 | qr_img = qr_img.convert("RGB") 48 | 49 | # Add Logo 50 | logo_img = Image.open("{}/assets/logo.png".format(SCRIPT_PATH)) 51 | template_img = Image.open("{}/assets/template.png".format(SCRIPT_PATH)) 52 | 53 | # Logo image area should not more than 5% of qr code area 54 | qr_image_area = qr_img.size[0] * qr_img.size[1] 55 | logo_img_area = logo_img.size[0] * logo_img.size[1] 56 | pct_logo_area = math.ceil(logo_img_area / qr_image_area) 57 | 58 | if pct_logo_area > 0.05: 59 | # Resize logo 60 | ratio = (qr_image_area * 0.05) / logo_img_area 61 | logo_img = logo_img.resize( 62 | ( 63 | round(logo_img.size[0] * ratio), 64 | round(logo_img.size[1] * ratio), 65 | ) 66 | ) 67 | 68 | # Center logo image 69 | pos = ((qr_img.size[0] - logo_img.size[0]) // 2, (qr_img.size[1] - logo_img.size[1]) // 2) 70 | qr_img.paste(logo_img, pos, mask=logo_img.split()[3]) 71 | 72 | # Resize for template 73 | qr_img = qr_img.resize((750, 750)) 74 | 75 | # paste qr image to template 76 | pos = (125, 407) 77 | template_img.paste(qr_img, pos) 78 | 79 | thaiqr_img = template_img.convert("RGB") 80 | return thaiqr_img 81 | 82 | 83 | def save(code, path): 84 | 85 | img = generate(code) 86 | img.save(path) 87 | 88 | 89 | def to_base64(code, include_uri=False): 90 | 91 | img = generate(code) 92 | 93 | buffered = BytesIO() 94 | img.save(buffered, format="png") 95 | img_str = base64.b64encode(buffered.getvalue()).decode() 96 | 97 | if include_uri: 98 | return "data:image/png;base64," + img_str 99 | 100 | return img_str 101 | 102 | 103 | def generate_code_from_mobile(number, amount): 104 | 105 | sanitized_number = sanitize_input(number) 106 | pp_type = ( 107 | BOT_ID_MERCHANT_EWALLET_ID 108 | if len(sanitized_number) >= 15 109 | else BOT_ID_MERCHANT_TAX_ID 110 | if len(sanitized_number) >= 13 111 | else BOT_ID_MERCHANT_PHONE_NUMBER 112 | ) 113 | 114 | pp_payload = generate_txt(ID_PAYLOAD_FORMAT, PAYLOAD_FORMAT_EMV_QRCPS_MERCHANT_PRESENTED_MODE) 115 | pp_amount_type = generate_txt( 116 | ID_POI_METHOD, POI_METHOD_DYNAMIC if amount else POI_METHOD_STATIC 117 | ) 118 | 119 | pp_merchant_info = generate_txt( 120 | ID_MERCHANT_INFORMATION_BOT, 121 | generate_txt(MERCHANT_INFORMATION_TEMPLATE_ID_GUID, GUID_PROMPTPAY) 122 | + generate_txt(pp_type, format_input(sanitized_number)), 123 | ) 124 | 125 | pp_country_code = generate_txt(ID_COUNTRY_CODE, COUNTRY_CODE_TH) 126 | pp_currency = generate_txt(ID_TRANSACTION_CURRENCY, TRANSACTION_CURRENCY_THB) 127 | pp_decimal_value = (amount if is_positive_decimal(amount) else 0) and generate_txt( 128 | ID_TRANSACTION_AMOUNT, format_amount(amount) 129 | ) 130 | 131 | raw_data = ( 132 | pp_payload 133 | + pp_amount_type 134 | + pp_merchant_info 135 | + pp_country_code 136 | + pp_currency 137 | + pp_decimal_value 138 | + ID_CRC 139 | + "04" 140 | ) 141 | 142 | return raw_data + str.upper( 143 | hex(crc16.crc16xmodem(raw_data.encode("ascii"), 0xFFFF)).replace("0x", "") 144 | ) 145 | 146 | 147 | def sanitize_input(input): 148 | return re.sub(r"(\D.*?)", "", input) 149 | 150 | 151 | def generate_txt(id, value): 152 | return id + str(len(value)).zfill(2) + value 153 | 154 | 155 | def format_input(id): 156 | numbers = sanitize_input(id) 157 | if len(numbers) >= 13: 158 | return numbers 159 | return (re.sub(r"^0", "66", numbers)).zfill(13) 160 | 161 | 162 | def format_amount(amount): 163 | TWOPLACES = Decimal(10) ** -2 164 | return str(Decimal(amount).quantize(TWOPLACES)) 165 | 166 | 167 | def is_positive_decimal(n): 168 | try: 169 | a = float(n) 170 | except ValueError: 171 | return False 172 | else: 173 | return True if a > 0 else False 174 | --------------------------------------------------------------------------------