0.0.6 • Published 2 months ago

@sfpy/atoms v0.0.6

Weekly downloads
-
License
-
Repository
-
Last release
2 months ago

Safepay Atoms

Safepay Atoms is a modular library that provides secure payment components for web applications. It includes both Web Components and React components for card capture and payer authentication, enabling seamless integration of Safepay's payment functionality into your applications.

Installation

Using npm

npm install @sfpy/atoms

Using yarn

yarn add @sfpy/atoms

Using CDN

<script src="https://unpkg.com/@sfpy/atoms@latest/dist/components/index.global.js"></script>

Web Components

CardCaptureAtom

The <safepay-card-atom> component provides card capture functionality.

<safepay-card-atom
  environment="sandbox"
  auth-token="your-auth-token"
  tracker="your-tracker"
></safepay-card-atom>

Available Props / Attributes

AttributeTypeDescription
environmentstringEnvironment setting ('sandbox' or 'production')
authTokenstringAuthentication token for the session
trackerstringTracking identifier
validationEventstringEvent that triggers validation
onErrorfunctionError callback handler
onValidatedfunctionValidation success callback
onProceedToAuthenticationfunctionAuthentication proceed callback

CardCaptureAtom Methods

The <safepay-card-atom> component exposes several methods that can be called imperatively:

// Get reference to the element
const cardAtom = document.querySelector('safepay-card-atom');
MethodDescriptionReturnsExample
submit()Triggers the submission of the card datavoidcardAtom.submit()
validate()Performs validation of the current card inputvoidcardAtom.validate()
fetchValidity()Checks if the current card input is validbooleanconst isValid = cardAtom.fetchValidity()
clear()Clears all input fieldsvoidcardAtom.clear()

Example Usage

// Example using Web Component methods
const cardAtom = document.querySelector('safepay-card-atom');

// Submit the form
cardAtom.submit();

// Validate the input
cardAtom.validate();

// Check if input is valid
const isValid = cardAtom.fetchValidity();
isValid.then((isValid) => {
  console.log(isValid);
  cardAtom.submit();
});
// Clear the form
cardAtom.clear();

PayerAuthenticationAtom

The <safepay-payer-authentication> component handles payer authentication flows.

<safepay-payer-authentication
  environment="sandbox"
  auth-token="your-auth-token"
  tracker="your-tracker"
></safepay-payer-authentication>

Available Props / Attributes

AttributeTypeDescription
environmentstringEnvironment setting ('sandbox' or 'production')
trackerstringTracking identifier
authTokenstringAuthentication token
billingobjectBilling information
deviceDataCollectionJWTstringDevice data collection JWT
deviceDataCollectionURLstringDevice data collection URL
onPayerAuthenticationFailurefunctionAuthentication failure callback
onPayerAuthenticationSuccessfunctionAuthentication success callback
onSafepayErrorfunctionError handling callback

React Components

Using Styles

The Safepay Atoms library includes pre-built CSS styles that you can import into your project. These styles are bundled into a single file for convenience.

Importing Styles

To use the styles in your project, import the bundled CSS file:

// Import the styles in your JavaScript/TypeScript project
import '@sfpy/atoms/styles'

CardCapture

import { Suspense, useRef } from 'react';
import { CardCapture } from '@sfpy/atoms';

function PaymentForm() {
  const cardRef = useRef(null);

  return (
    <Suspense fallback={<div>Loading card capture...</div>}>
      <CardCapture
        environment="sandbox"
        authToken="your-auth-token"
        tracker="your-tracker"
        validationEvent="onBlur" // Example event
        imperativeRef={cardRef}
        // Optional callbacks:
        // onValidated={() => console.log('Card validated')}
        // onProceedToAuthentication={(data) => console.log('Proceed to auth', data)}
        // onError={(error) => console.error('Error', error)}
      />
    </Suspense>
  );
}

Available Props

PropTypeDescriptionRequired
environmentstringEnvironment setting ('sandbox' or 'production')
authTokenstringAuthentication token
trackerstringTracking identifier
validationEventstringValidation trigger event (e.g., onBlur, onChange)
onProceedToAuthentication(data: any) => voidCallback when ready to proceed to authentication
onValidated() => voidCallback on successful validation
onError(error: string) => voidError handling callback
imperativeRefReact.MutableRefObjectRef to control the component imperatively

CardCapture Imperative Methods

The CardCapture component can be controlled using a ref. These methods are accessed through the imperativeRef prop:

// Define the ref
const cardRef = useRef<{
  submit: () => void;
  validate: () => void;
  fetchValidity: () => Promise<boolean>;
  clear: () => void;
}>(null);
MethodDescriptionReturnsExample
submit()Submits the card data for processingvoidcardRef.current?.submit()
validate()Triggers validation of the current inputvoidcardRef.current?.validate()
fetchValidity()Asynchronously checks input validityPromise<boolean>const isValid = await cardRef.current?.fetchValidity()
clear()Resets all input fieldsvoidcardRef.current?.clear()

Example Usage

import React, { useRef } from 'react';
import { CardCapture } from '@sfpy/atoms';

function PaymentForm() {
  const cardRef = useRef(null);

  const handleSubmit = async () => {
    // Submit the form
    cardRef.current?.submit();
  };

  const validateCard = async () => {
    // Validate the input
    cardRef.current?.validate();

    // Check validity
    const isValid = await cardRef.current?.fetchValidity();
    console.log('Is card valid:', isValid);
  };

  const resetForm = () => {
    // Clear all inputs
    cardRef.current?.clear();
  };

  return (
    <CardCapture
      imperativeRef={cardRef}
      environment="sandbox"
      authToken="your-auth-token"
      // ... other props
    />
  );
}

PayerAuthentication

import { Suspense, useRef } from 'react';
import { PayerAuthentication } from '@sfpy/atoms';

function AuthenticationForm() {
  const authRef = useRef(null);

  return (
    <Suspense fallback={<div>Loading authentication...</div>}>
      <PayerAuthentication
        environment="sandbox"
        tracker="your-tracker"
        authToken="your-auth-token"
        deviceDataCollectionJWT="your-device-jwt"
        deviceDataCollectionURL="https://your-collection-url"
        imperativeRef={authRef}
        // Optional callbacks you can pass if needed:
        // onPayerAuthenticationSuccess={(data) => console.log('Success', data)}
        // onPayerAuthenticationFailure={(error) => console.log('Failure', error)}
        // onSafepayError={(error) => console.error('Safepay Error', error)}
      />
    </Suspense>
  );
}

Available Props

PropTypeDescriptionRequired
environmentstringEnvironment setting ('sandbox' or 'production')
trackerstringTracking identifier
authTokenstringAuthentication token
deviceDataCollectionJWTstringDevice data collection JWT
deviceDataCollectionURLstringDevice data collection endpoint URL
billingBillingBilling information (optional)
authorizationOptions{ do_capture?: boolean; do_card_on_file?: boolean; }Authorization configuration options
onPayerAuthenticationFailure(data: PayerAuthErrorData) => voidCallback on authentication failure
onPayerAuthenticationSuccess(data: PayerAuthSuccessData) => voidCallback on authentication success
onPayerAuthenticationRequired(data: PayerAuthData) => voidCallback when authentication is required
onPayerAuthenticationFrictionless(data: PayerAuthData) => voidCallback when authentication is frictionless
onPayerAuthenticationUnavailable(data: PayerAuthData) => voidCallback when authentication is unavailable
onSafepayError(data: SafepayError) => voidGeneral error handling callback
imperativeRefReact.MutableRefObjectRef to control the component imperatively

Project Structure

.
├── README.md
├── examples
│   ├── card-links-demo.html
│   └── device-metrics-demo.html
├── package-lock.json
├── package.json
├── postcss.config.js
├── src
│   ├── atoms
│   │   ├── CardCaptureIframe
│   │   │   ├── iframe.tsx
│   │   │   └── index.tsx
│   │   ├── PayerAuthenticationIframe
│   │   │   ├── iframe.tsx
│   │   │   ├── index.tsx
│   │   │   └── types.ts
│   │   ├── hooks
│   │   │   ├── index.ts
│   │   │   └── useFunctionQueue.ts
│   │   └── index.ts
│   ├── bridge
│   │   ├── CardAtom.tsx
│   │   ├── PayerAuthentication.tsx
│   │   └── index.tsx
│   ├── elements
│   │   ├── CardCaptureAtom
│   │   │   └── index.ts
│   │   ├── PayerAuthenticationAtom
│   │   │   └── index.ts
│   │   └── index.ts
│   ├── errors.ts
│   ├── index.ts
│   ├── styles
│   │   ├── css
│   │   │   ├── card-link.css
│   │   │   ├── index.css
│   │   │   ├── payer-auth.css
│   │   │   └── seamless-iframe.css
│   │   ├── hooks
│   │   │   ├── index.ts
│   │   │   └── useStyles.ts
│   │   ├── index.css
│   │   ├── index.ts
│   │   └── loaders
│   │       ├── index.ts
│   │       ├── load-all.ts
│   │       ├── load-card-link.ts
│   │       ├── load-index.ts
│   │       ├── load-payer-auth.ts
│   │       └── load-seamless-iframe.ts
│   ├── types
│   │   ├── atoms.ts
│   │   ├── index.ts
│   │   └── safepay.ts
│   ├── utils
│   │   ├── funcs
│   │   │   ├── base64.ts
│   │   │   ├── defineReactiveProperties.ts
│   │   │   ├── generateUUID.ts
│   │   │   ├── isObject.ts
│   │   │   ├── isString.ts
│   │   │   ├── resolveBaseUrl.ts
│   │   │   └── toCamelCase.ts
│   │   └── index.ts
│   └── utils.ts
├── tsconfig.json
├── tsconfig.react.json
└── types
    ├── atoms
    │   ├── index.d.ts
    │   └── models.d.ts
    ├── index.d.ts
    └── models.d.ts
0.0.6

2 months ago

0.0.5

3 months ago

0.0.4

3 months ago

0.0.3

3 months ago

0.0.2

4 months ago

0.0.1

4 months ago