gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [taler-codeless] branch master updated: pay_url handler


From: gnunet
Subject: [GNUnet-SVN] [taler-codeless] branch master updated: pay_url handler
Date: Tue, 19 Jun 2018 07:21:44 +0200

This is an automated email from the git hooks/post-receive script.

shivam-kohli pushed a commit to branch master
in repository codeless.

The following commit(s) were added to refs/heads/master by this push:
     new 088a3ed  pay_url handler
088a3ed is described below

commit 088a3edd1582004cdf1fe791df40041a36f20e0e
Author: shivam kohli <address@hidden>
AuthorDate: Tue Jun 19 10:51:24 2018 +0530

    pay_url handler
---
 README.md          |  3 ++
 codeless/urls.py   |  1 +
 inventory/views.py | 91 ++++++++++++++++++++++++++++++++++++++++++++----------
 3 files changed, 78 insertions(+), 17 deletions(-)

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..0b5852b
--- /dev/null
+++ b/README.md
@@ -0,0 +1,3 @@
+# Payment/Donation Buttons and Code-less Payments with GNU Taler
+
+This is a component that sits between the seller's frontend and the GNU Taler 
merchant backend. This component has a web interface, where payment buttons or 
payment forms will be configured. Additional objective include inventory 
management, where the seller can configure the available stock for an item and 
will get notified when their stock runs low. Merchant will be able to 
communicate with the merchant’s backend via this API.
\ No newline at end of file
diff --git a/codeless/urls.py b/codeless/urls.py
index ed84a5d..e78e737 100644
--- a/codeless/urls.py
+++ b/codeless/urls.py
@@ -10,6 +10,7 @@ urlpatterns = [
     url(r'^admin/', include(admin.site.urls)),
     url(r'^test/$', 'inventory.views.test', name='test'),
     url(r'^payment/$', 'inventory.views.payment', name='payment'),
+    url(r'^payment/pay/$', 'inventory.views.pay', name='pay'),
     url(r'^signup/$', 'inventory.views.signup', name='signup'),
     url(r'^new_product/$', 'inventory.views.new_product', name='new_product'),
     url(r'^add_product/$', 'inventory.views.add_product', name='add_product'),
diff --git a/inventory/views.py b/inventory/views.py
index a03bf8e..e050a1a 100644
--- a/inventory/views.py
+++ b/inventory/views.py
@@ -12,35 +12,90 @@ from django.contrib.auth.decorators import login_required
 from django.shortcuts import render, redirect
 from django.core.urlresolvers import resolve
 import requests
+from urllib.parse import urljoin
+from django.http import HttpResponse
+from django.views.decorators.csrf import csrf_exempt
+import json
+from django.http import JsonResponse
 
 
 def test(request):
     return render(request, 'inventory/test.html')
 
 
address@hidden
+def pay(request):
+    if request.method == 'POST':
+        body_unicode = request.body.decode('utf-8')
+        json_data = json.loads(body_unicode)
+        if json_data is None:
+            return HttpResponse("no json in body")
+        r = requests.post("https://backend.demo.taler.net/public/pay";,
+            json=json_data,
+            headers={"Authorization": "ApiKey sandbox"})
+        if r.status_code != 200:
+            return HttpResponse(r.status_code)
+        return JsonResponse(r.json())
+
+
 def payment(request):
+    session_id = request.session.session_key
     # Creating an Order for a Payment
-    order = dict(order=dict(amount="KUDOS:1",
-        summary="test for codeless",
+    order = dict(order=dict(amount="KUDOS:0.1",
+        summary="codeless",
         instance="default",
-        extra=dict(article_name="test article"),
-        fulfillment_url="https://example.com/thanks.html";))
-    order_resp = requests.post("https://backend.demo.taler.net/order";,
-        json=order,
-        headers={"Authorization": "ApiKey sandbox"})
-    order_resp = order_resp.json()
-    order_id = order_resp["order_id"]
+        extra=dict(article_name="test"),
+        fulfillment_url="https://example.com/thanks.html";,
+        pay_url="http://localhost:8000/payment/pay/";,
+        )
+    )
+    order_resp = backend_post("order", order)
     # Checking Payment Status and Prompting for Payment
-    r = requests.get("https://backend.demo.taler.net/check-payment";,
-        params=dict(order_id=order_resp["order_id"]),
-        headers={"Authorization": "ApiKey sandbox"})
-    pay_url = r.json()["payment_redirect_url"]
-    return redirect(pay_url)
+    pay_params = dict(
+        instance="default",
+        order_id=order_resp["order_id"],
+        session_id=session_id,
+    )
+    pay_status = backend_get("check-payment", pay_params)
+    payment_redirect_url = pay_status["payment_redirect_url"]
+    return redirect(payment_redirect_url)
+
+
+def backend_get(endpoint, params):
+    headers = {"Authorization": "ApiKey sandbox"}
+    try:
+        resp = requests.get(urljoin("https://backend.demo.taler.net/";, 
endpoint),
+            params=params,
+            headers=headers)
+    except requests.ConnectionError:
+        return HttpResponse("Could not establish connection to backend")
+    try:
+        response_json = resp.json()
+    except ValueError:
+        return HttpResponse("Could not parse response from backend")
+    return response_json
+
+
+def backend_post(endpoint, json):
+    headers = {"Authorization": "ApiKey sandbox"}
+    try:
+        resp_url = urljoin("https://backend.demo.taler.net/";, endpoint)
+        resp = requests.post(resp_url,
+            json=json,
+            headers=headers)
+    except requests.ConnectionError:
+        return HttpResponse("Could not establish connection to backend")
+    try:
+        response_json = resp.json()
+    except ValueError:
+        return HttpResponse("Could not parse response from backend")
+    return response_json
 
 
 def update_inventory(name, quantity):
     product_instance = Product.objects.get(name=name)
-    product_instance.inventory_on_hand = product_instance.inventory_on_hand - 
quantity
+    inventory_on_hand = product_instance.inventory_on_hand - quantity
+    product_instance.inventory_on_hand = inventory_on_hand
 
 
 @login_required
@@ -72,7 +127,8 @@ def update_stock(request, uid):
     context_dict['description'] = product_instance.description
     context_dict['price'] = product_instance.price
     context_dict['inventory_on_hand'] = product_instance.inventory_on_hand
-    context_dict['url_update_inventory'] = str('/update_stock/') + 
product_instance.name
+    url_update_inventory = str('/update_stock/') + product_instance.name
+    context_dict['url_update_inventory'] = url_update_inventory
     return render(request, 'inventory/product.html', context_dict)
 
 
@@ -115,7 +171,8 @@ def product(request, uid):
     context_dict['description'] = product_instance.description
     context_dict['price'] = product_instance.price
     context_dict['inventory_on_hand'] = product_instance.inventory_on_hand
-    context_dict['url_update_inventory'] = str('/update_stock/') + 
product_instance.name
+    url_update_inventory = str('/update_stock/') + product_instance.name
+    context_dict['url_update_inventory'] = url_update_inventory
     return render(request, 'inventory/product.html', context_dict)
 
 

-- 
To stop receiving notification emails like this one, please contact
address@hidden



reply via email to

[Prev in Thread] Current Thread [Next in Thread]