Skip to main content

Hooks Documentation

This document provides an in-depth overview of the hooks available in the Mileston Payment Client SDK. Note: All hooks require the MilestonPaymentProvider to provide apikey (checkout api key) and businessid via context.


Example: Using the Provider with Hooks

To use any of the hooks in this SDK, you must wrap your application or component tree with the MilestonPaymentProvider. Here's an example:

import React from "react";
import {
MilestonPaymentProvider,
useFetchPayment,
} from "mileston-payment-client";

function PaymentDetails() {
const { data, error, isLoading } = useFetchPayment({
paymentId: "12345",
paymentType: "invoice",
});

if (isLoading) return <p>Loading payment details...</p>;
if (error) return <p>Error: {error.message}</p>;

return (
<div>
<h2>Payment Details</h2>
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
);
}

export default function App() {
return (
<MilestonPaymentProvider
apikey="your-api-key"
businessid="your-business-id"
>
<PaymentDetails />
</MilestonPaymentProvider>
);
}

useFetchPayment

Fetches payment details using a React hook. This hook is ideal for retrieving payment information in real-time.

Usage

import { useFetchPayment } from "mileston-payment-client";

const { data, error, isLoading } = useFetchPayment({
paymentId: "payment-id",
paymentType: "invoice", // or "payment-link", "recurring"
});

Parameters

Parameter NameTypeDescription
paymentIdstringThe ID of the payment to fetch.
paymentTypestringThe type of payment (e.g., "invoice", "payment-link", "recurring").

Returns

Return NameTypeDescription
dataobjectThe fetched payment details.
errorobjectAny error that occurred during the fetch.
isLoadingbooleanA boolean indicating whether the fetch is in progress.

Notes

  • Ensure the MilestonPaymentProvider wraps your component tree to provide the necessary context.
  • Handle errors gracefully to improve user experience.

useGetOnRampData

Fetches onramp data for payments. This hook is useful for integrating onramp services into your application.

Usage

import { useGetOnRampData } from "mileston-payment-client";

const { fetchOnRampData, data, error, loading } = useGetOnRampData();

await fetchOnRampData({
amount: "100",
recipientWalletAddress: "0xRecipientAddress",
chain: "eth", // or "avax", "base", "pol", "arb"
});

Parameters

Parameter NameTypeDescription
amountstringThe amount for the onramp.
recipientWalletAddressstringThe recipient's wallet address.
chainstringThe blockchain network (e.g., "eth").

Returns

Return NameTypeDescription
fetchOnRampDatafunctionA function to fetch onramp data.
dataOnRampLinkResponse | nullThe fetched onramp data.
errorstring | nullAny error that occurred during the fetch.
loadingbooleanA boolean indicating whether the fetch is in progress.

Notes

  • This hook depends on the MilestonPaymentProvider for context, which provides apikey and businessid.
  • Validate the params object to ensure all required fields are provided.
  • Handle errors gracefully to improve user experience.

useGetOnRampPaymentStatus

Fetches the status of an onramp payment. This hook is essential for tracking payment progress.

Usage

import { useGetOnRampPaymentStatus } from "mileston-payment-client";

const { fetchOnRampPaymentStatus, data, error, loading } =
useGetOnRampPaymentStatus();

await fetchOnRampPaymentStatus({
id: "payment-id",
amount: "100",
chain: "eth", // or "avax", "base", "pol", "arb"
recipientWalletAddress: "0xRecipientAddress",
});

Parameters

Parameter NameTypeDescription
idstringThe ID of the payment.
amountstringThe amount for the payment.
chainstringThe blockchain network (e.g., "eth").
recipientWalletAddressstringThe recipient's wallet address.

Returns

Return NameTypeDescription
fetchOnRampPaymentStatusfunctionA function to fetch the onramp payment status.
dataOnRampPaymentStatusResponse | nullThe fetched payment status data.
errorstring | nullAny error that occurred during the fetch.
loadingbooleanA boolean indicating whether the fetch is in progress.

Notes

  • This hook depends on the MilestonPaymentProvider for context, which provides apikey and businessid.
  • Use this hook to provide real-time updates to users.
  • Handle errors gracefully to improve user experience.

useUserDetails

Fetches user details using a React hook. This hook is useful for retrieving user-specific information.

Usage

import { useUserDetails } from "mileston-payment-client";

const { data, loading, error } = useUserDetails("business-id");

Parameters

Parameter NameTypeDescription
pathBusinessIdstringThe business ID to include in the URL path (optional).

Returns

Return NameTypeDescription
dataIGetUser | nullThe fetched user details.
loadingbooleanA boolean indicating whether the fetch is in progress.
errorError | nullAny error that occurred during the fetch.

Notes

  • This hook depends on the MilestonPaymentProvider for context, which provides apikey and businessid.
  • Use this hook to verify user information before initiating sensitive operations.
  • Handle errors gracefully to improve user experience.

usePayment

Handles payment operations using a React hook. This hook simplifies the process of initiating payments.

Usage

import { usePayment } from "mileston-payment-client";

const { initiatePayment, error, isProcessing } = usePayment();

await initiatePayment({
type: "invoice", // or "payment-link", "recurring"
body: {
/* payment details */
},
nativeTokens: "optional-native-tokens",
});

Parameters

Parameter NameTypeDescription
typestringThe type of payment (e.g., "invoice", "payment-link", "recurring").
bodyobjectThe payment details.
nativeTokensstringNative tokens for the payment (e.g., AVAX, POL, ETH). (optional).

Returns

Return NameTypeDescription
initiatePaymentfunctionA function to start the payment process.
errorobjectAny error that occurred during the payment process.
isProcessingbooleanA boolean indicating whether the payment is in progress.

Notes

  • Ensure the MilestonPaymentProvider wraps your component tree.
  • Handle errors gracefully to improve user experience.

useSavePayment

Saves payment details using a React hook. This hook is useful for storing payment information securely.

Usage

import { useSavePayment } from "mileston-payment-client";

const { triggerSavePayment, data, error, loading } = useSavePayment();

await triggerSavePayment(
"invoice", // or "payment-link", "recurring"
{
/* payment details */
},
"optional-native-tokens"
);

Parameters

Parameter NameTypeDescription
typestringThe type of payment (e.g., "invoice", "payment-link", "recurring").
bodyobjectThe payment details.
nativeTokensstringNative tokens for the payment (e.g., AVAX, POL, ETH). (optional).

Returns

Return NameTypeDescription
triggerSavePaymentfunctionA function to save the payment data.
dataSavePaymentResponse | nullThe response from the save operation.
errorstring | nullAny error that occurred during the save process.
loadingbooleanA boolean indicating whether the save is in progress.

Notes

  • This hook depends on the MilestonPaymentProvider for context, which provides apikey and businessid.
  • Use this hook to securely store payment data.
  • Handle errors gracefully to improve user experience.

useSuiPayment

A React hook for handling Sui blockchain payments.

Usage

import { useSuiPayment } from "mileston-payment-client";

const { handleSuiPayment } = useSuiPayment("test");

handleSuiPayment({
amount: "100",
recipientWalletAddress: "0xRecipientAddress",
});

Parameters

Parameter NameTypeDescription
envstringThe environment (e.g., "test", "prod").
amountstringThe amount for the payment.
recipientWalletAddressstringThe recipient's wallet address.

Returns

Return NameTypeDescription
handleSuiPaymentfunctionFunction to initiate Sui payments.

useGetPaymentWallet

Fetches wallet details for a specific wallet type using a React hook. This hook is useful for retrieving wallet information such as balance and transaction history.

Usage

import { useGetPaymentWallet } from "mileston-payment-client";

const { fetchWallet, wallet, error, loading } = useGetPaymentWallet();

await fetchWallet("sui"); // or "evm"

Parameters

Parameter NameTypeDescription
walletTypeWalletTypeThe type of wallet (e.g., "sui", "evm").

Returns

Return NameTypeDescription
fetchWalletfunctionA function to fetch wallet details.
walletGetPaymentWallet | nullThe fetched wallet details.
errorstring | nullAny error that occurred during the fetch.
loadingbooleanA boolean indicating whether the fetch is in progress.

Notes

  • This hook depends on the MilestonPaymentProvider for context, which provides apikey and businessid.
  • Use this hook to retrieve wallet information before initiating wallet-based transactions.
  • Handle errors gracefully to improve user experience.

useVerifyPaymentWithWallet

Verifies a payment using a wallet. This hook is useful for confirming payments made via wallets.

Usage

import { useVerifyPaymentWithWallet } from "mileston-payment-client";

const { verify, data, error, loading } = useVerifyPaymentWithWallet();

await verify(
"invoice", // or "payment-link", "recurring"
{
/* payment details */
},
"optional-native-tokens"
);

Parameters

Parameter NameTypeDescription
typestringThe type of payment (e.g., "invoice", "payment-link", "recurring").
bodyobjectThe payment details.
nativeTokensstringNative tokens for the payment (optional).

Returns

Return NameTypeDescription
verifyfunctionA function to verify the payment.
dataVerifyPaymentWithWallet | nullThe response from the verification process.
errorstring | nullAny error that occurred during the verification.
loadingbooleanA boolean indicating whether the verification is in progress.

Notes

  • This hook depends on the MilestonPaymentProvider for context, which provides apikey and businessid.
  • Use this hook to verify payments securely.
  • Handle errors gracefully to improve user experience.

usePaymentContext

Provides access to the PaymentContext, which contains the apikey and businessid passed to the MilestonPaymentProvider. This hook is essential for accessing these values in components or other hooks.

Usage

import { usePaymentContext } from "mileston-payment-client";

function MyComponent() {
const { apikey, businessid } = usePaymentContext();

return (
<div>
<p>API Key: {apikey}</p>
<p>Business ID: {businessid}</p>
</div>
);
}

Returns

Return NameTypeDescription
apikeystringThe API key provided to the MilestonPaymentProvider.
businessidstringThe business ID provided to the MilestonPaymentProvider.

Notes

  • This hook must be used within a component wrapped by the MilestonPaymentProvider.
  • If used outside the provider, it will throw an error.

useSolanaPayment

A React hook for handling Solana blockchain payments.

Usage

import { useSolanaPayment } from "mileston-payment-client";

const { handleSolanaPayment } = useSolanaPayment("test");

handleSolanaPayment({
amount: "100",
recipientWalletAddress: "RecipientAddress",
token: "SOL", // or "USDC", "USDT"
});

Parameters

Parameter NameTypeDescription
envstringThe environment (e.g., "test", "prod").
amountstringThe amount for the payment.
recipientWalletAddressstringThe recipient's wallet address.
tokenstringThe token type (e.g., "SOL", "USDC").

Returns

Return NameTypeDescription
handleSolanaPaymentfunctionFunction to initiate Solana payments.

Notes

  • The env parameter determines whether the hook operates in a test or production environment.
  • Ensure the recipientWalletAddress is valid to avoid payment failures.
  • Use the token parameter to specify the token type for the payment.