69.3 Item stock not updating and overloading payments

Currently, our item stock is not being updated when a user completes a purchase. Additionally, there is no verification in place to check whether an item in the user's cart has run out of stock by the time they attempt to finalize their purchase. This could lead to inconsistencies in stock management and user experience.

Another issue arises when a new payment instance is created each time the client clicks the "Confirm Order" button. These instances remain in the database indefinitely, as users cannot access older, unfinished payments. To resolve this, we will implement two fixes:

  1. Limit Unfinished Payments: Restrict the number of unfinished payments per user to a maximum of 5. Older unfinished payments will be automatically deleted if the limit is exceeded.

  2. Payment Link Expiry: Set a 1-hour expiration period for each payment link, ensuring that outdated links cannot be reused. This will improve payment management and enhance system efficiency.

views.py/finalize_payment
def finalize_payment(request) :
    data = request.GET.dict()
    status = data.get("status")
    payment_id = data.get("preference_id")
    if status == "approved" :
        payment = Payment.objects.get(payment_id=payment_id) #? getting the already existing payment
        payment.aproved = True
        order = payment.order
        order.finished = True
        order.end_date = datetime.now()

        #? updating the stock
        items_ordered = OrderedItem.objects.filter(order=order)
        for item_ordered in items_ordered :
            item_stock = ItemStock.objects.get(product=item_ordered.itemstock.product, size=item_ordered.itemstock.size, color=item_ordered.itemstock.color)
            item_stock.quantity -= item_ordered.quantity
            item_stock.save()


        order.save()
        payment.save()

        #? email system
        send_purchase_email(order)
        

        if request.user.is_authenticated :
            return redirect("my_orders") #? show finished orders
        else :
            return redirect("order_aproved", order.id)
    else :
        return redirect("checkout")

In the checkout function, we will introduce a verification step to ensure that each product in the order is available in stock. If any item in the cart has insufficient stock, the user will be notified with an appropriate error message, preventing the checkout process from proceeding. This step will help maintain inventory consistency and provide a better user experience.

A similar validation will be implemented in the cart function. If the stock quantity of any item is less than the quantity ordered, the number of products in the cart for that item will be automatically adjusted to match the available stock. This ensures that users cannot order more items than are currently available and provides a seamless shopping experience.

Update the add_to_cart function so the max number of products you can add is the quantity of item_stock

Observation: A new context variable, cart_exists, has been added to the cart function. This variable is used to determine whether the cart contains any items. If the cart is empty, it will trigger the display of an "empty cart" screen, improving user feedback and interface clarity.

Desktop view
Mobile view

Here is the updated cart.html

The cart functionality also includes error messages for scenarios where the stock quantity of an item is insufficient. These messages will inform users of stock limitations, ensuring they understand why the cart has been adjusted.

Finally, in the api_mercadopago.py file, we will implement a 1-hour expiration time for each purchase link. This ensures that payment links are valid only for a limited duration, enhancing security and maintaining accurate order processing workflows.

Last updated