|
|
|
|
@ -1,62 +1,24 @@ |
|
|
|
|
import json |
|
|
|
|
from datetime import datetime, timedelta |
|
|
|
|
|
|
|
|
|
import logging |
|
|
|
|
|
|
|
|
|
import os |
|
|
|
|
import requests |
|
|
|
|
from django_celery_beat.models import CrontabSchedule, PeriodicTask |
|
|
|
|
from yandex_money.models import Payment |
|
|
|
|
|
|
|
|
|
from finance.models import InvoiceRebilling |
|
|
|
|
from lms import celery_app |
|
|
|
|
from django.conf import settings |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger_yandex = logging.getLogger('yandex_money') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setup_periodic_billing(order_number): |
|
|
|
|
# TODO: настроить периодичность и срок окончания |
|
|
|
|
# 12:00 первого числа каждого месяца |
|
|
|
|
logger_yandex.info("Оформдение новой рассрочки") |
|
|
|
|
|
|
|
|
|
schedule, _ = CrontabSchedule.objects.get_or_create( |
|
|
|
|
minute='0', |
|
|
|
|
hour='*/3', |
|
|
|
|
) |
|
|
|
|
PeriodicTask.objects.create( |
|
|
|
|
crontab=schedule, |
|
|
|
|
name='Periodic billing (order_number={})'.format(order_number), |
|
|
|
|
task='finance.tasks.periodic_billing', |
|
|
|
|
kwargs=json.dumps({ |
|
|
|
|
'order_number': order_number |
|
|
|
|
}), |
|
|
|
|
expires=datetime.utcnow() + timedelta(days=180) # в течение полугода |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@celery_app.task |
|
|
|
|
def periodic_billing(order_number): |
|
|
|
|
try: |
|
|
|
|
sample = InvoiceRebilling.objects.get(yandex_pay__order_number=order_number) |
|
|
|
|
except InvoiceRebilling.DoesNotExist: |
|
|
|
|
raise ValueError('Номер заказа {} не найден'.format(order_number)) |
|
|
|
|
|
|
|
|
|
bill = sample.bill |
|
|
|
|
def periodic_billing(): |
|
|
|
|
logger_yandex.info("start periodic billing task") |
|
|
|
|
|
|
|
|
|
invoice = InvoiceRebilling.objects.create( |
|
|
|
|
status='P', |
|
|
|
|
price=sample.price, |
|
|
|
|
method=sample.method, |
|
|
|
|
is_open=sample.is_open, |
|
|
|
|
bill=bill, |
|
|
|
|
) |
|
|
|
|
for invoice in InvoiceRebilling.objects.filter(method='Y').exclude(status='F'): |
|
|
|
|
|
|
|
|
|
if invoice.method == 'Y': |
|
|
|
|
user = bill.user |
|
|
|
|
user = invoice.bill.user |
|
|
|
|
yandex_pay = Payment.objects.create( |
|
|
|
|
invoice_id=sample.yandex_pay.invoice_id, |
|
|
|
|
order_amount=invoice.price, |
|
|
|
|
customer_number=user.id, |
|
|
|
|
user=user, |
|
|
|
|
@ -72,7 +34,7 @@ def repeat_card_payment(invoice): |
|
|
|
|
resp = requests.post(settings.YANDEX_MONEY_MWS_URL + 'repeatCardPayment', |
|
|
|
|
data={ |
|
|
|
|
'clientOrderId': invoice.id, # уникальное возрастающее целое число |
|
|
|
|
'invoiceId': invoice.yandex_pay.invoice_id, |
|
|
|
|
'invoiceId': invoice.key, |
|
|
|
|
'amount': invoice.price, |
|
|
|
|
'orderNumber': invoice.yandex_pay.order_number |
|
|
|
|
}, |
|
|
|
|
|