@kasvith/przelewy24
Version:
A simple library for connecting przelewy24 service
134 lines • 5.69 kB
JavaScript
;
/**
* MIT License
*
* Copyright (c) 2019 Kasun Vithanage
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
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 axios_1 = __importDefault(require("axios"));
const crypto_1 = __importDefault(require("crypto"));
const querystring_1 = __importDefault(require("querystring"));
const P24Error_1 = require("./P24Error");
exports.ApiVersion = '3.2';
const Przelewy24Base = 'https://secure.przelewy24.pl';
const SandboxUrl = 'https://sandbox.przelewy24.pl';
const testConnection = '/testConnection';
const trnRegister = '/trnRegister';
const trnRequest = '/trnRequest';
const trnVerify = '/trnVerify';
class Przelewy24 {
/**
* Creates an instance of Przelewy24.
* @param {number} merchantId Merchant ID given by Przelewy24
* @param {number} posId Shop ID (defaults to merchantId)
* @param {string} salt md5 of p24_pos_id|CRC
* @param {boolean} [testMode=false] Whether to use sandbox or not
* @memberof Przelewy24
*/
constructor(merchantId, posId, salt, testMode = false) {
this.merchantId = merchantId;
this.posId = posId;
this.salt = salt;
if (this.posId === 0)
this.posId = this.merchantId;
if (!testMode)
this.baseUrl = Przelewy24Base;
else
this.baseUrl = SandboxUrl;
this.client = axios_1.default.create({ baseURL: this.baseUrl });
this.baseParams = {
p24_merchant_id: this.merchantId,
p24_pos_id: this.posId,
p24_api_version: exports.ApiVersion
};
}
/**
* TestConnection to Przelewy24
* return true on success
*/
testConnection() {
return __awaiter(this, void 0, void 0, function* () {
const hash = crypto_1.default.createHash('md5')
.update(`${this.posId}|${this.salt}`)
.digest('hex');
const data = {
p24_merchant_id: this.merchantId,
p24_pos_id: this.posId,
p24_sign: hash
};
const result = yield this.client.post(testConnection, data);
const responseData = querystring_1.default.decode(result.data);
if (responseData['error'] !== '0') {
throw new P24Error_1.P24Error(`${responseData['error']}`, `${responseData['errorMessage']}`);
}
return true;
});
}
/**
* Get a payment link
*
* @param {Payment} payment - Payment object
* @returns
* @memberof Przelewy24
*/
getPaymentLink(payment) {
return __awaiter(this, void 0, void 0, function* () {
const data = payment.build(this.baseParams);
const result = yield this.client.post(trnRegister, data);
const responseData = querystring_1.default.decode(result.data);
if (responseData['error'] === '0') {
return `${this.baseUrl}${trnRequest}/${responseData['token']}`;
}
throw new P24Error_1.P24Error(`${responseData['error']}`, `${responseData['errorMessage']}`);
});
}
/**
* Verifies a transaction
*
* @param {TransactionVerification} verification
* @memberof Przelewy24
*/
verifyTransaction(verification) {
return __awaiter(this, void 0, void 0, function* () {
const result = yield this.client.post(trnVerify, verification);
const responseData = querystring_1.default.decode(result.data);
if (responseData['error'] === '0') {
return true;
}
throw new P24Error_1.P24Error(`${responseData['error']}`, `${responseData['errorMessage']}`);
});
}
}
exports.Przelewy24 = Przelewy24;
//# sourceMappingURL=Przelewy24.js.map