Banners only for authenticated users.
[wolnelektury.git] / src / club / payment_methods.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from django.conf import settings
5 from django.urls import reverse
6 from paypal.rest import agreement_approval_url
7
8
9 class PaymentMethod(object):
10     is_onetime = False
11     is_recurring = False
12
13     def initiate(self, request, schedule):
14         return reverse('club_dummy_payment', args=[schedule.key])
15
16
17 class PayU(PaymentMethod):
18     is_onetime = True
19     slug = 'payu'
20     name = 'PayU'
21     template_name = 'club/payment/payu.html'
22
23     def __init__(self, pos_id):
24         self.pos_id = pos_id
25
26     def initiate(self, request, schedule):
27         # Create Order at once.
28         from .models import PayUOrder
29         order = PayUOrder.objects.create(
30             pos_id=self.pos_id,
31             customer_ip=request.META['REMOTE_ADDR'],
32             schedule=schedule,
33         )
34         return order.put()
35
36
37 class PayURe(PaymentMethod):
38     slug = 'payu-re'
39     name = 'PayU recurring'
40     template_name = 'club/payment/payu-re.html'
41     is_recurring = True
42
43     def __init__(self, pos_id):
44         self.pos_id = pos_id
45
46     def initiate(self, request, schedule):
47         return reverse('club_payu_rec_payment', args=[schedule.key])
48
49     def pay(self, request, schedule):
50         # Create order, put it and see what happens next.
51         from .models import PayUOrder
52         if request is not None:
53             ip = request.META['REMOTE_ADDR']
54         else:
55             ip = '127.0.0.1'
56         order = PayUOrder.objects.create(
57             pos_id=self.pos_id,
58             customer_ip=ip,
59             schedule=schedule,
60         )
61         return order.put()
62         
63
64 class PayPal(PaymentMethod):
65     slug = 'paypal'
66     name = 'PayPal'
67     template_name = 'club/payment/paypal.html'
68     is_recurring = True
69     is_onetime = False
70
71     def initiate(self, request, schedule):
72         app = request.GET.get('app')
73         return agreement_approval_url(schedule.amount, schedule.key, app=app)
74
75     def pay(self, request, schedule):
76         from datetime import date, timedelta, datetime
77         from pytz import utc
78         tomorrow = datetime(*(date.today() + timedelta(2)).timetuple()[:3], tzinfo=utc)
79         any_active = False
80         for ba in schedule.billingagreement_set.all():
81             active = ba.check_agreement()
82             ba.active = active
83             ba.save()
84             if active:
85                 any_active = True
86         if any_active:
87             schedule.expires_at = tomorrow
88             schedule.save()
89
90
91 methods = []
92
93 pos = getattr(settings, 'CLUB_PAYU_RECURRING_POS', None)
94 if pos:
95     recurring_payment_method = PayURe(pos)
96     methods.append(recurring_payment_method)
97 else:
98     recurring_payment_method = None
99
100 pos = getattr(settings, 'CLUB_PAYU_POS', None)
101 if pos:
102     single_payment_method = PayU(pos)
103     methods.append(single_payment_method)
104 else:
105     single_payment_method = None
106
107
108
109 methods.append(
110     PayPal()
111 )