Rev 1659 | Blame | Compare with Previous | Last modification | View Log | RSS feed
'''Created on 26-Aug-2010@author: ashish'''import datetimeimport sysfrom elixir import *from sqlalchemy import or_from shop2020.thriftpy.payments.ttypes import PaymentExceptionfrom shop2020.payments.impl.DataService import Payment, PaymentGateway,\PaymentAttributefrom shop2020.thriftpy.payments.ttypes import PaymentStatusfrom shop2020.utils.Utils import to_py_datefrom sqlalchemy.sql.expression import select, funcdef 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 = gatewayIdpayment.userId = userIdpayment.merchantTxnId = txnIdpayment.amount = amountpayment.initTimestamp = datetime.datetime.now()payment.status = PaymentStatus.INITsession.commit()return payment.iddef build_payment_query(from_time, to_time, status, gateway_id):query = Payment.queryif 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 querydef 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 = gatewayPaymentIdif sessionId:payment.sessionId = sessionIdif gatewayTxnStatus:payment.gatewayTxnStatus = gatewayTxnStatusif description:payment.description = descriptionif gatewayTxnId:payment.gatewayTxnId = gatewayTxnIdif authCode:payment.authCode = authCodeif referenceCode:payment.referenceCode = referenceCodeif errorCode:payment.errorCode = errorCodeif gatewayTxnDate:payment.gatewayTxnDate = gatewayTxnDateif status:payment.status = statusif 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.valueelse:payment_attr = PaymentAttribute(payment=payment, name = attr.name, value = attr.value)session.commit()return Trueexcept:return Falsedef close_session():if session.is_active:print "session is active. closing it."session.close()