-
Notifications
You must be signed in to change notification settings - Fork 1
/
Application.py
executable file
·212 lines (196 loc) · 10.3 KB
/
Application.py
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# -*- coding: utf-8 -*-
import os
import json
from .Request import Request
from .Response import Response
from .PaycomException import PaycomException
from .Merchant import Merchant
from .Order import Order
from .Transaction import Transaction
from .Format import Format
from .conf import config
from django.utils import timezone
# Import your app's Order model
from basic.models import Order as OrderModel
class Application:
method = None
request = None
response = None
merchant = None
json_content = None
formatter = Format()
def __init__(self, request):
if request.method == 'POST':
self.request = Request(request=request)
self.method = self.request.method
self.response = Response(request=self.request)
self.merchant = Merchant(conf=config, request=request)
else:
raise PaycomException(request_id=None, message='No post request detected', code=-32300)
def run(self):
if self.merchant.authorize():
try:
if self.method == 'CheckPerformTransaction':
self.check_perform_transaction()
elif self.method == 'CreateTransaction':
self.create_transaction()
elif self.method == 'PerformTransaction':
self.perform_transaction()
elif self.method == 'CancelTransaction':
self.cancel_transaction()
elif self.method == 'CheckTransaction':
self.check_transaction()
elif self.method == 'GetStatement':
self.get_statement()
elif self.method == 'ChangePassword':
self.change_password()
else:
self.json_content = self.response.error(code=PaycomException.ERROR_METHOD_NOT_FOUND,
message='Method not found', data=self.request.method)
except PaycomException:
self.json_content = self.response.error(
code=PaycomException.ERROR_INVALID_JSON_RPC_OBJECT,
message='Invalid RPC Object',
data='rpc data'
)
else:
self.json_content = self.response.error(
code=PaycomException.ERROR_INSUFFICIENT_PRIVILEGE,
message='Invalid login/password',
data='account'
)
return self.json_content
def check_perform_transaction(self):
order = Order(request_id=self.request.id)
if order.validate(account_params=self.request.params):
"""
Comment: Order successfully validated
"""
data = {
"result": order.response_message
}
self.json_content = json.dumps(data)
else:
self.json_content = self.response.send(result=None, error=order.response_message)
def create_transaction(self):
transaction = Transaction(params=self.request.params)
if transaction.exist():
"""
Comment: If transaction already exists,
then check transaction state is available and time is not expired
"""
if transaction.check_transaction_state():
if transaction.transaction_is_expired():
transaction.cancel_transaction(reason=Transaction.REASON_CANCELLED_BY_TIMEOUT)
"""
Comment: If transaction's time is expired,
cancel transaction and return error message code: -31008
"""
self.json_content = self.response.error(code=Transaction.TRANSACTION_CAN_NOT_PERFORM,
message='Transaction is expired.',
data='timeout')
else:
"""
Comment: If transaction's time is not expired,
return transaction details
"""
self.json_content = transaction.return_transaction_details()
else:
"""
Comment: If transaction not exist, return error message code: -31008
"""
self.json_content = self.response.error(code=Transaction.TRANSACTION_CAN_NOT_PERFORM,
message='Transaction found, but is not active.',
data='timeout')
else:
"""
Comment: If transaction not exist, try to create new one
"""
order = Order(request_id=self.request.id)
if order.validate(account_params=self.request.params) is False:
self.json_content = self.response.send(result=None, error=order.response_message)
else:
now_in_milliseconds = self.formatter.millisecond_timestamp_from_utc_to_time_zone(utc_datetime=timezone.now())
if (now_in_milliseconds - self.request.params['time']) > Transaction.TIMEOUT:
self.json_content = self.response.error(code=PaycomException.ERROR_INVALID_ACCOUNT,
message='Transaction is expired.',
data='timeout')
else:
transaction.save_transaction()
self.json_content = transaction.return_transaction_details()
def perform_transaction(self):
transaction = Transaction(params=self.request.params)
if transaction.exist():
if transaction.check_transaction_state(state=Transaction.STATE_CREATED):
if transaction.transaction_is_expired():
transaction.cancel_transaction(reason=Transaction.REASON_CANCELLED_BY_TIMEOUT)
"""
Comment: If transaction's time is expired,
cancel transaction and return error message code: -31008
"""
self.json_content = self.response.error(code=Transaction.TRANSACTION_CAN_NOT_PERFORM,
message='Transaction is expired.',
data='time')
else:
order = OrderModel.objects.get(pk=transaction.transaction.order.pk)
order.state = Order.STATE_PAY_ACCEPTED
order.save()
transaction.complete_transaction()
self.json_content = transaction.return_transaction_details(field='perform_time')
elif transaction.check_transaction_state(state=Transaction.STATE_COMPLETED):
"""
Comment: Return transaction details if transaction was completed
"""
self.json_content = transaction.return_transaction_details(field='perform_time')
else:
self.json_content = self.response.error(code=Transaction.TRANSACTION_CAN_NOT_PERFORM,
message='Transaction state is not valid.',
data='state')
else:
self.json_content = self.response.error(code=Transaction.TRANSACTION_NOT_FOUND,
message='Transaction is not found',
data='id')
def cancel_transaction(self):
transaction = Transaction(params=self.request.params)
if transaction.exist():
order = OrderModel.objects.get(pk=transaction.transaction.order.pk)
if transaction.check_transaction_state(state=Transaction.STATE_CREATED):
transaction.cancel_transaction(reason=self.request.params['reason'])
order.state = Order.STATE_CANCELLED
order.save()
self.json_content = transaction.return_transaction_details(field='cancel_time')
elif transaction.check_transaction_state(state=Transaction.STATE_COMPLETED):
"""
Comment: Here is checked, whether Transaction can be cancelable ?
If order is shipped then transaction will not be cancelled,
otherwise will be cancelled and so is "Order" .
"""
if int(order.state) == Order.STATE_DELIVERED:
self.json_content = self.response.error(code=Transaction.TRANSACTION_CAN_NOT_CANCEL,
message='Transaction can not be cancelled.',
data='order.state')
else:
order.state = Order.STATE_CANCELLED
order.save()
transaction.cancel_transaction(reason=self.request.params['reason'],
state=Transaction.STATE_CANCELLED_AFTER_COMPLETE)
self.json_content = transaction.return_transaction_details(field='cancel_time')
else:
self.json_content = transaction.return_transaction_details(field='cancel_time')
else:
self.json_content = self.response.error(code=Transaction.TRANSACTION_NOT_FOUND,
message='Transaction is not found',
data='id')
def check_transaction(self):
transaction = Transaction(params=self.request.params)
if transaction.exist():
self.json_content = transaction.get_transaction_details()
else:
self.json_content = self.response.error(code=Transaction.TRANSACTION_NOT_FOUND,
message='Transaction is not found',
data='id')
def get_statement(self):
all_transactions = Transaction.get_statement(_from=self.request.params['from'], _to=self.request.params['to'])
self.json_content = all_transactions
def change_password(self):
pass