diff --git a/backend/fees/admin.py b/backend/fees/admin.py index 4ba33e4..af17ca9 100644 --- a/backend/fees/admin.py +++ b/backend/fees/admin.py @@ -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) \ No newline at end of file +admin.site.register(Fees, FeesAdmin) +admin.site.register(BilldeskOrders, BilldeskOrdersAdmin) +admin.site.register(BilldeskTransactions, BilldeskTransactionsAdmin) \ No newline at end of file diff --git a/backend/fees/migrations/0002_billdeskorders_billdesktransactions.py b/backend/fees/migrations/0002_billdeskorders_billdesktransactions.py new file mode 100644 index 0000000..c6612bf --- /dev/null +++ b/backend/fees/migrations/0002_billdeskorders_billdesktransactions.py @@ -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', + }, + ), + ] diff --git a/backend/fees/models.py b/backend/fees/models.py index 34547fc..1829590 100644 --- a/backend/fees/models.py +++ b/backend/fees/models.py @@ -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' \ No newline at end of file diff --git a/backend/fees/views.py b/backend/fees/views.py index d2cbc55..ad5d10f 100644 --- a/backend/fees/views.py +++ b/backend/fees/views.py @@ -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 @@ -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) @@ -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'] } @@ -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) \ No newline at end of file + 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) \ No newline at end of file