medusa-payment-comgate-jc
Version:
Comgate payment gateway provider for MedusaJS
457 lines • 22.9 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
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 __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var comgate_node_1 = __importDefault(require("comgate-node"));
var os_1 = require("os");
var medusa_1 = require("@medusajs/medusa");
var types_1 = require("../types");
var ComgateBase = /** @class */ (function (_super) {
__extends(ComgateBase, _super);
function ComgateBase(_, options) {
var _this = _super.call(this, _, options) || this;
_this.options_ = options;
_this.init();
_this.cartService = _.cartService;
return _this;
}
ComgateBase.prototype.init = function () {
this.comgateClient =
this.comgateClient ||
new comgate_node_1.default({
merchant: this.options_.merchant,
secret: this.options_.secret,
test: this.options_.test,
});
};
ComgateBase.prototype.getPaymentStatus = function (paymentSessionData) {
return __awaiter(this, void 0, void 0, function () {
var transId, transactionStatus, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this.options_.debug) {
console.log("ComgateMedusa: " + "getPaymentStatus", "start");
}
transId = paymentSessionData.comgateData.transId;
if (this.options_.debug) {
console.log("ComgateMedusa: " + "getPaymentStatus", "data: ", {
transId: transId,
});
}
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.getComgateTransactionStatus(transId)];
case 2:
transactionStatus = _a.sent();
if (this.options_.debug) {
console.log("ComgateMedusa: " + "getPaymentStatus", "response: ", {
transId: transactionStatus,
});
}
return [2 /*return*/, transactionStatus.status];
case 3:
error_1 = _a.sent();
if (this.options_.debug) {
console.log("ComgateMedusa: " + "getPaymentStatus", "error: ", error_1);
}
return [2 /*return*/, medusa_1.PaymentSessionStatus.ERROR];
case 4: return [2 /*return*/];
}
});
});
};
ComgateBase.prototype.initiatePayment = function (context) {
return __awaiter(this, void 0, void 0, function () {
var session_data;
return __generator(this, function (_a) {
if (this.options_.debug) {
console.log("ComgateMedusa: " + "initiatePayment", "start");
}
session_data = {
status: medusa_1.PaymentSessionStatus.PENDING,
comgateData: {
transId: null,
status: "INITIATED",
redirect: null,
error: null,
},
};
if (this.options_.debug) {
console.log("ComgateMedusa: " + "initiatePayment", "session_data: ", session_data);
}
return [2 /*return*/, {
session_data: session_data,
}];
});
});
};
ComgateBase.prototype.authorizePayment = function (paymentSessionData, context) {
return __awaiter(this, void 0, void 0, function () {
var transId, transactionStatus, sessionData;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this.options_.debug) {
console.log("ComgateMedusa: " + "authorizePayment", "start");
}
transId = paymentSessionData.comgateData.transId;
if (this.options_.debug) {
console.log("ComgateMedusa: " + "authorizePayment", "transId: ", transId);
}
return [4 /*yield*/, this.getComgateTransactionStatus(transId)];
case 1:
transactionStatus = _a.sent();
if (this.options_.debug) {
console.log("ComgateMedusa: " + "authorizePayment", "response: ", transactionStatus);
}
sessionData = {
status: transactionStatus.status,
data: {
status: transactionStatus.status,
comgateData: {
transId: paymentSessionData.comgateData.transId,
status: transactionStatus.comgateStatus,
redirect: paymentSessionData.comgateData.redirect,
error: transactionStatus.comgateError,
},
},
};
if (this.options_.debug) {
console.log("ComgateMedusa: " + "authorizePayment", "sessionData: ", sessionData);
}
return [2 /*return*/, sessionData];
}
});
});
};
ComgateBase.prototype.cancelPayment = function (paymentSessionData) {
return __awaiter(this, void 0, void 0, function () {
var transId, _a, code, message, error, error, error_2;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
if (this.options_.debug) {
console.log("ComgateMedusa: " + "cancelPayment", "start");
}
transId = paymentSessionData.comgateData.transId;
if (this.options_.debug) {
console.log("ComgateMedusa: " + "cancelPayment", "transId: ", transId);
}
_b.label = 1;
case 1:
_b.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.comgateClient.cancel({
transId: transId,
})];
case 2:
_a = _b.sent(), code = _a.code, message = _a.message;
if (this.options_.debug) {
console.log("ComgateMedusa: " + "cancelPayment", "response: ", code, message);
}
if (code === 0) {
return [2 /*return*/, {
status: medusa_1.PaymentSessionStatus.CANCELED,
comgateData: {
transId: paymentSessionData.comgateData.transId,
status: "CANCELLED",
redirect: paymentSessionData.comgateData.redirect,
error: null,
},
}];
}
else if (code === 1400) {
error = {
error: message,
code: String(code),
};
return [2 /*return*/, error];
}
else {
error = {
error: message,
code: String(code),
};
return [2 /*return*/, error];
}
return [3 /*break*/, 4];
case 3:
error_2 = _b.sent();
return [2 /*return*/, this.buildError("An error occurred in cancelPayment", error_2)];
case 4: return [2 /*return*/];
}
});
});
};
ComgateBase.prototype.capturePayment = function (paymentSessionData) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
if (this.options_.debug) {
console.log("ComgateMedusa: " + "capturePayment", "start");
}
return [2 /*return*/, paymentSessionData];
});
});
};
ComgateBase.prototype.deletePayment = function (paymentSessionData) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this.options_.debug) {
console.log("ComgateMedusa: " + "deletePayment", "start");
}
return [4 /*yield*/, this.cancelPayment(paymentSessionData)];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
ComgateBase.prototype.refundPayment = function (paymentSessionData, refundAmount) {
return __awaiter(this, void 0, void 0, function () {
var transId, refundPayment, error, error_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this.options_.debug) {
console.log("ComgateMedusa: " + "refundPayment", "start");
}
transId = paymentSessionData.comgateData.transId;
if (this.options_.debug) {
console.log("ComgateMedusa: " + "refundPayment", "transId: ", transId);
console.log("ComgateMedusa: " + "refundPayment", "amount: ", String(refundAmount));
}
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.comgateClient.refund({
transId: transId,
amount: String(refundAmount),
})];
case 2:
refundPayment = _a.sent();
if (this.options_.debug) {
console.log("ComgateMedusa: " + "refundPayment", "response: ", refundPayment);
}
if (refundPayment.code !== 0) {
error = {
error: String(refundPayment.message),
code: String(refundPayment.code),
};
return [2 /*return*/, this.buildError("Failed to refund payment", error)];
}
return [2 /*return*/, {
status: paymentSessionData.status,
comgateData: {
transId: paymentSessionData.comgateData.transId,
status: paymentSessionData.comgateData.status,
redirect: paymentSessionData.comgateData.redirect,
error: null,
},
}];
case 3:
error_3 = _a.sent();
return [2 /*return*/, this.buildError("Failed to refund payment", error_3)];
case 4: return [2 /*return*/];
}
});
});
};
ComgateBase.prototype.retrievePayment = function (paymentSessionData) {
return __awaiter(this, void 0, void 0, function () {
var transId, transactionStatus, error_4;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this.options_.debug) {
console.log("ComgateMedusa: " + "retrievePayment", "start");
}
transId = paymentSessionData.comgateData.transId;
if (this.options_.debug) {
console.log("ComgateMedusa: " + "retrievePayment", "transId: ", transId);
}
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.getComgateTransactionStatus(transId)];
case 2:
transactionStatus = _a.sent();
if (this.options_.debug) {
console.log("ComgateMedusa: " + "retrievePayment", "response: ", transactionStatus);
}
return [2 /*return*/, {
status: transactionStatus.status,
comgateData: {
transId: paymentSessionData.comgateData.transId,
status: transactionStatus.comgateStatus,
redirect: paymentSessionData.comgateData.redirect,
error: transactionStatus.comgateError,
},
}];
case 3:
error_4 = _a.sent();
return [2 /*return*/, this.buildError("Failed to retrieve payment", error_4)];
case 4: return [2 /*return*/];
}
});
});
};
ComgateBase.prototype.updatePayment = function (context) {
return __awaiter(this, void 0, void 0, function () {
var paymentSessionData;
return __generator(this, function (_a) {
if (this.options_.debug) {
console.log("ComgateMedusa: " + "updatePayment", "start");
}
paymentSessionData = context.paymentSessionData;
// const transId = paymentSessionData.comgateData.transId as string;
if (this.options_.debug) {
console.log("ComgateMedusa: " + "updatePayment", "paymentSessionData: ", paymentSessionData);
}
return [2 /*return*/, {
session_data: paymentSessionData,
}];
});
});
};
ComgateBase.prototype.updatePaymentData = function (sessionId, data) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
if (this.options_.debug) {
console.log("ComgateMedusa: " + "updatePaymentData", "start");
console.log("ComgateMedusa: " + "updatePaymentData", "data: ", data);
}
return [2 /*return*/, data];
});
});
};
ComgateBase.prototype.buildError = function (message, e) {
var _a, _b;
return {
error: message,
code: "code" in e ? e.code : "",
detail: (0, medusa_1.isPaymentProcessorError)(e)
? "".concat(e.error).concat(os_1.EOL).concat((_a = e.detail) !== null && _a !== void 0 ? _a : "")
: "detail" in e
? e.detail
: (_b = e.message) !== null && _b !== void 0 ? _b : "",
};
};
ComgateBase.prototype.getComgateTransactionStatus = function (transId) {
return __awaiter(this, void 0, void 0, function () {
var status, comgateStatus, comgateError, paymentStatus, error, error_5;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
comgateError = null;
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.comgateClient.status({
transId: transId,
})];
case 2:
paymentStatus = _a.sent();
if (paymentStatus.code !== "0") {
status = medusa_1.PaymentSessionStatus.ERROR;
comgateStatus = "ERROR";
error = {
error: paymentStatus.message,
code: String(paymentStatus.code),
detail: paymentStatus.message,
};
comgateError = this.buildError("Error Comgate API payment status Response", error);
}
else {
switch (paymentStatus.status.toUpperCase()) {
case "PENDING":
status = medusa_1.PaymentSessionStatus.AUTHORIZED;
break;
case "PAID":
status = medusa_1.PaymentSessionStatus.AUTHORIZED;
break;
case "AUTHORIZED":
status = medusa_1.PaymentSessionStatus.AUTHORIZED;
break;
case "CANCELLED":
status = medusa_1.PaymentSessionStatus.CANCELED;
break;
default:
status = medusa_1.PaymentSessionStatus.PENDING;
}
comgateStatus = paymentStatus.status;
}
return [3 /*break*/, 4];
case 3:
error_5 = _a.sent();
status = medusa_1.PaymentSessionStatus.ERROR;
comgateStatus = "ERROR";
comgateError = this.buildError("Error in calling Comgate API payment status", error_5);
return [3 /*break*/, 4];
case 4: return [2 /*return*/, {
status: status,
comgateStatus: comgateStatus,
comgateError: comgateError,
}];
}
});
});
};
ComgateBase.identifier = types_1.PaymentProviderKeys.COMGATE_CARD;
return ComgateBase;
}(medusa_1.AbstractPaymentProcessor));
exports.default = ComgateBase;
//# sourceMappingURL=comgate-base.js.map