| 6147 |
rajveer |
1 |
#!/usr/bin/python
|
|
|
2 |
'''
|
|
|
3 |
It is used to process transactions for which the payment
|
|
|
4 |
was received but the order was not processed.
|
|
|
5 |
|
|
|
6 |
@author: Rajveer
|
|
|
7 |
'''
|
|
|
8 |
import optparse
|
|
|
9 |
import sys
|
| 6219 |
rajveer |
10 |
import datetime
|
| 6235 |
rajveer |
11 |
from elixir import *
|
| 6147 |
rajveer |
12 |
|
|
|
13 |
|
|
|
14 |
|
| 6219 |
rajveer |
15 |
|
| 6147 |
rajveer |
16 |
if __name__ == '__main__' and __package__ is None:
|
|
|
17 |
import os
|
|
|
18 |
sys.path.insert(0, os.getcwd())
|
|
|
19 |
|
|
|
20 |
from shop2020.thriftpy.model.v1.order.ttypes import RechargeOrderStatus
|
| 6254 |
rajveer |
21 |
from shop2020.model.v1.order.impl.DataAccessors import get_recharge_orders_for_status, update_recharge_order_status,\
|
|
|
22 |
update_amount_in_wallet
|
| 6235 |
rajveer |
23 |
from shop2020.model.v1.order.impl import DataService
|
|
|
24 |
from shop2020.model.v1.order.impl.model.RechargeOrder import RechargeOrder
|
|
|
25 |
from shop2020.model.v1.order.impl.RechargeService import checkTransactionStatus, getRefunds
|
| 6147 |
rajveer |
26 |
|
|
|
27 |
|
|
|
28 |
def main():
|
|
|
29 |
parser = optparse.OptionParser()
|
|
|
30 |
parser.add_option("-H", "--host", dest="hostname",
|
|
|
31 |
default="localhost",
|
|
|
32 |
type="string", help="The HOST where the DB server is running",
|
|
|
33 |
metavar="HOST")
|
| 6235 |
rajveer |
34 |
parser.add_option("-r", "--refund", dest="refund",
|
|
|
35 |
action="store_true",
|
|
|
36 |
help="")
|
|
|
37 |
parser.add_option("-u", "--unknown", dest="unknown",
|
|
|
38 |
action="store_true",
|
|
|
39 |
help="")
|
| 6451 |
rajveer |
40 |
parser.add_option("-u", "--authorized", dest="authorized",
|
|
|
41 |
action="store_true",
|
|
|
42 |
help="")
|
|
|
43 |
parser.add_option("-t", "--txn-id", dest="txn_id",
|
|
|
44 |
type="int",
|
|
|
45 |
help="mark the transaction(recharge order id) TXN_ID as successful",
|
|
|
46 |
metavar="TXN_ID")
|
| 6235 |
rajveer |
47 |
|
| 6147 |
rajveer |
48 |
(options, args) = parser.parse_args()
|
|
|
49 |
if len(args) != 0:
|
|
|
50 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
| 6254 |
rajveer |
51 |
DataService.initialize(db_hostname=options.hostname, echoOn=True)
|
|
|
52 |
|
| 6235 |
rajveer |
53 |
if options.refund:
|
|
|
54 |
processRefunds()
|
|
|
55 |
if options.unknown:
|
|
|
56 |
processUnknownTransactions()
|
| 6451 |
rajveer |
57 |
if options.authorized:
|
|
|
58 |
processAuthorizedTransactions(options.txn_id)
|
| 6235 |
rajveer |
59 |
|
|
|
60 |
def processRefunds():
|
|
|
61 |
todate = datetime.datetime.now()
|
|
|
62 |
for i in range(15):
|
|
|
63 |
orderDate = todate + datetime.timedelta(days= -i)
|
|
|
64 |
refunds = getRefunds(orderDate)
|
|
|
65 |
for key in refunds.keys():
|
|
|
66 |
refund = refunds.get(key)
|
|
|
67 |
refundAmount = refund[0]
|
|
|
68 |
refundDate = refund[1]
|
|
|
69 |
order = RechargeOrder.get_by(spiceTID = key)
|
|
|
70 |
if order.status == RechargeOrderStatus.RECHARGE_FAILED_REFUNDED:
|
|
|
71 |
print "Refund is already processed."
|
|
|
72 |
continue
|
|
|
73 |
if order.status != RechargeOrderStatus.RECHARGE_SUCCESSFUL:
|
|
|
74 |
print "Recharge is not successful. There is something wrong."
|
|
|
75 |
continue
|
|
|
76 |
if order.totalAmount != refundAmount:
|
|
|
77 |
print "Refund amount is not same as transaction amount"
|
|
|
78 |
continue
|
| 6279 |
rajveer |
79 |
update_recharge_order_status(order.id, RechargeOrderStatus.RECHARGE_FAILED_REFUNDED, refundDate)
|
| 6235 |
rajveer |
80 |
|
|
|
81 |
def processUnknownTransactions():
|
|
|
82 |
orders = get_recharge_orders_for_status(RechargeOrderStatus.PAYMENT_SUCCESSFUL)
|
| 6219 |
rajveer |
83 |
for order in orders:
|
| 6276 |
rajveer |
84 |
try:
|
|
|
85 |
if order.creationTimestamp + datetime.timedelta(minutes=10) < datetime.datetime.now():
|
|
|
86 |
status, description = checkTransactionStatus('', str(order.id))
|
|
|
87 |
print status, description
|
|
|
88 |
if status:
|
|
|
89 |
update_recharge_order_status(order.id, RechargeOrderStatus.RECHARGE_SUCCESSFUL)
|
|
|
90 |
else:
|
|
|
91 |
update_recharge_order_status(order.id, RechargeOrderStatus.RECHARGE_FAILED)
|
|
|
92 |
except:
|
|
|
93 |
print "Do Nothing"
|
| 6451 |
rajveer |
94 |
|
|
|
95 |
def processAuthorizedTransactions(txn_id):
|
|
|
96 |
update_recharge_order_status(txn_id, RechargeOrderStatus.PAYMENT_SUCCESSFUL)
|
| 6235 |
rajveer |
97 |
|
| 6147 |
rajveer |
98 |
if __name__ == '__main__':
|
|
|
99 |
main()
|