/* eslint-disable no-console */ import Inject from "@entity-access/entity-access/dist/di/di.js"; import { createHash, randomUUID } from "node:crypto"; import LedgerPaymentGateway from "../LedgerPaymentGateway.js"; import { StoreJournal } from "../../../model/entities/StoreJournal.js"; import EncryptionService from "../../encryption/EncryptionService.js"; import HtmlDocument from "@entity-access/server-pages/dist/html/HtmlDocument.js"; import XNode from "@entity-access/server-pages/dist/html/XNode.js"; import StorePaymentAttempt from "../../../model/entities/StorePaymentAttempt.js"; import StorePaymentGateway from "../../../model/entities/StorePaymentGateway.js"; import SecureFetchBuilder from "../../../../common/fetch/SecureFetchBuilder.js"; import EntityAccessError from "@entity-access/entity-access/dist/common/EntityAccessError.js"; const signRequest = (id: any, salt: any, path = "/pg/v1/pay", payload: string = ""): { request: any; verify: any; } => { const request = Buffer.from(payload, "utf-8").toString("base64"); const signPayload = `${request}${path}${salt}`; const sha256 = createHash("sha256").update(signPayload).digest("hex"); return { request, verify: `${sha256}###${id}` }; }; export default class extends LedgerPaymentGateway { @Inject ecs: EncryptionService; async createPayment(host: string, gateway: StorePaymentGateway, order: StoreJournal, pa: StorePaymentAttempt) { const merchantId = gateway.merchantID; const merchantTransactionId = pa.attemptID.toString(36); const userID = order.buyerID; const amount = order.total; const key = this.ecs.secure.decrypt(gateway.encryptedSalt); const keyIndex = gateway.saltIndex; const url = gateway.url; const pID = this.ecs.general.encrypt(pa.attemptID.toString()); const createRequest = { merchantId, merchantTransactionId, merchantUserId: userID ?? randomUUID().replaceAll("-", ""), amount: (Number(amount) * 100).toFixed(0), callbackUrl: `https://${host}/store/checkout-callback/${pID}`, redirectUrl: `https://${host}/store/checkout-callback/${pID}`, paymentInstrument: { type: "PAY_PAGE" } }; const { request, verify } = signRequest(keyIndex, key, "/pg/v1/pay", JSON.stringify(createRequest)); const rs = await SecureFetchBuilder.post((url.endsWith("/") ? url.substring(1) : url) + "/pg/v1/pay") .header("x-verify", verify) .jsonBody({ request}) .asJson(); if (rs.success) { const redirectUrl = rs?.data?.instrumentResponse?.redirectInfo?.url; if (redirectUrl) { return { content: Creating Payment Request

Creating Payment Request ...

}; } } throw new Error("Failed"); } async getStatus(pa: StorePaymentAttempt) { const { gateway, orderID } = pa; const merchantId = gateway.merchantID; const merchantTransactionId = orderID; const key = this.ecs.secure.decrypt(gateway.encryptedSalt); const keyIndex = gateway.saltIndex; const url = gateway.url; const request = `/pg/v1/status/${merchantId}/${merchantTransactionId}`; const { verify } = signRequest(keyIndex, key, request); const finalUrl = `${url.endsWith("/") ? url.substring(1) : url}${request}`; const result = await SecureFetchBuilder.get(finalUrl) .header("x-verify", verify) .header("x-merchant-id", merchantId) .asJson(); if(/success/i.test(result.data?.responseCode)) { return "paid"; } console.error(result); return "failed"; } async refund(pa: StorePaymentAttempt) { throw new EntityAccessError("Not implemented"); } }