import Inject, { RegisterSingleton } from "@entity-access/entity-access/dist/di/di.js"; import { StoreJournal } from "../../../model/entities/StoreJournal.js"; import StorePaymentAttempt from "../../../model/entities/StorePaymentAttempt.js"; import StorePaymentGateway from "../../../model/entities/StorePaymentGateway.js"; import EncryptionService from "../../encryption/EncryptionService.js"; import LedgerPaymentGateway from "../LedgerPaymentGateway.js"; import SocialMailContext from "../../../model/SocialMailContext.js"; import TimedCache from "@entity-access/entity-access/dist/common/cache/TimedCache.js"; import { Cashfree, CFEnvironment } from "cashfree-pg"; import HtmlDocument from "@entity-access/server-pages/dist/html/HtmlDocument.js"; import XNode from "@entity-access/server-pages/dist/html/XNode.js"; import EntityAccessError from "@entity-access/entity-access/dist/common/EntityAccessError.js"; @RegisterSingleton class CashfreeClientCache { cache = new TimedCache(); @Inject ecs: EncryptionService; getClient(gateway: StorePaymentGateway) { return this.cache.getOrCreate(gateway.accountID, gateway, (k, p) => { const secret = this.ecs.secure.decrypt(p.encryptedSecret); return new Cashfree(gateway.sandbox ? CFEnvironment.SANDBOX : CFEnvironment.PRODUCTION, gateway.clientID, secret ); }); } } export default class extends LedgerPaymentGateway { @Inject ecs: EncryptionService; @Inject pc: CashfreeClientCache; @Inject db: SocialMailContext; async createPayment(host: string, gateway: StorePaymentGateway, order: StoreJournal, pa: StorePaymentAttempt) { const pID = this.ecs.general.encrypt(pa.attemptID.toString()); const mode = gateway.sandbox ? "sandbox" : "production"; const items = await this.db.storeJournalLineItems.where(order, (x, p) => x.journalID === p.journalID) .include((x) => x.itemPrice.storeItem) .toArray(); const client = this.pc.getClient(gateway); const { emailAddress: { emailAddress: emailAddress} } = await this.db.storeAccountEmails .where(order, (x, p) => x.accountID === p.buyerID) .include((x) => x.emailAddress) .first(); const currencyCode = items[0].itemPrice.currency; const { phoneNumberE64 } = await this.db.storeAccountPhones .where(order, (x, p) => x.accountID === p.buyerID) .first(); const returnUrl = `https://${host}/store/checkout-callback/${pID}`; const pgOrder = await client.PGCreateOrder({ order_amount: order.total, order_currency: currencyCode, order_meta: { return_url: returnUrl }, customer_details: { customer_id: emailAddress, customer_email: emailAddress, customer_phone: phoneNumberE64, } }); const paymentSessionId = pgOrder.data.payment_session_id; pa.externalID = pgOrder.data.order_id; await this.db.storePaymentAttempts.statements.update({ externalID: pa.externalID }, { attemptID: pa.attemptID }); return { content: Creating Payment Request

Creating Payment Request ...

}; } async getStatus(pa: StorePaymentAttempt) { const { gateway, externalID: id } = pa; const client = this.pc.getClient(gateway); const order = await client.PGFetchOrder(id); if (/paid/i.test(order.data.order_status)) { return "paid"; } return "failed"; } async refund(pa: StorePaymentAttempt) { throw new EntityAccessError("Not implemented"); } }