Source

apis/Payment.js

import { useMutation, useQuery } from "@tanstack/react-query";
import Axios from "../utils/ApiConfiguration";
import { toast } from "react-toastify";
/**
 * Custom hook to make a payment with Khalti.
 *
 * This hook sends a request to initiate a payment with Khalti.
 *
 * @param {Object} data - Payment data including payment details.
 * @param {string} data.amount - The amount to be paid.
 * @param {string} data.mobile - The mobile number of the user.
 * @param {string} data.purchase_order_id - The ID of the purchase order.
 * @param {string} data.purchase_order_name - The name of the purchase order.
 * @param {string} data.transaction_id - The ID of the transaction.
 * @returns {Object} The mutation result object.
 * @throws {Error} If there's an issue with making the payment.
 *
 * @example
 * // Usage of usePaymentWithKhalti hook
 * const { mutate, isLoading, error } = usePaymentWithKhalti();
 * const paymentData = {
 *   amount: "1000",
 *   mobile: "9876543210",
 *   purchase_order_id: "PO123",
 *   purchase_order_name: "Sample Order",
 *   transaction_id: "TXN001",
 * };
 *
 * const handlePayment = () => {
 *   mutate(paymentData);
 * };
 */
export const usePaymentWithKhalti = () =>
  useMutation((data) => Axios.post("payment/khalti/", { ...data }), {
    onError: (err) => {
      toast.error(err?.response?.data?.message || "Something went wrong");
    },
  });
/**
 * Custom hook to fetch user payments.
 *
 * This hook sends a request to retrieve user payments from the server.
 *
 * @returns {Object} The query result object.
 * @throws {Error} If there's an issue with fetching user payments.
 *
 * @example
 * // Usage of useGetPayments hook
 * const { data, isLoading, error } = useGetPayments();
 */
export const useGetPayments = () =>
  useQuery({
    queryKey: ["payments"],
    queryFn: () => Axios.get("payment/"),
    refetchOnWindowFocus: false,
  });
/**
 * Custom hook to fetch user transactions.
 *
 * This hook sends a request to retrieve user transactions from the server.
 *
 * @returns {Object} The query result object.
 * @throws {Error} If there's an issue with fetching user transactions.
 *
 * @example
 * // Usage of useGetTransactions hook
 * const { data, isLoading, error } = useGetTransactions();
 */
export const useGetTransactions = () =>
  useQuery({
    queryKey: ["transactions"],
    queryFn: () => Axios.get("transaction/"),
    refetchOnWindowFocus: false,
  });
/**
 * Custom hook to verify a Khalti payment.
 *
 * This hook sends a request to verify a Khalti payment using provided details.
 *
 * @param {Object} data - Verification data including payment details.
 * @param {string} data.pidx - The Khalti payment index.
 * @param {string} data.txnId - The transaction ID.
 * @param {string} data.amount - The amount of the payment.
 * @param {string} data.mobile - The mobile number of the user.
 * @param {string} data.purchase_order_id - The ID of the purchase order.
 * @param {string} data.purchase_order_name - The name of the purchase order.
 * @param {string} data.transaction_id - The ID of the transaction.
 * @returns {Object} The mutation result object.
 * @throws {Error} If there's an issue with verifying the payment.
 *
 * @example
 * // Usage of useVerifyPayment hook
 * const { mutate, isLoading, error } = useVerifyPayment();
 * const verificationData = {
 *   pidx: "12345",
 *   txnId: "TXN001",
 *   amount: "1000",
 *   mobile: "9876543210",
 *   purchase_order_id: "PO123",
 *   purchase_order_name: "Sample Order",
 *   transaction_id: "TXN001",
 * };
 *
 * const handleVerifyPayment = () => {
 *   mutate(verificationData);
 * };
 */
export const useVerifyPayment = () =>
  useMutation({
    mutationFn: (data) =>
      Axios.post(
        `payment/khalti/lookup?pidx=${data?.pidx}&txnId=${data?.txnId}&amount=${data?.amount}&mobile=${data?.mobile}&purchase_order_id=${data?.purchase_order_id}&purchase_order_name=${data?.purchase_order_name}&transaction_id=${data?.transaction_id}`
      ),
    retry: false,

    onError: (err) => {
      toast.error(err?.response?.data?.message || "Something went wrong");
    },
  });