__init__.py
7 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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import json
import base64
import logging
import hashlib
import requests
from datetime import datetime
from opensipkd.base import get_settings, is_devel, get_host
from opensipkd.base.models import flush_row
from pyramid.httpexceptions import HTTPFound
from ..vendor import VendorClass, sha256
from urllib.parse import urlparse, urlencode
log = logging.getLogger(__name__)
class Vendor(VendorClass): # VendorClass
def __init__(self, vendor_produk, invoice_det, **kwargs):
VendorClass.__init__(self, vendor_produk, invoice_det=invoice_det, **kwargs)
settings = get_settings()
self.v_produk_kd = vendor_produk.kode
# self.bank_cd, self.pay_method = self.v_produk_kd.split('-')
self.url = 'omnipay_va_url' in settings and settings['omnipay_va_url'] or None
self.merchantid = 'omnipay_merchant_id' in settings and settings['omnipay_merchant_id'] or ''
self.password = 'omnipay_password' in settings and settings['omnipay_password'] or ''
self.callback_url = "{}/api/vendor/omnipay/callback".format(get_host())
self.notify_url = "{}/api/vendor/omnipay/notify".format(get_host())
#total yang harus dibayarkan = fee vendor+fee agratek + tagihan VA
self.amt = vendor_produk.harga + float(invoice_det.amt_sell)
self.amt = int(self.amt)
self.orderid = invoice_det.cust_inv_no
self.verify_key = 'omnipay_verify_key' in settings and settings['omnipay_verify_key'] or ''
vcode = hashlib.md5((str(self.amt) + self.merchantid + self.orderid + self.verify_key).encode()).hexdigest()
print('self.amt >>')
print(self.amt)
print('self.merchantid >>')
print(self.merchantid)
print('self.orderid >>')
print(self.orderid)
print('self.verify_key >>')
print(self.verify_key)
print('vcode >>')
print(vcode)
self.vcode = vcode
# customer / agratek_merchant
customer = self.invoice_det.customer
if customer:
self.cust_kd = customer.kode
self.cust_inv_no = self.invoice_det.cust_inv_no
def _get_minute_duration(self, time_stamp):
datetime_object = datetime.strptime(time_stamp, '%Y%m%d%H%M%S')
print('_get_minute_duration time_stamp >>')
print(time_stamp)
print('_get_minute_duration datetime_object >>')
print(datetime_object)
current_datetime = datetime.now().strftime('%Y%m%d%H%M%S')
current_datetime = datetime.strptime(current_datetime, '%Y%m%d%H%M%S')
print('_get_minute_duration current_datetime >>')
print(current_datetime)
delta_minute = datetime_object - current_datetime
print('_get_minute_duration delta_minute >>')
print(delta_minute)
minute_day = delta_minute.days * 1440
minute_hour = delta_minute.seconds // 60
return minute_day + minute_hour
def _send_data_json(self, data, apiUrl):
requestData = data
host = urlparse(apiUrl)
hostUrl = host.netloc
hostPath = host.path
headers = {"Content-type": "application/json"}
resp = requests.post(apiUrl, headers=headers, data=data)
return resp.text
def _transform_omnipay_response(self, omnipay_response):
plain_omni = omnipay_response
try:
omni_resp = json.loads(plain_omni)
self.response = omni_resp
except Exception as e:
omni_resp = {}
self.response = plain_omni
log.info("Omnipay RESPONSE: {}".format(json.dumps(self.response)))
response_dict = {}
time_stamp = datetime.now().strftime('%Y%m%d%H%M%S')
key = sha256("{}{}".format(self.cust_inv_no, time_stamp))
tx_id = "{}{}".format(self.invoice_det.customer.users.user_name, key)
response_dict["tx_id"] = tx_id
response_dict['invoice_no'] = self.invoice_det.cust_inv_no
response_dict['description'] = self.invoice_det.description
print('_transform_omnipay_response >> omni_resp')
print(omni_resp)
if 'va' in omni_resp:
response_dict['code'] = 0
response_dict['message'] = 'SUCCESS'
datetime_trans = datetime.strptime(omni_resp['date'], '%Y-%m-%d %H:%M:%S')
response_dict['trans_date'] = datetime_trans.strftime('%Y%m%d')
response_dict['trans_time'] = datetime_trans.strftime('%H%M%S')
va_dict = {}
va_dict['vacct_no'] = omni_resp['va']
datetime_due = datetime.strptime(omni_resp['due_date'], '%Y-%m-%d %H:%M:%S')
va_dict['valid_date'] = datetime_due.strftime('%Y%m%d')
va_dict['valid_time'] = datetime_due.strftime('%H%M%S')
response_dict['va'] = va_dict
else:
response_dict['code'] = 999
response_dict['message'] = plain_omni
return response_dict
def register(self):
json_request = {}
json_request['returnurl'] = self.notify_url
json_request['merchantid'] = self.merchantid
json_request['orderid'] = self.orderid
json_request['amount'] = self.amt
json_request['bill_name'] = self.invoice_det.inv_cust_nm
json_request['bill_email'] = self.invoice_det.inv_cust_email
json_request['bill_mobile'] = self.invoice_det.inv_cust_phone
json_request['bill_desc'] = 'Pembayaran via Virtual Account dari Tuan/Nyonya : ' + json_request['bill_name']
json_request['expiry_minute'] = self._get_minute_duration(self.invoice_det.inv_valid_date + self.invoice_det.inv_valid_time)
json_request['vcode'] = self.vcode
json_request['password'] = self.password
self.request = json_request
log.info("Omnipay REQUEST: {}".format(json.dumps(self.request)))
# send request to omnipay
result_data = self._send_data_json(json.dumps(self.request), self.url)
self.result = self._transform_omnipay_response(result_data)
log.info("Omnipay RESULT: {}".format(json.dumps(self.result)))
self.save_log(typ="inquiry")
return dict(data=self.result)
def notify(self, data):
log.info("Omnipay NOTIFY: {}".format(data))
print('omnipay notify >>>>>>>>>>>>>>>>>')
print(data)
# amount = self.invoice_det.amt_sell
# vend_inv_no = self.invoice_det.vend_inv_no
# signature = sha256("{mid}{vend_inv_no}{amount}{key}" \
# .format(mid=self.mid, key=self.key, amount=str(amount),
# vend_inv_no=vend_inv_no))
# # print('>>>>', signature)
# if merchant_token != signature:
# return
# status = int(data['status'])
# if status == 0:
# # pembayaran sukses
# self.status = 1
# elif status == 1:
# # pembayaran gagal / reversal
# self.status = 2
# self.response = data
# self.result = get_result(self.response, self.pay_method)
# self.save_log("notify")
return self.result