@xmoney/api-sdk
Version:
## Instalaltion
90 lines • 3.28 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommonService = void 0;
const tslib_1 = require("tslib");
const crypto = tslib_1.__importStar(require("crypto"));
const constants_1 = require("../typings/constants");
class CommonService {
constructor(initParams) {
var _a;
this.hostedCheckoutRedirectUrl = {
[constants_1.TEST_ENV]: constants_1.TEST_ENV_URL,
[constants_1.LIVE_ENV]: constants_1.LIVE_ENV_URL,
};
this.apiUrl = {
[constants_1.TEST_ENV]: constants_1.TEST_ENV_API_URL,
[constants_1.LIVE_ENV]: constants_1.LIVE_ENV_API_URL,
};
this.secretKey = this.extractKeyFromSecretKey(initParams.secretKey);
this.secretKeyEnv = this.extractEnvFromSecretKey(initParams.secretKey);
this.verbose = (_a = initParams.verbose) !== null && _a !== void 0 ? _a : false;
}
getPublicKey(input) {
const publicKey = input.publicKey;
const key = this.extractKeyFromPublicKey(publicKey);
if (!key) {
throw new Error('Invalid public key format. Expected format: pk_<env>_key');
}
return key;
}
getPrivateKey() {
return this.secretKey;
}
getSecretKeyEnv() {
const env = this.secretKeyEnv;
if (!env) {
throw new Error('Cannot detect url based on secret key');
}
return env;
}
getUrl() {
const env = this.getSecretKeyEnv();
const envUrl = this.hostedCheckoutRedirectUrl[env];
if (!envUrl) {
throw new Error('HostedCheckoutRedirect url missing');
}
return envUrl;
}
getApiBaseUrl() {
const env = this.getSecretKeyEnv();
const envUrl = this.apiUrl[env];
if (!envUrl) {
throw new Error('ApiUrl url missing');
}
return envUrl;
}
getBase64JsonRequest(orderData) {
const jsonText = JSON.stringify(orderData);
return Buffer.alloc(Buffer.byteLength(jsonText), jsonText).toString('base64');
}
getBase64Checksum(orderData) {
const hmacSha512 = crypto.createHmac('sha512', this.secretKey);
hmacSha512.update(JSON.stringify(orderData));
return hmacSha512.digest('base64');
}
extractKeyFromPublicKey(publicKey) {
const envPattern = `${constants_1.TEST_ENV}|${constants_1.LIVE_ENV}`;
const regexp = new RegExp(`^pk_(${envPattern})_(.+)$`);
const match = publicKey.match(regexp);
return match ? match[2] : null;
}
extractKeyFromSecretKey(secretKey) {
const regexp = this.getSecretKeyRegex();
const match = secretKey.match(regexp);
return match ? match[2] : secretKey;
}
extractEnvFromSecretKey(secretKey) {
if (this.secretKeyEnv) {
return this.secretKeyEnv;
}
const regexp = this.getSecretKeyRegex();
const match = secretKey.match(regexp);
return match ? match[1] : null;
}
getSecretKeyRegex() {
const envPattern = `${constants_1.TEST_ENV}|${constants_1.LIVE_ENV}`;
return new RegExp(`^sk_(${envPattern})_(.+)$`);
}
}
exports.CommonService = CommonService;
//# sourceMappingURL=common.service.js.map
;