remotes/origin/revert-6d879fe8
parent
713649c90c
commit
248c430ffa
1 changed files with 44 additions and 43 deletions
@ -1,61 +1,62 @@ |
|||||||
import logging |
import logging |
||||||
|
|
||||||
import os |
import os |
||||||
|
|
||||||
import requests |
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 yandex_money.models import Payment |
||||||
|
|
||||||
from finance.models import InvoiceRebilling |
from finance.models import InvoiceRebilling |
||||||
from lms import celery_app |
from lms import celery_app |
||||||
from django.conf import settings |
|
||||||
from django.utils import timezone |
|
||||||
|
|
||||||
logger_yandex = logging.getLogger('yandex_money') |
logger_yandex = logging.getLogger('yandex_money') |
||||||
|
|
||||||
|
|
||||||
@celery_app.task |
@celery_app.task |
||||||
def periodic_billing(): |
def periodic_billing(): |
||||||
try: |
|
||||||
logger_yandex.info("start periodic billing task") |
logger_yandex.info("start periodic billing task") |
||||||
invoices = InvoiceRebilling.objects.filter(method='Y').exclude(status='F') |
invoices = InvoiceRebilling.objects.filter(method='Y').exclude(status='F') |
||||||
|
for invoice in invoices.filter(expected_date__lt=timezone.now()): |
||||||
for invoice in invoices.filter( |
# выбираем все необработанные из прошлого |
||||||
expected_date__gt=timezone.now(), expected_date__lt=timezone.now() + relativedelta(days=1)): |
with transaction.atomic(): |
||||||
# TODO выбирать все, даже прошлые неотработанные - что бы не потерять |
try: |
||||||
|
_yandex_repeat_card_payment(invoice) |
||||||
user = invoice.bill.user |
except Exception as exc: |
||||||
yandex_pay = Payment.objects.create( |
logger_yandex.critical('periodic billing Exception', exc_info=True, extra={ |
||||||
order_amount=invoice.price, |
'invoice id': invoice.id, 'exc': exc |
||||||
customer_number=user.id, |
}) |
||||||
user=user, |
invoice.comment = 'Ошибка при попытке повторного платежа, свяжитесь с клиентской службой' |
||||||
cps_email=user.email, |
invoice.save() |
||||||
shop_id=settings.YANDEX_MONEY_REBILLING_SHOP_ID, |
|
||||||
scid=settings.YANDEX_MONEY_REBILLING_SCID |
|
||||||
) |
def _yandex_repeat_card_payment(invoice): |
||||||
invoice.yandex_pay = yandex_pay |
user = invoice.bill.user |
||||||
invoice.save() |
yandex_pay = Payment.objects.create( |
||||||
|
order_amount=invoice.price, |
||||||
repeat_card_payment(invoice) |
customer_number=user.id, |
||||||
except Exception as exc: |
user=user, |
||||||
logger_yandex.error('periodic billing Exception', exc_info=True, extra={ |
cps_email=user.email, |
||||||
'exc': exc |
shop_id=settings.YANDEX_MONEY_REBILLING_SHOP_ID, |
||||||
}) |
scid=settings.YANDEX_MONEY_REBILLING_SCID |
||||||
# TODO записывать в invoice.comments ошибку яндекса |
) |
||||||
|
invoice.yandex_pay = yandex_pay |
||||||
|
|
||||||
def repeat_card_payment(invoice): |
resp = requests.post( |
||||||
resp = requests.post(settings.YANDEX_MONEY_MWS_URL + 'repeatCardPayment', |
url=settings.YANDEX_MONEY_MWS_URL + 'repeatCardPayment', |
||||||
data={ |
data={ |
||||||
'clientOrderId': invoice.id, # уникальное возрастающее целое число |
'clientOrderId': invoice.id, # уникальное возрастающее целое число |
||||||
'invoiceId': invoice.key, |
'invoiceId': invoice.key, |
||||||
'amount': invoice.price, |
'amount': invoice.price, |
||||||
'orderNumber': invoice.yandex_pay.order_number |
'orderNumber': invoice.yandex_pay.order_number |
||||||
}, |
}, |
||||||
cert=( |
cert=( |
||||||
os.path.join(settings.SSL_ROOT, 'skillbox.cer'), |
os.path.join(settings.SSL_ROOT, 'skillbox.cer'), |
||||||
os.path.join(settings.SSL_ROOT, 'skillbox.key') |
os.path.join(settings.SSL_ROOT, 'skillbox.key') |
||||||
), |
), |
||||||
verify=os.path.join(settings.SSL_ROOT, 'yamoney_chain.cer')) |
verify=os.path.join(settings.SSL_ROOT, 'yamoney_chain.cer') |
||||||
|
) |
||||||
|
# TODO тут проверять нет ли ошибки яндекса (даже при 200 ответе) |
||||||
logger_yandex.info('periodic billing finish', exc_info=True, extra={ |
logger_yandex.info('periodic billing finish', exc_info=True, extra={ |
||||||
'response': resp.text, 'code': resp.status_code, |
'response': resp.text, 'code': resp.status_code, |
||||||
}) |
}) |
||||||
|
|||||||
Loading…
Reference in new issue