182 lines
19 KiB
JavaScript
182 lines
19 KiB
JavaScript
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
||
|
function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw new Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator.return && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.af
|
||
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
||
|
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
||
|
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, _toPropertyKey(descriptor.key), descriptor); } }
|
||
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
||
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
||
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
||
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
||
|
import { productSceneEnum } from "../types";
|
||
|
import APDID from "./jshield-apdid/apdid-core.umd.min";
|
||
|
export var getSecurityConfigStorageKey = function getSecurityConfigStorageKey(scene) {
|
||
|
return "AMSSDK_SECURITY_CONFIG_".concat(scene);
|
||
|
};
|
||
|
export var SecurityRegionEnum = /*#__PURE__*/function (SecurityRegionEnum) {
|
||
|
SecurityRegionEnum["SG"] = "SG";
|
||
|
SecurityRegionEnum["US"] = "US";
|
||
|
SecurityRegionEnum["DE"] = "DE";
|
||
|
return SecurityRegionEnum;
|
||
|
}({});
|
||
|
export var securityHost = {
|
||
|
SG: 'https://open-sea-global.alipay.com/api/open/risk_client',
|
||
|
US: 'https://open-na-global.alipay.com/api/open/risk_client',
|
||
|
DE: 'https://open-de-global.alipay.com/api/open/risk_client'
|
||
|
};
|
||
|
export var sceneMap = _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, productSceneEnum.CASHIER_PAYMENT, 'Acquirer'), productSceneEnum.AUTO_DEBIT, 'Acquirer'), productSceneEnum.VAULTING, 'Acquirer'), productSceneEnum.CARD_APPLE_PAY, 'Acquirer'), "EASYPAY", 'EasyPay'), productSceneEnum.EASY_PAY, 'EasyPay'), productSceneEnum.FLASH_BUY, 'OneAccount');
|
||
|
export var getSecurityHost = function getSecurityHost(region) {
|
||
|
return securityHost[region] || securityHost[SecurityRegionEnum.SG];
|
||
|
};
|
||
|
export var getSecurityScene = function getSecurityScene(product) {
|
||
|
return sceneMap[product] || '';
|
||
|
};
|
||
|
export var Security = /*#__PURE__*/function () {
|
||
|
function Security(options) {
|
||
|
_classCallCheck(this, Security);
|
||
|
_defineProperty(this, "scene", void 0);
|
||
|
_defineProperty(this, "h5gateway", void 0);
|
||
|
this.scene = options.scene;
|
||
|
this.h5gateway = options.h5gateway || securityHost[SecurityRegionEnum.SG];
|
||
|
}
|
||
|
_createClass(Security, [{
|
||
|
key: "initSecurity",
|
||
|
value: function initSecurity(successCallback, failCallback) {
|
||
|
if (!this.scene) {
|
||
|
throw new Error('securityConfig parameter error');
|
||
|
}
|
||
|
if (!Object.values(sceneMap).includes(this.scene) || !Object.values(securityHost).includes(this.h5gateway)) {
|
||
|
var errorMsg = "scene:".concat(this.scene, " or h5gateway:").concat(this.h5gateway, " do not supported");
|
||
|
console.error(errorMsg);
|
||
|
throw new Error(errorMsg);
|
||
|
}
|
||
|
this.initToken(successCallback, failCallback);
|
||
|
}
|
||
|
}, {
|
||
|
key: "initToken",
|
||
|
value: function initToken(successCallback, failCallback) {
|
||
|
try {
|
||
|
APDID === null || APDID === void 0 || APDID.initToken(this.scene, {
|
||
|
host: this.h5gateway
|
||
|
}, function (success, tokenResult, msg) {
|
||
|
if (success) {
|
||
|
successCallback && successCallback();
|
||
|
} else {
|
||
|
failCallback && failCallback(msg);
|
||
|
}
|
||
|
console.log('initToken result tokenResult', JSON.stringify(tokenResult), msg);
|
||
|
});
|
||
|
} catch (error) {
|
||
|
failCallback && failCallback(error === null || error === void 0 ? void 0 : error.toString());
|
||
|
}
|
||
|
}
|
||
|
}, {
|
||
|
key: "pollingGetApdidToken",
|
||
|
value: function () {
|
||
|
var _pollingGetApdidToken = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(config) {
|
||
|
var _this = this;
|
||
|
var tokenCollectTime, dataPollingInterval, asyncFunc;
|
||
|
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
|
||
|
while (1) switch (_context3.prev = _context3.next) {
|
||
|
case 0:
|
||
|
tokenCollectTime = parseInt((config === null || config === void 0 ? void 0 : config.tokenCollectTime) || '0', 10);
|
||
|
dataPollingInterval = parseInt((config === null || config === void 0 ? void 0 : config.dataPollingInterval) || '500', 10);
|
||
|
asyncFunc = /*#__PURE__*/function () {
|
||
|
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee2(resolve) {
|
||
|
var deviceId, interval, polling;
|
||
|
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
|
||
|
while (1) switch (_context2.prev = _context2.next) {
|
||
|
case 0:
|
||
|
_context2.next = 2;
|
||
|
return _this.getApdidToken();
|
||
|
case 2:
|
||
|
deviceId = _context2.sent;
|
||
|
if (!deviceId) {
|
||
|
_context2.next = 5;
|
||
|
break;
|
||
|
}
|
||
|
return _context2.abrupt("return", resolve(deviceId));
|
||
|
case 5:
|
||
|
polling = /*#__PURE__*/function () {
|
||
|
var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
|
||
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
||
|
while (1) switch (_context.prev = _context.next) {
|
||
|
case 0:
|
||
|
_context.next = 2;
|
||
|
return _this.getApdidToken();
|
||
|
case 2:
|
||
|
deviceId = _context.sent;
|
||
|
if (deviceId) {
|
||
|
clearInterval(interval);
|
||
|
resolve(deviceId);
|
||
|
}
|
||
|
case 4:
|
||
|
case "end":
|
||
|
return _context.stop();
|
||
|
}
|
||
|
}, _callee);
|
||
|
}));
|
||
|
return function polling() {
|
||
|
return _ref2.apply(this, arguments);
|
||
|
};
|
||
|
}();
|
||
|
interval = setInterval(polling, dataPollingInterval);
|
||
|
setTimeout(function () {
|
||
|
clearInterval(interval);
|
||
|
resolve(deviceId);
|
||
|
}, tokenCollectTime);
|
||
|
case 8:
|
||
|
case "end":
|
||
|
return _context2.stop();
|
||
|
}
|
||
|
}, _callee2);
|
||
|
}));
|
||
|
return function asyncFunc(_x2) {
|
||
|
return _ref.apply(this, arguments);
|
||
|
};
|
||
|
}();
|
||
|
return _context3.abrupt("return", new Promise(function (resolve) {
|
||
|
asyncFunc(resolve);
|
||
|
}));
|
||
|
case 4:
|
||
|
case "end":
|
||
|
return _context3.stop();
|
||
|
}
|
||
|
}, _callee3);
|
||
|
}));
|
||
|
function pollingGetApdidToken(_x) {
|
||
|
return _pollingGetApdidToken.apply(this, arguments);
|
||
|
}
|
||
|
return pollingGetApdidToken;
|
||
|
}()
|
||
|
}, {
|
||
|
key: "getApdidToken",
|
||
|
value: function () {
|
||
|
var _getApdidToken = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee4() {
|
||
|
var _this2 = this;
|
||
|
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
|
||
|
while (1) switch (_context4.prev = _context4.next) {
|
||
|
case 0:
|
||
|
return _context4.abrupt("return", new Promise(function (resolve) {
|
||
|
try {
|
||
|
APDID === null || APDID === void 0 || APDID.getApdidToken(_this2.scene, function (tokenResult) {
|
||
|
resolve(tokenResult === null || tokenResult === void 0 ? void 0 : tokenResult.token);
|
||
|
});
|
||
|
} catch (error) {
|
||
|
resolve('');
|
||
|
}
|
||
|
}));
|
||
|
case 1:
|
||
|
case "end":
|
||
|
return _context4.stop();
|
||
|
}
|
||
|
}, _callee4);
|
||
|
}));
|
||
|
function getApdidToken() {
|
||
|
return _getApdidToken.apply(this, arguments);
|
||
|
}
|
||
|
return getApdidToken;
|
||
|
}()
|
||
|
}]);
|
||
|
return Security;
|
||
|
}();
|