npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

josu-paymongo

v0.1.1

Published

A class-based PayMongo API wrapper for TypeScript/JavaScript with singleton support

Downloads

36

Readme

josu-paymongo

A class-based PayMongo API wrapper for TypeScript/JavaScript with singleton support. Compatible with both client-side and server-side usage in Next.js projects.

Installation

npm install josu-paymongo

Requirements

  • Node.js 18+ (uses native fetch)
  • TypeScript 5.0+ (optional, but recommended)

Quick Start

Server-side (Next.js API Routes / Server Actions)

import { PayMongo } from 'josu-paymongo';

// Singleton pattern (recommended)
const paymongo = PayMongo.getInstance({
  secretKey: process.env.PAYMONGO_SECRET_KEY!,
});

// Create a payment intent
const intent = await paymongo.paymentIntents.create({
  amount: 10000, // PHP 100.00 in centavos
  currency: 'PHP',
  paymentMethodAllowed: ['card', 'gcash', 'paymaya'],
  description: 'Order #12345',
});

// Return client_key to frontend
console.log(intent.attributes.client_key);

Client-side (React/Next.js)

import { PayMongo } from 'josu-paymongo';

// Use public key for client-side operations
const paymongo = new PayMongo({
  publicKey: process.env.NEXT_PUBLIC_PAYMONGO_PUBLIC_KEY!,
});

// Create a payment method (safe for client-side)
const method = await paymongo.paymentMethods.create({
  type: 'card',
  details: {
    cardNumber: '4343434343434345',
    expMonth: 12,
    expYear: 2025,
    cvc: '123',
  },
  billing: {
    name: 'John Doe',
    email: '[email protected]',
  },
});

// Attach payment method to payment intent
const result = await paymongo.paymentIntents.attach(paymentIntentId, {
  paymentMethod: method.id,
  clientKey: clientKey, // from server
  returnUrl: 'https://yoursite.com/payment/complete',
});

// Handle redirect for 3DS or e-wallet authentication
if (result.attributes.status === 'awaiting_next_action') {
  window.location.href = result.attributes.next_action!.redirect.url;
}

API Reference

PayMongo Class

Constructor Options

interface PayMongoConfig {
  publicKey?: string;  // For client-side (pk_test_*, pk_live_*)
  secretKey?: string;  // For server-side (sk_test_*, sk_live_*)
  baseUrl?: string;    // Default: 'https://api.paymongo.com/v1'
}

Singleton Pattern

// Get or create singleton instance
const paymongo = PayMongo.getInstance({ secretKey: 'sk_...' });

// Force create new instance
const paymongo = PayMongo.getInstance({ secretKey: 'sk_...', forceNew: true });

// Reset singleton
PayMongo.resetInstance();

Payment Intents

// Create a payment intent
const intent = await paymongo.paymentIntents.create({
  amount: 10000,
  currency: 'PHP',
  paymentMethodAllowed: ['card', 'gcash', 'paymaya'],
  description: 'Order description',
  metadata: { orderId: '12345' },
});

// Retrieve a payment intent
const intent = await paymongo.paymentIntents.retrieve('pi_xxxxx');

// Retrieve with client key (client-side)
const intent = await paymongo.paymentIntents.retrieve('pi_xxxxx', clientKey);

// Attach payment method
const intent = await paymongo.paymentIntents.attach('pi_xxxxx', {
  paymentMethod: 'pm_xxxxx',
  returnUrl: 'https://yoursite.com/complete',
});

Payment Methods

// Create card payment method
const method = await paymongo.paymentMethods.create({
  type: 'card',
  details: {
    cardNumber: '4343434343434345',
    expMonth: 12,
    expYear: 2025,
    cvc: '123',
  },
  billing: {
    name: 'John Doe',
    email: '[email protected]',
  },
});

// Create e-wallet payment method
const gcashMethod = await paymongo.paymentMethods.create({
  type: 'gcash',
  billing: {
    name: 'John Doe',
    email: '[email protected]',
    phone: '09171234567',
  },
});

// Retrieve payment method
const method = await paymongo.paymentMethods.retrieve('pm_xxxxx');

// Update payment method
const updated = await paymongo.paymentMethods.update('pm_xxxxx', {
  billing: { email: '[email protected]' },
});

Payments

// Retrieve a payment
const payment = await paymongo.payments.retrieve('pay_xxxxx');

// List all payments
const { data, hasMore } = await paymongo.payments.list({ limit: 10 });

// Paginate
const nextPage = await paymongo.payments.list({
  after: data[data.length - 1].id,
  limit: 10,
});

Customers

// Create a customer
const customer = await paymongo.customers.create({
  firstName: 'John',
  lastName: 'Doe',
  email: '[email protected]',
  phone: '09171234567',
});

// List all customers
const { data, hasMore } = await paymongo.customers.list({ limit: 10 });

// Filter customers by email
const customers = await paymongo.customers.list({ email: '[email protected]' });

// Retrieve a customer
const customer = await paymongo.customers.retrieve('cus_xxxxx');

// Update a customer
const updated = await paymongo.customers.update('cus_xxxxx', {
  email: '[email protected]',
});

// Delete a customer
await paymongo.customers.delete('cus_xxxxx');

// Get customer's payment methods
const methods = await paymongo.customers.getPaymentMethods('cus_xxxxx');

// Delete a payment method from customer
await paymongo.customers.deletePaymentMethod('cus_xxxxx', 'pm_xxxxx');

Refunds

// Create a refund
const refund = await paymongo.refunds.create({
  amount: 5000, // PHP 50.00 in centavos
  paymentId: 'pay_xxxxx',
  reason: 'requested_by_customer',
  notes: 'Customer requested refund',
});

// Retrieve a refund
const refund = await paymongo.refunds.retrieve('ref_xxxxx');

// List all refunds
const { data, hasMore } = await paymongo.refunds.list({ limit: 10 });

// List refunds for a specific payment
const refunds = await paymongo.refunds.list({ paymentId: 'pay_xxxxx' });

Webhooks

// Create a webhook
const webhook = await paymongo.webhooks.create({
  url: 'https://yoursite.com/webhooks/paymongo',
  events: ['payment.paid', 'payment.failed'],
});

// Store the secret_key for verifying signatures
console.log(webhook.attributes.secret_key);

// Retrieve a webhook
const webhook = await paymongo.webhooks.retrieve('hook_xxxxx');

// List all webhooks
const { data } = await paymongo.webhooks.list();

// Update a webhook
const updated = await paymongo.webhooks.update('hook_xxxxx', {
  events: ['payment.paid', 'payment.failed', 'payment.refunded'],
});

// Enable a webhook
await paymongo.webhooks.enable('hook_xxxxx');

// Disable a webhook
await paymongo.webhooks.disable('hook_xxxxx');

Checkout Sessions

// Create a checkout session
const session = await paymongo.checkoutSessions.create({
  lineItems: [
    {
      name: 'Premium Plan',
      amount: 99900, // PHP 999.00
      currency: 'PHP',
      quantity: 1,
    },
  ],
  paymentMethodTypes: ['card', 'gcash', 'paymaya'],
  successUrl: 'https://yoursite.com/success',
  cancelUrl: 'https://yoursite.com/cancel',
  description: 'Subscription payment',
});

// Redirect customer to checkout page
window.location.href = session.attributes.checkout_url;

// Retrieve a checkout session
const session = await paymongo.checkoutSessions.retrieve('cs_xxxxx');

// Expire a checkout session
await paymongo.checkoutSessions.expire('cs_xxxxx');

Error Handling

import { PayMongoError, PayMongoConfigError, PayMongoNetworkError } from 'josu-paymongo';

try {
  await paymongo.paymentIntents.create({ ... });
} catch (error) {
  if (error instanceof PayMongoError) {
    console.error('API Error:', error.message);
    console.error('Status:', error.status);
    console.error('Code:', error.code);
    console.error('Details:', error.errors);
  } else if (error instanceof PayMongoNetworkError) {
    console.error('Network Error:', error.message);
  } else if (error instanceof PayMongoConfigError) {
    console.error('Config Error:', error.message);
  }
}

Supported Payment Methods

| Type | Description | |------|-------------| | card | Visa/Mastercard credit and debit cards | | gcash | GCash e-wallet | | paymaya | Maya (formerly PayMaya) | | grab_pay | GrabPay | | dob | Direct Online Banking | | dob_ubp | UnionBank Online | | brankas_bdo | BDO via Brankas | | brankas_landbank | LandBank via Brankas | | brankas_metrobank | Metrobank via Brankas | | billease | BillEase Buy Now Pay Later | | qrph | QR Ph (InstaPay) |

Payment Intent Flow

1. Create Payment Intent (server-side)
   └── Returns client_key

2. Create Payment Method (client-side)
   └── Collects billing info and card/e-wallet details

3. Attach Payment Method to Intent
   └── May return redirect URL for authentication

4. Customer completes authentication
   └── Redirected back to your return_url

5. Check Payment Intent status
   └── 'succeeded' = payment complete

TypeScript Support

All types are exported for full TypeScript support:

import type {
  PaymentIntent,
  PaymentMethod,
  Payment,
  Customer,
  Refund,
  Webhook,
  CheckoutSession,
  CreatePaymentIntentParams,
  PaymentIntentStatus,
  PaymentMethodType,
  WebhookEventType,
} from 'josu-paymongo';

Available Resources

| Resource | Methods | |----------|---------| | paymentIntents | create, retrieve, attach | | paymentMethods | create, retrieve, update | | payments | retrieve, list | | customers | create, retrieve, update, delete, list, getPaymentMethods, deletePaymentMethod | | refunds | create, retrieve, list | | webhooks | create, retrieve, list, update, enable, disable | | checkoutSessions | create, retrieve, expire |

License

MIT