1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django.conf import settings
5 from django.urls import reverse
6 from django.utils.timezone import now
7 from paypal.rest import agreement_approval_url
10 class PaymentMethod(object):
13 expiration_reliable = False
17 def initiate(self, request, schedule):
18 return reverse('club_dummy_payment', args=[schedule.key])
21 class PayU(PaymentMethod):
23 expiration_reliable = True
26 template_name = 'club/payment/payu.html'
28 def __init__(self, pos_id):
31 def initiate(self, request, schedule):
32 # Create Order at once.
33 from .models import PayUOrder
34 order = PayUOrder.objects.create(
36 customer_ip=request.META['REMOTE_ADDR'],
42 class PayURe(PaymentMethod):
44 name = 'PayU recurring'
45 template_name = 'club/payment/payu-re.html'
47 expiration_reliable = True
51 def __init__(self, pos_id):
54 def initiate(self, request, schedule):
55 return reverse('club_payu_rec_payment', args=[schedule.key])
57 def pay(self, request, schedule):
58 # Create order, put it and see what happens next.
59 from .models import PayUOrder
60 if request is not None:
61 ip = request.META['REMOTE_ADDR']
66 if not self.needs_retry(schedule):
69 order = PayUOrder.objects.create(
76 def needs_retry(self, schedule):
77 retry_last = schedule.payuorder_set.exclude(
78 created_at=None).order_by('-created_at').first()
79 if retry_last is None:
83 days_since_last = (n - retry_last.created_at.date()).days
86 schedule.expires_at.date(),
87 settings.CLUB_RETRIES_START
89 retry_days = (n - retry_start).days
91 if retry_days > settings.CLUB_RETRY_DAYS_MAX:
94 if retry_days > settings.CLUB_RETRY_DAYS_DAILY:
95 print('retry less often now')
96 return days_since_last > settings.CLUB_RETRY_LESS
97 return days_since_last > 0
100 class PayPal(PaymentMethod):
103 template_name = 'club/payment/paypal.html'
107 def initiate(self, request, schedule):
108 app = request.GET.get('app')
109 return agreement_approval_url(schedule.amount, schedule.key, app=app)
111 def pay(self, request, schedule):
112 from datetime import date, timedelta, datetime
114 tomorrow = datetime(*(date.today() + timedelta(2)).timetuple()[:3], tzinfo=utc)
116 for ba in schedule.billingagreement_set.all():
117 active = ba.check_agreement()
123 schedule.expires_at = tomorrow
129 pos = getattr(settings, 'CLUB_PAYU_RECURRING_POS', None)
131 recurring_payment_method = PayURe(pos)
132 methods.append(recurring_payment_method)
134 recurring_payment_method = None
136 pos = getattr(settings, 'CLUB_PAYU_POS', None)
138 single_payment_method = PayU(pos)
139 methods.append(single_payment_method)
141 single_payment_method = None