Subversion Repositories SmartDukaan

Rev

Rev 1659 | Blame | Compare with Previous | Last modification | View Log | RSS feed

'''
Created on 26-Aug-2010

@author: ashish
'''
import datetime
import sys

from elixir import *
from sqlalchemy import or_

from shop2020.thriftpy.payments.ttypes import PaymentException
from shop2020.payments.impl.DataService import Payment, PaymentGateway,\
    PaymentAttribute
from shop2020.thriftpy.payments.ttypes import PaymentStatus
from shop2020.utils.Utils import to_py_date
from sqlalchemy.sql.expression import select, func

def create_payment(userId, txnId, amount, gatewayId):
    if not userId:
        raise PaymentException(101, "User cannot be null")
    if not txnId:
        raise PaymentException(101, "Transaction cannot be null")
    if not amount:
        raise PaymentException(101, "Amount cannot be null")
    if not gatewayId:
        raise PaymentException(101, "Gateway cannot be null")

    payment = Payment()
    payment.gatewayId = gatewayId
    payment.userId = userId
    payment.merchantTxnId = txnId
    payment.amount = amount
    payment.initTimestamp = datetime.datetime.now()
    payment.status = PaymentStatus.INIT
    session.commit()
    return payment.id

def build_payment_query(from_time, to_time, status, gateway_id):
    query = Payment.query
    if from_time:
        from_date = to_py_date(from_time)
        query = query.filter(Payment.initTimestamp >= from_date)
    if to_time:
        to_date = to_py_date(to_time)
        if status != PaymentStatus.INIT:
            query = query.filter(or_(Payment.successTimestamp <= to_date, Payment.errorTimestamp <= to_date))
        else:
            query = query.filter(Payment.initTimestamp <= to_date)
    if status is not None:
        query = query.filter(Payment.status == status)
    if gateway_id:
        query = query.filter(Payment.gatewayId == gateway_id)
    return query

def get_payments_for_user(user_id, from_time, to_time, status, gateway_id):
    if not user_id:
        raise PaymentException(101, "user cannot be null")
    query = build_payment_query(from_time, to_time, status, gateway_id)
    query = query.filter_by(userId=user_id)
    try:
        return query.all()
    except:
        print "Unexpected error:", sys.exc_info()[0]
        return []
        
def get_payments(from_time, to_time, status, gateway_id):
    query = build_payment_query(from_time, to_time, status, gateway_id)
    try:
        return query.all()
    except:
        print "Unexpected error:", sys.exc_info()[0]
        return []
        
def get_payment_gateway(id):
    try:
        return PaymentGateway.get_by(id=id)
    except:
        raise PaymentException(102, "No such payment gateway")
        
def get_payment(id):
    try:
        return Payment.get_by(id=id)
    except:
        raise PaymentException(101, "No such payment")
        
def get_payment_for_txn(txnId):
    try:
        return Payment.query.filter_by(merchantTxnId=txnId).one()
    except:
        raise PaymentException(103, "Unable to get payment for the given transaction")
        
def get_successful_payments_amount_range():
    payments = Payment.query.filter(Payment.status == PaymentStatus.SUCCESS).all()
    payment_amounts = [p.amount for p in payments]
    return [min(payment_amounts), max(payment_amounts)]
        
        
def update_payment_details(id, gatewayPaymentId, sessionId, gatewayTxnStatus, description, gatewayTxnId, authCode, referenceCode, errorCode, status, gatewayTxnDate, attributes):
    if not id:
        raise PaymentException(101, "Can't update a payment without an id")
    try:
        payment = Payment.get_by(id=id)
        if gatewayPaymentId:
            payment.gatewayPaymentId = gatewayPaymentId
        if sessionId:
            payment.sessionId = sessionId    
        if gatewayTxnStatus:
            payment.gatewayTxnStatus = gatewayTxnStatus
        if description:
            payment.description = description
        if gatewayTxnId:
            payment.gatewayTxnId = gatewayTxnId
        if authCode:
            payment.authCode = authCode
        if referenceCode:
            payment.referenceCode = referenceCode
        if errorCode:
            payment.errorCode = errorCode
        if gatewayTxnDate:
            payment.gatewayTxnDate = gatewayTxnDate
        if status:
            payment.status = status
            if status == PaymentStatus.SUCCESS:
                payment.successTimestamp = datetime.datetime.now()
            if status == PaymentStatus.FAILED:
                payment.errorTimestamp = datetime.datetime.now()
        if attributes: 
            for attr in attributes:
                payment_attr =  PaymentAttribute.query.filter_by(payment=payment, name=attr.name).first()
                if payment_attr:
                    payment_attr.value = attr.value
                else:
                    payment_attr = PaymentAttribute(payment=payment, name = attr.name, value = attr.value)
        session.commit()
        return True
    except:
        return False
        
def close_session():
    if session.is_active:
        print "session is active. closing it."
        session.close()