diff --git a/.DS_Store b/.DS_Store index f45e38db..75dd7790 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/osinaweb/.DS_Store b/osinaweb/.DS_Store index a9181839..efeb9561 100644 Binary files a/osinaweb/.DS_Store and b/osinaweb/.DS_Store differ diff --git a/osinaweb/billing/__pycache__/models.cpython-310.pyc b/osinaweb/billing/__pycache__/models.cpython-310.pyc index 7e366464..a20e4de8 100644 Binary files a/osinaweb/billing/__pycache__/models.cpython-310.pyc and b/osinaweb/billing/__pycache__/models.cpython-310.pyc differ diff --git a/osinaweb/billing/add/__pycache__/urls.cpython-310.pyc b/osinaweb/billing/add/__pycache__/urls.cpython-310.pyc index 0b7516ef..dfe03896 100644 Binary files a/osinaweb/billing/add/__pycache__/urls.cpython-310.pyc and b/osinaweb/billing/add/__pycache__/urls.cpython-310.pyc differ diff --git a/osinaweb/billing/add/__pycache__/views.cpython-310.pyc b/osinaweb/billing/add/__pycache__/views.cpython-310.pyc index 57b21366..bda80a6a 100644 Binary files a/osinaweb/billing/add/__pycache__/views.cpython-310.pyc and b/osinaweb/billing/add/__pycache__/views.cpython-310.pyc differ diff --git a/osinaweb/billing/migrations/0040_rename_due_at_orderitem_end_at_and_more.py b/osinaweb/billing/migrations/0040_rename_due_at_orderitem_end_at_and_more.py new file mode 100644 index 00000000..61cddd2f --- /dev/null +++ b/osinaweb/billing/migrations/0040_rename_due_at_orderitem_end_at_and_more.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.5 on 2024-04-23 17:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('billing', '0039_recurringcycle_cycle_price'), + ] + + operations = [ + migrations.RenameField( + model_name='orderitem', + old_name='due_at', + new_name='end_at', + ), + migrations.AddField( + model_name='orderitem', + name='terminated_at', + field=models.DateField(blank=True, null=True), + ), + ] diff --git a/osinaweb/billing/migrations/__pycache__/0039_recurringcycle_cycle_price.cpython-310.pyc b/osinaweb/billing/migrations/__pycache__/0039_recurringcycle_cycle_price.cpython-310.pyc new file mode 100644 index 00000000..7020ec89 Binary files /dev/null and b/osinaweb/billing/migrations/__pycache__/0039_recurringcycle_cycle_price.cpython-310.pyc differ diff --git a/osinaweb/billing/migrations/__pycache__/0040_rename_due_at_orderitem_end_at_and_more.cpython-310.pyc b/osinaweb/billing/migrations/__pycache__/0040_rename_due_at_orderitem_end_at_and_more.cpython-310.pyc new file mode 100644 index 00000000..a89a1116 Binary files /dev/null and b/osinaweb/billing/migrations/__pycache__/0040_rename_due_at_orderitem_end_at_and_more.cpython-310.pyc differ diff --git a/osinaweb/billing/models.py b/osinaweb/billing/models.py index 49841910..ead54455 100644 --- a/osinaweb/billing/models.py +++ b/osinaweb/billing/models.py @@ -58,11 +58,15 @@ class OrderItem(models.Model): order = models.ForeignKey(Order, on_delete=models.CASCADE) item = models.ForeignKey(Item, on_delete=models.CASCADE) purchased_at = models.DateField(null=True, blank=True) - due_at = models.DateField(blank=True, null=True) + end_at = models.DateField(blank=True, null=True) + terminated_at = models.DateField(blank=True, null=True) recurring_cycle = models.ForeignKey(RecurringCycle, on_delete=models.SET_NULL, null=True, blank=True) @property def get_total(self): - total = self.item.amount + if self.recurring_cycle: + total = self.recurring_cycle.cycle_price + else: + total = self.item.amount total = round(total, 2) return total def save(self, *args, **kwargs): diff --git a/osinaweb/customercore/__pycache__/views.cpython-310.pyc b/osinaweb/customercore/__pycache__/views.cpython-310.pyc index b52c64a1..7e0e5633 100644 Binary files a/osinaweb/customercore/__pycache__/views.cpython-310.pyc and b/osinaweb/customercore/__pycache__/views.cpython-310.pyc differ diff --git a/osinaweb/customercore/views.py b/osinaweb/customercore/views.py index 866d6f2f..c638559b 100644 --- a/osinaweb/customercore/views.py +++ b/osinaweb/customercore/views.py @@ -5,6 +5,9 @@ import requests from .decorators import * from billing.models import * import base64 +import json +import random +import string # Create your views here. @customer_login_required @@ -123,7 +126,7 @@ def basic_auth_header(username, password): return f"Basic {encoded_credentials}" -import json + @customer_login_required def initiate_checkout(request): @@ -143,11 +146,21 @@ def initiate_checkout(request): cycle = RecurringCycle.objects.get(id = cycle_id) price = cycle.cycle_price - order = Order.objects.create(status='None', customer=customer) + + 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) + + order_item = OrderItem.objects.create(order=order, item=item, end_at = datetime.now() + timedelta(days=(cycle.months * 30))) order_item.save() @@ -211,6 +224,26 @@ def check_order_status(request, merchant_id, order_id): due_date = order_item.purchased_at + timedelta(days=recurring_cycle) order_item.due_at = due_date order_item.save() + if order_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)) + 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 + } + } + response = requests.post(api_url, json=api_data) + print(response) return redirect('orders') error_message = 'Failed to retrieve order details: ' + response.text return JsonResponse({'error': error_message}, status=500) @@ -223,11 +256,12 @@ def check_order_status(request, merchant_id, order_id): 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': 'OSITCOM2024', + 'password': random_string, 'first_name': request.user.first_name, 'last_name': request.user.last_name }, @@ -259,11 +293,12 @@ def buy_free_osimenu(request): 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': 'OSITCOM2024', + 'password': random_string, 'first_name': request.user.first_name, 'last_name': request.user.last_name }, diff --git a/osinaweb/db.sqlite3 b/osinaweb/db.sqlite3 index 036f6e95..52cbfd60 100644 Binary files a/osinaweb/db.sqlite3 and b/osinaweb/db.sqlite3 differ diff --git a/osinaweb/osinacore/.DS_Store b/osinaweb/osinacore/.DS_Store index d624f690..0f63d7f7 100644 Binary files a/osinaweb/osinacore/.DS_Store and b/osinaweb/osinacore/.DS_Store differ diff --git a/osinaweb/static/images/.DS_Store b/osinaweb/static/images/.DS_Store index d6c95dc1..fbab9770 100644 Binary files a/osinaweb/static/images/.DS_Store and b/osinaweb/static/images/.DS_Store differ