Subversion Repositories SmartDukaan

Rev

Rev 1120 | Rev 1627 | Go to most recent revision | 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
import datetime
701 chandransh 7
import sys
8
 
420 ashish 9
from elixir import *
701 chandransh 10
from sqlalchemy import or_
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
420 ashish 16
from shop2020.utils.Utils import to_py_date
17
 
701 chandransh 18
def create_payment(userId, txnId, amount, gatewayId):
19
    if not userId:
859 chandransh 20
        raise PaymentException(101, "User cannot be null")
701 chandransh 21
    if not txnId:
22
        raise PaymentException(101, "Transaction cannot be null")
420 ashish 23
    if not amount:
24
        raise PaymentException(101, "Amount cannot be null")
701 chandransh 25
    if not gatewayId:
420 ashish 26
        raise PaymentException(101, "Gateway cannot be null")
27
 
701 chandransh 28
    payment = Payment()
854 rajveer 29
    payment.gatewayId = gatewayId
701 chandransh 30
    payment.userId = userId
31
    payment.merchantTxnId = txnId
32
    payment.amount = amount
33
    payment.initTimestamp = datetime.datetime.now()
34
    payment.ps = PaymentStatus.INIT
420 ashish 35
    session.commit()
36
    return payment.id
37
 
701 chandransh 38
def build_payment_query(from_time, to_time, status, gateway_id):
39
    query = Payment.query
420 ashish 40
    if from_time:
41
        from_date = to_py_date(from_time)
701 chandransh 42
        query = query.filter(Payment.initTimestamp >= from_date)
420 ashish 43
    if to_time:
44
        to_date = to_py_date(to_time)
701 chandransh 45
        query = query.filter(or_(Payment.successTimestamp <= to_date, Payment.errorTimestamp <= to_date))
1493 vikas 46
    if status is not None:
701 chandransh 47
        query = query.filter(Payment.status == status)
420 ashish 48
    if gateway_id:
701 chandransh 49
        query = query.filter(Payment.gatewayId == gateway_id)
50
    return query
51
 
52
def get_payments_for_user(user_id, from_time, to_time, status, gateway_id):
53
    if not user_id:
420 ashish 54
        raise PaymentException(101, "user cannot be null")
701 chandransh 55
    query = build_payment_query(from_time, to_time, status, gateway_id)
56
    query = query.filter_by(userId=user_id)
420 ashish 57
    try:
701 chandransh 58
        return query.all()
420 ashish 59
    except:
701 chandransh 60
        print "Unexpected error:", sys.exc_info()[0]
61
        return []
766 rajveer 62
 
420 ashish 63
def get_payments(from_time, to_time, status, gateway_id):
701 chandransh 64
    query = build_payment_query(from_time, to_time, status, gateway_id)
420 ashish 65
    try:
701 chandransh 66
        return query.all()
420 ashish 67
    except:
701 chandransh 68
        print "Unexpected error:", sys.exc_info()[0]
69
        return []
766 rajveer 70
 
701 chandransh 71
def get_payment_gateway(id):
420 ashish 72
    try:
701 chandransh 73
        return PaymentGateway.get_by(id=id)
420 ashish 74
    except:
701 chandransh 75
        raise PaymentException(102, "No such payment gateway")
766 rajveer 76
 
420 ashish 77
def get_payment(id):
78
    try:
701 chandransh 79
        return Payment.get_by(id=id)
420 ashish 80
    except:
701 chandransh 81
        raise PaymentException(101, "No such payment")
766 rajveer 82
 
701 chandransh 83
def get_payment_for_txn(txnId):
420 ashish 84
    try:
859 chandransh 85
        return Payment.query.filter_by(merchantTxnId=txnId).one()
420 ashish 86
    except:
701 chandransh 87
        raise PaymentException(103, "Unable to get payment for the given transaction")
766 rajveer 88
 
1120 rajveer 89
def update_payment_details(id, gatewayPaymentId, sessionId, gatewayTxnStatus, description, gatewayTxnId, authCode, referenceCode, errorCode, status, gatewayTxnDate, attributes):
701 chandransh 90
    if not id:
91
        raise PaymentException(101, "Can't update a payment without an id")
92
    try:
717 rajveer 93
        payment = Payment.get_by(id=id)
701 chandransh 94
        if gatewayPaymentId:
95
            payment.gatewayPaymentId = gatewayPaymentId
717 rajveer 96
        if sessionId:
97
            payment.sessionId = sessionId    
701 chandransh 98
        if gatewayTxnStatus:
99
            payment.gatewayTxnStatus = gatewayTxnStatus
100
        if description:
101
            payment.description = description
102
        if gatewayTxnId:
103
            payment.gatewayTxnId = gatewayTxnId
104
        if authCode:
105
            payment.authCode = authCode
106
        if referenceCode:
107
            payment.referenceCode = referenceCode
108
        if errorCode:
109
            payment.errorCode = errorCode
1120 rajveer 110
        if gatewayTxnDate:
111
            payment.gatewayTxnDate = gatewayTxnDate
701 chandransh 112
        if status:
113
            payment.status = status
1120 rajveer 114
            if status == PaymentStatus.SUCCESS:
115
                payment.successTimestamp = datetime.datetime.now()
116
            if status == PaymentStatus.FAILED:
117
                payment.errorTimestamp = datetime.datetime.now()
717 rajveer 118
        if attributes: 
119
            for attr in attributes:
854 rajveer 120
                payment_attr =  PaymentAttribute.query.filter_by(payment=payment, name=attr.name).first()
717 rajveer 121
                if payment_attr:
122
                    payment_attr.value = attr.value
123
                else:
124
                    payment_attr = PaymentAttribute(payment=payment, name = attr.name, value = attr.value)
701 chandransh 125
        session.commit()
126
        return True
127
    except:
766 rajveer 128
        return False
129
 
130
def close_session():
131
    if session.is_active:
132
        print "session is active. closing it."
133
        session.close()