xsolla
Version:
A Node.js client for working with the Xsolla Merchant API
71 lines (70 loc) • 2.98 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(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 };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Model_1 = __importDefault(require("./Model"));
const InvalidSignatureException_1 = __importDefault(require("../Exception/InvalidSignatureException"));
const Hash_1 = __importDefault(require("../Utility/Hash"));
class Project extends Model_1.default {
/**
* Create a signature for the given input.
*/
sign(input) {
return Hash_1.default('sha1', input + this.data.key);
}
/**
* Update a project.
*/
update(data) {
return this.client.put(`/projects/${data.project_id}`, data)
.then(({ data }) => this.renew(data));
}
/**
* Create a payment token for the current project.
*/
createPaymentToken(data) {
if (!data.settings) {
data.settings = {};
}
data.settings.project_id = this.data.id;
return this.client.post(`/merchants/${this.client.merchantId}/token`, data)
.then(({ data }) => data);
}
/**
* Create payment URL for the given payment data.
*/
createPaymentUrl(data) {
return __awaiter(this, void 0, void 0, function* () {
const { token } = yield this.createPaymentToken(data);
let baseUrl = 'https://secure.xsolla.com/paystation2/';
if (data.settings && data.settings.mode === 'sandbox') {
baseUrl = 'https://sandbox-secure.xsolla.com/paystation2/';
}
return `${baseUrl}?access_token=${token}`;
});
}
/**
* Validate an incoming webhook request.
*/
validateWebhookRequest(requestSignature, rawRequestBody) {
if (!requestSignature) {
throw new InvalidSignatureException_1.default('No signature provided!');
}
if (typeof requestSignature !== 'string') {
throw new InvalidSignatureException_1.default('Signature is not string! (Likely developer error)');
}
if (requestSignature.replace(/signature/i, '').trim() !== this.sign(rawRequestBody)) {
throw new InvalidSignatureException_1.default;
}
}
}
exports.default = Project;