redsys-connect
Version:
A simple and secure NPM package to integrate the RedSys payment gateway with modern web technologies like React, Next.js, and Angular.
331 lines (321 loc) • 7.6 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
createMerchantParameters: () => createMerchantParameters,
createMerchantSignature: () => createMerchantSignature,
generatePaymentForm: () => generatePaymentForm,
orderNumber: () => orderNumber,
signatureVersion: () => signatureVersion2
});
module.exports = __toCommonJS(index_exports);
// src/utils/currency.ts
var currencyCodes = {
USD: "840",
GBP: "826",
JPY: "392",
AUD: "36",
CLP: "152",
COP: "170",
CRC: "188",
MXP: "484",
PEI: "604",
UYP: "858",
TRY: "949",
ALL: "8",
DZD: "12",
AOK: "24",
DESA: "30",
AZM: "31",
ARP: "32",
BSD: "44",
BHD: "48",
BDT: "50",
AMD: "51",
BBD: "52",
BMD: "60",
BTN: "64",
BOP: "68",
BAD: "70",
BWP: "72",
BRC: "76",
SBD: "90",
BND: "96",
BGL: "100",
BUK: "104",
BIF: "108",
BYB: "112",
KHR: "116",
CAD: "124",
CVE: "132",
KYD: "136",
LKR: "144",
CNY: "156",
CNH: "157",
CNX: "158",
KMF: "174",
ZRZ: "180",
HRK: "191",
CUP: "192",
CYP: "196",
CSK: "200",
CZK: "203",
DKK: "208",
DOP: "214",
ECS: "218",
SVC: "222",
GQE: "226",
ETB: "230",
ERN: "232",
EEK: "233",
FKP: "238",
FJD: "242",
DJF: "262",
GEL: "268",
GMD: "270",
DDM: "278",
GHC: "288",
GIP: "292",
GTQ: "320",
GNS: "324",
GYD: "328",
HTG: "332",
HNL: "340",
HKD: "344",
HUF: "348",
ISK: "352",
INR: "356",
IDR: "360",
IRR: "364",
IRA: "365",
IQD: "368",
ILS: "376",
JMD: "388",
KZT: "398",
JOD: "400",
KES: "404",
KPW: "408",
KRW: "410",
KWD: "414",
KGS: "417",
LAK: "418",
LBP: "422",
LSM: "426",
LVL: "428",
LRD: "430",
LYD: "434",
LTL: "440",
MOP: "446",
MGF: "450",
MWK: "454",
MYR: "458",
MVR: "462",
MLF: "466",
MTL: "470",
MRO: "478",
MUR: "480",
MNT: "496",
MDL: "498",
MAD: "504",
MZM: "508",
OMR: "512",
NAD: "516",
NPR: "524",
ANG: "532",
AWG: "533",
NTZ: "536",
VUV: "548",
NZD: "554",
NAIRA: "566",
NGN: "566",
NOK: "578",
PCI: "582",
PKR: "586",
PAB: "590",
PGK: "598",
PYG: "600",
PHP: "608",
PLZ: "616",
GIN: "624",
TPE: "626",
QAR: "634",
ROL: "642",
RUB: "643",
RWF: "646",
SHP: "654",
STD: "678",
SAR: "682",
SCR: "690",
SLL: "694",
SGD: "702",
SKK: "703",
VND: "704",
SIT: "705",
SOS: "706",
ZAR: "710",
ZWD: "716",
YDD: "720",
SSP: "728",
SDP: "736",
SDA: "737",
SRG: "740",
SZL: "748",
SEK: "752",
CHF: "756",
SYP: "760",
TJR: "762",
THB: "764",
TOP: "776",
TTD: "780",
AED: "784",
TND: "788",
TRL: "792",
PTL: "793",
TMM: "795",
UGS: "800",
UAK: "804",
MKD: "807",
RUR: "810",
EGP: "818",
TZS: "834",
UZS: "860",
VEB: "862",
WST: "882",
YER: "886",
YUD: "890",
YUG: "891",
ZMK: "894",
TWD: "901",
TMT: "934",
GHS: "936",
RSD: "941",
MZN: "943",
AZN: "944",
RON: "946",
XAF: "950",
XCD: "951",
XOF: "952",
XPF: "953",
XEU: "954",
ZMW: "967",
SRD: "968",
MGA: "969",
AFN: "971",
TJS: "972",
AOA: "973",
BYR: "974",
BGN: "975",
CDF: "976",
BAM: "977",
EUR: "978",
UAH: "980",
PLN: "985",
BRL: "986",
ZAL: "991"
};
// src/utils/convertToRedSysParams.ts
function convertToRedSysParams(params) {
return {
DS_MERCHANT_AMOUNT: params.amount,
DS_MERCHANT_CURRENCY: currencyCodes[params.currency] || "978",
DS_MERCHANT_MERCHANTCODE: params.merchantCode,
DS_MERCHANT_MERCHANTURL: params.merchantUrl,
DS_MERCHANT_ORDER: params.order,
DS_MERCHANT_TERMINAL: params.terminal,
DS_MERCHANT_TRANSACTIONTYPE: params.transactionType,
DS_MERCHANT_URLKO: params.urlPaymentDenied,
DS_MERCHANT_URLOK: params.urlPaymentSuccess
};
}
// src/utils/crypto.ts
var CryptoJS = __toESM(require("crypto-js"));
function base64Decode(base64) {
return CryptoJS.enc.Base64.parse(base64);
}
function base64Encode(data) {
return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(data));
}
// src/redsys/encodeParameters.ts
function createMerchantParameters(params) {
const redSysParams = convertToRedSysParams(params);
return base64Encode(JSON.stringify(redSysParams));
}
// src/redsys/sign.ts
var CryptoJS2 = __toESM(require("crypto-js"));
function createMerchantSignature(params, merchantBase64, merchantKey) {
const decodedKey = base64Decode(merchantKey);
const iv = CryptoJS2.enc.Hex.parse("0000000000000000");
const cipher = CryptoJS2.TripleDES.encrypt(params.order, decodedKey, {
iv,
mode: CryptoJS2.mode.CBC,
padding: CryptoJS2.pad.ZeroPadding
});
const signature = CryptoJS2.HmacSHA256(merchantBase64, cipher.ciphertext);
return signature.toString(CryptoJS2.enc.Base64);
}
// src/utils/order.ts
function orderNumber() {
const prefix = Math.floor(1e3 + Math.random() * 9e3).toString();
const digits = "0123456789";
let suffix = "";
for (let i = 0; i < 8; i++) {
suffix += digits.charAt(Math.floor(Math.random() * digits.length));
}
return prefix + suffix;
}
// src/form/getForm.ts
var signatureVersion = "HMAC_SHA256_V1";
function generatePaymentForm(merchantParams, claveComercio, dev, submitText = "REALIZAR PAGO") {
const merchantBase64 = createMerchantParameters(merchantParams);
const dsSignature = createMerchantSignature(merchantParams, merchantBase64, claveComercio);
const actionUrl = dev ? "https://sis-t.redsys.es:25443/sis/realizarPago" : "https://sis.redsys.es/sis/realizarPago";
return `
<form
name="formularioPago"
method="POST"
action="${actionUrl}"
>
<input type="text" hidden name="DS_MERCHANTPARAMETERS" value="${merchantBase64}" readonly />
<input type="text" hidden name="DS_SIGNATURE" value="${dsSignature}" readonly />
<input type="text" hidden name="DS_SIGNATUREVERSION" value="${signatureVersion}" readonly />
<input type="submit" value="${submitText}" id="submitPayment"/>
</form>
`;
}
// src/index.ts
var signatureVersion2 = "HMAC_SHA256_V1";
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createMerchantParameters,
createMerchantSignature,
generatePaymentForm,
orderNumber,
signatureVersion
});