Повторные платежи See merge request !260remotes/origin/feature/test_courses_^2
commit
706c376114
7 changed files with 131 additions and 8 deletions
@ -0,0 +1,25 @@ |
|||||||
|
# -*- coding: utf-8 -*- |
||||||
|
# Generated by Django 1.11.6 on 2018-03-21 16:53 |
||||||
|
from __future__ import unicode_literals |
||||||
|
|
||||||
|
from django.db import migrations, models |
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration): |
||||||
|
|
||||||
|
dependencies = [ |
||||||
|
('finance', '0003_auto_20180315_1358'), |
||||||
|
] |
||||||
|
|
||||||
|
operations = [ |
||||||
|
migrations.AddField( |
||||||
|
model_name='invoice', |
||||||
|
name='rebilling', |
||||||
|
field=models.BooleanField(default=False, editable=False, verbose_name='Повторный платеж'), |
||||||
|
), |
||||||
|
migrations.AddField( |
||||||
|
model_name='invoice', |
||||||
|
name='rebilling_on', |
||||||
|
field=models.BooleanField(default=False, editable=False, verbose_name='Повторять платеж'), |
||||||
|
), |
||||||
|
] |
||||||
@ -0,0 +1,86 @@ |
|||||||
|
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(order_number): |
||||||
|
# TODO: настроить периодичность и срок окончания |
||||||
|
# 12:00 первого числа каждого месяца |
||||||
|
schedule, _ = CrontabSchedule.objects.get_or_create( |
||||||
|
minute='0', |
||||||
|
hour='12', |
||||||
|
day_of_week='*', |
||||||
|
day_of_month='1', |
||||||
|
month_of_year='*' |
||||||
|
) |
||||||
|
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 = Invoice.objects.get(yandex_pay__order_number=order_number) |
||||||
|
except Invoice.DoesNotExist: |
||||||
|
raise ValueError('Номер заказа {} не найден'.format(order_number)) |
||||||
|
|
||||||
|
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() |
||||||
|
|
||||||
|
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) |
||||||
Loading…
Reference in new issue