Browse Source

Add xml response wrapper, and test for pay system

devel
bashmak 8 years ago
parent
commit
4791642b26
  1. 86
      abonapp/pay_systems.py
  2. 198
      abonapp/tests.py
  3. 6
      djing/local_settings.py.template
  4. 5
      requirements.txt

86
abonapp/pay_systems.py

@ -4,23 +4,23 @@ from mydefs import safe_int, safe_float
from .models import Abon, AllTimePayLog from .models import Abon, AllTimePayLog
from django.db import DatabaseError from django.db import DatabaseError
from django.conf import settings from django.conf import settings
from xmlview.decorators import xml_view
SECRET = getattr(settings, 'PAY_SECRET') SECRET = getattr(settings, 'PAY_SECRET')
SERV_ID = getattr(settings, 'PAY_SERV_ID') SERV_ID = getattr(settings, 'PAY_SERV_ID')
@xml_view(root_node='pay-response')
def allpay(request): def allpay(request):
def bad_ret(err_id, err_description=None): def bad_ret(err_id, err_description=None):
current_date = timezone.now()
res = [
"<?xml version='1.0' encoding='UTF-8'?>",
"<pay-response>",
" <status_code>%d</status_code>" % safe_int(err_id),
" <time_stamp>%s</time_stamp>" % current_date.strftime("%d.%m.%Y %H:%M:%S"),
" <description>%s</description>" % err_description if err_description is not None else '',
"</pay-response>"
]
return '\n'.join(res)
now = timezone.now()
r = {
'status_code': safe_int(err_id),
'time_stamp': now.strftime("%d.%m.%Y %H:%M")
}
if err_description:
r.update({'description': err_description})
return r
try: try:
serv_id = request.GET.get('SERVICE_ID') serv_id = request.GET.get('SERVICE_ID')
@ -29,8 +29,10 @@ def allpay(request):
pay_id = request.GET.get('PAY_ID') pay_id = request.GET.get('PAY_ID')
pay_amount = safe_float(request.GET.get('PAY_AMOUNT')) pay_amount = safe_float(request.GET.get('PAY_AMOUNT'))
sign = request.GET.get('SIGN').lower() sign = request.GET.get('SIGN').lower()
current_date = timezone.now().strftime("%d.%m.%Y %H:%M")
if act <= 0: return bad_ret(-101, 'ACT less than zero')
if act <= 0:
return bad_ret(-101, 'ACT less than zero')
# check sign # check sign
md = md5() md = md5()
@ -44,18 +46,16 @@ def allpay(request):
abon = Abon.objects.get(username=pay_account) abon = Abon.objects.get(username=pay_account)
fio = abon.fio fio = abon.fio
ballance = float(abon.ballance) ballance = float(abon.ballance)
current_date = timezone.now().strftime("%d.%m.%Y %H:%M:%S")
return "<?xml version='1.0' encoding='UTF-8'?>\n" \
"<pay-response>\n" \
" <balance>%.2f</balance>\n" % ballance + \
" <name>%s</name>\n" % fio + \
" <account>%s</account>\n" % pay_account + \
" <service_id>%s</service_id>\n" % SERV_ID + \
" <min_amount>10.0</min_amount>\n" \
" <max_amount>50000</max_amount>\n" \
" <status_code>21</status_code>\n" \
" <time_stamp>%s</time_stamp>\n" % current_date + \
"</pay-response>"
return {
'balance': ballance,
'name': fio,
'account': pay_account,
'service_id': SERV_ID,
'min_amount': 10.0,
'max_amount': 5000,
'status_code': 21,
'time_stamp': current_date
}
elif act == 4: elif act == 4:
trade_point = safe_int(request.GET.get('TRADE_POINT')) trade_point = safe_int(request.GET.get('TRADE_POINT'))
receipt_num = safe_int(request.GET.get('RECEIPT_NUM')) receipt_num = safe_int(request.GET.get('RECEIPT_NUM'))
@ -74,30 +74,26 @@ def allpay(request):
trade_point=trade_point, trade_point=trade_point,
receipt_num=receipt_num receipt_num=receipt_num
) )
current_date = timezone.now().strftime("%d.%m.%Y %H:%M:%S")
return "<?xml version='1.0' encoding='UTF-8'?>" \
"<pay-response>\n" + \
" <pay_id>%s</pay_id>\n" % pay_id + \
" <service_id>%s</service_id>\n" % serv_id + \
" <amount>%.2f</amount>\n" % pay_amount + \
" <status_code>22</status_code>\n" + \
" <time_stamp>%s</time_stamp>\n" % current_date + \
"</pay-response>"
return {
'pay_id': pay_id,
'service_id': serv_id,
'amount': pay_amount,
'status_code': 22,
'time_stamp': current_date
}
elif act == 7: elif act == 7:
pay = AllTimePayLog.objects.get(pay_id=pay_id) pay = AllTimePayLog.objects.get(pay_id=pay_id)
current_date = timezone.now().strftime("%d.%m.%Y %H:%M:%S")
return "<?xml version='1.0' encoding='UTF-8'?>\n" \
"<pay-response>\n" \
" <status_code>11</status_code>\n" \
" <time_stamp>%s</time_stamp>\n" % current_date + \
" <transaction>\n" \
" <pay_id>%s</pay_id>\n" % pay_id + \
" <service_id>%s</service_id>\n" % serv_id + \
" <amount>%.2f</amount>\n" % float(pay.summ) + \
" <status>111</status>\n" + \
" <time_stamp>%s</time_stamp>\n" % current_date + \
" </transaction>\n" \
"</pay-response>"
return {
'status_code': 11,
'time_stamp': current_date,
'transaction': {
'pay_id': pay_id,
'service_id': serv_id,
'amount': pay.summ,
'status': 111,
'time_stamp': pay.date_add.strftime("%d.%m.%Y %H:%M")
}
}
else: else:
return bad_ret(-101, 'ACT is not passed') return bad_ret(-101, 'ACT is not passed')

198
abonapp/tests.py

@ -0,0 +1,198 @@
from hashlib import md5
from django.test import TestCase, RequestFactory
from django.conf import settings
from django.utils import timezone
from xmltodict import parse
from abonapp.models import Abon
from abonapp.pay_systems import allpay
rf = RequestFactory()
SERVICE_ID = getattr(settings, 'PAY_SERV_ID')
SECRET = getattr(settings, 'PAY_SECRET')
def _make_sign(act: int, pay_account: str, serv_id: str, pay_id):
md = md5()
s = "%d_%s_%s_%s_%s" % (act, pay_account, serv_id, pay_id, SECRET)
md.update(bytes(s, 'utf-8'))
return md.hexdigest()
class AllPayTestCase(TestCase):
pay_url = '/'
time_format = '%d.%m.%Y %H:%M'
def setUp(self):
a1 = Abon.objects.create_user(
telephone='+79785276481',
username='pay_account1',
password='passw1'
)
a1.ballance = -13.12
a1.fio = 'Test Name'
a1.save(update_fields=['ballance', 'fio'])
# Abon.objects.create_user(
# telephone='+79788163841',
# username='pay_account2',
# password='passw2'
# )
def user_pay_view(self):
print('test_user_pay_view')
current_date = timezone.now().strftime(self.time_format)
r = allpay(rf.get(self.pay_url, {
'ACT': 1,
'PAY_ACCOUNT': 'pay_account1',
'SERVICE_ID': SERVICE_ID,
'PAY_ID': '840ab457-e7d1-4494-8197-9570da035170',
'TRADE_POINT': 'term1',
'SIGN': _make_sign(1, 'pay_account1', SERVICE_ID, '840ab457-e7d1-4494-8197-9570da035170')
}
))
r = r.content.decode('utf-8')
self.assertXMLEqual(r, ''.join([
"<pay-response>",
"<balance>-13.12</balance>",
"<name>Test Name</name>",
"<account>pay_account1</account>",
"<service_id>%s</service_id>" % SERVICE_ID,
"<min_amount>10.0</min_amount>",
"<max_amount>5000</max_amount>",
"<status_code>21</status_code>",
"<time_stamp>%s</time_stamp>" % current_date,
"</pay-response>"
]))
def user_pay_pay(self):
print('test_user_pay_pay')
current_date = timezone.now().strftime(self.time_format)
r = allpay(rf.get(self.pay_url, {
'ACT': 4,
'PAY_ACCOUNT': 'pay_account1',
'PAY_AMOUNT': 18.21,
'RECEIPT_NUM': 2126235,
'SERVICE_ID': SERVICE_ID,
'PAY_ID': '840ab457-e7d1-4494-8197-9570da035170',
'TRADE_POINT': 'term1',
'SIGN': _make_sign(4, 'pay_account1', SERVICE_ID, '840ab457-e7d1-4494-8197-9570da035170')
}))
r = r.content.decode('utf-8')
xml = ''.join([
"<pay-response>",
"<pay_id>840ab457-e7d1-4494-8197-9570da035170</pay_id>",
"<service_id>%s</service_id>" % SERVICE_ID,
"<amount>18.21</amount>",
"<status_code>22</status_code>",
"<time_stamp>%s</time_stamp>" % current_date,
"</pay-response>"
])
self.test_pay_time = current_date
self.assertXMLEqual(r, xml)
def user_pay_check(self):
print('test_user_pay_check')
current_date = timezone.now().strftime(self.time_format)
r = allpay(rf.get(self.pay_url,
{
'ACT': 7,
'SERVICE_ID': SERVICE_ID,
'PAY_ID': '840ab457-e7d1-4494-8197-9570da035170',
'SIGN': _make_sign(7, '', SERVICE_ID, '840ab457-e7d1-4494-8197-9570da035170')
}
))
r = r.content.decode('utf-8')
xml = ''.join([
"<pay-response>",
"<status_code>11</status_code>",
"<time_stamp>%s</time_stamp>" % current_date,
"<transaction>",
"<pay_id>840ab457-e7d1-4494-8197-9570da035170</pay_id>",
"<service_id>%s</service_id>" % SERVICE_ID,
"<amount>18.21</amount>",
"<status>111</status>",
"<time_stamp>%s</time_stamp>" % self.test_pay_time,
"</transaction>"
"</pay-response>"
])
self.assertXMLEqual(r, xml)
def check_ballance(self):
print('check_ballance')
r = allpay(rf.get(self.pay_url,
{
'ACT': 1,
'PAY_ACCOUNT': 'pay_account1',
'SERVICE_ID': SERVICE_ID,
'PAY_ID': '840ab457-e7d1-4494-8197-9570da035170',
'TRADE_POINT': 'term1',
'SIGN': _make_sign(1, 'pay_account1', SERVICE_ID, '840ab457-e7d1-4494-8197-9570da035170')
}
))
r = r.content.decode('utf-8')
r = parse(r)
bl = float(r['pay-response']['balance'])
self.assertEqual(bl, 5.09)
def test_client_does_not_exist(self):
print('test_client_does_not_exist')
current_date = timezone.now().strftime(self.time_format)
r = allpay(rf.get(self.pay_url, {
'ACT': 1,
'PAY_ACCOUNT': 'not_existing_acc',
'SERVICE_ID': SERVICE_ID,
'PAY_ID': '840ab457-e7d1-4494-8197-9570da035170',
'TRADE_POINT': 'term1',
'SIGN': _make_sign(1, 'not_existing_acc', SERVICE_ID, '840ab457-e7d1-4494-8197-9570da035170')
}
))
r = r.content.decode('utf-8')
self.assertXMLEqual(r, ''.join([
"<pay-response>",
"<status_code>-40</status_code>",
"<time_stamp>%s</time_stamp>" % current_date,
"</pay-response>"
]))
def try_pay_double(self):
print('try_pay_double')
r = allpay(rf.get(self.pay_url, {
'ACT': 4,
'PAY_ACCOUNT': 'pay_account1',
'SERVICE_ID': SERVICE_ID,
'PAY_ID': '840ab457-e7d1-4494-8197-9570da035170',
'TRADE_POINT': 'term1',
'SIGN': _make_sign(4, 'pay_account1', SERVICE_ID, '840ab457-e7d1-4494-8197-9570da035170')
}))
r = r.content.decode('utf-8')
r = parse(r)
status_code = int(r['pay-response']['status_code'])
self.assertEqual(status_code, -100)
def non_existing_pay(self):
print('non_existing_pay')
current_date = timezone.now().strftime(self.time_format)
uuid = '9f154e93-d800-419a-92f7-da33529138be'
r = allpay(rf.get(self.pay_url, {
'ACT': 7,
'SERVICE_ID': SERVICE_ID,
'PAY_ID': uuid,
'SIGN': _make_sign(7, '', SERVICE_ID, uuid)
}))
r = r.content.decode('utf-8')
xml = ''.join([
"<pay-response>",
"<status_code>-10</status_code>",
"<time_stamp>%s</time_stamp>" % current_date,
"</pay-response>"
])
self.assertXMLEqual(r, xml)
def test_pays(self):
self.user_pay_view()
self.user_pay_pay()
self.user_pay_check()
self.check_ballance()
self.try_pay_double()
self.non_existing_pay()

6
djing/local_settings.py.template

@ -25,7 +25,11 @@ DATABASES = {
'NAME': 'djing_db', 'NAME': 'djing_db',
'USER': 'DJING_MYSQL_USERNAME', # You can change the user name 'USER': 'DJING_MYSQL_USERNAME', # You can change the user name
'PASSWORD': 'DJING_MYSQL_PASSWORD', # You can change the password 'PASSWORD': 'DJING_MYSQL_PASSWORD', # You can change the password
'HOST': 'localhost'
'HOST': 'localhost',
'TEST': {
'CHARSET': 'utf8',
'COLLATION': 'utf8_general_ci'
}
} }
} }

5
requirements.txt

@ -22,4 +22,7 @@ webdavclient
pyst2 pyst2
django-jsonview django-jsonview
django-bitfield django-bitfield
transliterate
transliterate
# django-xmlview for pay system allpay
-e git://github.com/nerosketch/django-xmlview.git#egg=django-xmlview
Loading…
Cancel
Save