Broadleaf Microservices
  • v1.0.0-latest-prod

PayPal Payment Services React Hooks

The Broadleaf PayPal Payment Services React SDK provides several hooks for implementors to make use of. These hooks are compatible with the PayPal SDK v6. For previous PayPal SDK versions, see the previous hooks.

usePayPalCreateOrderV6

The hook is used to create an event callback that can be used to create the PayPal Order.

Parameters

Parameter Type Required? Description

payPalPaymentServicesClient

PayPal 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

createPayPalOrder

( cartId: string, request: CreateOrderRequestBodyWithModules ) ⇒ Promise<{ orderId: string }>

✅

The callback method to use to handle creating a PayPal Order, and returns the new Order ID.

isFetching

boolean

✅

Whether the request is being processed.

error

ApiError

❌

The API Error if the request failed.

Example

import { PayPalPaymentServicesClient } from '@broadleaf/paypal-checkout-js';
import { usePayPalCreateOrderV6 } from '@broadleaf/paypal-checkout-react';
import type { ClientOptions } from '@broadleaf/commerce-core';
import { usePaymentAuthState } from '@app/common/hooks';

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

const { createPayPalOrder, isFetching, error } = usePayPalCreateOrderV6({
  payPalPaymentServicesClient,
  authState,
});

usePayPalGetOrder

Provides a callback to retrieve the PayPal Order by ID.

Parameters

Parameter Type Required? Description

payPalPaymentServicesClient

PayPal 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

getPayPalOrder

(cartId: string, paypalOrderId: string) ⇒ Promise<OrderResponseBodyWithModules | undefined>

✅

The callback to use to fetch a PayPal Order by its ID.

isFetching

boolean

✅

Whether the request is being processed.

error

ApiError

❌

The API Error if the request failed.

Example

import { useMemo } from 'react';
import { useAuth } from '@broadleaf/auth-react';
import { PayPalPaymentServicesClient } from '@broadleaf/paypal-checkout-js';
import { usePayPalGetOrder } from '@broadleaf/paypal-checkout-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 payPalPaymentServicesClient = new PayPalPaymentServicesClient(options);
const {
  getPayPalOrder,
  isFetching,
  error,
} = usePayPalGetOrder({
  payPalPaymentServicesClient,
  authState
});
Important
The following hooks are deprecated in favor of the hooks that support PayPal SDK v6.

usePayPalCreateOrder (prior to PayPal SDK v6)

The hook is used to create an event callback that can be used to create the PayPal Order.

Parameters

Parameter Type Required? Description

payPalPaymentServicesClient

PayPal 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

createPayPalOrder

( cartId: string, request: CreateOrderRequestBodyWithModules ) ⇒ Promise<string | undefined>

✅

The callback method to use to handle creating a PayPal Order, and returns the new Order ID.

isFetching

boolean

✅

Whether the request is being processed.

error

ApiError

❌

The API Error if the request failed.

Example

import { PayPalPaymentServicesClient } from '@broadleaf/paypal-checkout-js';
import { usePayPalCreateOrder } from '@broadleaf/paypal-checkout-react';
import type { ClientOptions } from '@broadleaf/commerce-core';
import { usePaymentAuthState } from '@app/common/hooks';

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

const { createPayPalOrder, isFetching, error } = usePayPalCreateOrder({
  payPalPaymentServicesClient,
  authState,
});