| 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,\
|
| 7127 |
rajveer |
22 |
update_amount_in_wallet, update_recharge_transaction_status
|
|
|
23 |
from shop2020.model.v1.order.impl import DataService, RechargeTransaction
|
| 6235 |
rajveer |
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="")
|
| 6452 |
rajveer |
40 |
parser.add_option("-a", "--authorized", dest="authorized",
|
| 6451 |
rajveer |
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)
|
| 7127 |
rajveer |
70 |
if order:
|
|
|
71 |
amount = order.totalAmount
|
|
|
72 |
isStoreOrder = False
|
|
|
73 |
else:
|
|
|
74 |
order = RechargeTransaction.get_by(spiceTID = key)
|
|
|
75 |
isStoreOrder = True
|
|
|
76 |
amount = order.amount
|
| 6235 |
rajveer |
77 |
if order.status == RechargeOrderStatus.RECHARGE_FAILED_REFUNDED:
|
|
|
78 |
print "Refund is already processed."
|
|
|
79 |
continue
|
| 7075 |
rajveer |
80 |
if order.status not in (RechargeOrderStatus.RECHARGE_SUCCESSFUL, RechargeOrderStatus.PAYMENT_SUCCESSFUL):
|
|
|
81 |
print "Recharge/Payment is not successful. There is something wrong."
|
| 6235 |
rajveer |
82 |
continue
|
| 7127 |
rajveer |
83 |
if amount != refundAmount:
|
| 6235 |
rajveer |
84 |
print "Refund amount is not same as transaction amount"
|
|
|
85 |
continue
|
| 7127 |
rajveer |
86 |
if isStoreOrder:
|
|
|
87 |
update_recharge_transaction_status(order.id, RechargeOrderStatus.RECHARGE_FAILED_REFUNDED, refundDate)
|
|
|
88 |
else:
|
|
|
89 |
update_recharge_order_status(order.id, RechargeOrderStatus.RECHARGE_FAILED_REFUNDED, refundDate)
|
| 6235 |
rajveer |
90 |
|
|
|
91 |
def processUnknownTransactions():
|
|
|
92 |
orders = get_recharge_orders_for_status(RechargeOrderStatus.PAYMENT_SUCCESSFUL)
|
| 6219 |
rajveer |
93 |
for order in orders:
|
| 6276 |
rajveer |
94 |
try:
|
|
|
95 |
if order.creationTimestamp + datetime.timedelta(minutes=10) < datetime.datetime.now():
|
|
|
96 |
status, description = checkTransactionStatus('', str(order.id))
|
|
|
97 |
print status, description
|
|
|
98 |
if status:
|
|
|
99 |
update_recharge_order_status(order.id, RechargeOrderStatus.RECHARGE_SUCCESSFUL)
|
|
|
100 |
else:
|
|
|
101 |
update_recharge_order_status(order.id, RechargeOrderStatus.RECHARGE_FAILED)
|
|
|
102 |
except:
|
|
|
103 |
print "Do Nothing"
|
| 6451 |
rajveer |
104 |
|
|
|
105 |
def processAuthorizedTransactions(txn_id):
|
|
|
106 |
update_recharge_order_status(txn_id, RechargeOrderStatus.PAYMENT_SUCCESSFUL)
|
| 6235 |
rajveer |
107 |
|
| 6147 |
rajveer |
108 |
if __name__ == '__main__':
|
|
|
109 |
main()
|