va.py
1.67 KB
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
# Winpay Create Virtual Account
from base64 import b64encode
from datetime import timedelta
from logging import getLogger
import pytz
import requests
from .signature import (
generator,
time_to_str,
)
class HttpErr(Exception):
pass
def create_now(timezone='Asia/Jakarta') -> datetime:
tz = pytz.timezone(timezone)
return datetime.now(tz)
def create(
url: str, private_key: bytes, partner_id: str, customer_no: str,
va_name: str, amount: int, channel: str, trx_id: str, expired_days=1,
timezone='Asia/Jakarta'):
log = getLogger('create_va()')
timestamp = create_now(timezone)
timestamp_str = time_to_str(timestamp)
expired_timestamp = timestamp + timedelta(expired_days)
expired_str = time_to_str(expired_timestamp)
data = dict(
customerNo=customer_no,
virtualAccountName=va_name,
trxId=trx_id,
totalAmount=dict(value=amount, currency='IDR'),
virtualAccountTrxType='c',
expiredDate=expired_str,
additionalInfo=dict(channel=channel))
signature = generator(private_key, data, timestamp)
signature_b64 = b64encode(signature)
signature_b64 = signature_b64.decode('utf-8')
headers = {
'X-TIMESTAMP': timestamp_str,
'X-SIGNATURE': signature_b64,
'X-PARTNER-ID': partner_id,
'X-EXTERNAL-ID': trx_id,
'CHANNEL-ID': channel}
log.info(f'Request: Headers {headers}, Data {data}')
r = requests.post(url, headers=headers, json=data)
if r.status_code != 200:
log.error(f'HTTP {r.status_code}, {[r.text]}')
raise HttpErr(r.status_code, r.text)
d = r.json()
log.info(f'Response: {d}')
return d