import Inject, { RegisterScoped, ServiceProvider } from "@entity-access/entity-access/dist/di/di.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; import LedgerPaymentGateway from "./LedgerPaymentGateway.js"; import XNode from "@entity-access/server-pages/dist/html/XNode.js"; import QueueInvokeWebHookWorkflow from "../../workflows/web-hooks/QueueInvokeWebHookWorkflow.js"; import { AppWorkflowContext } from "../../workflows/AppWorkflowContext.js"; import StorePaymentAttempt from "../../model/entities/StorePaymentAttempt.js"; import JournalService from "../accounts/JournalService.js"; /** * Syncing subscriptions can become very complicated and may require frequent manual changes into system. * * 1. Since Social Mail will serve as a full featured shopping cart, subscription should be managed within * own business logic. So subscription can be switched from one payment gateway to another gateway * without having to worry about integration logic. * 2. Payment Gateway's inbuilt subscription management logic can change in future, which might break * integration logic. So we should stick with our own subscription management. * 3. Subscription can be switched to different payment gateway that may not have any subscription management * at all. Like pay via Cash or Check. */ @RegisterScoped export default class StorePaymentGatewayService { @Inject db: SocialMailContext; @Inject context: AppWorkflowContext; async checkout({ host, storeID, orderID, pgAccountID = void 0 }: { host: string, storeID: number, pgAccountID?: number, orderID: number }): Promise { // check if any pending payment exists... let payment = await this.db.storePaymentAttempts.where({ orderID }, (x, p) => x.orderID === p.orderID && x.status === null) .first(); if (payment) { if(!payment.status) { await this.checkoutResult(payment); } if (payment.status === "paid") { payment.url = void 0; payment.content = void 0; await this.db.storePaymentAttempts .withKeys( { attemptID: payment.attemptID} ) .update({ url: null, content: null}); return payment; } } const order = await this.db.storeJournals.where({ orderID }, (x, p) => x.journalID === p.orderID) .first(); let q = this.db.storePaymentGateways.where({ storeID}, (x, p) => x.storeAccount.storeID === p.storeID ); if (pgAccountID) { q = q.where({ pgAccountID }, (x, p) => x.accountID === p.pgAccountID); } else{ q = q.where((x) => x.isGatewayActive === true); } const gateway = await q .orderByDescending((x) => x.isDefault) .first(); const dateCreated = DateTime.now; await using tx = await this.db.connection.createTransaction(); payment = await this.db.storePaymentAttempts.statements.insert({ orderID, gatewayID: gateway.accountID, dateCreated }); const pg = await LedgerPaymentGateway.for(this, gateway.paymentGateway); const { url, content } = await pg.createPayment( host, gateway, order, payment); payment.url = url; payment.content = (content as XNode)?.render(); await this.db.storePaymentAttempts .withKeys({ attemptID: payment.attemptID }) .update({ url, content: payment.content }); await tx.commit(); return payment; } async refund(payment: StorePaymentAttempt) { const gateway = await this.db.storePaymentGateways.statements.select({}, { accountID: payment.gatewayID }); payment.gateway = gateway; const pg = await LedgerPaymentGateway.for(this, gateway.paymentGateway); const status = await pg.refund(payment); if (status === "refunded") { payment.refund = "refunded"; await this.db.storePaymentAttempts.statements.update({ refund: "refunded", dateUpdated: DateTime.now }, { attemptID: payment.attemptID}); } } async checkoutResult(payment: StorePaymentAttempt) { if (!payment.status) { const gateway = await this.db.storePaymentGateways.statements.select({}, { accountID: payment.gatewayID }); payment.gateway = gateway; const pg = await LedgerPaymentGateway.for(this, gateway.paymentGateway); payment.status = await pg.getStatus(payment); } if (payment.status) { payment.url = void 0; payment.content = void 0; await this.db.storePaymentAttempts.statements.update({ url: null, content: null, status: payment.status}, { attemptID: payment.attemptID }); if (payment.status === "paid") { await this.updatePaymentStatus(payment.orderID); } } return payment; } private async updatePaymentStatus(orderID: any) { this.db.verifyFilters = false; const order = await this.db.storeJournals.where({ orderID }, (x, p) => x.journalID === p.orderID) .include((x) => x.lineItems) .first(); if (order.status !== "paid") { using scope = ServiceProvider.from(this).createScope(); const db = scope.resolve(SocialMailContext); if(order) { await using tx = await db.connection.createTransaction(); await db.storeJournals.statements.update({ status: "paid", dateUpdated: DateTime.now }, { journalID: order.journalID }); const { storeID } = order; const input = { storeID, orderID, }; for (const line of order.lineItems) { const { itemID: storeItemID, lineItemID } = line; await QueueInvokeWebHookWorkflow.queue(this.context, { type: "order-paid", storeItemID, input: { ... input, lineItemID, storeItemID } }); } await QueueInvokeWebHookWorkflow.queue(this.context, { type: "order-paid", storeID, input }); // change type and create sales invoice order.journalType = "sales-invoice"; await tx.commit(); } const js = scope.resolve(JournalService); const journal = await db.storeJournals .where(order, (x, p) => x.journalID === p.journalID) .include((x) => [x.lineItems.forEach((i) => i.itemPrice.storeItem)]) .first(); const entry = db.changeSet.getEntry(journal); await js.syncEntries(entry, journal); await db.saveChanges(); // mark subscription as active it if it is currentOrderID await db.storeSubscriptions .where(order, (x, p) => x.currentOrderID === p.journalID) .update(() => ({ status: "active"})); // await QueueInvokeWebHookWorkflow.queue(this.context, { // type: "order-paid", // storeItemID // }); } } }