Subversion Repositories SmartDukaan

Rev

Rev 701 | Rev 766 | 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:
420 ashish 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()
29
    payment.userId = userId
30
    payment.merchantTxnId = txnId
31
    payment.amount = amount
32
    payment.initTimestamp = datetime.datetime.now()
33
    payment.ps = PaymentStatus.INIT
420 ashish 34
    session.commit()
35
    return payment.id
36
 
701 chandransh 37
def build_payment_query(from_time, to_time, status, gateway_id):
38
    query = Payment.query
420 ashish 39
    if from_time:
40
        from_date = to_py_date(from_time)
701 chandransh 41
        query = query.filter(Payment.initTimestamp >= from_date)
420 ashish 42
    if to_time:
43
        to_date = to_py_date(to_time)
701 chandransh 44
        query = query.filter(or_(Payment.successTimestamp <= to_date, Payment.errorTimestamp <= to_date))
420 ashish 45
    if status != -1:
701 chandransh 46
        query = query.filter(Payment.status == status)
420 ashish 47
    if gateway_id:
701 chandransh 48
        query = query.filter(Payment.gatewayId == gateway_id)
49
    return query
50
 
51
def get_payments_for_user(user_id, from_time, to_time, status, gateway_id):
52
    if not user_id:
420 ashish 53
        raise PaymentException(101, "user cannot be null")
701 chandransh 54
    query = build_payment_query(from_time, to_time, status, gateway_id)
55
    query = query.filter_by(userId=user_id)
420 ashish 56
    try:
701 chandransh 57
        return query.all()
420 ashish 58
    except:
701 chandransh 59
        print "Unexpected error:", sys.exc_info()[0]
60
        return []
420 ashish 61
 
62
def get_payments(from_time, to_time, status, gateway_id):
701 chandransh 63
    query = build_payment_query(from_time, to_time, status, gateway_id)
420 ashish 64
    try:
701 chandransh 65
        return query.all()
420 ashish 66
    except:
701 chandransh 67
        print "Unexpected error:", sys.exc_info()[0]
68
        return []
69
 
70
def get_payment_gateway(id):
420 ashish 71
    try:
701 chandransh 72
        return PaymentGateway.get_by(id=id)
420 ashish 73
    except:
701 chandransh 74
        raise PaymentException(102, "No such payment gateway")
420 ashish 75
 
76
def get_payment(id):
77
    try:
701 chandransh 78
        return Payment.get_by(id=id)
420 ashish 79
    except:
701 chandransh 80
        raise PaymentException(101, "No such payment")
81
 
82
def get_payment_for_txn(txnId):
420 ashish 83
    try:
701 chandransh 84
        return Payment.query.filtery_by(merchantTxnId=txnId).one()
420 ashish 85
    except:
701 chandransh 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:
717 rajveer 92
        payment = Payment.get_by(id=id)
701 chandransh 93
        if gatewayPaymentId:
94
            payment.gatewayPaymentId = gatewayPaymentId
717 rajveer 95
        if sessionId:
96
            payment.sessionId = sessionId    
701 chandransh 97
        if gatewayTxnStatus:
98
            payment.gatewayTxnStatus = gatewayTxnStatus
99
        if description:
100
            payment.description = description
101
        if gatewayTxnId:
102
            payment.gatewayTxnId = gatewayTxnId
103
        if authCode:
104
            payment.authCode = authCode
105
        if referenceCode:
106
            payment.referenceCode = referenceCode
107
        if errorCode:
108
            payment.errorCode = errorCode
109
        if status:
110
            payment.status = status
717 rajveer 111
        if attributes: 
112
            for attr in attributes:
113
                payment_attr =  PaymentAttribute.query.filter_by(payment=payment, name=attr.name)
114
                if payment_attr:
115
                    payment_attr.value = attr.value
116
                else:
117
                    payment_attr = PaymentAttribute(payment=payment, name = attr.name, value = attr.value)
701 chandransh 118
        session.commit()
119
        return True
120
    except:
121
        return False