Broadleaf Microservices
  • v1.0.0-latest-prod

Stripe Payment Services React Hooks

The Broadleaf Stripe Payment Services React SDK provides several hooks for implementors to make use of.

useCreateSetupIntentRequest

The hook is used to create an event callback that can be used to create the SetupIntent. SetupIntent is used to set up future card payments

Parameters

Parameter Type Required? Description

stripePaymentServicesClient

Stripe Payment Services Client

The client needed to make API requests

authState

AuthState

Important information about the authentication state of the user.

Response

Parameter Type Required? Description

createSetupIntent

( request: SetupIntentRequest ) ⇒ Promise<{ response?: SetupIntentResponse; }> SetupIntentResponse.

The callback method to use to handle creating a Stripe setup intent request

isFetching

boolean

Whether the request is being processed.

error

ApiError

The API Error if the request to failed.

Example

import { StripePaymentServicesClient } from '@broadleaf/stripe-payment-services-api';
import { useCreateSetupIntentRequest } from '@broadleaf/stripe-payment-services-react';
import type { ClientOptions } from '@broadleaf/commerce-core';
import { usePaymentAuthState } from '@app/common/hooks';

const options: ClientOptions = {
  applicationToken,
  baseHost,
  locale: currentLocale,
  preview,
};
const stripePaymentServicesClient = new StripePaymentServicesClient(options);
const authState = usePaymentAuthState();

const { createSetupIntent, isFetching, error } = useCreateSetupIntentRequest({
  stripePaymentServicesClient,
  authState
});

useCreateSetupIntentResponse

Provides a callback to consume the response from Stripe after submitting a request to create a setup intent (i.e., Stripe saved payment). This will use the merchant reference to determine the id of an existing SavedPaymentMethod in Broadleaf to retrieve and update with the token name.

Parameters

Parameter Type Required? Description

stripePaymentServicesClient

Stripe Payment Services Client

The client needed to make API requests

authState

AuthState

Important information about the authentication state of the user.

Response

Parameter Type Required? Description

createSetupIntent

( request: SetupIntentRequest ) ⇒ Promise<{ response?: SetupIntentResponse; }>

The callback to use to create a Stripe setup intent.

isFetching

boolean

Whether the setup intent is being processed.

error

ApiError

The API Error if the request to process the token service response fails.

Example

import { useMemo } from 'react';
import { useAuth } from '@broadleaf/auth-react';
import { StripePaymentServicesClient } from '@broadleaf/stripe-payment-services-api';
import { useCreateSetupIntentRequest } from '@broadleaf/stripe-payment-services-react';
import type { AuthState } from '@broadleaf/payment-js';
import type { ClientOptions } from '@broadleaf/commerce-core';

const { isAuthenticated, isLoading, getAccessToken, user } = useAuth();
const { csrMode, csrAnonymous } = useCsrContext();
const authState = useMemo<AuthState>(() => {
  const customerEmail = user?.email_address;
  const customerId = user?.customer_id;
  return {
    isAuthenticated,
    isAuthenticating: isLoading,
    getAccessToken,
    customerEmail,
    customerId,
    isCsr: csrMode,
    isAnonymousCsr: csrAnonymous,
  };
}, [csrAnonymous, csrMode, getAccessToken, isAuthenticated, isLoading, user]);
const options: ClientOptions = {
  applicationToken,
  baseHost,
  locale: currentLocale,
  preview,
};
const stripePaymentServicesClient = new StripePaymentServicesClient(options);
const {
  createSetupIntent,
  isFetching,
  error,
} = useCreateSetupIntentRequest({
  stripePaymentServicesClient,
  authState
});