mirror of
https://github.com/domfelipe/mika-agent-assist.git
synced 2026-08-07 13:56:51 +00:00
Changes
Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com>
This commit is contained in:
parent
13b3951a1e
commit
5ef5bab121
9 changed files with 512 additions and 0 deletions
44
src/hooks/use-paddle-checkout.ts
Normal file
44
src/hooks/use-paddle-checkout.ts
Normal 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 };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue