diff --git a/components/PiPayButton.tsx b/components/PiPayButton.tsx new file mode 100644 index 0000000..f49a662 --- /dev/null +++ b/components/PiPayButton.tsx @@ -0,0 +1,14 @@ +"use client" + +import { usePiPayment } from "../hooks/usePiPayment" +import type { PaymentData } from "pi-sdk-js" + +export function PiPayButton({ paymentData }: { paymentData: PaymentData }) { + const { pay, status } = usePiPayment(paymentData) + + return ( + + ) +} diff --git a/components/components/PiAuthGuard.tsx b/components/components/PiAuthGuard.tsx new file mode 100644 index 0000000..0ee0dc0 --- /dev/null +++ b/components/components/PiAuthGuard.tsx @@ -0,0 +1,12 @@ +"use client" + +import { usePiConnection } from "../hooks/usePiConnection" + +export function PiAuthGuard({ children }: { children: React.ReactNode }) { + const { connected, ready } = usePiConnection() + + if (!ready) return

Loading Pi SDK...

+ if (!connected) return

Please authenticate with Pi

+ + return <>{children} +} diff --git a/core/core/core/payment-validator.ts b/core/core/core/payment-validator.ts new file mode 100644 index 0000000..304cf90 --- /dev/null +++ b/core/core/core/payment-validator.ts @@ -0,0 +1,16 @@ +import type { PaymentData } from "pi-sdk-js" +import { PiSdkError, PiSdkErrorCode } from "./error" + +export function validatePaymentData(data: PaymentData) { + if (!data.amount || data.amount <= 0) { + throw new PiSdkError(PiSdkErrorCode.INVALID_PAYMENT, "Invalid amount") + } + if (!data.memo) { + throw new PiSdkError(PiSdkErrorCode.INVALID_PAYMENT, "Memo is required") + } + try { + JSON.stringify(data.metadata ?? {}) + } catch { + throw new PiSdkError(PiSdkErrorCode.INVALID_PAYMENT, "Metadata must be serializable") + } +} diff --git a/core/core/pi-loader.ts b/core/core/pi-loader.ts new file mode 100644 index 0000000..597f7e8 --- /dev/null +++ b/core/core/pi-loader.ts @@ -0,0 +1,9 @@ +export function isPiReady(): boolean { + return typeof window !== "undefined" && !!window.Pi +} + +export function assertPiReady() { + if (!isPiReady()) { + throw new Error("Pi SDK not loaded. Ensure