@shopware-ag/meteor-admin-sdk
Version:
The Meteor SDK for the Shopware Administration.
118 lines • 5.21 kB
JavaScript
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());
});
};
import * as modal from '../../ui/modal';
import * as location from '../../location';
import * as context from '../../context';
import * as data from '../../data';
import Criteria from '../../data/Criteria';
import { jwtDecode } from 'jwt-decode';
export 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 || (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);
},
};
},
};
};
export const addPaymentIframe = (el, baseUrl, options) => __awaiter(void 0, void 0, void 0, function* () {
const { name, version } = yield context.getAppInformation();
const decodedLicense = yield 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 };
});
export 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;
});
export 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.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 = jwtDecode(token);
return decoded;
}
catch (_a) {
return null;
}
});
//# sourceMappingURL=index.js.map