@mrboombastic/node-przelewy24
Version:
A simple library for connecting przelewy24 service
210 lines • 8.2 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 });
exports.P24 = void 0;
const axios_1 = __importDefault(require("axios"));
const errors_1 = require("../errors");
const ips_1 = require("./ips");
const hash_1 = require("../utils/hash");
const endpoints_1 = require("./endpoints");
/**
* Represents a P24 payment system
*
* @export
* @class P24
*/
class P24 {
isValidP24Options(options) {
return typeof options === 'object' &&
'merchantId' in options &&
typeof options.merchantId === 'number' &&
'posId' in options &&
typeof options.posId === 'number' &&
'crcKey' in options &&
typeof options.crcKey === 'string' &&
'apiKey' in options &&
typeof options.apiKey === 'string' &&
'sandbox' in options &&
typeof options.sandbox === 'boolean';
}
/**
* Creates an instance of Przelewy24.
* @param {P24Options} [options={ sandbox: false }] - all necessary options
* @memberof P24
*/
constructor(options = { sandbox: false, merchantId: 0, posId: 0, crcKey: '', apiKey: '' }) {
if (!this.isValidP24Options(options)) {
throw new errors_1.P24Error('Invalid P24Options provided', -1);
}
this.options = options;
if (!this.options.posId)
this.options.posId = this.options.merchantId;
this.baseUrl = this.options.sandbox ? endpoints_1.SandboxUrl : endpoints_1.ProductionUrl;
this.baseParameters = {
merchantId: this.options.merchantId,
posId: this.options.posId
};
this.client = axios_1.default.create({
baseURL: `${this.baseUrl}/api/v1`,
auth: {
username: this.options.posId.toString(),
password: this.options.apiKey
}
});
}
/**
* Test access to the service
*
* @returns {Promise<boolean>}
* @throws {P24Error}
* @memberof P24
*/
testAccess() {
return __awaiter(this, void 0, void 0, function* () {
try {
const { data } = yield this.client.get(endpoints_1.EndpointTestAccess);
const res = data;
return res.data;
}
catch (error) {
if (error.response && error.response.data) {
const resp = error.response.data;
throw new errors_1.P24Error(resp.error, resp.code);
}
throw new errors_1.P24Error(`Unknown Error ${error}`, -1);
}
});
}
/**
* Creates a transaction
*
* @param {Order} order - order to be created
* @returns {Promise<Transaction>}
* @throws {P24Error}
* @memberof P24
*/
createTransaction(order) {
return __awaiter(this, void 0, void 0, function* () {
try {
const hashData = {
sessionId: order.sessionId,
merchantId: this.options.merchantId,
amount: order.amount,
currency: order.currency,
crc: this.options.crcKey
};
const sign = (0, hash_1.calculateSHA384)(JSON.stringify(hashData));
const orderData = Object.assign(Object.assign(Object.assign({}, this.baseParameters), order), { sign });
const { data } = yield this.client.post(endpoints_1.EndpointTransactionRegister, orderData);
const response = data;
return {
token: response.data.token,
link: `${this.baseUrl}${endpoints_1.EndpointTransactionRequest}/${response.data.token}`
};
}
catch (error) {
if (error.response && error.response.data) {
const resp = error.response.data;
throw new errors_1.P24Error(resp.error, resp.code);
}
throw new errors_1.P24Error(`Unknown Error ${error}`, -1);
}
});
}
/**
* Verify transaction
*
* @param {Verification} verification - verification request
* @returns {Promise<boolean>}
* @throws {P24Error}
* @memberof P24
*/
verifyTransaction(verification) {
return __awaiter(this, void 0, void 0, function* () {
try {
const hashData = {
sessionId: verification.sessionId,
orderId: verification.orderId,
amount: verification.amount,
currency: verification.currency,
crc: this.options.crcKey
};
const sign = (0, hash_1.calculateSHA384)(JSON.stringify(hashData));
const verificationData = Object.assign(Object.assign(Object.assign({}, this.baseParameters), verification), { sign });
const { data } = yield this.client.put(endpoints_1.EndpointTransactionVerify, verificationData);
const result = data;
return result.data.status === 'success';
}
catch (error) {
if (error.response && error.response.data) {
const resp = error.response.data;
throw new errors_1.P24Error(resp.error, resp.code);
}
throw new errors_1.P24Error(`Unknown Error ${error}`, -1);
}
});
}
/**
* Verify notification transaction with our CRC Key
*
* @param {NotificationRequest} notificationRequest
* @returns {boolean}
* @memberof P24
*/
verifyNotification(notificationRequest) {
const notificationHash = Object.assign(Object.assign({}, notificationRequest), { sign: undefined, crc: this.options.crcKey });
const sign = (0, hash_1.calculateSHA384)(JSON.stringify(notificationHash));
return sign === notificationRequest.sign;
}
/**
* Handle refund
*
* @param {RefundRequest} refundRequest
* @returns {Promise<RefundResult[]>}
* @memberof P24
*/
refund(refundRequest) {
return __awaiter(this, void 0, void 0, function* () {
try {
const { data } = yield this.client.post(endpoints_1.EndpointRefund, refundRequest);
const resp = data;
return resp.data;
}
catch (error) {
if (error.response && error.response.data) {
if (error.response.data.code === 409) {
const resp = error.response.data;
throw new errors_1.P24Error('Refund Conflict', resp.code, resp.error);
}
const resp = error.response.data;
throw new errors_1.P24Error(resp.error, resp.code);
}
throw new errors_1.P24Error(`Unknown Error ${error}`, -1);
}
});
}
/**
* Validates IP with P24 backends
*
* @static
* @param {string} ip - IP Address
* @returns {boolean} - true on validated ip
* @memberof P24
*/
static isIpValid(ip) {
return ips_1.validIps.includes(ip);
}
}
exports.P24 = P24;
//# sourceMappingURL=P24.js.map