Subversion Repositories SmartDukaan

Rev

Rev 2062 | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 26-Aug-2010

@author: ashish
'''
from shop2020.payments.impl import DataService
from shop2020.utils.Utils import log_entry
from shop2020.payments.impl.DataAccessor import create_payment,\
    get_payments_for_user, get_payments, get_payment, get_payment_for_txn,\
    get_payment_gateway, update_payment_details, close_session,\
    get_successful_payments_amount_range
from shop2020.payments.impl.converters import to_t_payment, to_t_payment_gateway

class PaymentsHandler:
    
    def __init__(self, dbname='payment'):
        DataService.initialize(dbname)
        log_entry(self, "Initialized the data model")
    
    def createPayment(self, userId, amount, gatewayId, txnId):
        """
        create a new payment and return payment id, throws an exception if gateway is not active
        
        
        Parameters:
        - userId
        - amount
        - gatewayId
        - txnId
        """
        try:
            id = create_payment(userId, txnId, amount, gatewayId)
            log_entry(self, "created payment for txn %d" %(txnId))
            return id
        finally:
            close_session()
            
    def getPaymentsForUser(self, userId, fromTime, toTime, status, gatewayId):
        """
        get payment for user. If status and gateway are null, it is ignored. Same for times as well.
        
        
        Parameters:
        - userId
        - fromTime
        - toTime
        - status
        - gatewayId
        """
        try:
            payments = get_payments_for_user(userId, fromTime, toTime, status, gatewayId)
            return [to_t_payment(payment) for payment in payments]
        finally:
            close_session()
            
    def getPayments(self, fromTime, toTime, status, gatewayId):
        """
        get all payments for user. If gatewayId is 0, then it is ignored while filtering.
        
        
        Parameters:
        - fromTime
        - toTime
        - status
        - gatewayId
        """
        try:
            payments = get_payments(fromTime, toTime, status, gatewayId)
            return [to_t_payment(payment) for payment in payments]
        finally:
            close_session()
            
    def getPaymentGateway(self, id):
        """
        Get a particular gateway
        
        
        Parameters:
        - id
        """
        try:
            return to_t_payment_gateway(get_payment_gateway(id))
        finally:
            close_session()
            
    def getPayment(self, id):
        """
        Get a particular payment info
        
        
        Parameters:
        - id
        """
        try:
            return to_t_payment(get_payment(id))
        finally:
            close_session()
            
    def getPaymentForTxnId(self, txnId):
        """
        Get a particular payment for a transaction. Will raise exception.
        
        
        Parameters:
        - txnId
        """
        try:
            return to_t_payment(get_payment_for_txn(txnId))
        finally:
            close_session()
            
    def updatePaymentDetails(self, id, gatewayPaymentId, sessionId, gatewayTxnStatus, description, gatewayTxnId, authCode, referenceCode, errorCode, status, gatewayTxnDate, attributes):
        """
        mark payment successful and store parameters
        
        
        Parameters:
        - id
        - gatewayPaymentId
        - sessionId
        - gatewayTxnStatus
        - description
        - gatewayTxnId
        - authCode
        - referenceCode
        - errorCode
        - status
        - gatewayTxnDate
        - attributes
        """
        try:
            return update_payment_details(id, gatewayPaymentId, sessionId, gatewayTxnStatus, description, gatewayTxnId, authCode, referenceCode, errorCode, status, gatewayTxnDate, attributes)
        finally:
            close_session()
            

    def getSuccessfulPaymentsAmountRange(self, ):
        """
        Returns the minimum and maximum amounts among successful payments.
        List contains two double values, first minimum and second maximum amount.
        """
        try:
            return get_successful_payments_amount_range()
        finally:
            close_session()
            
    def closeSession(self, ):
        close_session()