UNPKG

magpie-js-sdk

Version:
188 lines (178 loc) 4.49 kB
const request = require("request"); const utils = require("./utils"); const URI = "/charges"; /** * This class holds the APIs related to Charge * * @param {boolean} isSandbox * @param {string} key * @param {string} version */ function Charge(isSandbox = false, key = null, version) { const url = utils.getUrl(isSandbox); const authorizationKey = utils.getAuthorizationKey(key); /** * Creates a charge * @param {float} amount Amount of the charge. Should be: <the expected amount> times 100 * @param {string} currency Currency of the charge * @param {string} source Source of the charge. Should be a token id * @param {string} description Description of the charge * @param {string} statement Descriptor Statement descriptor of the charge * @param {bool} capture Determines if the charge should capture or not. true or false */ const create = function( amount, currency, source, description, statementDescriptor, capture, params ) { const body = Object.assign( {}, { amount, currency, source, description, statement_descriptor: statementDescriptor, capture }, params ); const options = { url: `${url}/${version}/${URI}`, method: "POST", json: true, headers: { Authorization: `Basic ${authorizationKey}` }, body: body }; return new Promise((resolve, reject) => { request(options, function(err, response, body) { if (err) { reject(err); } resolve({ statusCode: response.statusCode, body }); }); }); }; /** * Gets a specific charge * * @param {string} id Charge ID to fetch * * @returns Promise */ const get = function(id, bank = false) { let finalUrl = `${url}/${version}/${URI}/${id}/`; if (bank) { finalUrl = `${url}/${version}/${URI}/${id}/verify`; } const options = { url: finalUrl, method: "GET", json: true, headers: { Authorization: `Basic ${authorizationKey}` } }; return new Promise((resolve, reject) => { request(options, function(err, response, body) { if (err) { reject(err); } resolve({ statusCode: response.statusCode, body }); }); }); }; /** * Capture a charge * * @param {string} id Charge ID to capture * @param {float} amount Amount to capture from $id charge id. Should be: <expected amount> times 100 * * @returns Promise */ const capture = function(id, amount) { const options = { url: `${url}/${version}/${URI}/${id}/capture`, method: "POST", json: true, headers: { Authorization: `Basic ${authorizationKey}` }, body: { amount } }; return new Promise((resolve, reject) => { request(options, function(err, response, body) { if (err) { reject(err); } resolve({ statusCode: response.statusCode, body }); }); }); }; /** * Refund a charge * * @param {string} id Charge ID to capture * @param {float} amount Amount to capture from $id charge id. Should be: <expected amount> times 100 * * @returns Promise */ const refund = function(id, amount) { const options = { url: `${url}/${version}/${URI}/${id}/refund`, method: "POST", json: true, headers: { Authorization: `Basic ${authorizationKey}` }, body: { amount } }; return new Promise((resolve, reject) => { request(options, function(err, response, body) { if (err) { reject(err); } resolve({ statusCode: response.statusCode, body }); }); }); }; /** * Void a charge * * @param {string} id Charge ID to capture * * @returns Promise */ const voidCharge = function(id) { const options = { url: `${url}/${version}/${URI}/${id}/void`, method: "POST", json: true, headers: { Authorization: `Basic ${authorizationKey}` } }; return new Promise((resolve, reject) => { request(options, function(err, response, body) { if (err) { reject(err); } resolve({ statusCode: response.statusCode, body }); }); }); }; return { create, get, capture, refund, void: voidCharge }; } module.exports = Charge;