Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot] 2026-04-17 17:47:29 +00:00
parent 13b3951a1e
commit 5ef5bab121
9 changed files with 512 additions and 0 deletions

View file

@ -0,0 +1,44 @@
"use client";
import { useState } from "react";
import { toast } from "sonner";
import { initializePaddle, getPaddlePriceId } from "@/lib/paddle";
interface CheckoutOptions {
priceId: string;
quantity?: number;
customerEmail?: string;
userId: string;
successUrl?: string;
}
export function usePaddleCheckout() {
const [loading, setLoading] = useState(false);
const openCheckout = async (opts: CheckoutOptions) => {
setLoading(true);
try {
await initializePaddle();
const paddlePriceId = await getPaddlePriceId(opts.priceId);
window.Paddle.Checkout.open({
items: [{ priceId: paddlePriceId, quantity: opts.quantity ?? 1 }],
customer: opts.customerEmail ? { email: opts.customerEmail } : undefined,
customData: { userId: opts.userId },
settings: {
displayMode: "overlay",
successUrl: opts.successUrl || `${window.location.origin}/checkout/sucesso`,
allowLogout: false,
variant: "one-page",
},
});
} catch (e) {
console.error(e);
toast.error("Não foi possível abrir o checkout. Tente novamente.");
} finally {
setLoading(false);
}
};
return { openCheckout, loading };
}