mangopay
v1.3.0
Published
Mango API wrapper
Downloads
117
Keywords
Readme
MangoPay nodejs APIv2 wrapper
This is not the official Mangopay Node library.
At this time they don't have one, so we'll see what's next.
It's here for a personal use and inspired by Stripe node api wrapper.
Feel free to use & contribute
Installation
npm install mangopay
Documentation
Documentation is available at http://docs.mangopay.com/api-references
API Overview
Every resource is accessed via your mango
instance:
var mango = require('mangopay')({
username: 'username',
password: 'passphrase',
production: false
})
// mango.{ RESOURCE_NAME }.{ METHOD_NAME }()
Every resource method accepts an optional callback as the last argument:
mango.card.create({
UserId: '2565355',
CardNumber: '4970100000000154',
CardExpirationDate: '0216',
CardCvx: '123',
}, function(err, card, res){
err;
card; // mango card object
res; // raw 'http' response object => res.statusCode === 200
})
The callback gets called with three arguments: err
, data
and the raw response
object. In case of an error, the data and response are still passed because the MangoPay API returns data together with errors, e.g. when attempting to retrieve a failed payOut.
Methods that list items (cards, transactions, users, etc...) can paginate, filter and sort fields using a special $query
parameter:
mango.user.transactions({
UserId: "123456789",
$query:{
Sort:"LastName:asc",
page:1,
per_page:100
}
},
function(err, transactions, res){
console.log('err', err);
console.log('transactions', transactions);
console.log('res', res.statusCode);
});
or alternatively you can pass a String: $query: "Sort=LastName:asc&page=1&per_page=100"
More information on the specific documentation page
Available resources & methods
Where you see params
it is a plain JavaScript object, e.g. { Email: '[email protected]' }
- user
Create natural user:
mango.user.create({
FirstName: "Victor" // Required
, LastName: "Hugo" // Required
, Birthday: 1300186358 // Required
, Nationality: "FR" // Required - default: 'FR'
, CountryOfResidence: "FR" // Required - default: 'FR'
, Occupation: "Writer"
, IncomeRange: "6"
, ProofOfIdentity: ""
, ProofOfAddress: ""
, PersonType: "NATURAL"
, Email: "[email protected]"
, Tag: "custom tag"
, Address: {
AddressLine1: "4101 Reservoir Rd NW"
, AddressLine2: ""
, City: "Washington"
, Region: "District of Columbia"
, PostalCode: "20007"
, Country: "US"
, }
}, function(err, user, res){
console.log('err', err);
console.log('user', user);
console.log('res', res.statusCode);
});
Create natural user and wallet:
mango.user.signup({
FirstName: "Victor", // Required
LastName: "Hugo", // Required
Birthday: 1300186358, // Required
Nationality: "FR", // Required, default: 'FR'
CountryOfResidence: "FR", // Required, default: 'FR'
Address: "1 rue des Misérables, Paris",
Occupation: "Writer",
IncomeRange: "6",
ProofOfIdentity: null,
ProofOfAddress: null,
PersonType: "NATURAL",
Email: "[email protected]",
Tag: "custom tag",
}, function(err, user, wallet){
console.log('err', err);
console.log('user', user);
console.log('wallet', wallet);
});
Fetch natural user:
mango.user.fetch({
Id: "123456789", // Required
}, function(err, user, res){
console.log('err', err);
console.log('user', user);
console.log('res', res.statusCode);
});
Update natural user:
mango.user.update({
Id: "123456789", // Required
// all the fields to be updated
}, function(err, user, res){
console.log('err', err);
console.log('user', user);
console.log('res', res.statusCode);
});
List natural users:
mango.user.list(function(err,users){
console.log(users);
});
Create legal user:
mango.user.createLegal({
Name: 'mycompany.com',
Email: '[email protected]',
LegalPersonType: 'BUSINESS',
LegalRepresentativeFirstName: 'John',
LegalRepresentativeLastName: 'Doe',
LegalRepresentativeEmail: '[email protected]',
HeadquartersAddress: 'Canal Street, Madrid, Spain',
LegalRepresentativeAdress: 'Canal Street, Madrid, Spain',
LegalRepresentativeBirthday: moment('300681', 'DDMMYY').unix(),
LegalRepresentativeCountryOfResidence: 'ES',
LegalRepresentativeNationality: 'ES',
}, function(err, user, res){
console.log('err', err);
console.log('user', user);
console.log('res', res.statusCode);
});
Fetch legal user:
mango.user.fetchLegal({
Id: "123456789", // Required
}, function(err, user, res){
console.log('err', err);
console.log('user', user);
console.log('res', res.statusCode);
});
List all cards belonging to a user:
mango.user.cards({
UserId: "123456789", // Required
}, function(err, cards, res){
console.log('err', err);
console.log('cards', cards);
console.log('res', res.statusCode);
});
List all wallets belonging to a user:
mango.user.wallets({
UserId: "123456789", // Required
}, function(err, wallets, res){
console.log('err', err);
console.log('wallets', wallets);
console.log('res', res.statusCode);
});
List all transactions belonging to a user:
mango.user.transactions({
UserId: "123456789", // Required
}, function(err, transactions, res){
console.log('err', err);
console.log('transactions', transactions);
console.log('res', res.statusCode);
});
List all bank accounts linked to a user:
mango.user.banks({
UserId: "123456789", // Required
}, function(err, bankaccounts, res){
console.log('err', err);
console.log('bankaccounts', bankaccounts);
console.log('res', res.statusCode);
});
- wallet
Create wallet for a user:
mango.wallet.create({
Owners: ["1167492"], // Required
Description: "A very cool wallet", // Required, default: 'wallet'
Currency: "EUR", // Required, default: 'EUR'
Tag: "your custom tag"
}, function(err, wallet, res){
console.log('err', err);
console.log('wallet', wallet);
console.log('res', res.statusCode);
});
Fetch wallet by id:
mango.wallet.fetch({
Id: "1167492", // Required
}, function(err, wallet, res){
console.log('err', err);
console.log('wallet', wallet);
console.log('res', res.statusCode);
});
Transfer e-money from a wallet to another wallet:
mango.wallet.transfer({
AuthorId : "1167495", // Required
DebitedFunds: {Currency : "EUR", Amount : 1000}, // Required
Fees : {Currency : "EUR", Amount : 100}, // Required, default 'EUR' and 0
DebitedWalletID : "1167496", // Required (Where the funds are held before the transfer)
CreditedWalletID : "1167504", // Required (Where the funds will be held after the transfer)
CreditedUserId : "1167502",
Tag : "DefaultTag"
}, function(err, transfer, res){
console.log('err', err);
console.log('transfer', transfer);
console.log('res', res.statusCode);
});
For a complete list of available parameters check http://docs.mangopay.com/api-references/transfers/
Fetch transfer:
mango.wallet.fetchTransfer({
Id: "1167492", // Required
}, function(err, transfer, res){
console.log('err', err);
console.log('transfer', transfer);
console.log('res', res.statusCode);
});
Fetch all transactions for a given wallet:
mango.wallet.transactions({
Id: "123456789", // Required
}, function(err, transaction, res){
console.log('err', err);
console.log('transaction', transcation);
console.log('res', res.statusCode);
});
Create refund:
mango.wallet.createRefund({
Id: "1122477", // Required (The ID of the Transfer)
AuthorId: "1167492", // Required (The user ID of the Transfer transaction’s author)
}, function(err, refund, res){
console.log('err', err);
console.log('refund', refund);
console.log('res', res.statusCode);
})
Fetch refund:
mango.wallet.fetchRefund({
Id: "1348477", // Required (The ID of the Refund)
}, function(err, refund, res){
console.log('err', err);
console.log('refund', refund);
console.log('res', res.statusCode);
})
- card
Register a card:
mango.card.create({
UserId: '2565355',
CardNumber: '4970100000000154',
CardExpirationDate: '0216',
CardCvx: '123',
}, function(err, card, res){
err;
card; // mango card object
res; // raw 'http' response object => res.statusCode === 200
})
Init two-step card registration process:
mango.card.initRegistration({
UserId: '2565355',
Currency: "EUR"
}, function(err, registration, res){
err;
registration; // mango registration object
res; // raw 'http' response object => res.statusCode === 200
})
Fetch a registered card:
mango.card.fetch({
Id: '2565355', // Required
}, function(err, card, res){
console.log('err', err);
console.log('card', card);
console.log('res', res.statusCode);
})
Update a registered card:
The only editable parameter is Active
, that can be switched from true to false and this action is irreversible.
mango.card.update({
Id: "2565355", // Required
}, function(err, card, res){
console.log('err', err);
console.log('card', card);
console.log('res', res.statusCode);
});
- bank
Register a bank account for a user:
mango.bank.create({
OwnerName: "Victor Hugo", // Required
UserId: "1345678", // Required
Type: "IBAN", // Required, Default: 'IBAN'
OwnerAddress: "1 rue des Misérables", // Required
IBAN: "FR3020041010124530725S03383", // Required
BIC: "CRLYFRPP" // Required
}, function(err, bankaccount, res){
console.log('err', err);
console.log('bankaccount', bankaccount);
console.log('res', res.statusCode);
})
Get a bank account:
mango.bank.fetch({
UserId: '2565355', // Required
BankId: '1234566', // Required
}, function(err, bankaccount, res){
console.log('err', err);
console.log('bankaccount', bankaccount);
console.log('res', res.statusCode);
})
Withdraw money from a wallet to a bank account:
mango.bank.wire({
AuthorId:"12567875", // Required
DebitedWalletId:"12449234", // Required
DebitedFunds:{ // Required
Currency:"EUR",
Amount:"1000"
},
Fees:{ // Required, Default: 'EUR', 0
Currency:"EUR",
Amount:"100"
},
BankAccountId:"12449209", // Required
BIC: "CRLYFRPP" // Required
}, function(err, wire, res){
console.log('err', err);
console.log('wire', wire);
console.log('res', res.statusCode);
})
Get wire:
mango.bank.fetchWire({
Id: '2565355', // Required
}, function(err, wire, res){
console.log('err', err);
console.log('wire', wire);
console.log('res', res.statusCode);
})
Update a registered bank account:
The only editable parameter is Active
, that can be switched from true to false and this action is irreversible.
mango.bank.update({
Id: "2565355", // Required
}, function(err, bank, res){
console.log('err', err);
console.log('bank', bank);
console.log('res', res.statusCode);
});
author
create(params)
document
create(params)
createWithFile(params)
addFile(params)
fetch(params)
update(params)
payin
Create a direct payin by tokenized card:
mango.payin.createByToken({
AuthorId: "1167492", // Required (The user ID of the Payin transaction’s author)
CreditedUserId : "1167502", // Required (The ID of the owner of the credited wallet)
DebitedFunds: { // Required
Currency: "EUR",
Amount: 10000
},
Fees: { // Required
Currency: "EUR",
Amount: 100
},
CreditedWalletId: "1167810", // Required (The ID of the credited wallet)
CardId: "1262419", // Required
SecureMode:"DEFAULT",
SecureModeReturnURL:"https://www.mysite.com",
Tag: "payin" // Required
}, function(err, payin, res){
console.log('err', err);
console.log('payin', payin);
console.log('res', res.statusCode);
})
Create a direct payin by card:
mango.payin.createByCard({
AuthorId: "1167492", // Required (The user ID of the Payin transaction’s author)
CreditedUserId : "1167502", // Required (The ID of the owner of the credited wallet)
DebitedFunds: { // Required
Currency: "EUR",
Amount: 10000
},
DeclaredFees: { // Required
Currency: "EUR",
Amount: 100
},
CreditedWalletId: "1167810", // Required (The ID of the credited wallet)
ReturnURL:"https://www.mysite.com",
Culture: "nl",
CardType: "IDEAL", // Required
Tag: "payin" // Required
}, function(err, payin, res){
console.log('err', err);
console.log('payin', payin);
console.log('res', res.statusCode);
})
Fetch payin:
mango.payin.fetch({
Id: "1122477", // Required (The ID of the Payin)
}, function(err, payin, res){
console.log('err', err);
console.log('payin', payin);
console.log('res', res.statusCode);
})
Create refund:
mango.payin.createRefund({
Id: "1122477", // Required (The ID of the Payin)
AuthorId: "1167492", // Required (The user ID of the Payin transaction’s author)
}, function(err, refund, res){
console.log('err', err);
console.log('refund', refund);
console.log('res', res.statusCode);
})
Fetch refund:
mango.payin.fetchRefund({
Id: "1348477", // Required (The ID of the Refund)
}, function(err, refund, res){
console.log('err', err);
console.log('refund', refund);
console.log('res', res.statusCode);
})
- hook
Create a hook:
mango.hook.create({
Url: "https://my_notifications_end_point.com", // Required
EventType: "PAYIN_NORMAL_SUCCEDED", // Required
Tag: "hook"
}, function(err, hook, res){
console.log('err', err);
console.log('hook', hook);
console.log('res', res.statusCode);
})
List all hooks:
mango.hook.list(function(err, hooks, res){
console.log('err', err);
console.log('hooks', hooks);
console.log('res', res.statusCode);
})
Fetch hook:
mango.hook.fetch({
Id: "12345678"
}, function(err, hook, res){
console.log('err', err);
console.log('hook', hook);
console.log('res', res.statusCode);
})
update hook:
mango.hook.update({
Id: "12345678",
Status: "DISABLED"
}, function(err, hook, res){
console.log('err', err);
console.log('hook', hook);
console.log('res', res.statusCode);
})
Idempotency support
To leverage Mangopay's idempotency support, you can specify an $idempotencyKey
parameter:
mango.user.update({
Id: "123456789",
$idempotencyKey: "a_valid_idempotency_key"
// all the fields to be updated
}, function(err, user, res){
console.log('err', err);
console.log('user', user);
console.log('res', res.statusCode);
});
Note that the idempotency key must be between 16 and 36 characters and contain only alphanumeric characters or dashes.
Test
npm test
don't forget to provide credentials in test/__credentials.json
Todos
- oauth implementation
- user methods only works for "Natural Users"
- exhaustive api methods
- ...