| 420 |
ashish |
1 |
'''
|
|
|
2 |
Created on 26-Aug-2010
|
|
|
3 |
|
|
|
4 |
@author: ashish
|
|
|
5 |
'''
|
|
|
6 |
from shop2020.payment.ttypes import PaymentException
|
|
|
7 |
from shop2020.payments.impl.DataService import Pmnt
|
|
|
8 |
import datetime
|
|
|
9 |
from shop2020.thriftpy.payments import PaymentService
|
|
|
10 |
from elixir import *
|
|
|
11 |
from shop2020.utils.Utils import to_py_date
|
|
|
12 |
from shop2020.thriftpy import payments
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
|
16 |
def create_payment(user_id, cart_id, amount, gateway_id):
|
|
|
17 |
if not user_id:
|
|
|
18 |
raise PaymentException(101, "user cannot be null")
|
|
|
19 |
if not cart_id:
|
|
|
20 |
raise PaymentException(101, "cart cannot be null")
|
|
|
21 |
|
|
|
22 |
if not amount:
|
|
|
23 |
raise PaymentException(101, "Amount cannot be null")
|
|
|
24 |
if not gateway_id:
|
|
|
25 |
raise PaymentException(101, "Gateway cannot be null")
|
|
|
26 |
|
|
|
27 |
payment = Pmnt()
|
|
|
28 |
payment.user_id = user_id
|
|
|
29 |
payment.cart_id = cart_id
|
|
|
30 |
payment.amt = amount
|
|
|
31 |
payment.mid = str(cart_id)
|
|
|
32 |
payment.init_ts = datetime.datetime.now()
|
|
|
33 |
payment.ps = PaymentService.PaymentStatus.INIT
|
|
|
34 |
session.commit()
|
|
|
35 |
return payment.id
|
|
|
36 |
|
|
|
37 |
def get_payments_for_user(user_id, from_time, to_time, status, gateway_id):
|
|
|
38 |
|
|
|
39 |
if not user_id:
|
|
|
40 |
raise PaymentException(101, "user cannot be null")
|
|
|
41 |
|
|
|
42 |
query = Pmnt.query.filter_by(user_id=user_id)
|
|
|
43 |
if from_time:
|
|
|
44 |
from_date = to_py_date(from_time)
|
|
|
45 |
query = query.filter(Pmnt.init_ts >= from_date)
|
|
|
46 |
if to_time:
|
|
|
47 |
to_date = to_py_date(to_time)
|
|
|
48 |
query = query.filter(Pmnt.init_ts <= to_date)
|
|
|
49 |
|
|
|
50 |
if status != -1:
|
|
|
51 |
query = query.filter(Pmnt.ps == status)
|
|
|
52 |
|
|
|
53 |
if gateway_id:
|
|
|
54 |
query = query.filter(Pmnt.gateway == gateway_id)
|
|
|
55 |
|
|
|
56 |
try:
|
|
|
57 |
payments = query.all()
|
|
|
58 |
return payments
|
|
|
59 |
except:
|
|
|
60 |
return None
|
|
|
61 |
|
|
|
62 |
|
|
|
63 |
def get_payments_for_cart(cart_id, from_time, to_time, status, gateway_id):
|
|
|
64 |
|
|
|
65 |
if not cart_id:
|
|
|
66 |
raise PaymentException(101, "user cannot be null")
|
|
|
67 |
|
|
|
68 |
query = Pmnt.query.filter_by(cart_id=cart_id)
|
|
|
69 |
if from_time:
|
|
|
70 |
from_date = to_py_date(from_time)
|
|
|
71 |
query = query.filter(Pmnt.init_ts >= from_date)
|
|
|
72 |
if to_time:
|
|
|
73 |
to_date = to_py_date(to_time)
|
|
|
74 |
query = query.filter(Pmnt.init_ts <= to_date)
|
|
|
75 |
|
|
|
76 |
if status != -1:
|
|
|
77 |
query = query.filter(Pmnt.ps == status)
|
|
|
78 |
|
|
|
79 |
if gateway_id:
|
|
|
80 |
query = query.filter(Pmnt.gateway == gateway_id)
|
|
|
81 |
|
|
|
82 |
try:
|
|
|
83 |
payments = query.all()
|
|
|
84 |
return payments
|
|
|
85 |
except:
|
|
|
86 |
return None
|
|
|
87 |
|
|
|
88 |
def get_payments(from_time, to_time, status, gateway_id):
|
|
|
89 |
|
|
|
90 |
|
|
|
91 |
query = Pmnt.query
|
|
|
92 |
if from_time:
|
|
|
93 |
from_date = to_py_date(from_time)
|
|
|
94 |
query = query.filter(Pmnt.init_ts >= from_date)
|
|
|
95 |
if to_time:
|
|
|
96 |
to_date = to_py_date(to_time)
|
|
|
97 |
query = query.filter(Pmnt.init_ts <= to_date)
|
|
|
98 |
|
|
|
99 |
if status != -1:
|
|
|
100 |
query = query.filter(Pmnt.ps == status)
|
|
|
101 |
|
|
|
102 |
if gateway_id:
|
|
|
103 |
query = query.filter(Pmnt.gateway == gateway_id)
|
|
|
104 |
|
|
|
105 |
try:
|
|
|
106 |
payments = query.all()
|
|
|
107 |
return payments
|
|
|
108 |
except:
|
|
|
109 |
return None
|
|
|
110 |
|
|
|
111 |
def change_payment_status(id, status):
|
|
|
112 |
try:
|
|
|
113 |
payment = Pmnt.get_by(id=id)
|
|
|
114 |
payment.ps = status
|
|
|
115 |
session.commit()
|
|
|
116 |
except:
|
|
|
117 |
raise PaymentException(101, "no payment with such id")
|
|
|
118 |
|
|
|
119 |
def get_payment(id):
|
|
|
120 |
try:
|
|
|
121 |
payment = Pmnt.get_by(id=id)
|
|
|
122 |
return payment
|
|
|
123 |
|
|
|
124 |
except:
|
|
|
125 |
raise PaymentException(101, "no payment with such id")
|
|
|
126 |
|
|
|
127 |
def add_bank_details(id, bid, btxid, error_code, session_id, postdate, auth_code, ref_code):
|
|
|
128 |
try:
|
|
|
129 |
payment = Pmnt.get_by(id=id)
|
|
|
130 |
return payment
|
|
|
131 |
|
|
|
132 |
except:
|
|
|
133 |
raise PaymentException(101, "no payment with such id")
|
|
|
134 |
|
|
|
135 |
|
|
|
136 |
payment.bid = bid
|
|
|
137 |
payment.btxid = btxid
|
|
|
138 |
payment.error_code = error_code
|
|
|
139 |
payment.session_id = session_id
|
|
|
140 |
payment.post_date = postdate
|
|
|
141 |
payment.ref_code = ref_code
|
|
|
142 |
payment.auth_code = auth_code
|
|
|
143 |
|
|
|
144 |
|
|
|
145 |
|
|
|
146 |
session.commit()
|
|
|
147 |
|