From 8a0cd6bf810962888c6855a38cc52bb36bffdc3d Mon Sep 17 00:00:00 2001 From: Ivlev Denis Date: Thu, 22 Feb 2018 15:43:23 +0300 Subject: [PATCH] LIL-268, LIL-274. Add buy templates & integrate paymentwall widget --- .../templates/payment/payment_error.html | 12 ++ .../templates/payment/payment_success.html | 12 ++ .../templates/payment/paymentwall_widget.html | 9 + apps/payment/views.py | 177 ++++++++++-------- project/settings.py | 8 + project/urls.py | 4 + 6 files changed, 149 insertions(+), 73 deletions(-) create mode 100644 apps/payment/templates/payment/payment_error.html create mode 100644 apps/payment/templates/payment/payment_success.html create mode 100644 apps/payment/templates/payment/paymentwall_widget.html diff --git a/apps/payment/templates/payment/payment_error.html b/apps/payment/templates/payment/payment_error.html new file mode 100644 index 00000000..6e3cbdcb --- /dev/null +++ b/apps/payment/templates/payment/payment_error.html @@ -0,0 +1,12 @@ +{% extends "templates/lilcity/index.html" %} {% load static %} {% block content %} +
+
+
+
Произошла ошибка!
+ +
+
+
+{% endblock content %} diff --git a/apps/payment/templates/payment/payment_success.html b/apps/payment/templates/payment/payment_success.html new file mode 100644 index 00000000..4d22d6fa --- /dev/null +++ b/apps/payment/templates/payment/payment_success.html @@ -0,0 +1,12 @@ +{% extends "templates/lilcity/index.html" %} {% load static %} {% block content %} +
+
+
+
Вы успешно приобрели курс!
+ +
+
+
+{% endblock content %} diff --git a/apps/payment/templates/payment/paymentwall_widget.html b/apps/payment/templates/payment/paymentwall_widget.html new file mode 100644 index 00000000..bdde28e0 --- /dev/null +++ b/apps/payment/templates/payment/paymentwall_widget.html @@ -0,0 +1,9 @@ +{% extends "templates/lilcity/index.html" %} {% load static %} {% block content %} +
+
+ {% autoescape off %} + {{ widget }} + {% endautoescape %} +
+
+{% endblock content %} diff --git a/apps/payment/views.py b/apps/payment/views.py index b1c64c7e..f3da0799 100644 --- a/apps/payment/views.py +++ b/apps/payment/views.py @@ -1,76 +1,107 @@ from django.utils.decorators import method_decorator -from django.views.generic import View +from django.views.generic import View, TemplateView from django.views.decorators.csrf import csrf_exempt from django.http import HttpResponse - -from paymentwall.pingback import Pingback - -from .models import Purchase - - -@method_decorator(csrf_exempt, name='dispatch') -class PaymentwallCallbackView(View): - - CHARGEBACK = '1' - CREDIT_CARD_FRAUD = '2' - ORDER_FRAUD = '3' - BAD_DATA = '4' - FAKE_PROXY_USER = '5' - REJECTED_BY_ADVERTISER = '6' - DUPLICATED_CONVERSIONS = '7' - GOODWILL_CREDIT_TAKEN_BACK = '8' - CANCELLED_ORDER = '9' - PARTIALLY_REVERSED = '10' - - def get_request_ip(self): - x_forwarded_for = self.request.META.get('HTTP_X_FORWARDED_FOR') - if x_forwarded_for: - ip = x_forwarded_for.split(',')[0] - else: - ip = self.request.META.get('REMOTE_ADDR') - return ip - - def get(self, request, *args, **kwargs): - pingback = Pingback(request.GET.copy(), self.get_request_ip()) - - if pingback.validate(): - cart_id = pingback.get_product().get_id() - - # try: - # cart = CartModel.objects.get(pk=cart_id) - # except CartModel.DoesNotExist: - # log.error('Paymentwall pingback: Cant find cart, Paymentwall sent this data: {}'.format(request.GET.copy())) - # return HttpResponse(status=403) - - try: - purchase = Purchase.objects.get(transaction_id=pingback.get_reference_id()) - except Purchase.DoesNotExist: - # purchase = cart.create_purchase(transaction_id=pingback.get_reference_id()) - pass - - if pingback.is_deliverable(): - purchase.status = Purchase.COMPLETE - - elif pingback.is_cancelable(): - reason = pingback.get_parameter('reason') - - if reason == self.CHARGEBACK or reason == self.CREDIT_CARD_FRAUD or reason == self.ORDER_FRAUD or reason == self.PARTIALLY_REVERSED: - purchase.status = Purchase.CHARGEBACK - elif reason == self.CANCELLED_ORDER: - purchase.status = Purchase.REFUNDED - else: - purchase.status = Purchase.ERROR - - elif pingback.is_under_review(): - purchase.status = Purchase.PENDING - - else: - # log.error('Paymentwall pingback: Unknown pingback type, Paymentwall sent this data: {}'.format(request.GET.copy())) - pass - - # purchase.save() - return HttpResponse('OK', status=200) - else: - # log.error('Paymentwall pingback: Cant validate pingback, error: {} Paymentwall sent this data: {}'.format(pingback.get_error_summary(), request.GET.copy())) - pass - +from django.urls import reverse_lazy + +from paymentwall import Pingback, Product, Widget + +from apps.course.models import Course +from .models import CoursePayment +# from .models import Purchase + + +class CourseBuyView(TemplateView): + template_name = 'payment/paymentwall_widget.html' + + def get(self, request, pk=None, *args, **kwargs): + course = Course.objects.get(id=pk) + course_payment = CoursePayment.objects.create( + user=request.user, + course=course, + ) + product = Product( + f'course_{course.id}', + course.price, + 'RUB', + 'test', + ) + widget = Widget( + request.user.id, + 'pw', + [product], + extra_params={ + 'lang': 'ru_RU', + 'success_url': reverse_lazy('payment-success'), + 'failure_url': reverse_lazy('payment-error'), + } + ) + + return self.render_to_response(context={'widget': widget.get_html_code()}) + + +# @method_decorator(csrf_exempt, name='dispatch') +# class PaymentwallCallbackView(View): + +# CHARGEBACK = '1' +# CREDIT_CARD_FRAUD = '2' +# ORDER_FRAUD = '3' +# BAD_DATA = '4' +# FAKE_PROXY_USER = '5' +# REJECTED_BY_ADVERTISER = '6' +# DUPLICATED_CONVERSIONS = '7' +# GOODWILL_CREDIT_TAKEN_BACK = '8' +# CANCELLED_ORDER = '9' +# PARTIALLY_REVERSED = '10' + +# def get_request_ip(self): +# x_forwarded_for = self.request.META.get('HTTP_X_FORWARDED_FOR') +# if x_forwarded_for: +# ip = x_forwarded_for.split(',')[0] +# else: +# ip = self.request.META.get('REMOTE_ADDR') +# return ip + +# def get(self, request, *args, **kwargs): +# pingback = Pingback(request.GET.copy(), self.get_request_ip()) + +# if pingback.validate(): +# cart_id = pingback.get_product().get_id() + +# # try: +# # cart = CartModel.objects.get(pk=cart_id) +# # except CartModel.DoesNotExist: +# # log.error('Paymentwall pingback: Cant find cart, Paymentwall sent this data: {}'.format(request.GET.copy())) +# # return HttpResponse(status=403) + +# try: +# purchase = Purchase.objects.get(transaction_id=pingback.get_reference_id()) +# except Purchase.DoesNotExist: +# # purchase = cart.create_purchase(transaction_id=pingback.get_reference_id()) +# pass + +# if pingback.is_deliverable(): +# purchase.status = Purchase.COMPLETE + +# elif pingback.is_cancelable(): +# reason = pingback.get_parameter('reason') + +# if reason == self.CHARGEBACK or reason == self.CREDIT_CARD_FRAUD or reason == self.ORDER_FRAUD or reason == self.PARTIALLY_REVERSED: +# purchase.status = Purchase.CHARGEBACK +# elif reason == self.CANCELLED_ORDER: +# purchase.status = Purchase.REFUNDED +# else: +# purchase.status = Purchase.ERROR + +# elif pingback.is_under_review(): +# purchase.status = Purchase.PENDING + +# else: +# # log.error('Paymentwall pingback: Unknown pingback type, Paymentwall sent this data: {}'.format(request.GET.copy())) +# pass + +# # purchase.save() +# return HttpResponse('OK', status=200) +# else: +# # log.error('Paymentwall pingback: Cant validate pingback, error: {} Paymentwall sent this data: {}'.format(pingback.get_error_summary(), request.GET.copy())) +# pass diff --git a/project/settings.py b/project/settings.py index 170b11bf..2af789b3 100644 --- a/project/settings.py +++ b/project/settings.py @@ -232,6 +232,14 @@ try: except ImportError: pass +try: + from paymentwall import * +except ImportError: + pass +else: + Paymentwall.set_api_type(Paymentwall.API_GOODS) + Paymentwall.set_app_key('d6f02b90cf6b16220932f4037578aff7') + Paymentwall.set_secret_key('4ea515bf94e34cf28646c2e12a7b8707') # CORS settings diff --git a/project/urls.py b/project/urls.py index 2df911b7..e325194b 100644 --- a/project/urls.py +++ b/project/urls.py @@ -29,6 +29,7 @@ from apps.user.views import ( UserView, UserEditView, NotificationEditView, PaymentHistoryView, resend_email_verify, ) +from apps.payment.views import CourseBuyView urlpatterns = [ path('admin/', admin.site.urls), @@ -38,12 +39,15 @@ urlpatterns = [ path('course/on-moderation', CourseOnModerationView.as_view(), name='course-on-moderation'), path('course//', CourseView.as_view(), name='course'), path('course//', CourseView.as_view(), name='course'), + path('course//checkout', CourseBuyView.as_view(), name='course-checkout'), path('course//edit', CourseEditView.as_view(), name='course_edit'), path('course//lessons', CourseView.as_view(template_name='course/course_only_lessons.html'), name='course-only-lessons'), path('course//like', likes, name='likes'), path('course//comment', coursecomment, name='coursecomment'), path('lesson//', LessonView.as_view(), name='lesson'), path('lesson//comment', lessoncomment, name='lessoncomment'), + path('payments/success', TemplateView.as_view(template_name='payment/payment_success.html'), name='payment-success'), + path('payments/error', TemplateView.as_view(template_name='payment/payment_error.html'), name='payment-error'), path('search/', SearchView.as_view(), name='search'), path('user//', UserView.as_view(), name='user'), path('user//edit', UserEditView.as_view(), name='user-edit-profile'),