@muhammedaksam/sipay-node
Version:
Node.js TypeScript SDK for Sipay payment gateway
78 lines • 3.6 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.PaymentCompletion = void 0;
const base_1 = require("./base");
const crypto = __importStar(require("crypto"));
const { createHash, createCipheriv } = crypto;
class PaymentCompletion extends base_1.SipayResource {
/**
* Complete or cancel a 3D payment transaction
* This is used for merchant 3D model where payment completion is done by merchant
*/
async completePayment(completionData, options) {
const data = this.addMerchantKey(completionData);
// Generate hash key using the payment completion format
// Hash format: merchant_key|invoice_id|order_id|status
const hashKey = this.generatePaymentCompletionHashKey(this.client['config'].merchantKey, data.invoice_id, data.order_id, data.status, this.client['config'].appSecret);
data.hash_key = hashKey;
return this.post('/payment/complete', data, options);
}
/**
* Generate hash key for payment completion (using official method)
*/
generatePaymentCompletionHashKey(merchantKey, invoiceId, orderId, status, appSecret) {
try {
const data = merchantKey + '|' + invoiceId + '|' + orderId + '|' + status;
const iv = createHash('sha1').update(String(Math.random())).digest('hex').slice(0, 16);
const password = createHash('sha1').update(appSecret).digest('hex');
const salt = createHash('sha1').update(String(Math.random())).digest('hex').slice(0, 4);
const saltWithPassword = createHash('sha256')
.update(password + salt)
.digest('hex')
.slice(0, 32);
const cipher = createCipheriv('aes-256-cbc', saltWithPassword, iv);
let encrypted = cipher.update(data, 'binary', 'base64');
encrypted += cipher.final('base64');
const msgBundle = iv + ':' + salt + ':' + encrypted;
return msgBundle.replace(/\//g, '__');
}
catch (error) {
throw new Error(`Payment completion hash key generation failed: ${error}`);
}
}
}
exports.PaymentCompletion = PaymentCompletion;
//# sourceMappingURL=payment-completion.js.map