from django.shortcuts import render from django.contrib.auth.decorators import login_required from django.http import JsonResponse import requests from .decorators import * from billing.models import * import uuid import base64 # Create your views here. @customer_login_required def customer_invoices(request, *args, **kwargs): context = { } return render(request, 'listing_pages/customer-invoices.html', context) @customer_login_required def customer_products(request, *args, **kwargs): context = { } return render(request, 'listing_pages/customer-products.html', context) @customer_login_required def pricing(request, *args, **kwargs): context = { } return render(request, 'pricing.html', context) def basic_auth_header(username, password): credentials = f"merchant.{username}:{password}" encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf-8') return f"Basic {encoded_credentials}" @customer_login_required def initiate_checkout(request): api_username = 'merchant.TEST06127800' api_password = '37846250a67c70e7fe9f82cf6ca81f93' merchant_id = 'TEST06127800' merchant_name = 'Ositcom Sal' customer = request.user.customerprofile order = Order.objects.create(status='None', customer=customer) order.save() order_id = order.order_id payload = { 'apiOperation': 'INITIATE_CHECKOUT', 'apiUsername': api_username, 'apiPassword': api_password, 'merchant': merchant_id, 'interaction.operation': 'PURCHASE', 'interaction.merchant.name': merchant_name, 'order.id': order_id, 'order.amount': '100.00', 'order.currency': 'USD', 'order.description': 'description_of_order', 'order.notificationUrl' : 'https://newosina.osinode.com/webhooks/', 'interaction.returnUrl' : f"https://newosina.osinode.com/check-order-status/{merchant_id}/{order_id}/" } try: response = requests.post('https://creditlibanais-netcommerce.gateway.mastercard.com/api/nvp/version/78', data=payload) print('Response Content:', response.text) if response.status_code == 200: response_data = response.text parsed_data = dict(item.split('=') for item in response_data.split('&')) session_id = parsed_data.get('session.id') return JsonResponse({'session_id': session_id}, status=200) else: print('Response Status Code:', response.status_code) return JsonResponse({'error': 'Failed to initiate checkout'}, status=500) except Exception as e: print('Exception:', e) return JsonResponse({'error': 'Internal Server Error'}, status=500) def check_order_status(request, merchant_id, order_id): api_password = '37846250a67c70e7fe9f82cf6ca81f93' url = f"https://creditlibanais-netcommerce.gateway.mastercard.com/api/rest/version/78/merchant/{merchant_id}/order/{order_id}" order = Order.objects.get(order_id=order_id) headers = { 'Content-Type': 'application/json', 'Authorization': basic_auth_header(merchant_id, api_password) } try: response = requests.get(url, headers=headers) if response.status_code == 200: order_details = response.json() if order_details.get('result') == 'SUCCESS': order.status = 'Completed' order.save() return JsonResponse(order_details) else: error_message = 'Failed to retrieve order details: ' + response.text return JsonResponse({'error': error_message}, status=500) except Exception as e: error_message = 'Exception: ' + str(e) return JsonResponse({'error': error_message}, status=500) def webhook_handler(request): project_type = ProjectType.objects.create(name='Hello') return JsonResponse({'message': 'Webhook received and processed successfully'}, status=200)