Subversion Repositories SmartDukaan

Rev

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