Skip to content

Commit 9bde461

Browse files
committed
Implement recurring payments on The Pay
Depends on cuchac/thepay#4 Signed-off-by: Michal Čihař <michal@cihar.com>
1 parent 8cc7706 commit 9bde461

2 files changed

Lines changed: 55 additions & 37 deletions

File tree

wlhosted/integrations/tasks.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,26 @@ def pending_payments():
5252

5353
@app.task
5454
def recurring_payments():
55-
with transaction.atomic(using='payments_db'):
56-
cutoff = timezone.now().date() + timedelta(days=1)
57-
for billing in Billing.objects.filter(state=Billing.STATE_ACTIVE):
58-
if 'recurring' not in billing.payment:
59-
continue
60-
last_invoice = billing.invoice_set.order_by('-start')[0]
61-
if last_invoice.end > cutoff:
62-
continue
63-
64-
original = Payment.objects.get(pk=billing.payment['recurring'])
65-
66-
# Check if backend is still valid
67-
try:
68-
get_backend(original.backend)
69-
except KeyError:
70-
# Remove recurring flag
71-
del billing.payment['recurring']
72-
billing.save()
73-
continue
74-
55+
cutoff = timezone.now().date() + timedelta(days=1)
56+
for billing in Billing.objects.filter(state=Billing.STATE_ACTIVE):
57+
if 'recurring' not in billing.payment:
58+
continue
59+
last_invoice = billing.invoice_set.order_by('-start')[0]
60+
if last_invoice.end > cutoff:
61+
continue
62+
63+
original = Payment.objects.get(pk=billing.payment['recurring'])
64+
65+
# Check if backend is still valid
66+
try:
67+
get_backend(original.backend)
68+
except KeyError:
69+
# Remove recurring flag
70+
del billing.payment['recurring']
71+
billing.save()
72+
continue
73+
74+
with transaction.atomic(using='payments_db'):
7575
# Check for failed payments
7676
previous = Payment.objects.filter(repeat=original)
7777
if previous.exists():
@@ -103,18 +103,18 @@ def recurring_payments():
103103
}
104104
)
105105

106-
# Trigger payment processing
107-
request = Request(get_payment_url(payment))
108-
request.add_header('User-Agent', USER_AGENT)
109-
handle = urlopen(
110-
request,
111-
urlencode({
112-
'method': original.details['backend'],
113-
'secret': settings.PAYMENT_SECRET,
114-
}).encode('utf-8')
115-
)
116-
handle.read()
117-
handle.close()
106+
# Trigger payment processing
107+
request = Request(get_payment_url(payment))
108+
request.add_header('User-Agent', USER_AGENT)
109+
handle = urlopen(
110+
request,
111+
urlencode({
112+
'method': original.backend,
113+
'secret': settings.PAYMENT_SECRET,
114+
}).encode('utf-8')
115+
)
116+
handle.read()
117+
handle.close()
118118

119119
# We have created bunch of pending payments, process them now
120120
pending_payments()

wlhosted/payments/backends.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@
2626

2727
from django.conf import settings
2828
from django.core.mail import EmailMessage
29+
from django.core.serializers.json import DjangoJSONEncoder
2930
from django.shortcuts import redirect
3031
from django.utils.translation import ugettext as _, ugettext_lazy
3132

3233
from fakturace.storage import WebStorage
3334

3435
import thepay.config
36+
import thepay.gateApi
37+
import thepay.dataApi
3538
import thepay.payment
3639

40+
from zeep.helpers import serialize_object
41+
3742
from wlhosted.payments.models import Payment
3843

3944
BACKENDS = {}
@@ -143,7 +148,9 @@ def generate_invoice(self):
143148
invoice = storage.get(invoice_file)
144149
invoice.write_tex()
145150
invoice.build_pdf()
146-
invoice.mark_paid(json.dumps(self.payment.details, indent=2))
151+
invoice.mark_paid(
152+
json.dumps(self.payment.details, indent=2, cls=DjangoJSONEncoder)
153+
)
147154

148155
self.payment.invoice = invoice.invoiceid
149156

@@ -299,6 +306,14 @@ def __init__(self, payment):
299306
)
300307

301308
def perform(self, request, back_url, complete_url):
309+
if self.payment.repeat:
310+
api = thepay.gateApi.GateApi(self.config)
311+
api.cardCreateRecurrentPayment(
312+
str(self.payment.repeat.pk),
313+
str(self.payment.pk),
314+
self.payment.vat_amount,
315+
)
316+
return None
302317

303318
payment = thepay.payment.Payment(self.config)
304319

@@ -311,13 +326,16 @@ def perform(self, request, back_url, complete_url):
311326
payment.setMerchantData(str(self.payment.pk))
312327
if self.payment.recurring:
313328
payment.setIsRecurring(1)
314-
elif self.payment.repeat:
315-
# TODO: invoce recurring payment API
316-
pass
317-
318329
return redirect(payment.getCreateUrl())
319330

320331
def collect(self, request):
332+
if self.payment.repeat:
333+
data = thepay.dataApi.DataApi(self.config)
334+
payment = data.getPayments(
335+
merchant_data=str(self.payment.pk)
336+
).payments.payment[0]
337+
self.payment.details = serialize_object(payment)
338+
return True
321339
return_payment = thepay.payment.ReturnPayment(self.config)
322340
return_payment.parseData(request.GET)
323341

0 commit comments

Comments
 (0)