| 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="")
|
|
|
40 |
|
| 6147 |
rajveer |
41 |
(options, args) = parser.parse_args()
|
|
|
42 |
if len(args) != 0:
|
|
|
43 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
| 6254 |
rajveer |
44 |
DataService.initialize(db_hostname=options.hostname, echoOn=True)
|
|
|
45 |
|
| 6235 |
rajveer |
46 |
if options.refund:
|
|
|
47 |
processRefunds()
|
|
|
48 |
if options.unknown:
|
|
|
49 |
processUnknownTransactions()
|
|
|
50 |
|
|
|
51 |
def processRefunds():
|
|
|
52 |
todate = datetime.datetime.now()
|
|
|
53 |
for i in range(15):
|
|
|
54 |
orderDate = todate + datetime.timedelta(days= -i)
|
|
|
55 |
refunds = getRefunds(orderDate)
|
|
|
56 |
for key in refunds.keys():
|
|
|
57 |
refund = refunds.get(key)
|
|
|
58 |
refundAmount = refund[0]
|
|
|
59 |
refundDate = refund[1]
|
|
|
60 |
order = RechargeOrder.get_by(spiceTID = key)
|
|
|
61 |
if order.status == RechargeOrderStatus.RECHARGE_FAILED_REFUNDED:
|
|
|
62 |
print "Refund is already processed."
|
|
|
63 |
continue
|
|
|
64 |
if order.status != RechargeOrderStatus.RECHARGE_SUCCESSFUL:
|
|
|
65 |
print "Recharge is not successful. There is something wrong."
|
|
|
66 |
continue
|
|
|
67 |
if order.totalAmount != refundAmount:
|
|
|
68 |
print "Refund amount is not same as transaction amount"
|
|
|
69 |
continue
|
|
|
70 |
order.status = RechargeOrderStatus.RECHARGE_FAILED_REFUNDED
|
|
|
71 |
order.responseTimestamp = refundDate
|
|
|
72 |
session.commit()
|
| 6254 |
rajveer |
73 |
update_amount_in_wallet(order.userId, order.walletAmount, order.id)
|
|
|
74 |
update_amount_in_wallet(order.userId, order.totalAmount - order.walletAmount, order.id)
|
|
|
75 |
|
| 6235 |
rajveer |
76 |
|
|
|
77 |
def processUnknownTransactions():
|
|
|
78 |
orders = get_recharge_orders_for_status(RechargeOrderStatus.PAYMENT_SUCCESSFUL)
|
| 6219 |
rajveer |
79 |
for order in orders:
|
| 6276 |
rajveer |
80 |
try:
|
|
|
81 |
if order.creationTimestamp + datetime.timedelta(minutes=10) < datetime.datetime.now():
|
|
|
82 |
status, description = checkTransactionStatus('', str(order.id))
|
|
|
83 |
print status, description
|
|
|
84 |
if status:
|
|
|
85 |
update_recharge_order_status(order.id, RechargeOrderStatus.RECHARGE_SUCCESSFUL)
|
|
|
86 |
else:
|
|
|
87 |
update_recharge_order_status(order.id, RechargeOrderStatus.RECHARGE_FAILED)
|
|
|
88 |
except:
|
|
|
89 |
print "Do Nothing"
|
| 6235 |
rajveer |
90 |
|
| 6147 |
rajveer |
91 |
if __name__ == '__main__':
|
|
|
92 |
main()
|