@sk-py/upi-qr
Version:
Generate NPCI-compliant UPI QR codes for Indian digital payments. Supports payee VPA, amount, transaction note, and more.
187 lines (140 loc) β’ 5.97 kB
Markdown
# @sk-py/upi-qr
> Generate NPCI-compliant UPI QR codes for Indian digital payments
> Works in Node.js and React β returns both a Base64 QR image and a UPI intent string.
## Table of Contents
1. [Installation](#installation)
2. [Features](#features)
3. [Usage](#usage)
- [Node.js Example](#nodejs-example)
- [React Example](#react-example)
4. [API Reference: `generateUPIQR()`](#api-reference-generateupiqr)
5. [Component Props: `UPIQRComponent`](#component-props-upiqrcomponent)
6. [Production Tips](#production-tips)
7. [Author & License](#author--license)
8. [Publishing Scope](#publishing-scope)
9. [Contributing](#contributing)
## Installation
```bash
# Using npm
npm install @sk-py/upi-qr
# Or with yarn
yarn add @sk-py/upi-qr
```
> **Peer Dependency:**
> React β₯ 18.0.0 (for the React component)
## Features
- π― Generate UPI payment URLs compliant with NPCI
- πΌοΈ Produce Base64-encoded QR code images
- βοΈ Support all standard UPI fields:
- Payee VPA & Name (required)
- Amount, Currency, Transaction Note, Transaction Reference, URL (optional)
- π Return both:
1. `qr` β Base64 data-URI (PNG)
2. `intent` β `upi://pay?...` URL for deep-linking
- π¨ Ready-to-use React component for quick integration
## Usage
### Node.js Example
```js
import { generateUPIQR } from '@sk-py/upi-qr';
async function createUPIPayment() {
try {
const { qr, intent } = await generateUPIQR({
payeeVPA: 'merchant@bank',
payeeName: 'Acme Corp',
amount: '150.00',
transactionNote: 'Invoice #1234',
transactionRef: 'INV1234',
url: 'https://acme.example.com/order/1234',
currency: 'INR' // defaults to INR
});
console.log('UPI Intent URL:', intent);
// For example, save `qr` (data URI) to a file or send to frontend
} catch (err) {
console.error('Error generating UPI QR:', err);
}
}
createUPIPayment();
```
### React Example
```jsx
import React from 'react';
import { generateUPIQR } from '@sk-py/upi-qr';
import UPIQRComponent from '@sk-py/upi-qr/react';
function App() {
const [qrData, setQrData] = React.useState({ qr: '', intent: '' });
React.useEffect(() => {
generateUPIQR({
payeeVPA: 'merchant@bank',
payeeName: 'Acme Corp',
amount: '250.00',
transactionNote: 'Order #5678'
}).then(setQrData);
}, []);
return (
<div>
{qrData.qr
? <UPIQRComponent
qr={qrData.qr}
intent={qrData.intent}
size="180px"
alt="Pay Acme via UPI"
/>
: <p>Loading QR codeβ¦</p>
}
</div>
);
}
export default App;
```
## API Reference: `generateUPIQR()`
| Method | Description | Parameters | Returns |
| ----------------------- | ------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- |
| `generateUPIQR(options)`| Generate UPI QR & intent string asynchronously | **options** (object)<br>β’ `payeeVPA` **(string)** β Payeeβs Virtual Payment Address (required)<br>β’ `payeeName` **(string)** β Display name (required)<br>β’ `amount` **(string)** β UPI amount<br>β’ `transactionNote` **(string)** β Note/memo<br>β’ `transactionRef` **(string)** β Reference ID<br>β’ `url` **(string)** β Callback URL<br>β’ `currency` **(string)** β Currency code (default: `"INR"`) | `Promise<{ qr: string; intent: string }>` |
## Component Props: `UPIQRComponent`
| Prop | Type | Required | Default | Description |
| -------- | -------- | -------- | ---------- | ------------------------------------------------- |
| `qr` | `string` | β | β | Base64-encoded data URI of the QR code image |
| `intent` | `string` | β | β | UPI deep-link URL (e.g., `upi://pay?β¦`) |
| `alt` | `string` | β | `"UPI QR Code"` | Alt text for the `<img>` |
| `size` | `string` | β | `"200px"` | Width & height of the QR code (e.g., `"150px"`) |
```jsx
<UPIQRComponent
qr={qrData.qr}
intent={qrData.intent}
alt="Pay Acme"
size="250px"
/>
```
## Production Tips
- **Caching:** Generate the QR once per invoice/order and cache the result to avoid repeated CPU work.
- **CDN Delivery:** You can serve the Base64 string as an `<img>` src or convert it to a binary PNG and host on a CDN.
- **Security:** Never expose your merchant secrets in the clientβonly pass public UPI data.
- **Deep Linking:** On mobile devices, clicking the intent link should open the UPI app automatically.

*(Replace above with your generated QR code in production.)*
## Author & License
**Author:** sk-py
**License:** ISC
## Publishing Scope
This package is published under the [@sk-py](https://www.npmjs.com/~sk-py) scope on npm:
```bash
npm install @sk-py/upi-qr
```
## Contributing
1. Fork the repository: `https://github.com/sk-py/upi-qr`
2. Create a feature branch: `git checkout -b feature/my-feature`
3. Commit your changes & tests: `git commit -m "Add my feature"`
4. Push to your branch: `git push origin feature/my-feature`
5. Open a Pull Request
Feel free to file issues and suggest improvements!
Happy coding! π