Initiate paypal payment post request returning 404 error, but the url mapping in django backend is correct, no syntax errors in React frontend

My React and Django website is deployed on render, when i run the backend service from render the initiate paypal payment url is mapped there, i also checked Django urls its there, but when i send the post request from the frontend, i get a 404 error and iyour text checked properly i have no syntax errors, am sending the correct data to the backend, forward slash is there.

Am using Sandbox mode, i removed the client id and secret id from settings.py and i placed them in render's environment variable, so this is how i access them:

  • PAYPAL_CLIENT_ID = os.getenv('PAYPAL_CLIENT_ID')
  • PAYPAL_SECRET_CLIENT = os.getenv('PAYPAL_SECRET_CLIENT')

i feel like whenever i click on the 'pay with Paypal button' so as to send the request, something blocks it hence the 404 error.

i just don't no what the problem is, i have so far tried everything, any one who can help me, i appreciate thank you

backend code


@api_view(['POST'])
def initiate_paypal_payment(request):
    if request.method == 'POST' and request.user.is_authenticated:
        tx_ref = str(uuid.uuid4())
        user = request.user
        cart_id = request.data.get('cart_id')

        #checking if cart_id is available
        if not cart_id:
            return Response({"error": "Cart ID is required"}, status=status.HTTP_400_BAD_REQUEST)

        try:
            cart = Cart.objects.get(cart_id=cart_id)
        except Cart.DoesNotExist:
            return Response({"error": "Cart not found"}, status=status.HTTP_404_NOT_FOUND)

    
        from decimal import Decimal

        amount = sum(Decimal(cake.cake.price) * Decimal(cake.quantity) for cake in cart.cakes.all())
        tax = amount * Decimal('0.18')
        total = amount + tax

        payment = paypalrestsdk.Payment({
            "intent": "sale",
            "payer": {"payment method": "paypal"},
            "redirect_urls": {
                "return_url": f"{BASE_URL}/status?paymentStatus=success&tx_ref={tx_ref}",
                "cancel_url": f"{BASE_URL}/status?paymentStatus=cancel"
            },
            "transactions": [{
                "item_list": {
                    "items": [{
                        "name": "Cart-item",
                        "sku": "cart",
                        "price": f"{total:.2f}",
                        "currency": "USD",
                        "quantity": 1
                    }]
                },
                "amount": {
                    "total": f"{total:.2f}",
                    "currency": "USD"
                },
                "description": f"Payment for cart items for user"
            }]
        })

        transaction, created = Transaction.objects.get_or_create(
            tx_ref=tx_ref,
            cart=cart,
            user=user,
            amount=total,
            status='pending',
        )

        if payment.create():
            for link in payment.links:
                if link.rel == 'approval_url':
                    return Response({'approvalUrl': str(link.href)}, status=status.HTTP_201_CREATED)

        return Response({'error': payment.error}, status=status.HTTP_400_BAD_REQUEST)

this is the frontend code

Back to Top