You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
571 lines
17 KiB
Python
571 lines
17 KiB
Python
from django.shortcuts import render, get_object_or_404
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.http import JsonResponse
|
|
import requests
|
|
from .decorators import *
|
|
from billing.models import *
|
|
import base64
|
|
import json
|
|
import random
|
|
import string
|
|
from billing.add.views import *
|
|
from .models import *
|
|
from django.db.models import Q
|
|
|
|
# Create your views here.
|
|
@customer_login_required
|
|
def redirect_osimenu(request):
|
|
user_email = request.user.email
|
|
key = 'pbkdf2_sha256600000'
|
|
url = f"https://osimenu.com/login/{user_email}/{key}/"
|
|
return redirect(url)
|
|
|
|
|
|
@customer_login_required
|
|
def redirect_osicard(request):
|
|
user_email = request.user.email
|
|
key = 'pbkdf2_sha256600000'
|
|
url = f"https://mybusinesscardpage.com/login/{user_email}/{key}/"
|
|
return redirect(url)
|
|
|
|
|
|
|
|
# ADD
|
|
@customer_login_required
|
|
def customer_add_ticket(request, *args, **kwargs):
|
|
customer_products = OrderItem.objects.filter(order__status='Completed', active__in=[True, None], item__type = 'Product', order__customer = request.user.customerprofile)
|
|
customer_projects = Project.objects.filter(customer=request.user.customerprofile)
|
|
|
|
if request.method == 'POST':
|
|
project = None
|
|
product = None
|
|
department = None
|
|
regarding = 'General/Account/Billing'
|
|
if request.POST.get('project'):
|
|
project = get_object_or_404(Project, id=request.POST.get('project'))
|
|
department = project.project_type.department
|
|
regarding = 'Project/Product'
|
|
elif request.POST.get('product'):
|
|
product = get_object_or_404(Item, id=request.POST.get('product'))
|
|
department = product.item_type.department
|
|
regarding = 'Project/Product'
|
|
ticket = Ticket(
|
|
status = 'Open',
|
|
customer = request.user.customerprofile,
|
|
title = request.POST.get('title'),
|
|
description = request.POST.get('description'),
|
|
regarding = regarding,
|
|
project = project,
|
|
product = product,
|
|
department = department,
|
|
opened_by = request.user,
|
|
opened_date = datetime.now()
|
|
)
|
|
ticket.save()
|
|
ticket_status_update = TicketStatusUpdate(
|
|
ticket = ticket,
|
|
status = 'Open',
|
|
added_by = request.user,
|
|
date_added = datetime.now()
|
|
)
|
|
ticket_status_update.save()
|
|
for file in request.FILES.getlist('files'):
|
|
ticket_attachment = TicketAttachment(
|
|
ticket=ticket,
|
|
file=file
|
|
)
|
|
ticket_attachment.save()
|
|
return redirect('customerticketdetails', ticket_number=ticket.ticket_number)
|
|
|
|
context = {
|
|
'customer_products': customer_products,
|
|
'customer_projects': customer_projects,
|
|
|
|
}
|
|
|
|
return render(request, 'add_templates/customer-add-ticket.html', context)
|
|
|
|
|
|
@customer_login_required
|
|
def customer_add_ticket_update(request, ticket_id):
|
|
ticket = get_object_or_404(Ticket, id=ticket_id)
|
|
|
|
|
|
if request.method == 'POST':
|
|
ticket_update = TicketUpdate(
|
|
ticket = ticket,
|
|
description = request.POST.get('description'),
|
|
added_by = request.user,
|
|
date_added = datetime.now(),
|
|
)
|
|
ticket_update.save()
|
|
for file in request.FILES.getlist('files'):
|
|
ticket_attachment = TicketAttachment(
|
|
ticket_update=ticket_update,
|
|
file=file
|
|
)
|
|
ticket_attachment.save()
|
|
return redirect('customerticketdetails', ticket_number=ticket.ticket_number)
|
|
|
|
context = {
|
|
'ticket': ticket,
|
|
|
|
|
|
}
|
|
|
|
return render(request, 'add_templates/customer-add-ticket.html', context)
|
|
|
|
|
|
# LISTING
|
|
@customer_login_required
|
|
def customer_invoices(request, *args, **kwargs):
|
|
invoices = Invoice.objects.filter(order__customer = request.user.customerprofile)
|
|
|
|
context = {
|
|
'invoices' : invoices,
|
|
}
|
|
|
|
return render(request, 'listing_pages/customer-invoices.html', context)
|
|
|
|
|
|
@customer_login_required
|
|
def all_products(request, *args, **kwargs):
|
|
|
|
context = {
|
|
|
|
}
|
|
|
|
return render(request, 'listing_pages/products.html', context)
|
|
|
|
|
|
|
|
@customer_login_required
|
|
def customer_orders(request, *args, **kwargs):
|
|
customer = request.user.customerprofile
|
|
orders = Order.objects.filter(customer=customer, status='Completed').order_by()
|
|
|
|
|
|
context = {
|
|
'orders': orders,
|
|
}
|
|
|
|
return render(request, 'listing_pages/customer-orders.html', context)
|
|
|
|
|
|
@customer_login_required
|
|
def customer_tickets(request, *args, **kwargs):
|
|
open_tickets = Ticket.objects.filter(Q(status__in=['Open', 'Working On']) & Q(customer=request.user.customerprofile)).order_by('-id')
|
|
closed_tickets = Ticket.objects.filter(Q(status__in=['Closed']) & Q(customer=request.user.customerprofile)).order_by('-id')
|
|
|
|
context = {
|
|
'open_tickets': open_tickets,
|
|
'closed_tickets': closed_tickets,
|
|
}
|
|
|
|
return render(request, 'listing_pages/customer-tickets.html', context)
|
|
|
|
|
|
|
|
|
|
# DETAILS
|
|
def customer_ticket_details(request, ticket_number):
|
|
ticket = get_object_or_404(Ticket, ticket_number=ticket_number)
|
|
ticket_updates = TicketUpdate.objects.filter(ticket=ticket).order_by('id')
|
|
for update in TicketUpdate.objects.filter(ticket=ticket).exclude(added_by=request.user).order_by('id'):
|
|
if not TicketRead.objects.filter(ticket_update=update, user=request.user).exists():
|
|
TicketRead.objects.create(ticket_update=update, user=request.user, read=True)
|
|
context = {
|
|
'ticket': ticket,
|
|
'ticket_updates': ticket_updates,
|
|
}
|
|
|
|
return render(request, 'details_templates/inner-customer-ticket.html', context)
|
|
|
|
|
|
|
|
|
|
|
|
# PRODUCTS
|
|
@customer_login_required
|
|
def osimenu_plans(request, *args, **kwargs):
|
|
osimenu_basic = Item.objects.filter(title='OSIMENU BASIC').first()
|
|
osimenu_standard = Item.objects.filter(title='OSIMENU STANDARD').first()
|
|
osimenu_premium = Item.objects.filter(title='OSIMENU PREMIUM').first()
|
|
|
|
context = {
|
|
'osimenu_basic': osimenu_basic,
|
|
'osimenu_standard': osimenu_standard,
|
|
'osimenu_premium': osimenu_premium,
|
|
}
|
|
|
|
return render(request, 'products/osimenu-plans.html', context)
|
|
|
|
|
|
@customer_login_required
|
|
def osicard_plans(request, *args, **kwargs):
|
|
|
|
context = {
|
|
|
|
}
|
|
|
|
return render(request, 'products/osicard-plans.html', context)
|
|
|
|
|
|
|
|
@customer_login_required
|
|
def shared_hosting_plans(request, *args, **kwargs):
|
|
|
|
context = {
|
|
|
|
}
|
|
|
|
return render(request, 'products/shared-hosting-plans.html', context)
|
|
|
|
|
|
@customer_login_required
|
|
def cloud_vps_hosting_plans(request, *args, **kwargs):
|
|
|
|
context = {
|
|
|
|
}
|
|
|
|
return render(request, 'products/cloud-vps-hosting-plans.html', context)
|
|
|
|
|
|
@customer_login_required
|
|
def dedicated_servers_plans(request, *args, **kwargs):
|
|
|
|
context = {
|
|
|
|
}
|
|
|
|
return render(request, 'products/dedicated-servers-plans.html', context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Products Payment views
|
|
@customer_login_required
|
|
def payment(request, item_id):
|
|
item = get_object_or_404(Item, id=item_id)
|
|
cycles = RecurringCycle.objects.filter(item = item)
|
|
|
|
selected_cycle = None
|
|
|
|
context = {
|
|
'item' : item,
|
|
'cycles' : cycles,
|
|
'selected_cycle': selected_cycle,
|
|
}
|
|
|
|
return render(request, 'payment.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
|
|
|
|
data = json.loads(request.body)
|
|
item_id = data.get('item_id')
|
|
cycle_id = data.get('cycle_id')
|
|
|
|
item = Item.objects.get(id=item_id)
|
|
|
|
cycle = RecurringCycle.objects.get(id = cycle_id)
|
|
price = cycle.cycle_price
|
|
|
|
|
|
existing_order = Order.objects.filter(customer=customer, status='None').first()
|
|
|
|
|
|
if existing_order:
|
|
existing_order.orderitem_set.all().delete()
|
|
order = existing_order
|
|
else:
|
|
order = Order.objects.create(status='None', customer=customer)
|
|
|
|
order.save()
|
|
order_id = order.order_id
|
|
|
|
|
|
order_item = OrderItem.objects.create(order=order, item=item, end_at = datetime.now() + timedelta(days=(cycle.months * 30)))
|
|
order_item.save()
|
|
|
|
|
|
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': price,
|
|
'order.currency': 'USD',
|
|
'order.description': 'description_of_order',
|
|
'order.notificationUrl' : 'https://newosina.osinode.com/webhooks/',
|
|
'interaction.returnUrl' : f"https://osina.ositcom.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()
|
|
order_items = OrderItem.objects.filter(order=order)
|
|
for order_item in order_items:
|
|
order_item.purchased_at = datetime.now()
|
|
if order_item.item.item_type.name == 'OSIMENU':
|
|
api_url = 'https://osimenu.com/api/create-subscription/'
|
|
random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=10))
|
|
end_date_str = order_item.end_at.strftime('%Y-%m-%d')
|
|
api_data = {
|
|
'user': {
|
|
'username': request.user.email,
|
|
'email': request.user.email,
|
|
'password': random_string,
|
|
'first_name': request.user.first_name,
|
|
'last_name': request.user.last_name
|
|
},
|
|
'customer': {
|
|
'mobile_number': request.user.customerprofile.mobile_number
|
|
},
|
|
'subscription': {
|
|
'plan': order_item.item.title,
|
|
'end_date': end_date_str
|
|
}
|
|
}
|
|
response = requests.post(api_url, json=api_data)
|
|
order_item.active = True
|
|
old_order_items = OrderItem.objects.exclude(order__id=order.id).filter(item__item_type__name='OSIMENU')
|
|
for item in old_order_items:
|
|
item.active = False
|
|
item.save()
|
|
order_item.save()
|
|
add_invoice_pdf(request, order_id=order.id)
|
|
return redirect('customerorders')
|
|
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 buy_free_osimenu(request):
|
|
api_url = 'https://osimenu.com/api/create-subscription/'
|
|
random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=10))
|
|
api_data = {
|
|
'user': {
|
|
'username': request.user.email,
|
|
'email': request.user.email,
|
|
'password': random_string,
|
|
'first_name': request.user.first_name,
|
|
'last_name': request.user.last_name
|
|
},
|
|
'customer': {
|
|
'mobile_number': request.user.customerprofile.mobile_number
|
|
},
|
|
'subscription': {
|
|
'plan': 'OSIMENU BASIC'
|
|
}
|
|
}
|
|
|
|
order = Order.objects.create(
|
|
customer = request.user.customerprofile,
|
|
status = 'Completed'
|
|
)
|
|
|
|
order_item = OrderItem.objects.create(
|
|
order = order,
|
|
item = get_object_or_404(Item, title='OSIMENU BASIC'),
|
|
purchased_at = datetime.now(),
|
|
active = True,
|
|
)
|
|
|
|
old_order_items = OrderItem.objects.exclude(order__id=order.id).filter(item__item_type__name='OSIMENU')
|
|
for item in old_order_items:
|
|
item.active = False
|
|
item.save()
|
|
|
|
response = requests.post(api_url, json=api_data)
|
|
print(response)
|
|
return redirect('customerorders')
|
|
|
|
|
|
|
|
|
|
def buy_free_osicard(request):
|
|
api_url = 'https://mybusinesscardpage.com/api/create-subscription/'
|
|
random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=10))
|
|
api_data = {
|
|
'user': {
|
|
'username': request.user.email,
|
|
'email': request.user.email,
|
|
'password': random_string,
|
|
'first_name': request.user.first_name,
|
|
'last_name': request.user.last_name
|
|
},
|
|
'customer': {
|
|
'mobile_number': request.user.customerprofile.mobile_number
|
|
},
|
|
'subscription': {
|
|
'plan': 'OSICARD BASIC'
|
|
}
|
|
}
|
|
|
|
order = Order.objects.create(
|
|
customer = request.user.customerprofile,
|
|
status = 'Completed'
|
|
)
|
|
|
|
order_item = OrderItem.objects.create(
|
|
order = order,
|
|
item = get_object_or_404(Item, title='OSICARD BASIC'),
|
|
purchased_at = datetime.now(),
|
|
active = True,
|
|
)
|
|
|
|
old_order_items = OrderItem.objects.exclude(order__id=order.id).filter(item__item_type__name='OSICARD')
|
|
for item in old_order_items:
|
|
item.active = False
|
|
item.save()
|
|
|
|
response = requests.post(api_url, json=api_data)
|
|
print(response)
|
|
return redirect('customerorders')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#Invoice Payment views
|
|
@customer_login_required
|
|
def invoice_payment(request, invoice_id):
|
|
invoice = get_object_or_404(Invoice, id = invoice_id)
|
|
|
|
context = {
|
|
'invoice' : invoice,
|
|
}
|
|
|
|
return render(request, 'invoice-payment.html', context)
|
|
|
|
|
|
@customer_login_required
|
|
def initiate_invoice_checkout(request):
|
|
api_username = 'merchant.TEST06127800'
|
|
api_password = '37846250a67c70e7fe9f82cf6ca81f93'
|
|
merchant_id = 'TEST06127800'
|
|
merchant_name = 'Ositcom Sal'
|
|
|
|
customer = request.user.customerprofile
|
|
|
|
data = json.loads(request.body)
|
|
invoice_id = data.get('invoice_id')
|
|
|
|
invoice = get_object_or_404(Invoice, id=invoice_id)
|
|
|
|
|
|
order = invoice.order
|
|
order_id = order.order_id
|
|
|
|
price = order.get_cart_total
|
|
|
|
|
|
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': price,
|
|
'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) |