@shopware-ag/meteor-admin-sdk
Version:
The Meteor SDK for the Shopware Administration.
160 lines • 7.74 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "../../ui/modal", "../../location", "../../context", "../../data", "../../data/Criteria", "jwt-decode"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.decodeLicense = exports.startPaymentFlow = exports.addPaymentIframe = exports.MESSAGE_EVENT_TYPE = void 0;
const modal = __importStar(require("../../ui/modal"));
const location = __importStar(require("../../location"));
const context = __importStar(require("../../context"));
const data = __importStar(require("../../data"));
const Criteria_1 = __importDefault(require("../../data/Criteria"));
const jwt_decode_1 = require("jwt-decode");
var MESSAGE_EVENT_TYPE;
(function (MESSAGE_EVENT_TYPE) {
MESSAGE_EVENT_TYPE["PAYMENT_CLOSE"] = "payment_close";
MESSAGE_EVENT_TYPE["PAYMENT_SUCCESS"] = "payment_success";
MESSAGE_EVENT_TYPE["PAYMENT_ERROR"] = "payment_error";
MESSAGE_EVENT_TYPE["SYNC_HEIGHT"] = "sync_height";
})(MESSAGE_EVENT_TYPE = exports.MESSAGE_EVENT_TYPE || (exports.MESSAGE_EVENT_TYPE = {}));
let channel = null;
const getChannel = () => {
if (!channel) {
channel = new BroadcastChannel('payment');
}
return channel;
};
const servicePaymentModalLocationId = 'sw-service-payment-modal';
const _createFlow = () => {
return {
subscribe: (eventType, callback) => {
const func = (event) => {
if (event.data.type === eventType) {
callback();
}
};
getChannel().addEventListener('message', func);
return {
unsubscribe: () => {
getChannel().removeEventListener('message', func);
},
};
},
};
};
const addPaymentIframe = (el, baseUrl, options) => __awaiter(void 0, void 0, void 0, function* () {
const { name, version } = yield context.getAppInformation();
const decodedLicense = yield (0, exports.decodeLicense)();
let shopPlan = '';
let shopUrl = '';
if (decodedLicense && decodedLicense.exp > Date.now() / 1000) {
shopUrl = (decodedLicense === null || decodedLicense === void 0 ? void 0 : decodedLicense['aud']) || options.shopUrl || '';
shopPlan = (decodedLicense === null || decodedLicense === void 0 ? void 0 : decodedLicense['plan-name']) || options.shopPlan || '';
}
else {
shopUrl = options.shopUrl || '';
shopPlan = options.shopPlan || '';
}
const shopId = yield context.getShopId();
const link = `${baseUrl}/payment?service-name=${name}&service-version=${version}&shop-url=${shopUrl}&sw-version=${options.swVersion}&sw-user-language=${options.swUserLanguage}&shop-plan=${shopPlan}&shop-id=${shopId !== null && shopId !== void 0 ? shopId : ''}`;
const iframeEl = document.createElement('iframe');
iframeEl.width = '100%';
iframeEl.height = '100%';
iframeEl.src = link;
iframeEl.style.border = 'none';
iframeEl.style.borderRadius = 'var(--border-radius-card)';
const handleEvent = (event) => {
if (event.data.type === MESSAGE_EVENT_TYPE.SYNC_HEIGHT) {
iframeEl.style.height = `${event.data.payload}px`;
location.startAutoResizer();
}
else if (event.data.type === MESSAGE_EVENT_TYPE.PAYMENT_CLOSE) {
window.removeEventListener('message', handleEvent);
void modal.close({
locationId: servicePaymentModalLocationId,
});
}
else if ([MESSAGE_EVENT_TYPE.PAYMENT_SUCCESS].includes(event.data.type)) {
getChannel().postMessage(event.data);
}
};
window.addEventListener('message', handleEvent);
const unmount = () => {
window.removeEventListener('message', handleEvent);
channel === null || channel === void 0 ? void 0 : channel.close();
};
el.appendChild(iframeEl);
return { iframeEl, unmount };
});
exports.addPaymentIframe = addPaymentIframe;
const startPaymentFlow = () => __awaiter(void 0, void 0, void 0, function* () {
yield modal.open({
locationId: servicePaymentModalLocationId,
variant: 'small',
showHeader: false,
showFooter: false,
closable: false,
});
const flow = _createFlow();
return flow;
});
exports.startPaymentFlow = startPaymentFlow;
const decodeLicense = () => __awaiter(void 0, void 0, void 0, function* () {
try {
const SystemConfigRepository = data.repository('system_config');
const criteria = new data.Classes.Criteria();
criteria.addFilter(Criteria_1.default.equals('system_config.configurationKey', 'core.store.licenseKey'));
const res = yield SystemConfigRepository.search(criteria);
const value = res === null || res === void 0 ? void 0 : res.first();
const token = (value === null || value === void 0 ? void 0 : value.configurationValue) || '';
const decoded = (0, jwt_decode_1.jwtDecode)(token);
return decoded;
}
catch (_a) {
return null;
}
});
exports.decodeLicense = decodeLicense;
});
//# sourceMappingURL=index.js.map