| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- import { useContext, useState } from "react";
- import { useQuery } from "@tanstack/react-query";
- import { z } from "zod";
- import {
- ClientSecretResponse,
- PaymentMethodList,
- PaymentMethodValidator,
- } from "lib/billing/types";
- import api from "shared/api";
- import { Context } from "shared/Context";
- type TUsePaymentMethod = {
- paymentMethodList: PaymentMethodList;
- refetchPaymentMethods: any;
- isDeleting: boolean;
- deletePaymentMethod: (paymentMethodId: string) => Promise<void>;
- };
- type TCreatePaymentMethod = {
- createPaymentMethod: () => Promise<string>;
- };
- type TCheckHasPaymentEnabled = {
- hasPaymentEnabled: boolean;
- refetchPaymentEnabled: any;
- };
- export const usePaymentMethods = (): TUsePaymentMethod => {
- const { user, currentProject } = useContext(Context);
- // State has be shared so that payment methods can be removed
- // from the Billing page once they are deleted
- const [paymentMethodList, setPaymentMethodList] = useState<PaymentMethodList>(
- []
- );
- const [isDeleting, setIsDeleting] = useState<boolean>(false);
- // Fetch list of payment methods
- const paymentMethodReq = useQuery(
- ["getPaymentMethods", currentProject?.id],
- async () => {
- if (!currentProject?.id || currentProject.id === -1) {
- return;
- }
- await api.checkBillingCustomerExists(
- "<token>",
- { user_email: user?.email },
- { project_id: currentProject?.id }
- );
- const listResponse = await api.listPaymentMethod(
- "<token>",
- {},
- { project_id: currentProject?.id }
- );
- const data = PaymentMethodValidator.array().parse(listResponse.data);
- setPaymentMethodList(data);
- return data;
- }
- );
- // Delete list of payment methods
- const deletePaymentMethod = async (paymentMethodId: string) => {
- if (!currentProject?.id) {
- throw new Error("Project ID is missing");
- }
- if (!paymentMethodId) {
- throw new Error("Payment Method ID is missing");
- }
- setIsDeleting(true);
- const resp = await api.deletePaymentMethod(
- "<token>",
- {},
- { project_id: currentProject?.id, payment_method_id: paymentMethodId }
- );
- if (resp.status !== 200) {
- throw new Error("Failed to delete payment method");
- }
- setPaymentMethodList(
- paymentMethodList.filter(
- (paymentMethod) => paymentMethod.id !== paymentMethodId
- )
- );
- setIsDeleting(false);
- };
- return {
- paymentMethodList,
- refetchPaymentMethods: paymentMethodReq.refetch,
- isDeleting,
- deletePaymentMethod,
- };
- };
- export const useCreatePaymentMethod = (): TCreatePaymentMethod => {
- const { currentProject } = useContext(Context);
- const createPaymentMethod = async () => {
- const resp = await api.addPaymentMethod(
- "<token>",
- {},
- { project_id: currentProject?.id }
- );
- const clientSecret = ClientSecretResponse.parse(resp.data);
- return clientSecret;
- };
- return {
- createPaymentMethod,
- };
- };
- export const checkIfProjectHasPayment = (): TCheckHasPaymentEnabled => {
- const { currentProject } = useContext(Context);
- if (!currentProject?.id) {
- throw new Error("Project ID is missing");
- }
- // Fetch list of payment methods
- const paymentEnabledReq = useQuery(
- ["checkPaymentEnabled", currentProject?.id],
- async () => {
- const res = await api.getHasBilling(
- "<token>",
- {},
- { project_id: currentProject.id }
- );
- const data = z.boolean().parse(res.data);
- return data;
- }
- );
- return {
- hasPaymentEnabled: paymentEnabledReq.data ?? false,
- refetchPaymentEnabled: paymentEnabledReq.refetch,
- };
- };
|