1.0.3 • Published 3 years ago
paymongoo v1.0.3
Paymongo
A typed Nodejs client for Paymongo API
Contents
Installation
npm i paymongoo
yarn add paymongoo
Usage
import Paymongo from 'paymongoo'
const Paymongo = require('paymongoo')
// Secret key can be found at your dashboard developers tab
const paymongo = new Paymongo(process.env.SECRET_KEY)
// For nextjs client side
const paymongo = new Paymongo(process.env.NEXT_PUBLIC_PUBLIC_KEY)
Payment Methods
Create Payment Method
// These are the required data in the payload
const payload = {
data: {
attributes: {
type: 'card',
details: {
card_number: '5339080000000003',
exp_month: 02,
exp_year: 23,
cvc: '123',
},
},
},
}
const res = await paymongo.paymentMethods.create(payload)
Retrieve Payment Method
const res = await paymongo.paymentMethods.retrieve(id)
Result
{
data: {
id: 'pm_**'
type: 'payment_method'
attributes: {
details: {
last4: '0003'
exp_month: 02
exp_year: 23
}
}
livemode: false
type: 'card'
created_at: 1586097138
updated_at: 1586097138
}
}
Payment Intents
Create Payment Intent
const payload = {
data: {
attributes: {
amount: 10000,
payment_method_allowed: ['card'],
currency: 'PHP', // only supported as of the moment
},
},
}
const res = await paymongo.paymentIntents.create(payload)
Retrieve Payment Intent
const res = await paymongo.paymentIntents.retrieve(id)
Attach Payment Intent
const payload = {
data: {
attributes: {
payment_method: 'pm_**',
},
},
}
const res = await paymongo.paymentIntents.retrieve(id, payload)
Result
{
data: {
id: 'pi_**',
type: 'payment_intent',
amount: 10000,
payment_method_allowed: ['card'],
client_key: 'pi_**_client_**'
}
}
Sources
Create Source
const payload = {
data: {
type: 'gcash' | 'grab_pay',
amount: 10000,
currency: 'PHP',
redirect: {
success: 'http://localhost:4000/success'
failed: 'http://localhost:4000/failed',
},
},
}
const res = await paymongo.sources.create(payload)
Retrieve Source
const res = await paymongo.sources.retrieve(id)
Payments
Create Payment
const payload = {
data: {
attributes: {
amount: 10000,
currency: 'PHP',
source: {
id: 'src_**',
type: 'source',
},
},
},
}
const res = await paymongo.payments.create(payload)
Retrieve Payment
const res = await paymongo.payments.retrieve(id)
Result
{
data: {
id: 'pay_**',
type: 'payment',
attributes: {
amount: 10000,
currency: 'PHP',
source: {
id: 'src_**',
type: 'source',
},
},
},
}
List Payments
const res = await paymongo.payments.list()
type ListPaymentArgs = {
limit: number, // defaults to 20
before: number,
after: number
}
const args : Partial<ListPaymentArgs> = {...}
const res = await paymongo.payments.list({args: args})
Result
{
has_more: false,
data: []
}
Webhooks
Create Webhook
const payload = {
data: {
attributes: {
url: 'http://localhost:4000/webhooks',
events: ['source.chargeable'],
},
},
}
const res = await paymongo.webhooks.create(payload)
Retrieve Webhook
const res = await paymongo.webhooks.retrieve(id)
Toggle Webhook
const res = await paymongo.webhooks.toggle(id, 'enable' | 'disable')
Result
{
data: {
id: 'hook_**',
type: 'webhook',
secret_key: 'whsk_**',
status: 'enabled' | 'disabled'
attributes: {
url: 'http://localhost:4000/webhooks',
events: ['source.chargeable']
}
}
}
List Webhooks
const res = await paymongo.webhooks.list()
Result
{
data: []
}