Subversion Repositories SmartDukaan

Rev

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