|
|
|
|
@ -1,4 +1,6 @@ |
|
|
|
|
import csv |
|
|
|
|
import logging |
|
|
|
|
import datetime |
|
|
|
|
|
|
|
|
|
import requests |
|
|
|
|
from django.contrib.auth import get_user_model |
|
|
|
|
@ -18,6 +20,8 @@ from finance.serializers import BillSerializer, InvoiceSerializer |
|
|
|
|
from lms.global_decorators import transaction_decorator |
|
|
|
|
from lms.tools import get_real_name |
|
|
|
|
|
|
|
|
|
logger_yandex = logging.getLogger('yandex_money') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BillListView(APIView): |
|
|
|
|
renderer_classes = (JSONRenderer,) |
|
|
|
|
@ -59,7 +63,7 @@ class BillListView(APIView): |
|
|
|
|
yandex_pay, _is_create = Payment.objects.get_or_create( |
|
|
|
|
order_amount=i['price'], |
|
|
|
|
order_number=invoice.id, |
|
|
|
|
shop_amount=invoice.real_price, |
|
|
|
|
shop_amount=0, |
|
|
|
|
customer_number=bill_obj.user.id, |
|
|
|
|
user=bill_obj.user, |
|
|
|
|
cps_email=bill_obj.user.email, |
|
|
|
|
@ -213,3 +217,84 @@ class YandexFailView(APIView): |
|
|
|
|
def get(request): |
|
|
|
|
logger.error('YandexFailView: {}'.format(request)) |
|
|
|
|
return Response(status=204) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class YandexCheckView(APIView): |
|
|
|
|
renderer_classes = (JSONRenderer,) |
|
|
|
|
|
|
|
|
|
@staticmethod |
|
|
|
|
def post(request): |
|
|
|
|
data = dict() |
|
|
|
|
for i in request.body.decode('utf-8').split('&'): |
|
|
|
|
key = i.split('=')[0] |
|
|
|
|
val = i.split('=')[1] |
|
|
|
|
data[key] = val |
|
|
|
|
|
|
|
|
|
logger_yandex.info(data) |
|
|
|
|
try: |
|
|
|
|
pay = Payment.objects.get(order_number=data['orderNumber']) |
|
|
|
|
except Payment.DoesNotExist: |
|
|
|
|
logger_yandex.error("Payment with id=%s not found" % data['orderNumber']) |
|
|
|
|
return Response(status=204) |
|
|
|
|
|
|
|
|
|
if not pay.status == Payment.STATUS.CHOICES.PROCESSED: |
|
|
|
|
logger_yandex.error("Payment with id=%s have status %s" % (data['orderNumber'], pay.status)) |
|
|
|
|
return Response(status=204) |
|
|
|
|
|
|
|
|
|
if not pay.shop_id == data['shopId']: |
|
|
|
|
logger_yandex.error("ShopId=%s not match" % (data['shopId'],)) |
|
|
|
|
return Response(status=204) |
|
|
|
|
|
|
|
|
|
if not pay.scid == data['scid']: |
|
|
|
|
logger_yandex.error("scid=%s not match" % (data['scid'],)) |
|
|
|
|
return Response(status=204) |
|
|
|
|
|
|
|
|
|
if not pay.order_amount == data['orderSumAmount']: |
|
|
|
|
logger_yandex.error("Expected amount is %s received amount is %s" |
|
|
|
|
% (pay.order_amount, data['orderSumAmount'])) |
|
|
|
|
return Response(status=204) |
|
|
|
|
|
|
|
|
|
pay.invoice_id = data['invoiceId'] |
|
|
|
|
pay.save() |
|
|
|
|
logger_yandex.info('Send success pay with invoice_id(yandex) %s' % pay.invoice_id) |
|
|
|
|
|
|
|
|
|
return Response({ |
|
|
|
|
'code': 0, |
|
|
|
|
'shopId': pay.shop_id, |
|
|
|
|
'invoiceId': pay.invoice_id, |
|
|
|
|
'orderSumAmount': pay.order_amount, |
|
|
|
|
}, status=200) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class YandexAvisoView(APIView): |
|
|
|
|
renderer_classes = (JSONRenderer,) |
|
|
|
|
|
|
|
|
|
@staticmethod |
|
|
|
|
def post(request): |
|
|
|
|
data = dict() |
|
|
|
|
for i in request.body.decode('utf-8').split('&'): |
|
|
|
|
key = i.split('=')[0] |
|
|
|
|
val = i.split('=')[1] |
|
|
|
|
data[key] = val |
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
pay = Payment.objects.get(invoice_id = data['invoiceId']) |
|
|
|
|
except Payment.DoesNotExist: |
|
|
|
|
logger_yandex.error("Payment with invoice_id=%s not found" % data['orderNumber']) |
|
|
|
|
return Response(status=204) |
|
|
|
|
|
|
|
|
|
logger_yandex.info('Get success pay with invoice_id(yandex) %s' % pay.invoice_id) |
|
|
|
|
|
|
|
|
|
pay.shop_amount = data['shopSumAmount'] |
|
|
|
|
pay.status = Payment.STATUS.CHOICES.SUCCESS |
|
|
|
|
pay.save() |
|
|
|
|
|
|
|
|
|
logger_yandex.info('Finish success pay with invoice_id(yandex) %s' % pay.invoice_id) |
|
|
|
|
|
|
|
|
|
return Response({ |
|
|
|
|
'performedDatetime': datetime.datetime.now(), |
|
|
|
|
'code': 0, |
|
|
|
|
'shopId': pay.shop_id, |
|
|
|
|
'invoiceId': pay.invoice_id, |
|
|
|
|
'orderSumAmount': pay.order_amount, |
|
|
|
|
}, status=200) |