Files
cal.diy/packages/platform/atoms/hooks/stripe/useConnect.ts
T
Somay Chauhan 59ab38db98 feat: add guards to stripe teams controller (#20540)
* feat: add guards to stripe teams controller

* remove logs and comments

* fix return type status

* refactor: move PlatformSubscription to a dedicated module

* reroute to `organizations/stripe/save` for teams

* fix: type errors

* feat: fixed it for conferencing apps

* feat: Add error handling and fallback URL support in Stripe callback

* Refactor OAuth callback handling and move token validation to service layer

* Add documentation for OAuth callback proxying in conferencing and stripe controllers

* Move OAuthCallbackState type from organizations to stripe service module
2025-04-17 16:36:31 +00:00

55 lines
1.8 KiB
TypeScript

import { useQuery } from "@tanstack/react-query";
import { SUCCESS_STATUS, ERROR_STATUS } from "@calcom/platform-constants";
import type { ApiResponse } from "@calcom/platform-types";
import http from "../../lib/http";
import { useAtomsContext } from "../useAtomsContext";
export const useGetRedirectUrl = (returnTo?: string, onErrorReturnTo?: string, teamId?: number | null) => {
const { organizationId } = useAtomsContext();
// Determine the appropriate endpoint based on whether teamId is provided
let pathname = "/stripe/connect";
if (teamId && organizationId) {
pathname = `/organizations/${organizationId}/teams/${teamId}/stripe/connect`;
}
// Add query parameters
const queryParams = new URLSearchParams();
if (returnTo) queryParams.append("returnTo", returnTo);
if (onErrorReturnTo) queryParams.append("onErrorReturnTo", onErrorReturnTo);
const fullPath = queryParams.toString() ? `${pathname}?${queryParams.toString()}` : pathname;
return useQuery({
queryKey: ["get-stripe-connect-redirect-uri", teamId, organizationId],
staleTime: Infinity,
enabled: false,
queryFn: () => {
return http?.get<ApiResponse<{ authUrl: string }>>(fullPath).then(({ data: responseBody }) => {
if (responseBody.status === SUCCESS_STATUS) {
return responseBody.data.authUrl;
}
if (responseBody.status === ERROR_STATUS) throw new Error(responseBody.error.message);
return "";
});
},
});
};
export const useConnect = (returnTo?: string, onErrorReturnTo?: string, teamId?: number | null) => {
const { refetch } = useGetRedirectUrl(returnTo, onErrorReturnTo, teamId);
const connect = async () => {
const redirectUri = await refetch();
if (redirectUri.data) {
window.location.href = redirectUri.data;
}
};
return { connect };
};