import json from datetime import datetime, timedelta from django_celery_beat.models import CrontabSchedule, PeriodicTask from yandex_money.models import Payment from finance.models import Invoice from lms import celery_app def setup_periodic_billing(invoice_id): # TODO: настроить периодичность и срок окончания schedule, _ = CrontabSchedule.objects.get_or_create( minute='*', hour='*', day_of_week='*', day_of_month='*', month_of_year='*' ) PeriodicTask.objects.create( crontab=schedule, name='Periodic billing #{}'.format(invoice_id), task='finance.tasks.periodic_billing', args=json.dumps([invoice_id]), expires=datetime.utcnow() + timedelta(minutes=5) ) @celery_app.task def periodic_billing(invoice_id): try: sample = Invoice.objects.get(id=invoice_id) except Invoice.DoesNotExist: raise ValueError('Платеж с id={} не найден'.format(invoice_id)) bill = sample.bill invoice = Invoice.objects.create( status='P', price=sample.price, method=sample.method, rebilling=True, bill=bill ) if invoice.method == 'Y': user = bill.user yandex_pay = Payment.objects.create( invoice_id=sample.yandex_pay.invoice_id, order_amount=invoice.price, customer_number=user.id, user=user, cps_email=user.email ) invoice.yandex_pay = yandex_pay invoice.save() # TODO: запрос repeatCardPayment