Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
420 ashish 1
'''
2
Created on 26-Aug-2010
3
 
4
@author: ashish
5
'''
6
from shop2020.payments.impl import DataService
7
from shop2020.utils.Utils import log_entry
8
from shop2020.payments.impl.DataAccessor import create_payment,\
766 rajveer 9
    get_payments_for_user, get_payments, get_payment, get_payment_for_txn,\
1627 ankur.sing 10
    get_payment_gateway, update_payment_details, close_session,\
1731 ankur.sing 11
    get_successful_payments_amount_range
701 chandransh 12
from shop2020.payments.impl.converters import to_t_payment, to_t_payment_gateway
420 ashish 13
 
14
class PaymentsHandler:
15
 
1248 chandransh 16
    def __init__(self, dbname='payment'):
17
        DataService.initialize(dbname)
420 ashish 18
        log_entry(self, "Initialized the data model")
19
 
701 chandransh 20
    def createPayment(self, userId, amount, gatewayId, txnId):
420 ashish 21
        """
701 chandransh 22
        create a new payment and return payment id, throws an exception if gateway is not active
23
 
24
 
420 ashish 25
        Parameters:
701 chandransh 26
        - userId
27
        - amount
28
        - gatewayId
29
        - txnId
420 ashish 30
        """
766 rajveer 31
        try:
32
            id = create_payment(userId, txnId, amount, gatewayId)
33
            log_entry(self, "created payment for txn %d" %(txnId))
34
            return id
35
        finally:
36
            close_session()
37
 
701 chandransh 38
    def getPaymentsForUser(self, userId, fromTime, toTime, status, gatewayId):
420 ashish 39
        """
701 chandransh 40
        get payment for user. If status and gateway are null, it is ignored. Same for times as well.
41
 
42
 
420 ashish 43
        Parameters:
701 chandransh 44
        - userId
45
        - fromTime
46
        - toTime
47
        - status
48
        - gatewayId
420 ashish 49
        """
766 rajveer 50
        try:
51
            payments = get_payments_for_user(userId, fromTime, toTime, status, gatewayId)
52
            return [to_t_payment(payment) for payment in payments]
53
        finally:
54
            close_session()
55
 
701 chandransh 56
    def getPayments(self, fromTime, toTime, status, gatewayId):
420 ashish 57
        """
2062 ankur.sing 58
        get all payments for user. If gatewayId is 0, then it is ignored while filtering.
701 chandransh 59
 
60
 
420 ashish 61
        Parameters:
701 chandransh 62
        - fromTime
63
        - toTime
64
        - status
65
        - gatewayId
420 ashish 66
        """
766 rajveer 67
        try:
68
            payments = get_payments(fromTime, toTime, status, gatewayId)
69
            return [to_t_payment(payment) for payment in payments]
70
        finally:
71
            close_session()
72
 
701 chandransh 73
    def getPaymentGateway(self, id):
420 ashish 74
        """
701 chandransh 75
        Get a particular gateway
420 ashish 76
 
77
 
78
        Parameters:
701 chandransh 79
        - id
420 ashish 80
        """
766 rajveer 81
        try:
82
            return to_t_payment_gateway(get_payment_gateway(id))
83
        finally:
84
            close_session()
85
 
701 chandransh 86
    def getPayment(self, id):
420 ashish 87
        """
701 chandransh 88
        Get a particular payment info
420 ashish 89
 
90
 
91
        Parameters:
701 chandransh 92
        - id
420 ashish 93
        """
766 rajveer 94
        try:
95
            return to_t_payment(get_payment(id))
96
        finally:
97
            close_session()
98
 
701 chandransh 99
    def getPaymentForTxnId(self, txnId):
420 ashish 100
        """
701 chandransh 101
        Get a particular payment for a transaction. Will raise exception.
420 ashish 102
 
103
 
104
        Parameters:
701 chandransh 105
        - txnId
420 ashish 106
        """
766 rajveer 107
        try:
108
            return to_t_payment(get_payment_for_txn(txnId))
109
        finally:
110
            close_session()
111
 
1129 rajveer 112
    def updatePaymentDetails(self, id, gatewayPaymentId, sessionId, gatewayTxnStatus, description, gatewayTxnId, authCode, referenceCode, errorCode, status, gatewayTxnDate, attributes):
420 ashish 113
        """
701 chandransh 114
        mark payment successful and store parameters
420 ashish 115
 
116
 
117
        Parameters:
701 chandransh 118
        - id
119
        - gatewayPaymentId
120
        - sessionId
121
        - gatewayTxnStatus
122
        - description
123
        - gatewayTxnId
124
        - authCode
125
        - referenceCode
126
        - errorCode
127
        - status
1129 rajveer 128
        - gatewayTxnDate
701 chandransh 129
        - attributes
420 ashish 130
        """
766 rajveer 131
        try:
1129 rajveer 132
            return update_payment_details(id, gatewayPaymentId, sessionId, gatewayTxnStatus, description, gatewayTxnId, authCode, referenceCode, errorCode, status, gatewayTxnDate, attributes)
766 rajveer 133
        finally:
134
            close_session()
135
 
1627 ankur.sing 136
 
1731 ankur.sing 137
    def getSuccessfulPaymentsAmountRange(self, ):
1627 ankur.sing 138
        """
1731 ankur.sing 139
        Returns the minimum and maximum amounts among successful payments.
140
        List contains two double values, first minimum and second maximum amount.
1627 ankur.sing 141
        """
2192 chandransh 142
        try:
143
            return get_successful_payments_amount_range()
144
        finally:
145
            close_session()
1627 ankur.sing 146
 
766 rajveer 147
    def closeSession(self, ):
148
        close_session()