| 2133 |
chandransh |
1 |
#!/usr/bin/python
|
| 4090 |
chandransh |
2 |
'''
|
|
|
3 |
It is used to process transactions for which the payment
|
|
|
4 |
was received but the order was not submitted for processing.
|
|
|
5 |
A few known cases:
|
|
|
6 |
1. Missing transfer price for an item
|
|
|
7 |
2. Lack of AWB numbers
|
|
|
8 |
3. Amount mismatch
|
|
|
9 |
4. Payment was successful but the user was not redirected to
|
|
|
10 |
our website.
|
| 2133 |
chandransh |
11 |
|
| 4090 |
chandransh |
12 |
@author: Chandranshu
|
|
|
13 |
'''
|
| 2133 |
chandransh |
14 |
import optparse
|
|
|
15 |
import sys
|
|
|
16 |
|
| 2185 |
rajveer |
17 |
from thrift.Thrift import TException
|
| 2247 |
chandransh |
18 |
import traceback
|
| 5527 |
anupam.sin |
19 |
from shop2020.thriftpy.logistics.ttypes import PickUpType
|
| 7293 |
anupam.sin |
20 |
from shop2020.thriftpy.model.v1.order.ttypes import OrderType, OrderSource
|
| 2185 |
rajveer |
21 |
|
| 2133 |
chandransh |
22 |
if __name__ == '__main__' and __package__ is None:
|
|
|
23 |
import os
|
|
|
24 |
sys.path.insert(0, os.getcwd())
|
|
|
25 |
|
|
|
26 |
from shop2020.model.v1.order.impl import DataAccessors, DataService
|
|
|
27 |
from shop2020.thriftpy.model.v1.order.ttypes import TransactionStatus
|
| 2186 |
rajveer |
28 |
from shop2020.thriftpy.model.v1.user.ttypes import ShoppingCartException,\
|
|
|
29 |
PromotionException
|
|
|
30 |
from shop2020.clients.UserClient import UserClient
|
|
|
31 |
from shop2020.clients.PromotionClient import PromotionClient
|
| 2133 |
chandransh |
32 |
|
| 2185 |
rajveer |
33 |
def reset_cart(transaction, userServiceClient):
|
|
|
34 |
items = {}
|
|
|
35 |
for order in transaction.orders:
|
|
|
36 |
for line_item in order.lineitems:
|
|
|
37 |
item_id = line_item.item_id
|
| 2240 |
chandransh |
38 |
if items.has_key(item_id):
|
|
|
39 |
quantity = items[item_id] + line_item.quantity
|
|
|
40 |
else:
|
| 2185 |
rajveer |
41 |
quantity = line_item.quantity
|
|
|
42 |
items[item_id] = quantity
|
|
|
43 |
|
|
|
44 |
print items
|
|
|
45 |
|
|
|
46 |
try:
|
| 2240 |
chandransh |
47 |
userServiceClient.get_client().resetCart(transaction.shopping_cart_id, items);
|
| 2245 |
chandransh |
48 |
except ShoppingCartException, TException:
|
| 2185 |
rajveer |
49 |
print "Error while resetting the cart in the cart database"
|
| 2247 |
chandransh |
50 |
traceback.print_exc()
|
| 2185 |
rajveer |
51 |
except:
|
|
|
52 |
print "Unexpected exception"
|
| 2247 |
chandransh |
53 |
traceback.print_exc()
|
| 2185 |
rajveer |
54 |
|
|
|
55 |
def track_coupon_usage(transaction, userServiceClient):
|
|
|
56 |
try:
|
| 2277 |
chandransh |
57 |
coupon_code = transaction.coupon_code
|
| 2185 |
rajveer |
58 |
|
|
|
59 |
if coupon_code is not None and coupon_code != '':
|
|
|
60 |
PromotionClient().get_client().trackCouponUsage(coupon_code, transaction.id, transaction.customer_id);
|
|
|
61 |
except ShoppingCartException:
|
|
|
62 |
print "Error occurred in reading CardId for transaction"
|
| 2247 |
chandransh |
63 |
traceback.print_exc()
|
| 2185 |
rajveer |
64 |
except PromotionException:
|
|
|
65 |
print "Promotion Exception: "
|
| 2247 |
chandransh |
66 |
traceback.print_exc()
|
| 2185 |
rajveer |
67 |
except TException:
|
|
|
68 |
print "Transport from Promotion Service failed:"
|
| 2247 |
chandransh |
69 |
traceback.print_exc()
|
| 2185 |
rajveer |
70 |
except Exception:
|
|
|
71 |
print "Unexpected exception:"
|
| 2247 |
chandransh |
72 |
traceback.print_exc()
|
| 2185 |
rajveer |
73 |
|
| 2133 |
chandransh |
74 |
def main():
|
|
|
75 |
parser = optparse.OptionParser()
|
|
|
76 |
parser.add_option("-t", "--txn-id", dest="txn_id",
|
|
|
77 |
type="int",
|
|
|
78 |
help="mark the transaction TXN_ID and all its orders as successful",
|
|
|
79 |
metavar="TXN_ID")
|
| 3243 |
chandransh |
80 |
parser.add_option("-c", "--cod", dest="cod",
|
|
|
81 |
action="store_true", default=False,
|
|
|
82 |
help="process the transaction as a COD transaction")
|
| 4016 |
chandransh |
83 |
parser.add_option("-H", "--host", dest="hostname",
|
|
|
84 |
default="localhost",
|
|
|
85 |
type="string", help="The HOST where the DB server is running",
|
|
|
86 |
metavar="HOST")
|
| 5583 |
anupam.sin |
87 |
parser.add_option("-O", "--type", dest="orderType",
|
|
|
88 |
type="string", help="The type of order viz. B2B, B2C, B2Cbulk")
|
| 2133 |
chandransh |
89 |
(options, args) = parser.parse_args()
|
|
|
90 |
if len(args) != 0:
|
|
|
91 |
parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
|
|
|
92 |
if options.txn_id == None:
|
|
|
93 |
parser.error("No Transaction id supplied")
|
| 5583 |
anupam.sin |
94 |
if options.orderType == None:
|
|
|
95 |
parser.error("Please pass an orderType")
|
|
|
96 |
|
|
|
97 |
orderTypeInteger = OrderType._NAMES_TO_VALUES.get(options.orderType)
|
|
|
98 |
|
|
|
99 |
if orderTypeInteger == None:
|
|
|
100 |
parser.error("Invalid OrderType. Allowed values are B2B, B2C and B2Cbulk")
|
|
|
101 |
|
| 4016 |
chandransh |
102 |
DataService.initialize(db_hostname=options.hostname, echoOn=True)
|
| 2185 |
rajveer |
103 |
transaction = DataAccessors.get_transaction(options.txn_id)
|
| 3243 |
chandransh |
104 |
if(options.cod):
|
| 7293 |
anupam.sin |
105 |
DataAccessors.change_transaction_status(options.txn_id, TransactionStatus.COD_IN_PROCESS, "COD payment awaited", PickUpType.COURIER, orderTypeInteger, OrderSource.WEBSITE)
|
| 3243 |
chandransh |
106 |
else:
|
| 7293 |
anupam.sin |
107 |
DataAccessors.change_transaction_status(options.txn_id, TransactionStatus.AUTHORIZED, "Payment received for the order", PickUpType.COURIER, orderTypeInteger, OrderSource.WEBSITE)
|
| 3243 |
chandransh |
108 |
|
| 2185 |
rajveer |
109 |
DataAccessors.enqueue_transaction_info_email(options.txn_id)
|
|
|
110 |
|
|
|
111 |
user_service_client = UserClient()
|
|
|
112 |
reset_cart(transaction, user_service_client)
|
|
|
113 |
track_coupon_usage(transaction, user_service_client)
|
| 2133 |
chandransh |
114 |
|
|
|
115 |
if __name__ == '__main__':
|
|
|
116 |
main()
|