| 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:
|
|
|
92 |
payment = Payment.get_by(id)
|
|
|
93 |
if gatewayPaymentId:
|
|
|
94 |
payment.gatewayPaymentId = gatewayPaymentId
|
|
|
95 |
if gatewayTxnStatus:
|
|
|
96 |
payment.gatewayTxnStatus = gatewayTxnStatus
|
|
|
97 |
if description:
|
|
|
98 |
payment.description = description
|
|
|
99 |
if gatewayTxnId:
|
|
|
100 |
payment.gatewayTxnId = gatewayTxnId
|
|
|
101 |
if authCode:
|
|
|
102 |
payment.authCode = authCode
|
|
|
103 |
if referenceCode:
|
|
|
104 |
payment.referenceCode = referenceCode
|
|
|
105 |
if errorCode:
|
|
|
106 |
payment.errorCode = errorCode
|
|
|
107 |
if status:
|
|
|
108 |
payment.status = status
|
|
|
109 |
for attr in attributes:
|
|
|
110 |
payment_attr = PaymentAttribute.query.filter_by(payment=payment, name=attr.name)
|
|
|
111 |
if payment_attr:
|
|
|
112 |
payment_attr.value = attr.value
|
|
|
113 |
else:
|
|
|
114 |
payment_attr = PaymentAttribute(payment=payment, name = attr.name, value = attr.value)
|
|
|
115 |
session.commit()
|
|
|
116 |
return True
|
|
|
117 |
except:
|
|
|
118 |
return False
|