T402 API Reference - v2.8.0
    Preparing search index...

    Function useAsyncPayment

    • Hook for managing async payment operations with loading states.

      Type Parameters

      • T

      Parameters

      • options: UseAsyncPaymentOptions<T>

        Configuration including the payment function and callbacks.

      Returns UseAsyncPaymentResult<T>

      State and methods for managing the async payment.

      import { useAsyncPayment } from "@t402/react";

      function PaymentButton({ paymentPayload }) {
      const { execute, isLoading, isSuccess, error } = useAsyncPayment({
      paymentFn: async () => {
      const response = await fetch("/api/protected", {
      headers: { "X-Payment": paymentPayload },
      });
      return response.json();
      },
      onSuccess: (data) => console.log("Payment succeeded:", data),
      onError: (err) => console.error("Payment failed:", err),
      });

      return (
      <button onClick={execute} disabled={isLoading}>
      {isLoading ? "Processing..." : "Pay Now"}
      </button>
      );
      }