Subversion Repositories SmartDukaan

Rev

Rev 7127 | Rev 7141 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/python
'''
It is used to process transactions for which the payment 
was received but the order was not processed.

@author: Rajveer
'''
import optparse
import sys
import datetime
from elixir import *




if __name__ == '__main__' and __package__ is None:
    import os
    sys.path.insert(0, os.getcwd())

from shop2020.thriftpy.model.v1.order.ttypes import RechargeOrderStatus
from shop2020.model.v1.order.impl.DataAccessors import get_recharge_orders_for_status, update_recharge_order_status,\
    update_amount_in_wallet, update_recharge_transaction_status
from shop2020.model.v1.order.impl import DataService
from shop2020.model.v1.order.impl.DataService import RechargeTransaction
from shop2020.model.v1.order.impl.model.RechargeOrder import RechargeOrder
from shop2020.model.v1.order.impl.RechargeService import checkTransactionStatus, getRefunds


def main():
    parser = optparse.OptionParser()
    parser.add_option("-H", "--host", dest="hostname",
                      default="localhost",
                      type="string", help="The HOST where the DB server is running",
                      metavar="HOST")
    parser.add_option("-r", "--refund", dest="refund",
                      action="store_true",
                      help="")
    parser.add_option("-u", "--unknown", dest="unknown",
                      action="store_true",
                      help="")
    parser.add_option("-a", "--authorized", dest="authorized",
                      action="store_true",
                      help="")
    parser.add_option("-t", "--txn-id", dest="txn_id",
                   type="int",
                   help="mark the transaction(recharge order id) TXN_ID as successful",
                   metavar="TXN_ID")

    (options, args) = parser.parse_args()
    if len(args) != 0:
        parser.error("You've supplied extra arguments. Are you sure you want to run this program?")
    DataService.initialize(db_hostname=options.hostname, echoOn=True)
        
    if options.refund:
        processRefunds()
    if options.unknown:
        processUnknownTransactions()
    if options.authorized:
        processAuthorizedTransactions(options.txn_id)

def processRefunds():
    todate = datetime.datetime.now()
    for i in range(15):
        orderDate = todate + datetime.timedelta(days= -i)
        refunds = getRefunds(orderDate)
        for key in refunds.keys():
            refund = refunds.get(key)
            refundAmount = refund[0]
            refundDate = refund[1]
            order = RechargeOrder.get_by(spiceTID = key)
            if order:
                amount = order.totalAmount
                isStoreOrder = False
            else:
                order = RechargeTransaction.get_by(spiceTID = key)
                isStoreOrder = True
                amount = order.amount
            if order.status == RechargeOrderStatus.RECHARGE_FAILED_REFUNDED:
                print "Refund is already processed."
                continue
            if order.status not in (RechargeOrderStatus.RECHARGE_SUCCESSFUL, RechargeOrderStatus.PAYMENT_SUCCESSFUL):
                print "Recharge/Payment is not successful. There is something wrong."
                continue
            if amount != refundAmount:
                print "Refund amount is not same as transaction amount"
                continue
            if isStoreOrder:
                update_recharge_transaction_status(order.id, RechargeOrderStatus.RECHARGE_FAILED_REFUNDED, refundDate)
            else:
                update_recharge_order_status(order.id, RechargeOrderStatus.RECHARGE_FAILED_REFUNDED, refundDate)

def processUnknownTransactions():    
    orders = get_recharge_orders_for_status(RechargeOrderStatus.PAYMENT_SUCCESSFUL)
    for order in orders:
        try:
            if order.creationTimestamp + datetime.timedelta(minutes=10) < datetime.datetime.now():
                status, description = checkTransactionStatus('', str(order.id))
                print status, description
                if status:
                    update_recharge_order_status(order.id, RechargeOrderStatus.RECHARGE_SUCCESSFUL)
                else:
                    update_recharge_order_status(order.id, RechargeOrderStatus.RECHARGE_FAILED)
        except:
            print "Do Nothing"

def processAuthorizedTransactions(txn_id):
    update_recharge_order_status(txn_id, RechargeOrderStatus.PAYMENT_SUCCESSFUL)
                
if __name__ == '__main__':
    main()