From aa8fc2ee2118cf59bbd204a864d0c105ecd153e4 Mon Sep 17 00:00:00 2001 From: aibaq Date: Thu, 11 Jun 2020 23:56:41 +0600 Subject: [PATCH] ADD: example views --- README.md | 42 +++++++++++++++++++++++++++++++ test_app/urls.py | 12 +++++---- test_app/utils.py | 3 --- test_app/views.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++ views.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 175 insertions(+), 8 deletions(-) delete mode 100644 test_app/utils.py create mode 100644 test_app/views.py create mode 100644 views.py diff --git a/README.md b/README.md index e69de29..2eb7b56 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,42 @@ +# Installation + +``` +pip install git+ssh://git@github.com/aibaq/django-paybox-api.git@0.0.1 +``` + +# Settings + +``` +INSTALLED_APPS += ['paybox_api'] + +PG_URL = 'https://api.paybox.money/payment.php' +PG_REVOKE_URL = 'https://api.paybox.money/revoke.php' +PG_SECRET = os.getenv('PG_SECRET') +PG_SECRET_TICKET = os.getenv('PG_SECRET_TICKET') +PG_MERCHANT_ID = os.getenv('PG_MERCHANT_ID') +PG_TESTING_MODE = '1' +PG_RESULT_URL = 'http://yourdomain/payments/paybox/result/' +PG_SUCCESS_URL = 'http://yourdomain/payments/paybox/success/' +PG_CHECK_URL = 'http://yourdomain/payments/paybox/check/' +PG_REFUND_URL = 'http://yourdomain/payments/paybox/refund/' +PG_CAPTURE_URL = 'http://yourdomain/payments/paybox/capture/' +PG_FAILURE_URL = 'http://yourdomain/payments/paybox/failure/' +``` + +# Usage + +``` +from app.payments.paybox import PayboxAPI + +PayboxAPI().get_url( + order_id=1, + amount=1000, + description='Iphone11', + salt=PayboxAPI.generate_salt(), + email='testemail@gmail.com', + phone='+77777777777', +) +``` +Open generated url in browser + +handle callbacks at views.py : see test_app diff --git a/test_app/urls.py b/test_app/urls.py index ede2ec9..ebaa0b0 100644 --- a/test_app/urls.py +++ b/test_app/urls.py @@ -1,6 +1,8 @@ -from django.conf.urls import url -from django.contrib import admin +from rest_framework.routers import DefaultRouter -urlpatterns = [ - url(r'^admin/', admin.site.urls), -] +from app.payments import views + + +router = DefaultRouter() +router.register('paybox', views.PayboxViewSet, basename='paybox') +urlpatterns = router.urls diff --git a/test_app/utils.py b/test_app/utils.py deleted file mode 100644 index b16144e..0000000 --- a/test_app/utils.py +++ /dev/null @@ -1,3 +0,0 @@ - -def get_pagetype(request): - return 'Home' if request.path == '/' else 'Not-Home' diff --git a/test_app/views.py b/test_app/views.py new file mode 100644 index 0000000..2450f3a --- /dev/null +++ b/test_app/views.py @@ -0,0 +1,63 @@ +import logging + +from django.http import HttpResponseRedirect +from django.utils.decorators import method_decorator +from django.urls import reverse + +from rest_framework import ( + viewsets, + serializers, +) +from rest_framework.decorators import action +from rest_framework.permissions import AllowAny +from rest_framework.response import Response + +from paybox_api import PayboxAPI + + +logger = logging.getLogger(__name__) + + +class PayboxViewSet(viewsets.ViewSet): + permission_classes = (AllowAny,) + + @action(methods=['post'], detail=False) + @method_decorator(PayboxAPI.verify_paybox()) + def check(self, request): + # Do smth here + return Response() + + @action(methods=['post'], detail=False) + @method_decorator(PayboxAPI.verify_paybox()) + def result(self, request): + body = PayboxAPI.parse_body(request.body) + + if body['pg_result'] == '1': + # Handle success result + else: + # Handle failure result + return Response() + + @action(methods=['post'], detail=False) + @method_decorator(PayboxAPI.verify_paybox()) + def refund(self, request): + # Do smth here + return Response({'refund': 'refund'}) + + @action(methods=['post'], detail=False) + @method_decorator(PayboxAPI.verify_paybox()) + def capture(self, request): + # Do smth here + return Response({'capture': 'capture'}) + + @action(methods=['post'], detail=False) + @method_decorator(PayboxAPI.verify_paybox()) + def success(self, request): + # Do smth here + return HttpResponseRedirect('google.com') + + @action(methods=['post'], detail=False) + @method_decorator(PayboxAPI.verify_paybox()) + def failure(self, request): + # Do smth here + return HttpResponseRedirect('google.com') diff --git a/views.py b/views.py new file mode 100644 index 0000000..2450f3a --- /dev/null +++ b/views.py @@ -0,0 +1,63 @@ +import logging + +from django.http import HttpResponseRedirect +from django.utils.decorators import method_decorator +from django.urls import reverse + +from rest_framework import ( + viewsets, + serializers, +) +from rest_framework.decorators import action +from rest_framework.permissions import AllowAny +from rest_framework.response import Response + +from paybox_api import PayboxAPI + + +logger = logging.getLogger(__name__) + + +class PayboxViewSet(viewsets.ViewSet): + permission_classes = (AllowAny,) + + @action(methods=['post'], detail=False) + @method_decorator(PayboxAPI.verify_paybox()) + def check(self, request): + # Do smth here + return Response() + + @action(methods=['post'], detail=False) + @method_decorator(PayboxAPI.verify_paybox()) + def result(self, request): + body = PayboxAPI.parse_body(request.body) + + if body['pg_result'] == '1': + # Handle success result + else: + # Handle failure result + return Response() + + @action(methods=['post'], detail=False) + @method_decorator(PayboxAPI.verify_paybox()) + def refund(self, request): + # Do smth here + return Response({'refund': 'refund'}) + + @action(methods=['post'], detail=False) + @method_decorator(PayboxAPI.verify_paybox()) + def capture(self, request): + # Do smth here + return Response({'capture': 'capture'}) + + @action(methods=['post'], detail=False) + @method_decorator(PayboxAPI.verify_paybox()) + def success(self, request): + # Do smth here + return HttpResponseRedirect('google.com') + + @action(methods=['post'], detail=False) + @method_decorator(PayboxAPI.verify_paybox()) + def failure(self, request): + # Do smth here + return HttpResponseRedirect('google.com')