-
Notifications
You must be signed in to change notification settings - Fork 13
/
views.py
61 lines (48 loc) · 2.07 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from django import forms
from django_coinpayments.models import Payment
from django_coinpayments.exceptions import CoinPaymentsProviderError
from django.views.generic import FormView, ListView, DetailView
from django.shortcuts import render, get_object_or_404
from decimal import Decimal
class ExamplePaymentForm(forms.ModelForm):
class Meta:
model = Payment
fields = ['amount', 'currency_original', 'currency_paid']
def create_tx(request, payment):
context = {}
try:
tx = payment.create_tx()
payment.status = Payment.PAYMENT_STATUS_PENDING
payment.save()
context['object'] = payment
except CoinPaymentsProviderError as e:
context['error'] = e
return render(request, 'django_coinpayments/payment_result.html', context)
class PaymentDetail(DetailView):
model = Payment
template_name = 'django_coinpayments/payment_result.html'
context_object_name = 'object'
class PaymentSetupView(FormView):
template_name = 'django_coinpayments/payment_setup.html'
form_class = ExamplePaymentForm
def form_valid(self, form):
cl = form.cleaned_data
payment = Payment(currency_original=cl['currency_original'],
currency_paid=cl['currency_paid'],
amount=cl['amount'],
amount_paid=Decimal(0),
status=Payment.PAYMENT_STATUS_PROVIDER_PENDING)
return create_tx(self.request, payment)
class PaymentList(ListView):
model = Payment
template_name = 'django_coinpayments/payment_list.html'
def create_new_payment(request, pk):
payment = get_object_or_404(Payment, pk=pk)
if payment.status in [Payment.PAYMENT_STATUS_PROVIDER_PENDING, Payment.PAYMENT_STATUS_TIMEOUT]:
pass
elif payment.status in [Payment.PAYMENT_STATUS_PENDING]:
payment.provider_tx.delete()
else:
error = "Invalid status - {}".format(payment.get_status_display())
return render(request, 'django_coinpayments/payment_result.html', {'error': error})
return create_tx(request, payment)