UNPKG

ifthenpay

Version:

ifthenpay unofficial javascript module

244 lines (175 loc) 10.7 kB
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.IfThenPay = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ 'use strict'; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /** * PO License * @OWNER: José Moreira * @COPYRIGHTER: José Moreira */ var _generateMultibanco = require('./generate-multibanco'); var _generateMultibanco2 = _interopRequireDefault(_generateMultibanco); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var DEFAULT_OPTIONS = { rejectDeepDecimalValues: false, generateReferenceOnly: false }; var IfThenPay = function () { function IfThenPay(options) { _classCallCheck(this, IfThenPay); options = this.options = _extends({}, DEFAULT_OPTIONS, options); if (typeof options.entity == 'number') { options.entity = "" + options.entity; } if (typeof options.entity != 'string' || options.entity.length !== 5) { throw new TypeError("options.entity should be a 5 chars string"); } if (typeof options.subentity == 'number') { options.subentity = "" + options.subentity; } if (typeof options.subentity != 'string' || options.subentity.length > 3) { throw new TypeError("options.subentity should be a under 3 chars string"); } } _createClass(IfThenPay, [{ key: 'generate', value: function generate(value) { var id = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var _options = this.options, entity = _options.entity, subentity = _options.subentity, generateReferenceOnly = _options.generateReferenceOnly; // Arguments validation if (typeof id != 'number') { throw new TypeError("id must be a number"); } if (typeof value != 'number' || value <= 0 || value >= 1000000) { throw TypeError("value should be a float between 0 and 1.000.000"); } if (this.options.rejectDeepDecimalValues) { if (value !== value.toFixed(2)) { throw new TypeError("value should NOT have more than two decimal numbers"); } } else { value = +value.toFixed(2); } var reference = (0, _generateMultibanco2.default)(entity, subentity, value, id); return generateReferenceOnly ? reference : { entity: entity, reference: reference, value: value }; } }]); return IfThenPay; }(); module.exports = IfThenPay; },{"./generate-multibanco":4}],2:[function(require,module,exports){ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.HASH_CALC = undefined; exports.checkDigits = checkDigits; var _cutPad = require('./cut-pad'); var _cutPad2 = _interopRequireDefault(_cutPad); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var HASH_CALC = exports.HASH_CALC = [51, 73, 17, 89, 38, 62, 45, 53, 15, 50, 5, 49, 24, 81, 76, 27, 90, 9, 30, 3]; /** * PO License * @OWNER: José Moreira * @COPYRIGHTER: José Moreira */ function checkDigits(entity, ref, value) { // Arguments validation if (typeof entity != 'string' || entity.length !== 5) { throw TypeError("entity should be a 5 chars string"); } if (typeof ref != 'string' || ref.length !== 7) { throw TypeError("ref should be a 7 chars string"); } if (typeof value != 'number' || value <= 0 || value >= 1000000) { throw TypeError("value should be a float between 0 and 1.000.000"); } else { value = (0, _cutPad2.default)(Math.floor(value * 100), 8); } // Init helper variables var chk_string = entity + ref + value; var chk_val = 0; // Just to be sure... if (chk_string.length !== 20) { throw new Error("Something went wrong but it shouldn't"); } // Calculate value for (var i in HASH_CALC) { chk_val += +chk_string[i] * HASH_CALC[i]; } // Return the two check digits return (0, _cutPad2.default)(98 - chk_val % 97, 2); } exports.default = checkDigits; },{"./cut-pad":3}],3:[function(require,module,exports){ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.cutPad = cutPad; var _padStart = require('pad-start'); var _padStart2 = _interopRequireDefault(_padStart); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function cutPad(number, length) { return (0, _padStart2.default)("" + number, length, '0').substr(length * -1, length); } /** * PO License * @OWNER: José Moreira * @COPYRIGHTER: José Moreira */ exports.default = cutPad; },{"pad-start":5}],4:[function(require,module,exports){ 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateMultibanco = generateMultibanco; var _cutPad = require('./cut-pad'); var _cutPad2 = _interopRequireDefault(_cutPad); var _checkDigits = require('./check-digits'); var _checkDigits2 = _interopRequireDefault(_checkDigits); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * PO License * @OWNER: José Moreira * @COPYRIGHTER: José Moreira */ function generateMultibanco(entity, subentity, value) { var id = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0; var ref = subentity + (0, _cutPad2.default)(id, 7 - subentity.length); var chkdgts = (0, _checkDigits2.default)(entity, ref, value); return ref + chkdgts; } exports.default = generateMultibanco; },{"./check-digits":2,"./cut-pad":3}],5:[function(require,module,exports){ 'use strict'; module.exports = function (string, maxLength, fillString) { if (string == null || maxLength == null) { return string; } var result = String(string); var targetLen = typeof maxLength === 'number' ? maxLength : parseInt(maxLength, 10); if (isNaN(targetLen) || !isFinite(targetLen)) { return result; } var length = result.length; if (length >= targetLen) { return result; } var fill = fillString == null ? '' : String(fillString); if (fill === '') { fill = ' '; } var fillLen = targetLen - length; while (fill.length < fillLen) { fill += fill; } var truncated = fill.length > fillLen ? fill.substr(0, fillLen) : fill; return truncated + result; }; },{}]},{},[1])(1) });