Skip to content
Projects

About

An open-source Node.js library that supports payments through VNPay (vnpay.vn).

Business & Finance

How these counts are measured

Measured star growth

in 24h
0
in 7 days
0
in 15 days
0
in 1 month
Collecting history

Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.

README_en-US.md

Original

📦 vnpayjs

🇻🇳 Tiếng Việt | 🇺🇸 English

NPM Version Package License NPM Downloads GitHub Stars GitHub Issues Coverage

🚀 Modern Node.js library for integrating VNPay payment gateway into your applications.

TypeScript Support • Tree-shaking • Modular imports • Minimal dependencies


✨ Key Features

🎯 Easy to Use 🧩 Modular 📘 TypeScript ⚡ Optimized
Simple & intuitive API Import by modules Full type support Tree-shaking ready

🆕 New in v2.4.0

  • 📦 Modular imports - Reduce bundle size by up to 80%
  • 🎯 Types-only imports - 0KB runtime for TypeScript projects
  • ⚡ Optimized builds - Code splitting and tree-shaking
  • 🔧 Flexible endpoints - Custom API endpoints

📚 Documentation

📖 Documentation 🔗 Link 📝 Description
Official Docs vnpay.js.org Detailed documentation with examples
VNPay API sandbox.vnpayment.vn/apis Official VNPay documentation
GitHub github.com/lehuygiang28/vnpay Source code & Issues

🚀 Installation

[!TIP] NestJS users: Use nestjs-vnpay - a dedicated wrapper for NestJS.

# NPM
npm install vnpay

# Yarn
yarn add vnpay

# PNPM
pnpm install vnpay

📦 Import Options (v2.4.0+)

🏆 Full Import (Backward Compatible)

import { VNPay, HashAlgorithm, ProductCode } from 'vnpay';

🦩 Modular Imports (Recommended)

import { VNPay } from 'vnpay/vnpay';
import { HashAlgorithm, ProductCode } from 'vnpay/enums';
import { VNP_VERSION, PAYMENT_ENDPOINT } from 'vnpay/constants';
import { resolveUrlString, dateFormat } from 'vnpay/utils';

📘 Types-only (TypeScript)

import type { VNPayConfig, BuildPaymentUrl, Bank } from 'vnpay/types-only';

[!WARNING] ⚠️ IMPORTANT: Client-side (Frontend) Usage

The VNPay library is designed for Node.js backends only as it uses modules like fs, crypto, and other server-only APIs.

❌ DON'T do this in React/Vue/Angular components:

import { VNPay } from 'vnpay'; // Error: Module not found: Can't resolve 'fs'

✅ DO this for frontend:

import type { VNPayConfig, BuildPaymentUrl, Bank, VerifyReturnUrl } from 'vnpay/types-only';
  • Backend (Node.js): Use normal imports for payment processing
  • Frontend (React/Vue/Angular): Only import types for type checking
  • API calls: Call the backend APIs from the frontend instead of direct imports

💡 Usage

🔧 Initialize VNPay

import { VNPay, ignoreLogger } from 'vnpay';

const vnpay = new VNPay({
    // ⚡ Required configuration
    tmnCode: '2QXUI4B4',
    secureSecret: 'your-secret-key',
    vnpayHost: 'https://sandbox.vnpayment.vn',

    // 🔧 Optional configuration
    testMode: true, // Enable test mode
    hashAlgorithm: 'SHA512', // Hash algorithm
    enableLog: true, // Enable/disable logging
    loggerFn: ignoreLogger, // Custom logger

    // 🔧 Custom endpoints
    endpoints: {
        paymentEndpoint: 'paymentv2/vpcpay.html',
        queryDrRefundEndpoint: 'merchant_webapi/api/transaction',
        getBankListEndpoint: 'qrpayauth/api/merchant/get_bank_list',
    },
});

💳 Create Payment URL

const paymentUrl = vnpay.buildPaymentUrl({
    vnp_Amount: 100000, // 100,000 VND
    vnp_IpAddr: '192.168.1.1',
    vnp_ReturnUrl: 'https://yourapp.com/return',
    vnp_TxnRef: 'ORDER_123',
    vnp_OrderInfo: 'Payment for order #123',
});

console.log('Payment URL:', paymentUrl);

✅ Verify Payment

// Verify the return URL. `isSuccess` is meaningful only when the signature is valid.
const verify = vnpay.verifyReturnUrl(req.query);
if (!verify.isVerified) {
    console.log('❌ Invalid signature:', verify.message);
} else if (verify.isSuccess) {
    console.log('✅ Payment successful!', verify.message);
} else {
    console.log('❌ Payment failed:', verify.message);
}

Sponsored by GitAds


🤝 Support & Contribute

🎉 vnpayjs is an open-source and completely free project!

If this library helps you, please give us a ⭐ and buy us a coffee ☕ and click the ads above to support us


GitHub Stars


Buy me a coffee Momo donation

🛠️ How to Contribute

📋 Step 🔗 Action
1 Read Contribution Guidelines
2 Fork repo and create Pull Request
3 Join discussions in Issues

👥 Contributors

Thanks to all contributors who have helped shape this project:


📄 License

MIT License © Lê Huy Giang

Made with ❤️ in Vietnam

💡 Check out practical examples:

</div>

Explore related projects

Loading…