fix
[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     expiration_reliable = False
13     cancellable = False
14     updateable = False
15
16     def initiate(self, request, schedule):
17         return reverse('club_dummy_payment', args=[schedule.key])
18
19
20 class PayU(PaymentMethod):
21     is_onetime = True
22     expiration_reliable = True
23     slug = 'payu'
24     name = 'PayU'
25     template_name = 'club/payment/payu.html'
26
27     def __init__(self, pos_id):
28         self.pos_id = pos_id
29
30     def initiate(self, request, schedule):
31         # Create Order at once.
32         from .models import PayUOrder
33         order = PayUOrder.objects.create(
34             pos_id=self.pos_id,
35             customer_ip=request.META['REMOTE_ADDR'],
36             schedule=schedule,
37         )
38         return order.put()
39
40
41 class PayURe(PaymentMethod):
42     slug = 'payu-re'
43     name = 'PayU recurring'
44     template_name = 'club/payment/payu-re.html'
45     is_recurring = True
46     expiration_reliable = True
47     cancellable = True
48     updateable = True
49
50     def __init__(self, pos_id):
51         self.pos_id = pos_id
52
53     def initiate(self, request, schedule):
54         return reverse('club_payu_rec_payment', args=[schedule.key])
55
56     def pay(self, request, schedule):
57         # Create order, put it and see what happens next.
58         from .models import PayUOrder
59         if request is not None:
60             ip = request.META['REMOTE_ADDR']
61         else:
62             ip = '127.0.0.1'
63         order = PayUOrder.objects.create(
64             pos_id=self.pos_id,
65             customer_ip=ip,
66             schedule=schedule,
67         )
68         return order.put()
69         
70
71 class PayPal(PaymentMethod):
72     slug = 'paypal'
73     name = 'PayPal'
74     template_name = 'club/payment/paypal.html'
75     is_recurring = True
76     is_onetime = False
77
78     def initiate(self, request, schedule):
79         app = request.GET.get('app')
80         return agreement_approval_url(schedule.amount, schedule.key, app=app)
81
82     def pay(self, request, schedule):
83         from datetime import date, timedelta, datetime
84         from pytz import utc
85         tomorrow = datetime(*(date.today() + timedelta(2)).timetuple()[:3], tzinfo=utc)
86         any_active = False
87         for ba in schedule.billingagreement_set.all():
88             active = ba.check_agreement()
89             ba.active = active
90             ba.save()
91             if active:
92                 any_active = True
93         if any_active:
94             schedule.expires_at = tomorrow
95             schedule.save()
96
97
98 methods = []
99
100 pos = getattr(settings, 'CLUB_PAYU_RECURRING_POS', None)
101 if pos:
102     recurring_payment_method = PayURe(pos)
103     methods.append(recurring_payment_method)
104 else:
105     recurring_payment_method = None
106
107 pos = getattr(settings, 'CLUB_PAYU_POS', None)
108 if pos:
109     single_payment_method = PayU(pos)
110     methods.append(single_payment_method)
111 else:
112     single_payment_method = None
113
114
115
116 methods.append(
117     PayPal()
118 )