unify-payment-gateway
Version:
Unify payment gateway to support all payment gateways and provide one solution
354 lines (339 loc) • 11.7 kB
JavaScript
"use strict";
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);
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// src/index.ts
var index_exports = {};
__export(index_exports, {
CurrencyEnum: () => CurrencyEnum,
EnvironmentEnum: () => EnvironmentEnum,
ProviderTypes: () => ProviderTypes,
UnifyPaymentGatewayClient: () => UnifyPaymentGatewayClient,
default: () => index_default
});
module.exports = __toCommonJS(index_exports);
// src/enums/ProviderEnum.ts
var ProviderTypes = /* @__PURE__ */ ((ProviderTypes2) => {
ProviderTypes2["RAZORPAY"] = "razorpay";
ProviderTypes2["PAYU"] = "payu";
return ProviderTypes2;
})(ProviderTypes || {});
// src/payment-integrations/razorpay/index.ts
var import_razorpay = __toESM(require("razorpay"));
// src/payment-integrations/razorpay/Order.ts
var Order = class {
constructor(instance) {
this._razorPayInstance = instance;
}
fetchPayments(orderId) {
return __async(this, null, function* () {
const order = yield this._razorPayInstance.orders.fetchPayments(orderId);
return order;
});
}
fetch(orderId) {
return __async(this, null, function* () {
const order = yield this._razorPayInstance.orders.fetch(orderId);
return order;
});
}
all(options) {
return __async(this, null, function* () {
const orders = yield this._razorPayInstance.orders.all(options);
return orders;
});
}
createOrder(order) {
return __async(this, null, function* () {
const data = yield this._razorPayInstance.orders.create(order);
return data;
});
}
};
// src/payment-integrations/razorpay/Payment.ts
var Payment = class {
constructor(instance) {
this._razorPayInstance = instance;
}
};
// src/payment-integrations/razorpay/Customer.ts
var Customer = class {
constructor(instance) {
this._razorPayInstance = instance;
}
all(options) {
return __async(this, null, function* () {
const customers = yield this._razorPayInstance.customers.all(options);
return customers;
});
}
update(customerId, payload) {
return __async(this, null, function* () {
const customer = yield this._razorPayInstance.customers.edit(customerId, payload);
return customer;
});
}
create(payload) {
return __async(this, null, function* () {
const customer = yield this._razorPayInstance.customers.create(payload);
return customer;
});
}
fetchCustomerById(customerId) {
return __async(this, null, function* () {
const customer = yield this._razorPayInstance.customers.fetch(customerId);
return customer;
});
}
addBankAccount(customerId, payload) {
return __async(this, null, function* () {
const customer = yield this._razorPayInstance.customers.addBankAccount(customerId, payload);
return customer;
});
}
deleteBankAccount(customerId, accountId) {
return __async(this, null, function* () {
const bank = yield this._razorPayInstance.customers.deleteBankAccount(customerId, accountId);
return bank;
});
}
};
// src/payment-integrations/razorpay/index.ts
var RazorPay = class {
constructor(keyId, keySecret) {
if (keyId == null || keySecret == null) {
throw new Error("keyId and keySecret are required");
}
this._keyId = keyId;
this._keySecret = keySecret;
this._instance = new import_razorpay.default({
key_id: this._keyId,
key_secret: this._keySecret
});
this.order = new Order(this._instance);
this.payment = new Payment(this._instance);
this.customer = new Customer(this._instance);
}
};
// src/payment-integrations/payu/index.ts
var import_crypto = __toESM(require("crypto"));
var PayU = class {
constructor(config) {
if (!config.salt)
throw new Error("PayU requires a salt in the configuration");
if (!config.clientId)
throw new Error("PayU requires a merchant in the configuration");
_config = config;
}
all(options) {
throw new Error("Method not implemented.");
}
fetch(orderId) {
throw new Error("Method not implemented.");
}
fetchPayments(orderId) {
throw new Error("Method not implemented.");
}
createOrder(orderPayload) {
return __async(this, null, function* () {
const hashString = `${this._config.clientId}|${orderPayload.txn_id}|${orderPayload.amount}|${orderPayload.product_info}|${orderPayload.name}|${orderPayload.email}|||||||||||${this._config.salt}`;
const hash = import_crypto.default.createHash("sha512").update(hashString).digest("hex");
const order = {
amount: orderPayload.amount,
name: orderPayload.name,
email: orderPayload.email,
phone: orderPayload.phone,
product_info: orderPayload.product_info,
txn_id: orderPayload.txn_id,
merchantKey: this._config.clientId,
hash,
service_provider: "payu_paisa"
};
return order;
});
}
};
// src/gateway/Order.ts
var Order2 = class {
constructor(config) {
this._config = config;
if (this._config.provider == "razorpay" /* RAZORPAY */) {
if (!this._config.clientSecret)
throw new Error("Client Secret is required for RazorPay");
const razopay = new RazorPay(this._config.clientId, this._config.clientSecret);
this.order = razopay.order;
} else if (this._config.provider == "payu" /* PAYU */) {
this.order = new PayU(this._config);
}
}
createOrder(order) {
return __async(this, null, function* () {
if (this._config.provider == "razorpay" /* RAZORPAY */) {
return yield this.order.createOrder(order);
} else {
return yield this.order.createOrder(order);
}
});
}
all(options) {
return __async(this, null, function* () {
if (this._config.provider == "payu" /* PAYU */)
throw new Error("PayU does not support this method for payu");
return yield this.order.all(options);
});
}
fetch(orderId) {
return __async(this, null, function* () {
if (this._config.provider == "payu" /* PAYU */)
throw new Error("PayU does not support this method for payu");
return yield this.order.fetch(orderId);
});
}
fetchPayments(orderId) {
return __async(this, null, function* () {
if (this._config.provider == "payu" /* PAYU */)
throw new Error("PayU does not support this method for payu");
return yield this.order.fetchPayments(orderId);
});
}
};
// src/gateway/Payment.ts
var Payment2 = class {
constructor(config) {
this._config = config;
if (this._config.provider == "razorpay" /* RAZORPAY */) {
if (!this._config.clientSecret)
throw new Error("Client Secret is required for RazorPay");
const razopay = new RazorPay(this._config.clientId, this._config.clientSecret);
this.payment = razopay.payment;
}
}
};
// src/gateway/Customer.ts
var Customer2 = class {
constructor(config) {
this._config = config;
if (this._config.provider == "razorpay" /* RAZORPAY */) {
if (!this._config.clientSecret)
throw new Error("Client Secret is required for RazorPay");
const razopay = new RazorPay(this._config.clientId, this._config.clientSecret);
this.customer = razopay.customer;
}
}
create(payload) {
return __async(this, null, function* () {
if (this._config.provider == "payu" /* PAYU */)
throw new Error("PayU does not support this method for payu");
return yield this.customer.create(payload);
});
}
update(customerId, payload) {
return __async(this, null, function* () {
if (this._config.provider == "payu" /* PAYU */)
throw new Error("PayU does not support this method for payu");
return this.customer.update(customerId, payload);
});
}
all(options) {
return __async(this, null, function* () {
if (this._config.provider == "payu" /* PAYU */)
throw new Error("PayU does not support this method for payu");
return this.customer.all(options);
});
}
fetchCustomerById(customerId) {
return __async(this, null, function* () {
if (this._config.provider == "payu" /* PAYU */)
throw new Error("PayU does not support this method for payu");
return this.customer.fetchCustomerById(customerId);
});
}
addBankAccount(customerId, payload) {
return __async(this, null, function* () {
if (this._config.provider == "payu" /* PAYU */)
throw new Error("PayU does not support this method for payu");
return yield this.customer.addBankAccount(customerId, payload);
});
}
deleteBankAccount(customerId, accountId) {
return __async(this, null, function* () {
if (this._config.provider == "payu" /* PAYU */)
throw new Error("PayU does not support this method for payu");
return this.customer.deleteBankAccount(customerId, accountId);
});
}
};
// src/enums/EnvironmentEnum.ts
var EnvironmentEnum = /* @__PURE__ */ ((EnvironmentEnum2) => {
EnvironmentEnum2["SANDBOX"] = "sandbox";
EnvironmentEnum2["TEST"] = "test";
EnvironmentEnum2["PRODUCTION"] = "production";
EnvironmentEnum2["LIVE"] = "live";
return EnvironmentEnum2;
})(EnvironmentEnum || {});
// src/enums/CurrencyEnum.ts
var CurrencyEnum = /* @__PURE__ */ ((CurrencyEnum2) => {
CurrencyEnum2["INR"] = "INR";
return CurrencyEnum2;
})(CurrencyEnum || {});
// src/index.ts
var UnifyPaymentGatewayClient = class {
constructor(config) {
this._config = config;
this.order = new Order2(config);
this.customer = new Customer2(config);
this.payment = new Payment2(config);
}
};
var index_default = UnifyPaymentGatewayClient;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CurrencyEnum,
EnvironmentEnum,
ProviderTypes,
UnifyPaymentGatewayClient
});