From 248c430ffa698ee643f6397a2dba210afa448456 Mon Sep 17 00:00:00 2001 From: wad Date: Fri, 20 Apr 2018 17:02:53 +0300 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=20=D0=BF=D0=B5=D1=80=D0=B5=D0=BE=D0=B4=D0=B8=D1=87=D0=B5?= =?UTF-8?q?=D1=81=D0=BA=D0=BE=D0=B9=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B8?= =?UTF-8?q?:=20=D1=82=D1=80=D0=B0=D0=BD=D0=B7=D0=B0=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D0=BE=D0=BD=D0=BD=D0=BE=D1=81=D1=82=D1=8C,=20=D0=BB=D0=BE?= =?UTF-8?q?=D0=B3=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B8?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B4=D0=BE=D0=BB=D0=B6=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=86=D0=B8=D0=BA=D0=BB=D0=B0=20=D0=BF=D0=BE=20=D0=B2?= =?UTF-8?q?=D1=81=D0=B5=D0=BC=20=D0=BF=D0=BB=D0=B0=D1=82=D0=B5=D0=B6=D0=B0?= =?UTF-8?q?=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- finance/tasks.py | 87 ++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 43 deletions(-) diff --git a/finance/tasks.py b/finance/tasks.py index 33a0615..4577298 100644 --- a/finance/tasks.py +++ b/finance/tasks.py @@ -1,61 +1,62 @@ import logging - import os + import requests -from dateutil.relativedelta import relativedelta +from django.conf import settings +from django.db import transaction +from django.utils import timezone from yandex_money.models import Payment from finance.models import InvoiceRebilling from lms import celery_app -from django.conf import settings -from django.utils import timezone logger_yandex = logging.getLogger('yandex_money') @celery_app.task def periodic_billing(): - try: logger_yandex.info("start periodic billing task") invoices = InvoiceRebilling.objects.filter(method='Y').exclude(status='F') - - for invoice in invoices.filter( - expected_date__gt=timezone.now(), expected_date__lt=timezone.now() + relativedelta(days=1)): - # TODO выбирать все, даже прошлые неотработанные - что бы не потерять - - user = invoice.bill.user - yandex_pay = Payment.objects.create( - order_amount=invoice.price, - customer_number=user.id, - user=user, - cps_email=user.email, - shop_id=settings.YANDEX_MONEY_REBILLING_SHOP_ID, - scid=settings.YANDEX_MONEY_REBILLING_SCID - ) - invoice.yandex_pay = yandex_pay - invoice.save() - - repeat_card_payment(invoice) - except Exception as exc: - logger_yandex.error('periodic billing Exception', exc_info=True, extra={ - 'exc': exc - }) - # TODO записывать в invoice.comments ошибку яндекса - - -def repeat_card_payment(invoice): - resp = requests.post(settings.YANDEX_MONEY_MWS_URL + 'repeatCardPayment', - data={ - 'clientOrderId': invoice.id, # уникальное возрастающее целое число - 'invoiceId': invoice.key, - 'amount': invoice.price, - 'orderNumber': invoice.yandex_pay.order_number - }, - cert=( - os.path.join(settings.SSL_ROOT, 'skillbox.cer'), - os.path.join(settings.SSL_ROOT, 'skillbox.key') - ), - verify=os.path.join(settings.SSL_ROOT, 'yamoney_chain.cer')) + for invoice in invoices.filter(expected_date__lt=timezone.now()): + # выбираем все необработанные из прошлого + with transaction.atomic(): + try: + _yandex_repeat_card_payment(invoice) + except Exception as exc: + logger_yandex.critical('periodic billing Exception', exc_info=True, extra={ + 'invoice id': invoice.id, 'exc': exc + }) + invoice.comment = 'Ошибка при попытке повторного платежа, свяжитесь с клиентской службой' + invoice.save() + + +def _yandex_repeat_card_payment(invoice): + user = invoice.bill.user + yandex_pay = Payment.objects.create( + order_amount=invoice.price, + customer_number=user.id, + user=user, + cps_email=user.email, + shop_id=settings.YANDEX_MONEY_REBILLING_SHOP_ID, + scid=settings.YANDEX_MONEY_REBILLING_SCID + ) + invoice.yandex_pay = yandex_pay + + resp = requests.post( + url=settings.YANDEX_MONEY_MWS_URL + 'repeatCardPayment', + data={ + 'clientOrderId': invoice.id, # уникальное возрастающее целое число + 'invoiceId': invoice.key, + 'amount': invoice.price, + 'orderNumber': invoice.yandex_pay.order_number + }, + cert=( + os.path.join(settings.SSL_ROOT, 'skillbox.cer'), + os.path.join(settings.SSL_ROOT, 'skillbox.key') + ), + verify=os.path.join(settings.SSL_ROOT, 'yamoney_chain.cer') + ) + # TODO тут проверять нет ли ошибки яндекса (даже при 200 ответе) logger_yandex.info('periodic billing finish', exc_info=True, extra={ 'response': resp.text, 'code': resp.status_code, })