gamemoney-frorff
Version:
GameMoney API wrapper for Node.js
53 lines • 2.43 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const request_promise_native_1 = __importDefault(require("request-promise-native"));
const utils_1 = require("./utils");
class GameMoneyRequest {
constructor(hmacKey, privateKey) {
this.hmacKey = hmacKey;
this.privateKey = privateKey;
this.request = request_promise_native_1.default.defaults({
baseUrl: GameMoneyRequest.uri,
json: true
});
}
send(url, body, signType = 'hmac') {
return __awaiter(this, void 0, void 0, function* () {
if (!body.rand) {
body.rand = utils_1.getRandomString(20);
}
const signature = signType === 'hmac'
? utils_1.generateHmacSignature(body, this.hmacKey)
: utils_1.generateRsaSignature(body, this.privateKey);
const response = yield this.request.post({
url,
form: Object.assign(Object.assign({}, body), { signature })
});
if (!utils_1.verifyRsaSignature(response)) {
throw new Error('Response signature mismatch');
}
if (body.rand && body.rand !== response.rand) {
throw new Error(`Wrong rand parameter: ${response.rand}`);
}
if (response.state === 'error') {
throw new Error(response.error);
}
return response;
});
}
}
exports.default = GameMoneyRequest;
GameMoneyRequest.uri = 'https://paygate.gamemoney.com';
//# sourceMappingURL=request.js.map