Subversion Repositories SmartDukaan

Rev

Rev 4016 | Rev 5527 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
2185 rajveer 19
 
2133 chandransh 20
if __name__ == '__main__' and __package__ is None:
21
    import os
22
    sys.path.insert(0, os.getcwd())
23
 
24
from shop2020.model.v1.order.impl import DataAccessors, DataService
25
from shop2020.thriftpy.model.v1.order.ttypes import TransactionStatus
2186 rajveer 26
from shop2020.thriftpy.model.v1.user.ttypes import ShoppingCartException,\
27
    PromotionException
28
from shop2020.clients.UserClient import UserClient
29
from shop2020.clients.PromotionClient import PromotionClient
2133 chandransh 30
 
2185 rajveer 31
def reset_cart(transaction, userServiceClient):
32
    items = {}
33
    for order in transaction.orders:
34
        for line_item in order.lineitems:
35
            item_id = line_item.item_id
2240 chandransh 36
            if items.has_key(item_id):
37
                quantity = items[item_id] + line_item.quantity
38
            else:
2185 rajveer 39
                quantity = line_item.quantity
40
            items[item_id] = quantity
41
 
42
    print items
43
 
44
    try:
2240 chandransh 45
        userServiceClient.get_client().resetCart(transaction.shopping_cart_id, items);
2245 chandransh 46
    except ShoppingCartException, TException:
2185 rajveer 47
        print "Error while resetting the cart in the cart database"
2247 chandransh 48
        traceback.print_exc()
2185 rajveer 49
    except:
50
        print "Unexpected exception"
2247 chandransh 51
        traceback.print_exc()
2185 rajveer 52
 
53
def track_coupon_usage(transaction, userServiceClient):
54
    try:
2277 chandransh 55
        coupon_code = transaction.coupon_code
2185 rajveer 56
 
57
        if coupon_code is not None and coupon_code != '':
58
            PromotionClient().get_client().trackCouponUsage(coupon_code, transaction.id, transaction.customer_id);
59
    except ShoppingCartException:
60
        print "Error occurred in reading CardId for transaction"
2247 chandransh 61
        traceback.print_exc()
2185 rajveer 62
    except PromotionException:
63
        print "Promotion Exception: "
2247 chandransh 64
        traceback.print_exc()
2185 rajveer 65
    except TException:
66
        print "Transport from Promotion Service failed:"
2247 chandransh 67
        traceback.print_exc()
2185 rajveer 68
    except Exception:
69
        print "Unexpected exception:"
2247 chandransh 70
        traceback.print_exc()
2185 rajveer 71
 
2133 chandransh 72
def main():
73
    parser = optparse.OptionParser()
74
    parser.add_option("-t", "--txn-id", dest="txn_id",
75
                   type="int",
76
                   help="mark the transaction TXN_ID and all its orders as successful",
77
                   metavar="TXN_ID")
3243 chandransh 78
    parser.add_option("-c", "--cod", dest="cod",
79
                      action="store_true", default=False,
80
                      help="process the transaction as a COD transaction")
4016 chandransh 81
    parser.add_option("-H", "--host", dest="hostname",
82
                      default="localhost",
83
                      type="string", help="The HOST where the DB server is running",
84
                      metavar="HOST")
2133 chandransh 85
    (options, args) = parser.parse_args()
86
    if len(args) != 0:
87
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
88
    if options.txn_id == None:
89
        parser.error("No Transaction id supplied")
4016 chandransh 90
    DataService.initialize(db_hostname=options.hostname, echoOn=True)
2185 rajveer 91
    transaction = DataAccessors.get_transaction(options.txn_id)
3243 chandransh 92
    if(options.cod):
93
        DataAccessors.change_transaction_status(options.txn_id, TransactionStatus.COD_IN_PROCESS, "COD payment awaited")
94
    else:
95
        DataAccessors.change_transaction_status(options.txn_id, TransactionStatus.AUTHORIZED, "Payment received for the order")
96
 
2185 rajveer 97
    DataAccessors.enqueue_transaction_info_email(options.txn_id)
98
 
99
    user_service_client = UserClient()
100
    reset_cart(transaction, user_service_client)
101
    track_coupon_usage(transaction, user_service_client)
2133 chandransh 102
 
103
if __name__ == '__main__':
104
    main()