An open API service indexing awesome lists of open source software.

https://github.com/moiseshp/niubiz


https://github.com/moiseshp/niubiz

Last synced: 9 months ago
JSON representation

Awesome Lists containing this project

README

          

# 🧾 `useNiubiz` – React Hook for Niubiz Decoupled SDK Integration

This hook simplifies the integration of the **Niubiz Payment SDK** (in [decoupled mode](https://desarrolladores.niubiz.com.pe/docs/desacoplado)) into modern React applications.

It handles all core aspects of the SDK setup, such as script injection, form mounting, token creation, and form reset, so developers can focus on building a seamless payment experience.

---

## 🎯 Purpose

Niubiz's decoupled SDK allows payment form fields (card number, expiry date, and CVV) to be embedded securely via iframes, keeping sensitive data off your frontend and compliant with PCI standards.

This hook was built to solve the following common needs:

- βœ… Dynamically initialize and unmount the Niubiz SDK
- βœ… Mount secure card fields to specific DOM elements
- βœ… Track validity and errors for each card field
- βœ… Easily retrieve a token (`getTransactionToken`) to send to your backend
- βœ… Reset the payment form after completion (`resetFields`)
- βœ… Know when the form is ready (`isReady`) and fully valid (`isValid`)

---

## πŸ’‘ When to Use

Use this hook if:

- You're building a React app that accepts credit/debit card payments through **Niubiz**.
- You want to follow best practices for PCI compliance using the decoupled SDK.
- You prefer a clean, declarative abstraction over manually invoking `window.payform` logic.
- You need a reusable payment form logic in multiple parts of your app (e.g., checkout, subscription, top-up).

---

## βœ… Features Overview

| Feature | Description |
| --------------------- | -------------------------------------------------------------------------- |
| `isReady` | True when the SDK has loaded and all fields are mounted |
| `fields` | Contains the state of each field (`error`, `isValid`, etc.) |
| `isValid` | Indicates if **all** fields are valid and the form is ready to submit |
| `getTransactionToken` | Triggers the Niubiz tokenization process and returns the transaction token |
| `resetFields` | Clears all Niubiz field inputs and validation states |
| `error` | Captures any SDK initialization failure |

---

## πŸ“¦ Example Integration

This library is designed to be used with minimal setup. You only need to provide your configuration and mount 3 `

`s for the secure fields.

> You can view a full implementation example in the `src/app/page.tsx` file of this repo.

---

## πŸ“Š Example Cards (cvv: 123)

- 371064649323968 - 03/2028 - 111
- 5160030000000317 - 03/2028 - 111
- 4551708161768059 - 03/2028 - 111
- 4919148107859067
- 4500340090000016
- 4551708161768059
- 4280820087569434
- 4634020606098014
- 4110905000721943

---

## πŸ” Why decoupled mode?

The **decoupled mode** is preferred because:

- Fields are securely rendered inside iframes (PCI DSS compliance)
- You have full control over form layout and styling
- Better UX across platforms, including responsive design and resets
- You don’t store or handle sensitive card data directly

---

## 🧰 Requirements

- React 17+ or 18+
- A valid configuration object from Niubiz
- DOM elements with the expected `id`s for mounting each field (e.g., `card-number`, `card-expiry`, `card-cvc`)
- Internet access to load the Niubiz SDK

---

## πŸ›Ÿ Support

If you encounter any issues integrating with Niubiz, we recommend:

- Reviewing the [Niubiz official docs](https://desarrolladores.niubiz.com.pe/docs/desacoplado)
- Checking the developer console for `payform` errors
- Ensuring SDK script is loaded before initializing the hook

---

## πŸ“„ License

MIT