Michael Fayez's avatar

Failed to load resource: the server responded with a status of 402 (Payment Required)

what is the error with this code

import Link from "next/link";
import { useRouter } from "next/router";
import React, { useContext, useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { toast } from "react-toastify";
import apiRequest from "../../../../utils/apiRequest";
import languageModel from "../../../../utils/languageModel";
import { fetchWishlist } from "../../../store/wishlistData";
import LoginContext from "../../Contexts/LoginContexts";
import InputCom from "../../Helpers/InputCom";
import LoaderStyleOne from "../../Helpers/Loaders/LoaderStyleOne";

const SEND = ({ action, des, btn }) => {
  return (
    <div>
      <p className="text-xs text-qblack">{des}</p>
      <button
        type="button"
        onClick={action}
        className="text-sm text-blue-500 font-bold mt-2"
      >
        {btn}
      </button>
    </div>
  );
};

function LoginWidget({ redirect = true, loginActionPopup, notVerifyHandler }) {
  const { websiteSetup } = useSelector((state) => state.websiteSetup);
  const loginPopupBoard = useContext(LoginContext);
  const router = useRouter();
  const dispatch = useDispatch();
  const [email, setEmail] = useState("");
  const [password, setPassword] = useState("");
  const [loading, setLoading] = useState(false);
  const [checked, setValue] = useState(false);
  const [langCntnt, setLangCntnt] = useState(null);
  const [defaultProfileImg, setDefault] = useState(null);
  useEffect(() => {
    setLangCntnt(languageModel());
  }, []);

  useEffect(() => {
    if (!defaultProfileImg) {
      if (websiteSetup) {
        setDefault(websiteSetup.payload.defaultProfile);
      }
    }
  }, [defaultProfileImg]);
  const rememberMe = () => {
    setValue(!checked);
  };
  const sendOtpHandler = () => {
    apiRequest
      .resend({
        email: email,
      })
      .then(() => {
        router.push(`/verify-you?email=${email}`);
      })
      .catch((err) => {
        console.log(err);
      });
  };
  const doLogin = async () => {
    setLoading(true);
    await apiRequest
      .login({
        email: email,
        password: password,
      })
      .then((res) => {
        setLoading(false);
        toast.success(langCntnt && langCntnt.Login_Successfully);
        setEmail("");
        setPassword("");
        localStorage.removeItem("auth");
        localStorage.setItem("auth", JSON.stringify(res.data));
        const activeUser = res.data && {
          name: res.data.user.name,
          phone: res.data.user.phone,
          image: res.data.user.image
            ? res.data.user.image
            : defaultProfileImg && defaultProfileImg.image,
        };
        if (activeUser) {
          localStorage.setItem("active-user", JSON.stringify(activeUser));
        }
        dispatch(fetchWishlist());
        if (redirect) {
          router.push("/");
        } else {
          if (res.data) {
            loginPopupBoard.handlerPopup(false);
          }
        }
      })
      .catch((err) => {
        setLoading(false);
        if (err.response) {
          if (
            err.response.data.notification ===
            "Please verify your acount. If you didn't get OTP, please resend your OTP and verify"
          ) {
            toast.warn(
              <SEND
                des={
                  langCntnt &&
                  langCntnt.Please_verify_your_account__If_you_didnt_get_OTP__please_resend_your_OTP_and_verify
                }
                action={sendOtpHandler}
                btn={langCntnt && langCntnt.Send_OTP}
              />,
              {
                autoClose: 5000,
                icon: false,
                theme: "colored",
              }
            );
          } else {
            toast.error(langCntnt && langCntnt.Invalid_Credentials);
          }
        } else {
          return false;
        }
        console.log(err);
      });
  };
  return (
    <div className="w-full">
      <div className="title-area flex flex-col justify-center items-center relative text-center mb-7">
        <h1 className="text-[34px] font-bold leading-0 text-qblack">
          {langCntnt && langCntnt.Log_In}
        </h1>
      </div>
      <div className="input-area">
        <div className="input-item mb-5">
          <InputCom
            placeholder={langCntnt && langCntnt.Email}
            label={langCntnt && langCntnt.Email_Address + "*"}
            name="email"
            type="email"
            inputClasses="h-[50px]"
            inputHandler={(e) => setEmail(e.target.value)}
            value={email}
          />
        </div>
        <div className="input-item mb-5">
          <InputCom
            placeholder="******"
            label={langCntnt && langCntnt.Password + "*"}
            name="password"
            type="password"
            inputClasses="h-[50px]"
            inputHandler={(e) => setPassword(e.target.value)}
            value={password}
          />
        </div>
        <div className="forgot-password-area flex justify-between items-center mb-7">
          <div className="remember-checkbox flex items-center space-x-2.5">
            <button
              onClick={rememberMe}
              type="button"
              className="w-5 h-5 text-qblack flex justify-center items-center border border-light-gray"
            >
              {checked && (
                <svg
                  xmlns="http://www.w3.org/2000/svg"
                  className="h-5 w-5"
                  viewBox="0 0 20 20"
                  fill="currentColor"
                >
                  <path
                    fillRule="evenodd"
                    d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z"
                    clipRule="evenodd"
                  />
                </svg>
              )}
            </button>
            <span onClick={rememberMe} className="text-base text-qblack">
              {langCntnt && langCntnt.Remember_Me}
            </span>
          </div>
          {redirect && (
            <Link href="/forgot-password">
              <span className="text-base text-qpurple cursor-pointer">
                {langCntnt && langCntnt.Forgot_password}?
              </span>
            </Link>
          )}
        </div>
        <div className="signin-area mb-3.5">
          <div className="flex justify-center">
            <button
              onClick={doLogin}
              type="button"
              className="bg-qpurple rounded-full text-sm text-white w-full h-[50px] font-semibold flex justify-center bg-purple items-center"
            >
              <span>{langCntnt && langCntnt.Log_In}</span>
              {loading && (
                <span className="w-5 " style={{ transform: "scale(0.3)" }}>
                  <LoaderStyleOne />
                </span>
              )}
            </button>
          </div>
        </div>
        <div className="signup-area flex justify-center">
          <p className="text-base text-qgray font-normal">
            {langCntnt && langCntnt.Dontt_have_an_account} ?
            {redirect ? (
              <Link href="/signup" passhref>
                <a rel="noopener noreferrer">
                  <span className="ml-2 text-qblack cursor-pointer capitalize">
                    {langCntnt && langCntnt.sign_up_free}
                  </span>
                </a>
              </Link>
            ) : (
              <button onClick={loginActionPopup} type="button">
                <span className="ml-2 text-qblack cursor-pointer capitalize">
                  {langCntnt && langCntnt.sign_up_free}
                </span>
              </button>
            )}
          </p>
        </div>
      </div>
    </div>
  );
}

export default LoginWidget;
0 likes
2 replies
LaryAI's avatar
Level 58

The error message "Failed to load resource: the server responded with a status of 402 (Payment Required)" indicates that the server is expecting payment for the requested resource or service. This is not an error with the code itself, but rather with the API or service that the code is trying to access.

The code you've posted is a React component that seems to be part of a login system. The error could be occurring in one of the API requests made by the apiRequest object, specifically in the resend or login methods. These methods are likely making HTTP requests to a backend server that requires payment for the service.

To resolve this issue, you should:

  1. Check the API or service documentation to understand why the 402 error is being returned. It could be that you need to set up a payment method or subscribe to a plan to use the API.
  2. Ensure that any required API keys or authentication tokens are correctly configured and passed with the request.
  3. If the API is your own, check the server-side code to ensure that the payment requirement is intentional and correctly implemented.
  4. If you are using a third-party API, you may need to contact their support to clarify the payment requirements and how to proceed.

If you have access to the apiRequest object's implementation, you should look into the resend and login methods to see how they handle HTTP requests and what endpoints they are hitting. This will give you more insight into why the 402 error is being returned.

Unfortunately, without more context or access to the server-side code or the API's documentation, it's not possible to provide a more specific solution.

martinbean's avatar

@michael fayez Are you really expecting someone to read over almost 300 lines of code to find a bug? Provide some more details.

You’re getting a 402 response from a request. So narrow down which request it is, and look there.

Please or to participate in this conversation.