Skip to content

Commit

Permalink
tables for transactions and orders
Browse files Browse the repository at this point in the history
  • Loading branch information
singhchanmeet committed Feb 7, 2024
1 parent affe0cb commit bec7a5f
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 13 deletions.
28 changes: 26 additions & 2 deletions backend/fees/admin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
from django.contrib import admin
from . models import Fees
from . models import Fees, BilldeskOrders, BilldeskTransactions
from import_export.admin import ExportActionMixin

class FeesAdmin(ExportActionMixin, admin.ModelAdmin):
list_display = ['batch', 'total_fee']

class BilldeskOrdersAdmin(admin.ModelAdmin):
list_display = ['order_id', 'bd_order_id', 'order_time', 'order_response']

# This will disbale add functionality
def has_add_permission(self, request):
return False

# This will disable delete functionality
def has_delete_permission(self, request, obj=None):
return False

class BilldeskTransactionsAdmin(admin.ModelAdmin):
list_display = ['order_id', 'transaction_id', 'transaction_status', 'payment_method', 'transaction_time', 'transaction_response']

# This will disbale add functionality
def has_add_permission(self, request):
return False

# This will disable delete functionality
def has_delete_permission(self, request, obj=None):
return False

admin.site.register(Fees, FeesAdmin)
admin.site.register(Fees, FeesAdmin)
admin.site.register(BilldeskOrders, BilldeskOrdersAdmin)
admin.site.register(BilldeskTransactions, BilldeskTransactionsAdmin)
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 4.2.6 on 2024-02-07 07:44

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('fees', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='BilldeskOrders',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('order_id', models.CharField(max_length=25)),
('bd_order_id', models.CharField(max_length=25)),
('order_time', models.DateTimeField(auto_now_add=True)),
('order_response', models.TextField()),
],
options={
'verbose_name_plural': 'Billdesk Orders',
'db_table': 'billdesk_orders',
},
),
migrations.CreateModel(
name='BilldeskTransactions',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('order_id', models.CharField(max_length=25)),
('transaction_id', models.CharField(max_length=25)),
('transaction_status', models.CharField(max_length=25)),
('payment_method', models.CharField(max_length=25)),
('transaction_time', models.DateTimeField(auto_now_add=True)),
('transaction_response', models.TextField()),
],
options={
'verbose_name_plural': 'Billdesk Transactions',
'db_table': 'billdesk_transactions',
},
),
]
27 changes: 27 additions & 0 deletions backend/fees/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,30 @@ def save(self, *args, **kwargs):
class Meta:
db_table = 'fees'
verbose_name_plural = "Fees"



class BilldeskOrders(models.Model):

order_id = models.CharField(max_length=25)
bd_order_id = models.CharField(max_length=25)
order_time = models.DateTimeField(auto_now_add=True)
order_response = models.TextField()

class Meta:
db_table = 'billdesk_orders'
verbose_name_plural = 'Billdesk Orders'


class BilldeskTransactions(models.Model):

order_id = models.CharField(max_length=25)
transaction_id = models.CharField(max_length=25)
transaction_status = models.CharField(max_length=25)
payment_method = models.CharField(max_length=25)
transaction_time = models.DateTimeField(auto_now_add=True)
transaction_response = models.TextField()

class Meta:
db_table = 'billdesk_transactions'
verbose_name_plural = 'Billdesk Transactions'
37 changes: 26 additions & 11 deletions backend/fees/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.shortcuts import render, redirect, HttpResponse
from django.http import JsonResponse
from . models import Fees
from . models import Fees, BilldeskOrders, BilldeskTransactions

from django.views.decorators.csrf import csrf_exempt

Expand Down Expand Up @@ -109,7 +109,7 @@ def create_billdesk_order(request):
"clientid": f"{env('CLIENT_ID')}"
}

token = jwt.encode(json_data, env('BD_SECRET_KEY'), algorithm="HS256", headers=jws_header )
token = jwt.encode(json_data, env('BD_SECRET_KEY'), algorithm=env('ALG'), headers=jws_header )

# Make the POST request
response = requests.post(post_url, data=token, headers=headers)
Expand All @@ -129,10 +129,16 @@ def create_billdesk_order(request):
# return JsonResponse(json_response, json_dumps_params={'indent': 2})
if response.status_code == 200:

decoded_response = jwt.decode(response.text, key=env('BD_SECRET_KEY'), algorithms=["HS256"])
decoded_response = jwt.decode(response.text, key=env('BD_SECRET_KEY'), algorithms=[env('ALG')])

new_order = BilldeskOrders.objects.create(order_id=json_data['orderid'],
bd_order_id=decoded_response.get('bdorderid', ''),
order_response=decoded_response)

new_order.save()

context = {"merchantId" : env('MERCHANT_ID'),
"bdOrderId" : decoded_response.get('bdorderid', None),
"bdOrderId" : decoded_response.get('bdorderid', ''),
"authToken" : decoded_response['links'][1]['headers']['authorization']
}

Expand All @@ -141,16 +147,25 @@ def create_billdesk_order(request):
else:
return HttpResponse(response.text)

except FileNotFoundError:
return JsonResponse({'status': 'error', 'message': 'File not found'})
except json.JSONDecodeError:
return JsonResponse({'status': 'error', 'message': 'Invalid JSON format'})
except Exception as e:
return JsonResponse({'status': 'error', 'message': str(e)})

@csrf_exempt
def billdesk_order_callback(request):

# print (request.data)
decoded_response = jwt.decode(request.POST.get('transaction_response'), key=env('BD_SECRET_KEY'), algorithms=["HS256"])
return JsonResponse(decoded_response)
decoded_response = jwt.decode(request.POST.get('transaction_response'), key=env('BD_SECRET_KEY'), algorithms=[env('ALG')])

new_transaction = BilldeskTransactions.objects.create(order_id=decoded_response.get('orderid', ''),
transaction_id=decoded_response.get('transactionid', ''), transaction_status=decoded_response.get('transaction_error_type', ''),
payment_method=decoded_response.get('payment_method_type', ''), transaction_response=decoded_response)

new_transaction.save()

response = {
"Transaction ID " : decoded_response.get('transactionid', ''),
"Order ID" : decoded_response.get('orderid', ''),
"Transaction Status" : decoded_response.get('transaction_error_type', '').toupper(),
"Transaction Time" : decoded_response.get('transaction_date', '')
}

return JsonResponse(response)

0 comments on commit bec7a5f

Please sign in to comment.