Browse Source

pay forward

devel
Dmitry Novikov 8 years ago
parent
commit
c03dbba00b
  1. 3
      abonapp/urls.py
  2. 104
      abonapp/views.py
  3. 83
      forward_pay.php

3
abonapp/urls.py

@ -62,5 +62,6 @@ urlpatterns = [
# Api's # Api's
path('api/abons/', views.abons), path('api/abons/', views.abons),
path('api/abon_filter/', views.search_abon), path('api/abon_filter/', views.search_abon),
path('api/dhcp_lever/', views.DhcpLever.as_view())
path('api/dhcp_lever/', views.DhcpLever.as_view()),
path('api/duplicate_pay/', views.DublicatePay.as_view())
] ]

104
abonapp/views.py

@ -2,13 +2,14 @@ from ipaddress import ip_address
from typing import Dict, Optional from typing import Dict, Optional
from datetime import datetime, date from datetime import datetime, date
from django.core.exceptions import PermissionDenied, ValidationError from django.core.exceptions import PermissionDenied, ValidationError
from django.db import IntegrityError, ProgrammingError, transaction, OperationalError
from django.db import IntegrityError, ProgrammingError, transaction, OperationalError, DatabaseError
from django.db.models import Count, Q from django.db.models import Count, Q
from django.shortcuts import render, redirect, get_object_or_404, resolve_url from django.shortcuts import render, redirect, get_object_or_404, resolve_url
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseRedirect from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseRedirect
from django.contrib import messages from django.contrib import messages
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.utils import timezone
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.views.generic import ListView, UpdateView, CreateView, DeleteView from django.views.generic import ListView, UpdateView, CreateView, DeleteView
@ -33,6 +34,7 @@ from djing import ping
from djing import lib from djing import lib
from djing.lib.decorators import json_view, only_admins from djing.lib.decorators import json_view, only_admins
from djing.global_base_views import OrderedFilteredList, SecureApiView from djing.global_base_views import OrderedFilteredList, SecureApiView
from xmlview.decorators import xml_view
login_decs = login_required, only_admins login_decs = login_required, only_admins
@ -1325,3 +1327,103 @@ class DhcpLever(SecureApiView):
print('Duplicate:', e) print('Duplicate:', e)
return str(e) return str(e)
class DublicatePay(SecureApiView):
http_method_names = 'get',
@staticmethod
def _bad_ret(err_id, err_description=None):
now = timezone.now()
r = {
'status_code': lib.safe_int(err_id),
'time_stamp': now.strftime("%d.%m.%Y %H:%M")
}
if err_description:
r.update({'description': err_description})
return r
@method_decorator(xml_view(root_node='pay-response'))
def get(self, request, *args, **kwargs):
act = lib.safe_int(request.GET.get('ACT'))
self.current_date = timezone.now().strftime("%d.%m.%Y %H:%M")
if act <= 0:
return self._bad_ret(-101, 'ACT less than zero')
try:
if act == 1:
return self._fetch_user_info(request.GET)
elif act == 4:
return self._make_pay(request.GET)
elif act == 7:
return self._check_pay(request.GET)
else:
return self._bad_ret(-101, 'ACT is not passed')
except models.Abon.DoesNotExist:
return self._bad_ret(-40)
except DatabaseError:
return self._bad_ret(-90)
except models.AllTimePayLog.DoesNotExist:
return self._bad_ret(-10)
except AttributeError:
return self._bad_ret(-101)
def _fetch_user_info(self, data: dict):
pay_account = data.get('PAY_ACCOUNT')
abon = models.Abon.objects.get(pk=pay_account)
fio = abon.fio
ballance = float(abon.ballance)
return {
'balance': ballance,
'name': fio,
'account': pay_account,
'service_id': getattr(settings, 'PAY_SERV_ID'),
'min_amount': 10.0,
'max_amount': 5000,
'status_code': 21,
'time_stamp': self.current_date
}
def _make_pay(self, data: dict):
trade_point = lib.safe_int(data.get('TRADE_POINT'))
receipt_num = lib.safe_int(data.get('RECEIPT_NUM'))
pay_account = data.get('PAY_ACCOUNT')
pay_id = data.get('PAY_ID')
pay_amount = lib.safe_float(data.get('PAY_AMOUNT'))
abon = models.Abon.objects.get(pk=pay_account)
pays = models.AllTimePayLog.objects.filter(pay_id=pay_id)
if pays.count() > 0:
return self._bad_ret(-100)
abon.add_ballance(None, pay_amount, comment='KonikaForward %.2f' % pay_amount)
abon.save(update_fields=('ballance',))
models.AllTimePayLog.objects.create(
pay_id=pay_id,
summ=pay_amount,
abon=abon,
trade_point=trade_point,
receipt_num=receipt_num
)
return {
'pay_id': pay_id,
'service_id': data.get('SERVICE_ID'),
'amount': pay_amount,
'status_code': 22,
'time_stamp': self.current_date
}
def _check_pay(self, data: dict):
pay_id = data.get('PAY_ID')
pay = models.AllTimePayLog.objects.get(pay_id=pay_id)
return {
'status_code': 11,
'time_stamp': self.current_date,
'transaction': {
'pay_id': pay_id,
'service_id': data.get('SERVICE_ID'),
'amount': pay.summ,
'status': 111,
'time_stamp': pay.date_add.strftime("%d.%m.%Y %H:%M")
}
}

83
forward_pay.php

@ -0,0 +1,83 @@
#!/usr/bin/env php
<?php
define("API_AUTH_SECRET", "secret");
define("SERVER_DOMAIN", 'http://localhost:8000');
define('N', '
');
function calc_hash($str)
{
$hash = bin2hex(mhash(MHASH_SHA256,$str));
return $hash;
}
function make_sign($data)
{
asort($data, SORT_STRING);
array_push($data, API_AUTH_SECRET);
$str_to_hash = join('_', $data);
return calc_hash($str_to_hash);
}
function send_to($data)
{
$sign = make_sign($data);
$data['sign'] = $sign;
$url_params = http_build_query($data);
$r = file_get_contents(SERVER_DOMAIN."/abons/api/duplicate_pay/?".$url_params);
return $r;
}
function forward_pay_request($act, $pay_account, $service_id, $trade_point, $receipt_num, $pay_id, $pay_amount)
{
require('./users_uname_pk_pairs.php');
// $user_id_pairs
if($act == 1)
{
$pay = [
"ACT" => 1,
"PAY_ACCOUNT" => $user_id_pairs[$pay_account]
];
return send_to($pay);
}else if($act == 4)
{
$pay = [
"ACT" => 4,
"PAY_ACCOUNT" => $user_id_pairs[$pay_account],
"TRADE_POINT" => $trade_point,
"RECEIPT_NUM" => $receipt_num,
"PAY_ID" => $pay_id,
"PAY_AMOUNT" => $pay_amount,
"SERVICE_ID" => $service_id
];
return send_to($pay);
}else if($act == 7)
{
$pay = [
"ACT" => 7,
"PAY_ID" => $pay_id,
"SERVICE_ID" => $service_id
];
return send_to($pay);
}
}
# Request
echo forward_pay_request(1, '1234', null, null, null, null, null);
# Add cash
echo forward_pay_request('4', '1234', 'mypaysrv', '3432', '289473', '897879-989-68669', '1');
# check cash
echo forward_pay_request(7, null, 'mypaysrv', null, null, '897879-989-68669', null);
?>
Loading…
Cancel
Save