{"size": 3907, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "itssadon/gladepay-node", "max_stars_repo_stars_event_min_datetime": "2019-10-05T01:41:58.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-14T19:21:36.000Z", "max_issues_repo_path": "index.js", "max_issues_repo_name": "itssadon/gladepay-node", "max_issues_repo_issues_event_min_datetime": "2018-10-30T19:41:57.000Z", "max_issues_repo_issues_event_max_datetime": "2019-09-16T01:38:13.000Z", "max_forks_repo_path": "index.js", "max_forks_repo_name": "itssadon/gladepay-node", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 8, "max_forks_count": null, "avg_line_length": 30.7637795276, "max_line_length": 88, "alphanum_fraction": 0.4171998976}
/**
 * GladePay API wrapper
 * @author Abubakar Hassan < @itssadon >
 */

const request = require('request-promise');
const endpoint = 'https://api.gladepay.com';
const demoEndpoint = 'https://demo.api.gladepay.com';
const Events = require("./resources/events");

function GladePay(merchantId, merchantKey, mode = false) {
    if (!(this instanceof GladePay)) {
        return new GladePay(merchantId, merchantKey);
    }

    this.endpoint = (mode) ? endpoint : demoEndpoint;
    this.mid = merchantId;
    this.key = merchantKey;
    this.importResources();
    this.Events = new Events(this.key);
}

const resources = {
    payment: require("./resources/payment"),
    disbursement: require("./resources/disbursement"),
    resources: require("./resources/resources"),
    loansAndInvestments: require("./resources/loans_and_investments")
};

GladePay.prototype = {
    extend: function (func) {
        const me = this;
        return function () {
            const data = arguments[0] || {};

            const method = ["post", "get", "put"].includes(func.method) ?
                func.method :
                (function () {
                    throw new Error("Method not Allowed! - Resource declaration error");
                })();

            var endpoint = me.endpoint + func.route,
                qs = {};

            var argsInEndpoint = endpoint.match(/{[^}]+}/g);
            if (argsInEndpoint) {
                argsInEndpoint.map(arg => {
                    arg = arg.replace(/\W/g, "");
                    if (!(arg in data)) {
                        throw new Error(`Argument '${arg}' is required`);
                    } else {
                        endpoint = endpoint.replace(`{${arg}}`, data[`${arg}`]);
                        delete data[arg];
                    }
                });
            }

            if (func.params) {
                func.params.filter(param => {
                    if (typeof param === 'string') {
                        if (!param.includes("*")) return;

                        param = param.replace("*", "");
                        if (!(param in data)) {
                            throw new Error(`Parameter '${param}' is required`);
                        }

                        return;
                    }

                    return;
                });
            }

            if (func.args) {
                func.args.filter(a => {
                    if (!a.includes("*")) {
                        if (a in data) {
                            qs[`${a}`] = data[`${a}`];
                        }
                        return;
                    }

                    a = a.replace("*", "");
                    if (!(a in data)) {
                        throw new Error(`Argument '${a}' is required`);
                    } else {
                        qs[`${a}`] = data[`${a}`];
                    }

                    return;
                });
            }

            const options = {
                url: endpoint,
                json: true,
                method: method.toUpperCase(),
                headers: {
                    'Content-Type': 'application/json',
                    key: `${me.key}`,
                    mid: `${me.mid}`
                }
            };

            if (method == "post" || method == "put") {
                const reqData = Object.assign(func.data, data);
                options.body = reqData;
            } else {
                options.qs = qs;
            }

            return request(options);
        };
    },
    importResources: function () {
        for (var i in resources) {
            const anon = function () {};
            for (var j in resources[i]) {
                anon.prototype[j] = this.extend(resources[i][j]);
            }
            GladePay.prototype[i] = new anon();
        }
    }
};

module.exports = GladePay;