Broadleaf Microservices
  • v1.0.0-latest-prod

Payment JS SDK Utils

JS utilities provided by @broadleaf/payment-js.

areAddressesEquivalent

Compares two addresses for equivalency. Addresses can have additional attributes that are not relevant when considering equality. This is particularly useful when detecting if the selected address or the address on the cart are the same as a saved address.

Parameters

Parameter Type Required? Description

adress1

Address

The first address

adress2

Address

The second address

Response

True or false.

Example

import { areAddressesEquivalent } from '@broadleaf/payment-js';

const areEqual = areAddressesEquivalent(address1, address2);

findMatchingSavedAddress

Utility that can find a matching CustomerAddress for the given Address. CustomerAddresses add some additional info to a base Address that aren’t necessarily used in a Billing or Shipping address form.

Parameters

Parameter Type Required? Description

savedAddresses

Array of saved CustomerAddresses

The saved addresses to find a match within

address

Address

The address to find a matching saved (customer) address for.

Response

The CustomerAddress with the same address info as the given Address.

Example

import { findMatchingSavedAddress } from '@broadleaf/payment-js';

const match = findMatchingSavedAddress(savedAddresses, addressToFind);

maskCardNumber

Masks the given card number, e.g., obfuscates the first 'n' digits with asterisks.

Parameters

Parameter Type Required? Description

cardNumber

`string

number`

The number to obfuscate.

options

{ firstN?: number, lastN?: number, length?: number }

Response

The masked number.

Example

import { maskCardNumber } from '@broadleaf/payment-js';

const number = '1234567890'
let masked = maskCardNumber(number);
// masked = '****0987'
masked = maskCardNumber(number, { firstN: 1 });
// masked = '1***0987'
masked = maskCardNumber(number, { length: 10 });
// masked = '******0987'

parsePassthroughPaymentToken

Parses a passthrough payment token created with createPassthroughPaymentToken.

Parameters

Parameter Type Required? Description

paymentToken

string

The payment token.

Response

Parameter Type Required? Description

creditCardHolder

string

The name of the cardholder.

creditCardNumber

string

The credit card number.

creditCardExpDateMonth

string

The expiration month.

creditCardExpDateYear

string

The expiration year.

creditCardType

string

The type of credit card, e.g., VISA, AMEX, MASTERCARD.

Example

import { parsePassthroughPaymentToken } from '@broadleaf/payment-js';

const info = parsePassthroughPaymentToken(token);

createPassthroughPaymentToken

Creates a passthrough payment token to enable passthrough payment gateway testing. This token is a stringification of the payment info object and then base 64 encoded. It simulates a payment token from a real provider like Stripe.

Parameters

Parameter Type Required? Description

creditCardHolder

string

The name of the cardholder.

creditCardNumber

string

The credit card number.

creditCardExpDateMonth

string

The expiration month.

creditCardExpDateYear

string

The expiration year.

creditCardType

string

The type of credit card, e.g., VISA, AMEX, MASTERCARD.

Response

String token representing the passthrough credit card form data.

Example

import { createPassthroughPaymentToken } from '@broadleaf/payment-js';

const token = createPassthroughPaymentToken(info);

determineCreditCardType

Returns the matching card type for the given card number. This uses regex to match standard patterns for card issuers.

Parameters

Parameter Type Required? Description

number

string

The card number.

Response

The matching CardType.

Example

import { determineCreditCardType } from '@broadleaf/payment-js';

const cardNumber = "4263982640269299"; // CardType.VISA
const type = determineCreditCardType(cardNumber);

getCardExpirationValues

Gets the month and year options for card expiration date selectors.

Parameters

None.

Response

Parameter Type Required? Description

months

Array<{ label: string, value: number }>

The list of month values and labels. See getCardExpirationMonths

years

Array<{ label: string, value: number }>

The list of year values and labels. See getCardExpirationYears

Example

import { getCardExpirationValues } from '@broadleaf/payment-js';

const { months, years } = getCardExpirationValues();

getCardExpirationYears

Gets the year options for a card expiration date selector.

Parameters

None.

Response

An array of objects with the following shape:

Parameter Type Required? Description

label

string

Display label. This displays the last 2 digits of the year.

value

number

The form value to use. This goes from the current UTC year (4 digit) and increments it 20 times.

Example

import { getCardExpirationYears } from '@broadleaf/payment-js';

const months = getCardExpirationYears();

getCardExpirationMonths

Gets the month options for a card expiration date selector.

Parameters

None.

Response

An array of objects with the following shape:

Parameter Type Required? Description

label

string

Display label.

value

number

The form value to use.

Example

import { getCardExpirationMonths } from '@broadleaf/payment-js';

const months = getCardExpirationMonths();

calculateFulfillmentGroupAdjustmentsTotal

Takes a FulfillmentGroup and returns an object containing the total adjustments amount including item adjustments and currency for that group.

Parameters

Parameter Type Required? Description

fulfillmentGroup

FulfillmentGroup

The fulfillment group for which to get the total adjustments including items.

Response

Parameter Type Required? Description

currency

string

Currency unit, e.g., USD.

amount

number

The amount, e.g., 1.00.

Example

import { calculateFulfillmentGroupAdjustmentsTotal } from '@broadleaf/payment-js';

const savings = calculateFulfillmentGroupAdjustmentsTotal(fulfillmentGroup);