2021-09-26 22:44:26 +00:00
|
|
|
import { useEffect, useRef } from "react";
|
|
|
|
import { useQuery, useMutation, useRouter, useSession } from "blitz";
|
|
|
|
|
2021-09-30 22:18:03 +00:00
|
|
|
import type { Subscription } from "db";
|
2021-09-26 22:44:26 +00:00
|
|
|
import getSubscription from "../queries/get-subscription";
|
2021-09-30 22:59:35 +00:00
|
|
|
import getPayments from "../queries/get-payments";
|
2021-09-26 22:44:26 +00:00
|
|
|
import usePaddle from "./use-paddle";
|
|
|
|
import useCurrentUser from "../../core/hooks/use-current-user";
|
|
|
|
import updateSubscription from "../mutations/update-subscription";
|
|
|
|
|
2021-09-30 22:18:03 +00:00
|
|
|
type Params = {
|
|
|
|
initialData?: Subscription;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function useSubscription({ initialData }: Params = {}) {
|
2021-09-26 22:44:26 +00:00
|
|
|
const session = useSession();
|
|
|
|
const { user } = useCurrentUser();
|
2021-09-30 22:59:35 +00:00
|
|
|
const [subscription] = useQuery(getSubscription, null, { initialData });
|
|
|
|
const [payments] = useQuery(getPayments, null);
|
2021-09-26 22:44:26 +00:00
|
|
|
const [updateSubscriptionMutation] = useMutation(updateSubscription);
|
|
|
|
|
|
|
|
const router = useRouter();
|
|
|
|
const resolve = useRef<() => void>();
|
|
|
|
const promise = useRef<Promise<void>>();
|
|
|
|
|
|
|
|
const { Paddle } = usePaddle({
|
|
|
|
eventCallback(data) {
|
|
|
|
if (["Checkout.Close", "Checkout.Complete"].includes(data.event)) {
|
|
|
|
resolve.current!();
|
|
|
|
promise.current = new Promise((r) => (resolve.current = r));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
promise.current = new Promise((r) => (resolve.current = r));
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
type BuyParams = {
|
2021-10-01 21:04:12 +00:00
|
|
|
planId: number;
|
2021-09-26 22:44:26 +00:00
|
|
|
coupon?: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
async function subscribe(params: BuyParams) {
|
|
|
|
if (!user || !session.orgId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { planId, coupon } = params;
|
|
|
|
const checkoutOpenParams = {
|
|
|
|
email: user.email,
|
|
|
|
product: planId,
|
|
|
|
allowQuantity: false,
|
2021-09-30 22:59:35 +00:00
|
|
|
passthrough: JSON.stringify({ organizationId: session.orgId }),
|
2021-09-26 22:44:26 +00:00
|
|
|
coupon: "",
|
|
|
|
};
|
|
|
|
|
|
|
|
if (coupon) {
|
|
|
|
checkoutOpenParams.coupon = coupon;
|
|
|
|
}
|
|
|
|
|
|
|
|
Paddle.Checkout.open(checkoutOpenParams);
|
|
|
|
|
|
|
|
return promise.current;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updatePaymentMethod({ updateUrl }: { updateUrl: string }) {
|
|
|
|
const checkoutOpenParams = { override: updateUrl };
|
|
|
|
|
|
|
|
Paddle.Checkout.open(checkoutOpenParams);
|
|
|
|
|
|
|
|
return promise.current;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cancelSubscription({ cancelUrl }: { cancelUrl: string }) {
|
|
|
|
const checkoutOpenParams = { override: cancelUrl };
|
|
|
|
|
|
|
|
Paddle.Checkout.open(checkoutOpenParams);
|
|
|
|
|
|
|
|
return promise.current;
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChangePlanParams = {
|
2021-10-01 21:04:12 +00:00
|
|
|
planId: number;
|
2021-09-26 22:44:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
async function changePlan({ planId }: ChangePlanParams) {
|
|
|
|
try {
|
|
|
|
await updateSubscriptionMutation({ planId });
|
|
|
|
router.reload();
|
|
|
|
} catch (error) {
|
|
|
|
console.log("error", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
subscription,
|
2021-09-30 22:59:35 +00:00
|
|
|
payments,
|
2021-09-26 22:44:26 +00:00
|
|
|
subscribe,
|
|
|
|
updatePaymentMethod,
|
|
|
|
cancelSubscription,
|
|
|
|
changePlan,
|
|
|
|
};
|
|
|
|
}
|