diff --git a/payments/migrations/0001_initial.py b/payments/migrations/0001_initial.py
new file mode 100644
index 0000000..27db43e
--- /dev/null
+++ b/payments/migrations/0001_initial.py
@@ -0,0 +1,26 @@
+# Generated by Django 3.1.3 on 2021-04-04 16:24
+
+from django.conf import settings
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+ initial = True
+
+ dependencies = [
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='Invoice',
+ fields=[
+ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+ ('total', models.FloatField(blank=True, null=True)),
+ ('amount', models.FloatField(blank=True, null=True)),
+ ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
+ ],
+ ),
+ ]
diff --git a/payments/migrations/0002_invoice_payment_complete.py b/payments/migrations/0002_invoice_payment_complete.py
new file mode 100644
index 0000000..84b3de6
--- /dev/null
+++ b/payments/migrations/0002_invoice_payment_complete.py
@@ -0,0 +1,18 @@
+# Generated by Django 3.1.3 on 2021-04-04 16:54
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('payments', '0001_initial'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='invoice',
+ name='payment_complete',
+ field=models.BooleanField(default=False),
+ ),
+ ]
diff --git a/payments/migrations/0003_invoice_invoice_code.py b/payments/migrations/0003_invoice_invoice_code.py
new file mode 100644
index 0000000..282be31
--- /dev/null
+++ b/payments/migrations/0003_invoice_invoice_code.py
@@ -0,0 +1,18 @@
+# Generated by Django 3.1.3 on 2021-04-05 05:02
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('payments', '0002_invoice_payment_complete'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='invoice',
+ name='invoice_code',
+ field=models.CharField(blank=True, max_length=200, null=True),
+ ),
+ ]
diff --git a/payments/models.py b/payments/models.py
index 71a8362..6c32b82 100644
--- a/payments/models.py
+++ b/payments/models.py
@@ -1,3 +1,12 @@
from django.db import models
+from django.contrib.auth.views import get_user_model
-# Create your models here.
+User = get_user_model()
+
+
+class Invoice(models.Model):
+ user = models.ForeignKey(User, on_delete=models.CASCADE)
+ total = models.FloatField(null=True, blank=True)
+ amount = models.FloatField(null=True, blank=True)
+ payment_complete = models.BooleanField(default=False)
+ invoice_code = models.CharField(max_length=200, blank=True, null=True)
diff --git a/payments/templates/invoice_detail.html b/payments/templates/invoice_detail.html
new file mode 100644
index 0000000..b2b81c7
--- /dev/null
+++ b/payments/templates/invoice_detail.html
@@ -0,0 +1,5 @@
+
Invoices
+
+{{ invoice.user }}
+{{ invoice.amount }}
+{{ invoice.total }}
diff --git a/payments/templates/invoices.html b/payments/templates/invoices.html
new file mode 100644
index 0000000..4dbde04
--- /dev/null
+++ b/payments/templates/invoices.html
@@ -0,0 +1,13 @@
+Invoices
+
+
+
+{% for invoice in invoices %}
+{{ invoice.user }}
+{{ invoice.amount }}
+{{ invoice.total }}
+{{ invoice.payment_complete }}
+{% endfor %}
diff --git a/payments/templates/payments/home.html b/payments/templates/payments/home.html
deleted file mode 100644
index f7d0fa8..0000000
--- a/payments/templates/payments/home.html
+++ /dev/null
@@ -1,9 +0,0 @@
-Buy for $5.00
-
diff --git a/payments/templates/payments/payment_gateways.html b/payments/templates/payments/payment_gateways.html
index 53e3520..c05cebb 100644
--- a/payments/templates/payments/payment_gateways.html
+++ b/payments/templates/payments/payment_gateways.html
@@ -3,6 +3,9 @@
{% load static %}
{% block header %}
+
+
+
{% endblock %}
@@ -68,13 +71,16 @@
Payment GateWays
+
+
+
+
Stripe
@@ -87,7 +93,6 @@
data-locale="auto">
-
Make payment with stripe and get jobs done.
@@ -106,10 +111,6 @@
Make payment with stripe and get jobs done.
-
@@ -119,35 +120,85 @@
{% block js %}
-
-
+
+
- onApprove: function(data, actions) {
- return actions.order.capture().then(function(details) {
- alert('Transaction completed by ' + details.payer.name.given_name + '!');
- });
- },
- onError: function(err) {
- console.log(err);
- }
- }).render('#paypal-button-container');
- }
- initPayPalButton();
-
{% endblock js %}
diff --git a/payments/templates/payments/payment_succeed.html b/payments/templates/payments/payment_succeed.html
new file mode 100644
index 0000000..8b49780
--- /dev/null
+++ b/payments/templates/payments/payment_succeed.html
@@ -0,0 +1,50 @@
+{% extends 'base.html' %}
+{% block title %}PayPal{% endblock title %}
+{% load static %}
+
+{% block header %}
+
+{% endblock %}
+
+{% block content %}
+
+
+
+ Payment Succeed, You has been make payment successfuly.
+ Redirect to the invoice in 8
+
+
+
+{% endblock content %}
+
+{% block js %}
+
+
+
+{% endblock %}
diff --git a/payments/templates/payments/paypal.html b/payments/templates/payments/paypal.html
index bb0e0d2..6cdac4e 100644
--- a/payments/templates/payments/paypal.html
+++ b/payments/templates/payments/paypal.html
@@ -1,45 +1,56 @@
-
-
-
+
+
+
+
+
+
+
+ PayPal Smart Payment Buttons Integration | Client Demo
-
+
+
-
-
-
+
+
+
diff --git a/payments/templates/payments/thank_you.html b/payments/templates/payments/thank_you.html
deleted file mode 100644
index 3e7a242..0000000
--- a/payments/templates/payments/thank_you.html
+++ /dev/null
@@ -1,9 +0,0 @@
-{% extends 'base.html' %}
-{% block title %}Thank you{% endblock title %}
-{% load static %}
-
-{% block content %}
-
-
Thank you
-
-{% endblock content %}
diff --git a/payments/urls.py b/payments/urls.py
index 1011296..7df04f3 100644
--- a/payments/urls.py
+++ b/payments/urls.py
@@ -2,17 +2,22 @@ from django.urls import path
from . import views
urlpatterns = [
+ path('', views.PaymentGetwaysView.as_view(), name='payment_gateways'),
# path('gateways/', views.payment_gateways, name="gateways"),
path('paypal/', views.payment_paypal, name="paypal"),
path('stripe/', views.payment_stripe, name="stripe"),
path('coinbase/', views.payment_coinbase, name="coinbase"),
path('paylike/', views.payment_paylike, name="paylike"),
+
+ path('create_invoice/', views.create_invoice, name="create_invoice"),
+ path('invoice_detail/
/', views.invoice_detail, name="invoice_detail"),
# path('charge/', views.charge, name="charge"),
- path('thank_you/', views.thank_you, name="thank_you"),
-
+ path('payment-succeed/', views.payment_succeed, name="payment-succeed"),
path('charge/', views.charge, name='charge'), # new
- path('', views.PaymentGetwaysView.as_view(), name='home_'),
+
+ path('complete/', views.paymentComplete, name="complete"),
+ path('gopay_payment/', views.gopay_payment, name="gopay_payment"),
]
diff --git a/payments/views.py b/payments/views.py
index ac32b33..a8dbdff 100644
--- a/payments/views.py
+++ b/payments/views.py
@@ -1,6 +1,14 @@
+
from django.shortcuts import render
from django.conf import settings
+
+
+from django.http import JsonResponse
+from .models import Invoice
+
import stripe
+import uuid
+import json
stripe.api_key = settings.STRIPE_SECRET_KEY
# stripe.ApplePayDomain.create(
@@ -31,8 +39,8 @@ def payment_paylike(request):
return render(request, 'payments/paylike.html', context={})
-def thank_you(request):
- return render(request, 'payments/thank_you.html', context={})
+def payment_succeed(request):
+ return render(request, 'payments/payment_succeed.html', context={})
# def charge(request):
@@ -48,17 +56,19 @@ def thank_you(request):
-
+from django.shortcuts import redirect
from django.views.generic.base import TemplateView
class PaymentGetwaysView(TemplateView):
template_name = 'payments/payment_gateways.html'
def get_context_data(self, **kwargs): # new
- context = super().get_context_data(**kwargs)
+ context = super(PaymentGetwaysView, self).get_context_data(**kwargs)
context['key'] = settings.STRIPE_PUBLISHABLE_KEY
context['amount'] = 500
context['description'] = "Stripe Payment"
+ context['invoice_session'] = self.request.session['invoice_session']
+ print(context['invoice_session'])
return context
@@ -67,8 +77,171 @@ def charge(request): # new
if request.method == 'POST':
charge = stripe.Charge.create(
amount=500,
- currency='usd',
+ currency='eur',
description='A Django charge',
source=request.POST['stripeToken']
)
- return render(request, 'payments/charge.html')
+ invoice_code = request.session['invoice_session']
+ invoice = Invoice.objects.get(invoice_code=invoice_code)
+ invoice.payment_complete = True
+ invoice.save()
+ return redirect('completed')
+ # return JsonResponse({"invoice_code": invoice.invoice_code}, status=201)
+ # return render(request, 'payments/charge.html')
+
+
+def create_invoice(request):
+ print(request.is_ajax())
+ if request.method == 'POST':
+ invoice = Invoice.objects.create(
+ user = request.user,
+ amount = request.POST.get('amount'),
+ total=26,
+ invoice_code=str(uuid.uuid4()),
+ )
+ request.session['invoice_session'] = invoice.invoice_code
+ return redirect('payment_gateways')
+ # if request.is_ajax():
+ # invoice = Invoice.objects.create(
+ # user = request.user,
+ # amount = 15,
+ # total=26,
+ # )
+ # return JsonResponse({'invoice': invoice}, status=201) # created
+
+ return render(request, 'invoices.html', context={
+ 'invoices': Invoice.objects.filter(user=request.user)
+ })
+
+
+def invoice_detail(request, slug):
+ return render(request, 'invoice_detail.html', context={
+ 'invoice': Invoice.objects.get(invoice_code=slug)
+ })
+
+
+def paymentComplete(request):
+ print(request.is_ajax())
+ if request.is_ajax() or request.method == 'POST':
+ invoice_id = request.session['invoice_session']
+ invoice = Invoice.objects.get(id=invoice_id)
+ invoice.payment_complete = True
+ invoice.save()
+ # return redirect('invoice', invoice.invoice_code)
+ body = json.loads(request.body)
+ print('BODY:', body)
+ return JsonResponse('Payment completed!', safe=False)
+
+
+from django.http import JsonResponse
+
+import gopay
+from gopay.enums import Recurrence, PaymentInstrument, BankSwiftCode, Currency, Language
+
+
+def gopay_payment(request):
+ print("\nrequest \n", request.method)
+ # api = gopay.payments({
+ # 'goid': '8302931681',
+ # 'clientId': '1061399163',
+ # 'clientSecret': 'stDTmVXF',
+ # 'isProductionMode': False,
+ # 'scope': gopay.TokenScope.ALL,
+ # 'language': gopay.Language.ENGLISH,
+ # 'timeout': 30
+ # })
+ # # token is retrieved automatically, you don't have to call some method `get_token`
+
+ # response = api.get_status('3000006542')
+ # if response.has_succeed():
+ # print("hooray, API returned " + str(response))
+ # else:
+ # print("oops, API returned " + str(response.status_code) + ": " + str(response))
+
+ # payments = gopay.payments({
+ # 'goid': 'my goid',
+ # 'clientId': 'my id',
+ # 'clientSecret': 'my secret',
+ # 'isProductionMode': False
+ # })
+ if request.method == 'POST':
+ user = request.user
+
+ payments = gopay.payments({
+ 'goid': '8302931681',
+ 'clientId': '1061399163',
+ 'clientSecret': 'stDTmVXF',
+ 'isProductionMode': False,
+ 'scope': gopay.TokenScope.ALL,
+ 'language': gopay.Language.ENGLISH,
+ 'timeout': 30
+ })
+
+ # recurrent payment must have field ''
+ recurrentPayment = {
+ 'recurrence': {
+ 'recurrence_cycle': Recurrence.DAILY,
+ 'recurrence_period': "7",
+ 'recurrence_date_to': '2015-12-31'
+ }
+ }
+
+ # pre-authorized payment must have field 'preauthorization'
+ preauthorizedPayment = {
+ 'preauthorization': True
+ }
+
+ response = payments.create_payment({
+ 'payer': {
+ 'default_payment_instrument': PaymentInstrument.BANK_ACCOUNT,
+ 'allowed_payment_instruments': [PaymentInstrument.BANK_ACCOUNT],
+ 'default_swift': BankSwiftCode.FIO_BANKA,
+ 'allowed_swifts': [BankSwiftCode.FIO_BANKA, BankSwiftCode.MBANK],
+ 'contact': {
+ 'first_name': user.first_name,
+ 'last_name': user.last_name,
+ 'email': user.email,
+ 'phone_number': user.phone,
+ 'city': 'example city',
+ 'street': 'Plana 67',
+ 'postal_code': '373 01',
+ 'country_code': 'CZE',
+ },
+ # 'contact': {
+ # 'first_name': 'Zbynek',
+ # 'last_name': 'Zak',
+ # 'email': 'zbynek.zak@gopay.cz',
+ # 'phone_number': '+420777456123',
+ # 'city': 'C.Budejovice',
+ # 'street': 'Plana 67',
+ # 'postal_code': '373 01',
+ # 'country_code': 'CZE',
+ # },
+ },
+ 'amount': 150,
+ 'currency': Currency.CZECH_CROWNS,
+ 'order_number': '001',
+ 'order_description': 'pojisteni01',
+ 'items': [
+ {'name': 'item01', 'amount': 50},
+ {'name': 'item02', 'amount': 100},
+ ],
+ 'additional_params': [
+ {'name': 'invoicenumber', 'value': '2015001003'}
+ ],
+ 'callback': {
+ 'return_url': 'http://www.your-url.tld/return',
+ 'notification_url': 'http://www.your-url.tld/notify'
+ },
+ 'lang': Language.CZECH, # if lang is not specified, then default lang is used
+ })
+
+ if response.has_succeed():
+ print("\nPayment Succeed\n")
+ print("hooray, API returned " + str(response))
+ else:
+ print("\nPayment Fail\n")
+ print("oops, API returned " + str(response.status_code) + ": " + str(response))
+ return JsonResponse({"message": str(response)})
+
+ return JsonResponse({"message": "GET requested"})
\ No newline at end of file