|
|
|
|
@ -1,38 +1,51 @@ |
|
|
|
|
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 Invoice |
|
|
|
|
from lms import celery_app |
|
|
|
|
from django.conf import settings |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
logger_yandex = logging.getLogger('yandex_money') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def setup_periodic_billing(invoice_id): |
|
|
|
|
def setup_periodic_billing(order_number): |
|
|
|
|
# TODO: настроить периодичность и срок окончания |
|
|
|
|
# 12:00 первого числа каждого месяца |
|
|
|
|
schedule, _ = CrontabSchedule.objects.get_or_create( |
|
|
|
|
minute='*', |
|
|
|
|
hour='*', |
|
|
|
|
minute='0', |
|
|
|
|
hour='12', |
|
|
|
|
day_of_week='*', |
|
|
|
|
day_of_month='*', |
|
|
|
|
day_of_month='1', |
|
|
|
|
month_of_year='*' |
|
|
|
|
) |
|
|
|
|
PeriodicTask.objects.create( |
|
|
|
|
crontab=schedule, |
|
|
|
|
name='Periodic billing #{}'.format(invoice_id), |
|
|
|
|
name='Periodic billing (order_number={})'.format(order_number), |
|
|
|
|
task='finance.tasks.periodic_billing', |
|
|
|
|
args=json.dumps([invoice_id]), |
|
|
|
|
expires=datetime.utcnow() + timedelta(minutes=5) |
|
|
|
|
kwargs=json.dumps({ |
|
|
|
|
'order_number': order_number |
|
|
|
|
}), |
|
|
|
|
expires=datetime.utcnow() + timedelta(days=180) # в течение полугода |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@celery_app.task |
|
|
|
|
def periodic_billing(invoice_id): |
|
|
|
|
def periodic_billing(order_number): |
|
|
|
|
try: |
|
|
|
|
sample = Invoice.objects.get(id=invoice_id) |
|
|
|
|
sample = Invoice.objects.get(yandex_pay__order_number=order_number) |
|
|
|
|
except Invoice.DoesNotExist: |
|
|
|
|
raise ValueError('Платеж с id={} не найден'.format(invoice_id)) |
|
|
|
|
raise ValueError('Номер заказа {} не найден'.format(order_number)) |
|
|
|
|
|
|
|
|
|
bill = sample.bill |
|
|
|
|
|
|
|
|
|
invoice = Invoice.objects.create( |
|
|
|
|
status='P', |
|
|
|
|
price=sample.price, |
|
|
|
|
@ -40,6 +53,7 @@ def periodic_billing(invoice_id): |
|
|
|
|
rebilling=True, |
|
|
|
|
bill=bill |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
if invoice.method == 'Y': |
|
|
|
|
user = bill.user |
|
|
|
|
yandex_pay = Payment.objects.create( |
|
|
|
|
@ -51,4 +65,22 @@ def periodic_billing(invoice_id): |
|
|
|
|
) |
|
|
|
|
invoice.yandex_pay = yandex_pay |
|
|
|
|
invoice.save() |
|
|
|
|
# TODO: запрос repeatCardPayment |
|
|
|
|
|
|
|
|
|
repeat_card_payment(invoice) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def repeat_card_payment(invoice): |
|
|
|
|
resp = requests.post(settings.YANDEX_MONEY_MWS_URL + 'repeatCardPayment', |
|
|
|
|
data={ |
|
|
|
|
'clientOrderId': invoice.id, # уникальное возрастающее целое число |
|
|
|
|
'invoiceId': invoice.yandex_pay.invoice_id, |
|
|
|
|
'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')) |
|
|
|
|
|
|
|
|
|
logger_yandex.info(resp.text) |
|
|
|
|
|