Subversion Repositories SmartDukaan

Rev

Rev 420 | Rev 717 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 420 Rev 701
Line 1... Line 1...
1
'''
1
'''
2
Created on 26-Aug-2010
2
Created on 26-Aug-2010
3
 
3
 
4
@author: ashish
4
@author: ashish
5
'''
5
'''
6
from shop2020.payment.ttypes import PaymentException
-
 
7
from shop2020.payments.impl.DataService import Pmnt
-
 
8
import datetime
6
import datetime
9
from shop2020.thriftpy.payments import PaymentService
-
 
10
from elixir import *
7
import sys
11
from shop2020.utils.Utils import to_py_date
-
 
12
from shop2020.thriftpy import payments
-
 
13
 
8
 
-
 
9
from elixir import *
-
 
10
from sqlalchemy import or_
14
 
11
 
-
 
12
from shop2020.thriftpy.payments.ttypes import PaymentException
-
 
13
from shop2020.payments.impl.DataService import Payment, PaymentGateway,\
-
 
14
    PaymentAttribute
-
 
15
from shop2020.thriftpy.payments.ttypes import PaymentStatus
-
 
16
from shop2020.utils.Utils import to_py_date
15
 
17
 
16
def create_payment(user_id, cart_id, amount, gateway_id):
18
def create_payment(userId, txnId, amount, gatewayId):
17
    if not user_id:
19
    if not userId:
18
        raise PaymentException(101, "user cannot be null")
20
        raise PaymentException(101, "user cannot be null")
19
    if not cart_id:
21
    if not txnId:
20
        raise PaymentException(101, "cart cannot be null")
22
        raise PaymentException(101, "Transaction cannot be null")
21
    
-
 
22
    if not amount:
23
    if not amount:
23
        raise PaymentException(101, "Amount cannot be null")
24
        raise PaymentException(101, "Amount cannot be null")
24
    if not gateway_id:
25
    if not gatewayId:
25
        raise PaymentException(101, "Gateway cannot be null")
26
        raise PaymentException(101, "Gateway cannot be null")
26
 
27
 
27
    payment = Pmnt()
28
    payment = Payment()
28
    payment.user_id = user_id
29
    payment.userId = userId
29
    payment.cart_id = cart_id
30
    payment.merchantTxnId = txnId
30
    payment.amt = amount
31
    payment.amount = amount
31
    payment.mid = str(cart_id)
-
 
32
    payment.init_ts = datetime.datetime.now()
32
    payment.initTimestamp = datetime.datetime.now()
33
    payment.ps = PaymentService.PaymentStatus.INIT
33
    payment.ps = PaymentStatus.INIT
34
    session.commit()
34
    session.commit()
35
    return payment.id
35
    return payment.id
36
 
36
 
37
def get_payments_for_user(user_id, from_time, to_time, status, gateway_id):
37
def build_payment_query(from_time, to_time, status, gateway_id):
38
    
-
 
39
    if not user_id:
-
 
40
        raise PaymentException(101, "user cannot be null")
-
 
41
    
-
 
42
    query = Pmnt.query.filter_by(user_id=user_id)
38
    query = Payment.query
43
    if from_time:
39
    if from_time:
44
        from_date = to_py_date(from_time)
40
        from_date = to_py_date(from_time)
45
        query = query.filter(Pmnt.init_ts >= from_date)
41
        query = query.filter(Payment.initTimestamp >= from_date)
46
    if to_time:
42
    if to_time:
47
        to_date = to_py_date(to_time)
43
        to_date = to_py_date(to_time)
48
        query = query.filter(Pmnt.init_ts <= to_date)
44
        query = query.filter(or_(Payment.successTimestamp <= to_date, Payment.errorTimestamp <= to_date))
49
    
-
 
50
    if status != -1:
45
    if status != -1:
51
        query = query.filter(Pmnt.ps == status)
46
        query = query.filter(Payment.status == status)
52
    
-
 
53
    if gateway_id:
47
    if gateway_id:
54
        query = query.filter(Pmnt.gateway == gateway_id)
48
        query = query.filter(Payment.gatewayId == gateway_id)
55
    
-
 
56
    try:
-
 
57
        payments = query.all()
-
 
58
        return payments
-
 
59
    except:
-
 
60
        return None
49
    return query
61
    
-
 
62
    
50
 
63
def get_payments_for_cart(cart_id, from_time, to_time, status, gateway_id):
51
def get_payments_for_user(user_id, from_time, to_time, status, gateway_id):
64
    
-
 
65
    if not cart_id:
52
    if not user_id:
66
        raise PaymentException(101, "user cannot be null")
53
        raise PaymentException(101, "user cannot be null")
67
    
-
 
68
    query = Pmnt.query.filter_by(cart_id=cart_id)
54
    query = build_payment_query(from_time, to_time, status, gateway_id)
69
    if from_time:
-
 
70
        from_date = to_py_date(from_time)
-
 
71
        query = query.filter(Pmnt.init_ts >= from_date)
-
 
72
    if to_time:
-
 
73
        to_date = to_py_date(to_time)
-
 
74
        query = query.filter(Pmnt.init_ts <= to_date)
-
 
75
    
-
 
76
    if status != -1:
-
 
77
        query = query.filter(Pmnt.ps == status)
55
    query = query.filter_by(userId=user_id)
78
    
-
 
79
    if gateway_id:
-
 
80
        query = query.filter(Pmnt.gateway == gateway_id)
-
 
81
    
-
 
82
    try:
56
    try:
83
        payments = query.all()
57
        return query.all()
84
        return payments
-
 
85
    except:
58
    except:
-
 
59
        print "Unexpected error:", sys.exc_info()[0]
86
        return None
60
        return []
87
 
61
 
88
def get_payments(from_time, to_time, status, gateway_id):
62
def get_payments(from_time, to_time, status, gateway_id):
89
    
-
 
90
    
-
 
91
    query = Pmnt.query
-
 
92
    if from_time:
-
 
93
        from_date = to_py_date(from_time)
-
 
94
        query = query.filter(Pmnt.init_ts >= from_date)
-
 
95
    if to_time:
-
 
96
        to_date = to_py_date(to_time)
-
 
97
        query = query.filter(Pmnt.init_ts <= to_date)
-
 
98
    
-
 
99
    if status != -1:
-
 
100
        query = query.filter(Pmnt.ps == status)
-
 
101
    
-
 
102
    if gateway_id:
-
 
103
        query = query.filter(Pmnt.gateway == gateway_id)
63
    query = build_payment_query(from_time, to_time, status, gateway_id)
104
    
-
 
105
    try:
64
    try:
106
        payments = query.all()
65
        return query.all()
107
        return payments
-
 
108
    except:
66
    except:
-
 
67
        print "Unexpected error:", sys.exc_info()[0]
109
        return None
68
        return []
110
    
69
 
111
def change_payment_status(id, status):
70
def get_payment_gateway(id):
112
    try:
71
    try:
113
        payment = Pmnt.get_by(id=id)
72
        return PaymentGateway.get_by(id=id)
114
        payment.ps = status
-
 
115
        session.commit()
-
 
116
    except:
73
    except:
117
        raise PaymentException(101, "no payment with such id")
74
        raise PaymentException(102, "No such payment gateway")
118
 
75
 
119
def get_payment(id):
76
def get_payment(id):
120
    try:
77
    try:
121
        payment = Pmnt.get_by(id=id)
-
 
122
        return payment
-
 
123
        
-
 
124
    except:
-
 
125
        raise PaymentException(101, "no payment with such id")
-
 
126
    
-
 
127
def add_bank_details(id, bid, btxid, error_code, session_id, postdate, auth_code, ref_code):
-
 
128
    try:
-
 
129
        payment = Pmnt.get_by(id=id)
-
 
130
        return payment
-
 
131
        
-
 
132
    except:
-
 
133
        raise PaymentException(101, "no payment with such id")
-
 
134
    
-
 
135
    
-
 
136
    payment.bid = bid
-
 
137
    payment.btxid = btxid
-
 
138
    payment.error_code = error_code
-
 
139
    payment.session_id = session_id
-
 
140
    payment.post_date = postdate
-
 
141
    payment.ref_code = ref_code
-
 
142
    payment.auth_code = auth_code
-
 
143
    
-
 
144
    
-
 
145
    
-
 
146
    session.commit()
-
 
147
    
-
 
148
78
        return Payment.get_by(id=id)
-
 
79
    except:
-
 
80
        raise PaymentException(101, "No such payment")
-
 
81
 
-
 
82
def get_payment_for_txn(txnId):
-
 
83
    try:
-
 
84
        return Payment.query.filtery_by(merchantTxnId=txnId).one()
-
 
85
    except:
-
 
86
        raise PaymentException(103, "Unable to get payment for the given transaction")
-
 
87
 
-
 
88
def update_payment_details(id, gatewayPaymentId, sessionId, gatewayTxnStatus, description, gatewayTxnId, authCode, referenceCode, errorCode, status, attributes):
-
 
89
    if not id:
-
 
90
        raise PaymentException(101, "Can't update a payment without an id")
-
 
91
    try:
-
 
92
        payment = Payment.get_by(id)
-
 
93
        if gatewayPaymentId:
-
 
94
            payment.gatewayPaymentId = gatewayPaymentId
-
 
95
        if gatewayTxnStatus:
-
 
96
            payment.gatewayTxnStatus = gatewayTxnStatus
-
 
97
        if description:
-
 
98
            payment.description = description
-
 
99
        if gatewayTxnId:
-
 
100
            payment.gatewayTxnId = gatewayTxnId
-
 
101
        if authCode:
-
 
102
            payment.authCode = authCode
-
 
103
        if referenceCode:
-
 
104
            payment.referenceCode = referenceCode
-
 
105
        if errorCode:
-
 
106
            payment.errorCode = errorCode
-
 
107
        if status:
-
 
108
            payment.status = status
-
 
109
        for attr in attributes:
-
 
110
            payment_attr =  PaymentAttribute.query.filter_by(payment=payment, name=attr.name)
-
 
111
            if payment_attr:
-
 
112
                payment_attr.value = attr.value
-
 
113
            else:
-
 
114
                payment_attr = PaymentAttribute(payment=payment, name = attr.name, value = attr.value)
-
 
115
        session.commit()
-
 
116
        return True
-
 
117
    except:
-
 
118
        return False
-
 
119
149
120