mastercard-hosted-session
Version:
Wrapper around Mastercard hosted session, allowing you to treat it as a modern Promise-based Javascript API.
241 lines (178 loc) • 10.4 kB
JavaScript
;
require("core-js/modules/es.symbol");
require("core-js/modules/es.symbol.description");
require("core-js/modules/es.symbol.iterator");
require("core-js/modules/es.array.concat");
require("core-js/modules/es.array.filter");
require("core-js/modules/es.array.for-each");
require("core-js/modules/es.array.index-of");
require("core-js/modules/es.array.iterator");
require("core-js/modules/es.date.to-string");
require("core-js/modules/es.map");
require("core-js/modules/es.object.define-property");
require("core-js/modules/es.object.get-own-property-descriptor");
require("core-js/modules/es.object.get-prototype-of");
require("core-js/modules/es.object.keys");
require("core-js/modules/es.object.set-prototype-of");
require("core-js/modules/es.object.to-string");
require("core-js/modules/es.promise");
require("core-js/modules/es.reflect.construct");
require("core-js/modules/es.regexp.to-string");
require("core-js/modules/es.string.iterator");
require("core-js/modules/web.dom-collections.for-each");
require("core-js/modules/web.dom-collections.iterator");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.HostedSessionError = exports.HostedSession = void 0;
var _v = _interopRequireDefault(require("uuid/v4"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _wrapNativeSuper(Class) { var _cache = typeof Map === "function" ? new Map() : undefined; _wrapNativeSuper = function _wrapNativeSuper(Class) { if (Class === null || !_isNativeFunction(Class)) return Class; if (typeof Class !== "function") { throw new TypeError("Super expression must either be null or a function"); } if (typeof _cache !== "undefined") { if (_cache.has(Class)) return _cache.get(Class); _cache.set(Class, Wrapper); } function Wrapper() { return _construct(Class, arguments, _getPrototypeOf(this).constructor); } Wrapper.prototype = Object.create(Class.prototype, { constructor: { value: Wrapper, enumerable: false, writable: true, configurable: true } }); return _setPrototypeOf(Wrapper, Class); }; return _wrapNativeSuper(Class); }
function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _construct(Parent, args, Class) { if (isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
function _isNativeFunction(fn) { return Function.toString.call(fn).indexOf("[native code]") !== -1; }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var HostedSession =
/*#__PURE__*/
function () {
function HostedSession(paymentSession, config) {
_classCallCheck(this, HostedSession);
this.paymentSession = paymentSession;
this.config = config;
this.sessionizeCalls = [];
this.sessionResultStack = [];
this.activeRequest = null;
}
/**
* Initialises a new PaymentSession instance against a form in your page.
* See Mastercard's session.js API docs for how the configuration options
* work.
* @returns {Promise<HostedSession>}
*/
_createClass(HostedSession, [{
key: "initialize",
value: function initialize() {
var _this = this;
return new Promise(function (resolve, reject) {
_this.paymentSession.configure(_objectSpread({}, _this.config, {
callbacks: {
initialized: function initialized(result) {
if (result.status === 'ok') {
return resolve(_this);
} else {
return reject(new HostedSessionError(result.message, result.status));
}
},
formSessionUpdate: function formSessionUpdate(result) {
_this._sessionUpdated(result);
}
}
}));
});
}
/**
* Executes a sessionization flow. Roughly equivalent to executing
* PaymentSession.updateSessionFromForm, except that you get a promise
* that will resolve to the result (or reject with failures). You can call
* this method as many times as you like and guarantee you'll get the
* results for your particular call.
* @returns {Promise<any>}
*/
}, {
key: "sessionize",
value: function sessionize() {
var requestId = (0, _v.default)();
this.sessionizeCalls.push(requestId);
return this._sessionize(requestId);
}
}, {
key: "onFocus",
value: function onFocus(selectors, callback) {
this.paymentSession.onFocus(selectors, callback);
}
}, {
key: "onBlur",
value: function onBlur(selectors, callback) {
this.paymentSession.onBlur(selectors, callback);
}
}, {
key: "onChange",
value: function onChange(selectors, callback) {
this.paymentSession.onChange(selectors, callback);
}
}, {
key: "onMouseOver",
value: function onMouseOver(selectors, callback) {
this.paymentSession.onMouseOver(selectors, callback);
}
}, {
key: "onMouseOut",
value: function onMouseOut(selectors, callback) {
this.paymentSession.onMouseOut(selectors, callback);
}
}, {
key: "_sessionize",
value: function _sessionize(requestId) {
var _this2 = this;
return new Promise(function (resolve, reject) {
// Ugly hack: each promise polls for its turn, executes PaymentSession
// and then waits for a result to resolve or reject on it.
// Ugly. But it works!
var intervalId = setInterval(function () {
// 1. Queue up an active request if there is none right now
if (!_this2.activeRequest) {
_this2.activeRequest = _this2.sessionizeCalls.shift();
_this2.paymentSession.updateSessionFromForm(_this2.config.scope || 'card');
} // 2. Wait our turn to process
if (_this2.activeRequest !== requestId) {
return;
} // 3. Wait until we have a result on our turn
if (_this2.sessionResultStack.length === 0) {
return;
}
clearInterval(intervalId);
_this2.activeRequest = null;
var result = _this2.sessionResultStack.pop();
if (result.status === 'ok' || result.status === 'fields_in_error') {
return resolve(result);
} else {
return reject(new HostedSessionError(result.errors.message, result.status));
}
}, 10);
});
}
}, {
key: "_sessionUpdated",
value: function _sessionUpdated(result) {
this.sessionResultStack.push(result);
}
}]);
return HostedSession;
}();
exports.HostedSession = HostedSession;
var HostedSessionError =
/*#__PURE__*/
function (_Error) {
_inherits(HostedSessionError, _Error);
function HostedSessionError(message, code) {
var _this3;
_classCallCheck(this, HostedSessionError);
_this3 = _possibleConstructorReturn(this, _getPrototypeOf(HostedSessionError).call(this));
_this3.message = message;
_this3.stack = new Error(message).stack;
_this3.code = code;
return _this3;
}
return HostedSessionError;
}(_wrapNativeSuper(Error));
exports.HostedSessionError = HostedSessionError;