A lighweight Node.js client for Paymongo API.
As seen on Paymongo's community-made libraries. ❤️
If you like this project, please give it a star, and consider following the author. :)
- Installation
- Usage
- Payment Methods
- Payment Intents
- Sources
- Payments
- Tokens (Deprecated)
- Webhooks
- Test Cards
- Change Logs
- FAQs
$ npm install paymongo
$ yarn add paymongo
import Paymongo from 'paymongo';
// Retrieve the secret key from your paymongo
// dashboard under developers tab.
const paymongo = new Paymongo(process.env.SECRET_KEY);
A PaymentMethod
resource describes which payment method was used to fulfill a payment. It is used with a PaymentIntent
to collect payments.
paymongo.paymentMethods
/**
* These are the required properties
* @param {Object} data The payload.
* @param {Object} data.attributes Payload attributes.
* @param {string} data.attributes.type The type of payment method. The possible value is card for now.
* @param {string} data.attributes.details.card_number Credit/Debit Card number of the PaymentMethod.
* @param {number} data.attributes.details.exp_month Expiry month of the Credit/Debit Card.
* @param {number} data.attributes.details.exp_year Expiry year of the Credit/Debit Card.
* @param {string} data.attributes.details.cvc CVC of the Credit/Debit Card.
*/
const result = await paymongo.paymentMethods.create(data);
Payload
{
data: {
attributes: {
type: 'card', // The only available type for now is 'card'.
details: {
card_number: '4343434343434345',
exp_month: 02,
exp_year: 23,
cvc: '123',
}
}
}
}
/**
* @param {string} id The PaymentMethod id
*/
const result = await paymongo.paymentMethods.retrieve(id);
A PaymentIntent
resource is used to track and handle different states of the payment until it succeeds.
paymongo.paymentIntents
/**
* These are the required properties
* @param {Object} data The payload.
* @param {Object} data.attributes Payload attributes.
* @param {number} data.attributes.amount Amount to be collected by the PaymentIntent.
* @param {string[]} data.attributes.payment_method_allowed The list of payment method types that the PaymentIntent is allowed to use. Possible value is card for now.
* @param {string} data.attributes.currency Three-letter ISO currency code, in uppercase. PHP is the only supported currency as of the moment.
*/
const result = await paymongo.paymentIntents.create(data);
Payload
{
data: {
attributes: {
amount: 10000, // 10000 or 100 in money value is the smallest allowed amount.
currency: 'PHP', // Three-letter ISO currency code. Only supports PHP for now.
payment_method_allowed: ['card'] // The only available value for now is 'card'.
}
}
}
/**
* @param {string} id PaymentIntent id
*/
const result = await paymongo.paymentIntents.retrieve(id);
Attach to PaymentIntent
/**
* These are the required properties
* @param {string} id PaymentIntent id.
* @param {Object} data The payload.
* @param {Object} data.attributes Payload attributes.
* @param {string} data.attributes.payment_method Id of PaymentMethod to attach to the PaymentIntent.
*/
const result = await paymongo.paymentIntents.attach(id, data);
Payload
{
data: {
attributes: {
payment_method: 'abc123'
}
}
}
A Source is a resource to generate your customer's payment instrument. This is normally used to generate check out URLs for e-wallet payments. To learn more about e-wallet integrations, you can visit GCash or GrabPay integration.
paymongo.sources
/**
* These are the required properties
* @param {Object} data Data paypload
* @param {Object} data.attributes Payload attributes
* @param {string} data.attributes.type The type of source. Possible values are gcash and grab_pay.
* @param {number} data.attributes.amount Amount int32
* @param {string} data.attributes.currency Three-letter ISO currency code, in uppercase. PHP is the only supported currency as of the moment.
* @param {Object} data.attributes.redirect
* @param {string} data.attributes.redirect.success Success url
* @param {string} data.attributes.redirect.failed Error url
*/
const result = await paymongo.sources.create(data);
Payload
{
data: {
attributes: {
type: 'gcash',
amount: 20000, // PHP200,
currency: 'PHP',
redirect: {
success: 'https://yoururl.com/success',
failed: 'https://yoururl.com/failed'
}
}
}
}
/**
* @param {string} id Source id
*/
const result = await paymongo.sources.retrieve(id);
A Payment
resource is an attempt by your customer to send you money in exchange for your product. This is a reference to an amount that you are expecting to receive if a payment resource with paid status becomes a part of a payout. If the payment status is failed
, you can determine the reason for failure.
paymongo.payments
/**
* These are the required properties
* @param {Object} data Data payload
* @param {Object} data.attributes Payload attributes
* @param {number} data.attributes.amount Amount int32
* @param {number} data.attributes.currency Three-letter ISO currency code, in uppercase. PHP is the only supported currency as of the moment.
* @param {Object} data.attributes.source The source object from checkout
* @param {string} data.attributes.source.id Id of a Source resource
* @param {string} data.attributes.source.type Type of a Source resource. Possible value is 'source'.
*/
const result = await paymongo.payments.create(data);
Payload
{
data: {
attributes: {
amount: 30000,
currency: 'PHP',
source: {
id: 'abc', // Id of the Source resource.
type: 'source', //
}
}
}
}
const result = await paymongo.payments.list();
Result
{
data: [] // array of payments
}
/**
* @param {string} id Payment id
*/
const result = await paymongo.payments.retrieve();
DEPRECATED
A Webhook
resource primarily refers to a certain URL where we send events that are happening from your account. You can check our GCash and GrabPay integrations to find out some good use cases for webhooks.
paymongo.webhooks
/**
* These are the required properties
* @param {Object} data Data payload
* @param {Object} data.attributes Payload attributes
* @param {string} data.attributes.url The destination URL of the events that happened from your account. Please make sure that the URL is publicly accessible in order for you to receive the event.
* @param {string[]} data.attributes.events The list of events to be sent to this webhook. Possible value in the meantime is source.chargeable.
*/
const result = await paymongo.webhooks.create(data);
Payload
{
data: {
attributes: {
url: 'https://yourwebsite.com/webook-listener', // Developer's note: this is unique in paymongo. You can't create multiple webhooks with same url.
events: ['source.chargeable'] // The only event supported for now is 'source.chargeable'.
}
}
}
const result = await paymongo.webhooks.list();
Result
{
data: [] // Array of webhooks
}
/**
* @param {string} id Webhook id
*/
const result = await paymongo.webhooks.retrieve(id);
Enable or disable a webhook.
/**
* @param {string} id Webhook id
* @param {string} action Toggle options 'enable' or 'disable'
*/
const result = await paymongo.webhooks.toggle(id, action);
Card Number | Brand | CVC | Expiration Date |
---|---|---|---|
4343434343434345 | Visa | Any 3 digits | Any future date |
4571736000000075 | Visa (debit) | Any 3 digits | Any future date |
5555444444444457 | Mastercard | Any 3 digits | Any future date |
2221000000000918 | Mastercard (2-series) | Any 3 digits | Any future date |
5455590000000009 | Mastercard (debit) | Any 3 digits | Any future date |
5339080000000003 | Mastercard (prepaid) | Any 3 digits | Any future date |
More cards here, including 3D Secure Test Cards.
- Add
retrieve
function to sources. - New syntax patterned with Stripe's Node.js library.
- Enhance method verbs
- Deprecate Tokens. See deprecation note here.
-
How to make payment using gcash or grabpay?
- GCash guide - https://developers.paymongo.com/docs/accepting-gcash-payments
- GrabPay guide - https://developers.paymongo.com/docs/accepting-grabpay-payments
-
How to test a webhook?
- I have a web application still in development for testing any webooks. Follow this repo for updates.
Made with ❤️ by Jofferson Ramirez Tiquez