mollie-es6
Advanced tools
Comparing version 2.5.0 to 3.0.0
54
app.js
@@ -1,8 +0,46 @@ | ||
module.exports = { | ||
payments: require('./lib/payments'), | ||
methods: require('./lib/methods'), | ||
issuers: require('./lib/issuers'), | ||
refunds: require('./lib/refunds'), | ||
customers: require('./lib/customers'), | ||
api_key: null | ||
}; | ||
class Mollie { | ||
constructor(key) { | ||
this.key = key; | ||
this.customers = {}; | ||
this.issuers = {}; | ||
this.methods = {}; | ||
this.payments = {}; | ||
this.refunds = {}; | ||
const customers = require('./lib/customers'); | ||
const issuers = require('./lib/issuers'); | ||
const methods = require('./lib/methods'); | ||
const payments = require('./lib/payments'); | ||
const refunds = require('./lib/refunds'); | ||
for (let key in customers) | ||
this.customers[key] = customers[key].bind(this) | ||
for (let key in issuers) | ||
this.issuers[key] = issuers[key].bind(this); | ||
for (let key in methods) | ||
this.methods[key] = methods[key].bind(this); | ||
for (let key in payments) | ||
this.payments[key] = payments[key].bind(this); | ||
for (let key in refunds) | ||
this.refunds[key] = refunds[key].bind(this); | ||
this.request = require('./lib/request').bind(this); | ||
} | ||
* test() { | ||
try { | ||
const result = yield this.payments.list(); | ||
return !!result.data; | ||
} catch (e) { | ||
if (e.error && e.error.message && e.error.message === 'Unauthorized request') | ||
return false; | ||
throw e; | ||
} | ||
} | ||
} | ||
module.exports = Mollie; |
@@ -1,3 +0,3 @@ | ||
const request = require('./request'); | ||
const {assign} = require('lodash'); | ||
const denied = require('obj-denied'); | ||
@@ -12,3 +12,3 @@ /** | ||
*/ | ||
function *create(name, email, {locale, metadata} = {}) { | ||
function* create(name, email, {locale, metadata} = {}) { | ||
if (!name || !email) { | ||
@@ -24,6 +24,6 @@ throw {error: 'Not all required parameters are given'}; | ||
if (locale) { | ||
const allowed_locale = ['de', 'en', 'es', 'fr', 'be', 'be-fr', 'nl']; | ||
const allowedLocale = ['de', 'en', 'es', 'fr', 'be', 'be-fr', 'nl']; | ||
if (allowed_locale.indexOf(locale) === -1) { | ||
throw {error: `Locale has a non-allowed value.\nAllowed values: ${allowed_locale.toString()}`}; | ||
if (allowedLocale.indexOf(locale) === -1) { | ||
throw {error: `Locale has a non-allowed value.\nAllowed values: ${allowedLocale.toString()}`}; | ||
} | ||
@@ -38,3 +38,3 @@ | ||
const result = yield request( | ||
const result = yield this.request( | ||
'POST', | ||
@@ -57,3 +57,3 @@ 'customers', | ||
*/ | ||
function *get(id) { | ||
function* get(id) { | ||
if (!id) { | ||
@@ -63,3 +63,3 @@ throw {error: 'No id is given'}; | ||
const result = yield request( | ||
const result = yield this.request( | ||
'GET', | ||
@@ -81,8 +81,8 @@ `customers/${id}` | ||
*/ | ||
function *list(options) { | ||
if (options && options.count && options.count > 250) { | ||
function* list(options = {}) { | ||
if (!denied(options, 'count') && options.count > 250) { | ||
throw {error: 'Count larger than 250 is not allowed'}; | ||
} | ||
const result = yield request( | ||
const result = yield this.request( | ||
'GET', | ||
@@ -89,0 +89,0 @@ 'customers', |
@@ -1,2 +0,2 @@ | ||
const request = require('./request'); | ||
const denied = require('obj-denied'); | ||
@@ -8,8 +8,8 @@ /** | ||
*/ | ||
function *list(options) { | ||
if (options.count && options.count > 250) { | ||
function* list(options = {}) { | ||
if (!denied(options, 'count') && options.count > 250) { | ||
throw {error: 'Count larger than 250 is not allowed'}; | ||
} | ||
const result = yield request( | ||
const result = yield this.request( | ||
'GET', | ||
@@ -33,3 +33,3 @@ 'issuers', | ||
*/ | ||
function *get(id) { | ||
function* get(id) { | ||
if (!id) { | ||
@@ -39,3 +39,3 @@ throw {error: 'No id is given'}; | ||
const result = yield request( | ||
const result = yield this.request( | ||
'GET', | ||
@@ -42,0 +42,0 @@ `issuers/${id}` |
@@ -1,5 +0,5 @@ | ||
const request = require('./request'); | ||
const {assign} = require('lodash'); | ||
const denied = require('obj-denied'); | ||
const method_functions = { | ||
const methodFunctions = { | ||
/** | ||
@@ -43,8 +43,8 @@ * Returns the minimum order amount for this Payment Method | ||
*/ | ||
function *list(options) { | ||
if (options.count && options.count > 250) { | ||
function* list(options = {}) { | ||
if (!denied(options, 'count') && options.count > 250) { | ||
throw {error: 'Count larger than 250 is not allowed'}; | ||
} | ||
const result = yield request( | ||
const result = yield this.request( | ||
'GET', | ||
@@ -69,3 +69,3 @@ 'methods', | ||
*/ | ||
function *get(id) { | ||
function* get(id) { | ||
if (!id) { | ||
@@ -75,3 +75,3 @@ throw {error: 'No id is given'}; | ||
const result = yield request( | ||
const result = yield this.request( | ||
'GET', | ||
@@ -91,6 +91,6 @@ `methods/${id}` | ||
if (method.constructor === Object) { | ||
assign(method, method_functions); | ||
assign(method, methodFunctions); | ||
} else { | ||
for (let i = 0; i < method.length; i++) { | ||
assign(method[i], method_functions); | ||
assign(method[i], methodFunctions); | ||
} | ||
@@ -97,0 +97,0 @@ } |
@@ -1,6 +0,5 @@ | ||
const request = require('./request'); | ||
const {assign} = require('lodash'); | ||
const denied = require('obj-denied'); | ||
const payment_functions = { | ||
const paymentFunctions = { | ||
/** | ||
@@ -30,3 +29,3 @@ * Returns the paymentUrl you redirect the user to, so they can pay the order | ||
*/ | ||
function *create(amount, description, redirectUrl, options) { | ||
function* create(amount, description, redirectUrl, options) { | ||
if (!amount || !description || !redirectUrl) { | ||
@@ -52,3 +51,3 @@ throw {error: 'Not all required parameters are given'}; | ||
const result = yield request( | ||
const result = yield this.request( | ||
'POST', | ||
@@ -72,3 +71,3 @@ 'payments', | ||
*/ | ||
function *get(id) { | ||
function* get(id) { | ||
if (!id) { | ||
@@ -78,3 +77,3 @@ throw {error: 'No id is given'}; | ||
const result = yield request( | ||
const result = yield this.request( | ||
'GET', | ||
@@ -97,8 +96,8 @@ `payments/${id}` | ||
*/ | ||
function *list(options) { | ||
if (options && options.count && options.count > 250) { | ||
function* list(options = {}) { | ||
if (!denied(options, 'count') && options.count > 250) { | ||
throw {error: 'Count larger than 250 is not allowed'}; | ||
} | ||
const result = yield request( | ||
const result = yield this.request( | ||
'GET', | ||
@@ -120,6 +119,6 @@ 'payments', | ||
if (payments.constructor === Object) { | ||
assign(payments, payment_functions); | ||
assign(payments, paymentFunctions); | ||
} else { | ||
for (let i = 0; i < payments.length; i++) { | ||
assign(payments[i], payment_functions); | ||
assign(payments[i], paymentFunctions); | ||
} | ||
@@ -126,0 +125,0 @@ } |
@@ -1,5 +0,5 @@ | ||
const request = require('./request'); | ||
const {assign} = require('lodash'); | ||
const denied = require('obj-denied'); | ||
const refund_functions = { | ||
const refundFunctions = { | ||
/** | ||
@@ -35,3 +35,3 @@ * Returns a boolean stating the order is paid or not | ||
*/ | ||
function *create(id, amount) { | ||
function* create(id, amount) { | ||
if (!id) { | ||
@@ -42,3 +42,3 @@ throw {error: 'Not all required parameters are given'}; | ||
const result = yield request( | ||
const result = yield this.request( | ||
'POST', | ||
@@ -59,14 +59,14 @@ `payments/${id}/refunds`, | ||
* Get information about a payments specific refund | ||
* @param {String} payment_id The Payments id | ||
* @param {String} refund_id The Refunds id | ||
* @param {String} paymentId The Payments id | ||
* @param {String} refundId The Refunds id | ||
* @returns {Object} Refund info or error, given by Mollie | ||
*/ | ||
function *get(payment_id, refund_id) { | ||
if (!payment_id || !refund_id) { | ||
throw {error: 'No payment_id or refund_id is given'}; | ||
function* get(paymentId, refundId) { | ||
if (!paymentId || !refundId) { | ||
throw {error: 'No paymentId or refundId is given'}; | ||
} | ||
const result = yield request( | ||
const result = yield this.request( | ||
'GET', | ||
`payments/${payment_id}/refunds/${refund_id}` | ||
`payments/${paymentId}/refunds/${refundId}` | ||
); | ||
@@ -88,11 +88,11 @@ | ||
*/ | ||
function *list(id, options) { | ||
function* list(id, options = {}) { | ||
if (!id) { | ||
throw {error: 'No id is given'}; | ||
} | ||
if (options && options.count && options.count > 250) { | ||
if (!denied(options, 'count') && options.count > 250) { | ||
throw {error: 'Count larger than 250 is not allowed'}; | ||
} | ||
const result = yield request( | ||
const result = yield this.request( | ||
'GET', | ||
@@ -114,14 +114,14 @@ `payments/${id}/refunds`, | ||
* Cancels a Refund | ||
* @param {String} payment_id The Payments id | ||
* @param {String} refund_id The Refunds id | ||
* @param {String} paymentId The Payments id | ||
* @param {String} refundId The Refunds id | ||
* @returns {Object} Refund info or error, given by Mollie | ||
*/ | ||
function *cancel(payment_id, refund_id) { | ||
if (!payment_id || !refund_id) { | ||
throw {error: 'No payment_id or refund_id is given'}; | ||
function* cancel(paymentId, refundId) { | ||
if (!paymentId || !refundId) { | ||
throw {error: 'No paymentId or refundId is given'}; | ||
} | ||
const result = yield request( | ||
const result = yield this.request( | ||
'DELETE', | ||
`payments/${payment_id}/refunds/${refund_id}` | ||
`payments/${paymentId}/refunds/${refundId}` | ||
); | ||
@@ -139,6 +139,6 @@ | ||
if (refunded.constructor === Object) { | ||
assign(refunded, refund_functions); | ||
assign(refunded, refundFunctions); | ||
} else { | ||
for (let i = 0; i < refunded.length; i++) { | ||
assign(refunded[i], refund_functions); | ||
assign(refunded[i], refundFunctions); | ||
} | ||
@@ -145,0 +145,0 @@ } |
const superagent = require('superagent'); | ||
const denied = require('obj-denied'); | ||
module.exports = function* (method, extension, data, url_parameters) { | ||
const mollie = require('../app'); | ||
if (!mollie.api_key) { | ||
throw {error: 'There is no API key I can use, please set your key `mollie.api_key`'}; | ||
module.exports = function* (method, extension, data, urlParameters) { | ||
if (!this.key) { | ||
throw {error: 'There is no API key I can use, please set your key `this.key`'}; | ||
} | ||
@@ -12,6 +11,6 @@ method = method.toLowerCase(); | ||
// Get the base promise | ||
const request = getMethodPromise(method, extension, url_parameters); | ||
const request = getMethodPromise(method, extension, urlParameters); | ||
// Set api_key | ||
request.set('Authorization', `Bearer ${mollie.api_key}`); | ||
// Set api key | ||
request.set('Authorization', `Bearer ${this.key}`); | ||
@@ -27,21 +26,24 @@ // Add data to request | ||
} catch (e) { | ||
throw e.response.body; | ||
if (denied(e, 'response')) | ||
throw e; | ||
else | ||
throw e.response.body; | ||
} | ||
}; | ||
function getMethodPromise(method, extension, url_parameters) { | ||
const url = `https://api.mollie.nl/v1/${extension}${addURLParams(url_parameters)}`; | ||
function getMethodPromise(method, extension, urlParameters) { | ||
const url = `https://api.mollie.nl/v1/${extension}${addURLParams(urlParameters)}`; | ||
switch (method) { | ||
case 'get': | ||
return superagent.get(url); | ||
case 'post': | ||
return superagent.post(url); | ||
case 'delete': | ||
return superagent.delete(url); | ||
case 'get': | ||
return superagent.get(url); | ||
case 'post': | ||
return superagent.post(url); | ||
case 'delete': | ||
return superagent.delete(url); | ||
} | ||
} | ||
function addURLParams(url_parameters = {}) { | ||
if (Object.keys(url_parameters).length === 0) | ||
function addURLParams(urlParameters = {}) { | ||
if (Object.keys(urlParameters).length === 0) | ||
return ''; | ||
@@ -51,4 +53,4 @@ | ||
for (let prop in url_parameters) { | ||
urlParams += `&${prop}=${url_parameters[prop]}`; | ||
for (let prop in urlParameters) { | ||
urlParams += `&${prop}=${urlParameters[prop]}`; | ||
} | ||
@@ -55,0 +57,0 @@ |
{ | ||
"name": "mollie-es6", | ||
"version": "2.5.0", | ||
"version": "3.0.0", | ||
"description": "Mollie module in ES6", | ||
@@ -62,3 +62,3 @@ "main": "app.js", | ||
"obj-denied": "^1.3.1", | ||
"superagent": "^3.5.2" | ||
"superagent": "^3.6.0" | ||
}, | ||
@@ -65,0 +65,0 @@ "devDependencies": { |
60488
541
Updatedsuperagent@^3.6.0