1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# pip install requests qrcode
import sys
from configparser import ConfigParser
from argparse import ArgumentParser
from datetime import datetime
import hmac
import hashlib
import base64
import requests
import qrcode
def get_headers(userid: str, password: str, timestamp: str) -> dict:
msg = '&'.join([str(userid), timestamp])
msg = msg.encode('utf-8')
password = password.encode('utf-8')
signature = hmac.new(password, msg=msg, digestmod=hashlib.sha256)
signature = signature.digest()
s = base64.encodebytes(signature)
s = s.decode()
s = s.replace('\n', '')
return dict(userid=userid, signature=s, key=timestamp)
image_file = 'qris.png'
help_image = f'default {image_file}'
pars = ArgumentParser()
pars.add_argument('conf')
pars.add_argument('--amount', type=int, required=True)
pars.add_argument('--image-file', default=image_file, help=help_image)
option = pars.parse_args(sys.argv[1:])
conf = ConfigParser()
conf.read(option.conf)
cf = dict(conf.items('main'))
url = cf.get('url', 'https://devel.agratek.id/api/merchant')
dt = datetime.now()
seconds = int(dt.timestamp())
seconds = str(seconds)
headers = get_headers(cf['user_id'], cf['key'], seconds)
data = {
"jsonrpc": "2.0",
"id": 1,
"method": "register",
"params": {
"data": {
"time_stamp": seconds,
"denom": "QRIS",
"amount": option.amount,
"invoice_no": "-",
"description": "-",
"fee": 0,
"vat": 0,
"req_dt": dt.strftime('%Y%m%d'),
"req_tm": dt.strftime('%H%M%S'),
"customer": {
"name": "-",
"phone": "-",
"email": "-",
"city": "-",
"state": "-",
"post_code": "-",
"country": "Indonesia",
"ip": "-",
},
"deliver_to": {
"name": "-",
"phone": "-",
"address": "-",
"city": "-",
"state": "-",
"post_code": "-",
"country": "Indonesia"
},
}
}
}
r = requests.post(url, headers=headers, json=data)
if r.status_code != 200:
print(r.text)
print(f'HTTP Error {r.status_code}')
sys.exit()
result = r.json()
if 'result' not in result:
print(result)
print('Tidak ada key result')
sys.exit()
result = result['result']
if result['code'] != 0:
print(result)
print(f'Ada kesalahan, code = {result["code"]}')
sys.exit()
data = result['qris']['qris']
print(data)
qr = qrcode.QRCode(version=1, box_size=10, border=5)
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color='black', back_color='white')
img.save(option.image_file)
print(f'Sudah disimpan di {option.image_file}')