Merge pull request #1352 from Infisical/license-server-conn

Add customer portal redirect and patch free trial endpoint
This commit is contained in:
BlackMagiq
2024-01-30 21:17:05 +07:00
committed by GitHub
4 changed files with 85 additions and 7 deletions

View File

@@ -79,7 +79,7 @@ export const registerLicenseRouter = async (server: FastifyZodProvider) => {
},
onRequest: verifyAuth([AuthMode.JWT]),
handler: async (req) => {
const data = await server.services.license.startOrgTrail({
const data = await server.services.license.startOrgTrial({
actorId: req.permission.id,
actor: req.permission.type,
orgId: req.params.organizationId,
@@ -89,6 +89,26 @@ export const registerLicenseRouter = async (server: FastifyZodProvider) => {
}
});
server.route({
url: "/:organizationId/customer-portal-session",
method: "POST",
schema: {
params: z.object({ organizationId: z.string().trim() }),
response: {
200: z.any()
}
},
onRequest: verifyAuth([AuthMode.JWT]),
handler: async (req) => {
const data = await server.services.license.createOrganizationPortalSession({
actorId: req.permission.id,
actor: req.permission.type,
orgId: req.params.organizationId
});
return data;
}
});
server.route({
url: "/:organizationId/plan/billing",
method: "GET",

View File

@@ -24,7 +24,8 @@ import {
TOrgPlanDTO,
TOrgPlansTableDTO,
TOrgPmtMethodsDTO,
TStartOrgTrailDTO,
TStartOrgTrialDTO,
TCreateOrgPortalSession,
TUpdateOrgBillingDetailsDTO
} from "./license-types";
@@ -201,7 +202,7 @@ export const licenseServiceFactory = ({
return plan;
};
const startOrgTrail = async ({ orgId, actorId, actor, success_url }: TStartOrgTrailDTO) => {
const startOrgTrial = async ({ orgId, actorId, actor, success_url }: TStartOrgTrialDTO) => {
const { permission } = await permissionService.getOrgPermission(actor, actorId, orgId);
ForbiddenError.from(permission).throwUnlessCan(
OrgPermissionActions.Create,
@@ -222,13 +223,67 @@ export const licenseServiceFactory = ({
const {
data: { url }
} = await licenseServerCloudApi.request.post(
`/api/license-server/v1/customers/${organization.customerId}/session/trail`,
`/api/license-server/v1/customers/${organization.customerId}/session/trial`,
{ success_url }
);
featureStore.del(FEATURE_CACHE_KEY(orgId));
return { url };
};
const createOrganizationPortalSession = async ({ orgId, actorId, actor }: TCreateOrgPortalSession) => {
const { permission } = await permissionService.getOrgPermission(actor, actorId, orgId);
ForbiddenError.from(permission).throwUnlessCan(
OrgPermissionActions.Create,
OrgPermissionSubjects.Billing
);
ForbiddenError.from(permission).throwUnlessCan(
OrgPermissionActions.Edit,
OrgPermissionSubjects.Billing
);
const organization = await orgDAL.findOrgById(orgId);
if (!organization) {
throw new BadRequestError({
message: "Failed to find organization"
});
}
const {
data: { pmtMethods }
} = await licenseServerCloudApi.request.get(
`/api/license-server/v1/customers/${organization.customerId}/billing-details/payment-methods`
);
if (pmtMethods.length < 1) {
// case: organization has no payment method on file
// -> redirect to add payment method portal
const {
data: { url }
} = await licenseServerCloudApi.request.post(
`/api/license-server/v1/customers/${organization.customerId}/billing-details/payment-methods`,
{
success_url: appCfg.SITE_URL + "/dashboard",
cancel_url: appCfg.SITE_URL + "/dashboard"
}
);
return { url };
} else {
// case: organization has payment method on file
// -> redirect to billing portal
const {
data: { url }
} = await licenseServerCloudApi.request.post(
`/api/license-server/v1/customers/${organization.customerId}/billing-details/billing-portal`,
{
return_url: appCfg.SITE_URL + "/dashboard"
}
);
return { url };
}
}
const getOrgBillingInfo = async ({ orgId, actor, actorId }: TGetOrgBillInfoDTO) => {
const { permission } = await permissionService.getOrgPermission(actor, actorId, orgId);
ForbiddenError.from(permission).throwUnlessCan(
@@ -511,7 +566,8 @@ export const licenseServiceFactory = ({
refreshPlan,
getOrgPlan,
getOrgPlansTableByBillCycle,
startOrgTrail,
startOrgTrial,
createOrganizationPortalSession,
getOrgBillingInfo,
getOrgPlanTable,
getOrgBillingDetails,

View File

@@ -40,10 +40,12 @@ export type TOrgPlanDTO = {
projectId?: string;
} & TOrgPermission;
export type TStartOrgTrailDTO = {
export type TStartOrgTrialDTO = {
success_url: string;
} & TOrgPermission;
export type TCreateOrgPortalSession = TOrgPermission;
export type TGetOrgBillInfoDTO = TOrgPermission;
export type TOrgPlanTableDTO = TOrgPermission;

View File

@@ -328,7 +328,7 @@ export const useCreateCustomerPortalSession = () => {
return useMutation({
mutationFn: async (organizationId: string) => {
const { data } = await apiRequest.post(
`/api/v1/organization/${organizationId}/customer-portal-session`
`/api/v1/organizations/${organizationId}/customer-portal-session`
);
return data;
}