From e49f1370eed3e4f7001dc5afb1202c1c9ce6a87e Mon Sep 17 00:00:00 2001 From: Ivlev Denis Date: Mon, 26 Feb 2018 15:11:56 +0300 Subject: [PATCH] LIL-274. Add PW callback view --- apps/payment/views.py | 119 ++++++++++++++++++------------------------ project/urls.py | 3 +- 2 files changed, 53 insertions(+), 69 deletions(-) diff --git a/apps/payment/views.py b/apps/payment/views.py index 55c4e775..f381a100 100644 --- a/apps/payment/views.py +++ b/apps/payment/views.py @@ -7,8 +7,9 @@ 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 +from apps.school.models import SchoolSchedule + +from .models import AuthorBalance, CoursePayment, SchoolPayment class CourseBuyView(TemplateView): @@ -22,7 +23,7 @@ class CourseBuyView(TemplateView): course=course, ) product = Product( - f'course_{course.id}', + f'course_{course_payment.id}', course.price, 'RUB', f'Курс "{course.title}"', @@ -42,68 +43,50 @@ class CourseBuyView(TemplateView): 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 +@method_decorator(csrf_exempt, name='dispatch') +class PaymentwallCallbackView(View): + + 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): + payment_raw_data = request.GET.copy() + pingback = Pingback(payment_raw_data, self.get_request_ip()) + + if pingback.validate(): + product_type_name, payment_id = pingback.get_product().get_id().split('_') + + if product_type_name == 'course': + product_payment_class = CoursePayment + elif product_type_name == 'school': + product_payment_class = SchoolPayment + else: + return HttpResponse(status=403) + + try: + payment = product_payment_class.objects.get(pk=payment_id) + except product_payment_class.DoesNotExist: + return HttpResponse(status=403) + + payment.status = pingback.get_type() + payment.data = payment_raw_data + payment.save() + + author_balance = getattr(payment, 'author_balance', None) + if author_balance: + if pingback.is_deliverable(): + payment.author_balance.status = AuthorBalance.ACCEPTED + elif pingback.is_under_review(): + payment.author_balance.status = AuthorBalance.PENDING + else: + payment.author_balance.status = AuthorBalance.DECLINED + + payment.author_balance.save() + return HttpResponse('OK', status=403) + else: + return diff --git a/project/urls.py b/project/urls.py index e325194b..7619ecdf 100644 --- a/project/urls.py +++ b/project/urls.py @@ -29,7 +29,7 @@ from apps.user.views import ( UserView, UserEditView, NotificationEditView, PaymentHistoryView, resend_email_verify, ) -from apps.payment.views import CourseBuyView +from apps.payment.views import CourseBuyView, PaymentwallCallbackView urlpatterns = [ path('admin/', admin.site.urls), @@ -46,6 +46,7 @@ urlpatterns = [ path('course//comment', coursecomment, name='coursecomment'), path('lesson//', LessonView.as_view(), name='lesson'), path('lesson//comment', lessoncomment, name='lessoncomment'), + path('payments/ping', PaymentwallCallbackView.as_view(), name='payment-ping'), 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'),