@voyant-travel/finance 0.119.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +192 -0
- package/dist/action-ledger-drift.d.ts +29 -0
- package/dist/action-ledger-drift.d.ts.map +1 -0
- package/dist/action-ledger-drift.js +166 -0
- package/dist/booking-tax.d.ts +124 -0
- package/dist/booking-tax.d.ts.map +1 -0
- package/dist/booking-tax.js +264 -0
- package/dist/checkout-routes.d.ts +1154 -0
- package/dist/checkout-routes.d.ts.map +1 -0
- package/dist/checkout-routes.js +116 -0
- package/dist/checkout-service-plan.d.ts +137 -0
- package/dist/checkout-service-plan.d.ts.map +1 -0
- package/dist/checkout-service-plan.js +119 -0
- package/dist/checkout-service.d.ts +9 -0
- package/dist/checkout-service.d.ts.map +1 -0
- package/dist/checkout-service.js +324 -0
- package/dist/checkout-validation.d.ts +1682 -0
- package/dist/checkout-validation.d.ts.map +1 -0
- package/dist/checkout-validation.js +228 -0
- package/dist/document-download.d.ts +3 -0
- package/dist/document-download.d.ts.map +1 -0
- package/dist/document-download.js +1 -0
- package/dist/fx-money.d.ts +17 -0
- package/dist/fx-money.d.ts.map +1 -0
- package/dist/fx-money.js +194 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +108 -0
- package/dist/invoice-fx.d.ts +134 -0
- package/dist/invoice-fx.d.ts.map +1 -0
- package/dist/invoice-fx.js +240 -0
- package/dist/invoice-number-errors.d.ts +2 -0
- package/dist/invoice-number-errors.d.ts.map +1 -0
- package/dist/invoice-number-errors.js +58 -0
- package/dist/markets-ref.d.ts +149 -0
- package/dist/markets-ref.d.ts.map +1 -0
- package/dist/markets-ref.js +17 -0
- package/dist/payment-link.d.ts +23 -0
- package/dist/payment-link.d.ts.map +1 -0
- package/dist/payment-link.js +30 -0
- package/dist/payment-policy.d.ts +113 -0
- package/dist/payment-policy.d.ts.map +1 -0
- package/dist/payment-policy.js +193 -0
- package/dist/route-runtime.d.ts +22 -0
- package/dist/route-runtime.d.ts.map +1 -0
- package/dist/route-runtime.js +18 -0
- package/dist/routes-action-ledger.d.ts +181 -0
- package/dist/routes-action-ledger.d.ts.map +1 -0
- package/dist/routes-action-ledger.js +142 -0
- package/dist/routes-booking-billing.d.ts +852 -0
- package/dist/routes-booking-billing.d.ts.map +1 -0
- package/dist/routes-booking-billing.js +223 -0
- package/dist/routes-booking-create.d.ts +3 -0
- package/dist/routes-booking-create.d.ts.map +1 -0
- package/dist/routes-booking-create.js +194 -0
- package/dist/routes-booking-reads.d.ts +46 -0
- package/dist/routes-booking-reads.d.ts.map +1 -0
- package/dist/routes-booking-reads.js +20 -0
- package/dist/routes-documents.d.ts +195 -0
- package/dist/routes-documents.d.ts.map +1 -0
- package/dist/routes-documents.js +93 -0
- package/dist/routes-invoice-core.d.ts +794 -0
- package/dist/routes-invoice-core.d.ts.map +1 -0
- package/dist/routes-invoice-core.js +238 -0
- package/dist/routes-invoice-documents.d.ts +401 -0
- package/dist/routes-invoice-documents.d.ts.map +1 -0
- package/dist/routes-invoice-documents.js +91 -0
- package/dist/routes-invoice-issue.d.ts +384 -0
- package/dist/routes-invoice-issue.d.ts.map +1 -0
- package/dist/routes-invoice-issue.js +208 -0
- package/dist/routes-payment-processing.d.ts +1193 -0
- package/dist/routes-payment-processing.d.ts.map +1 -0
- package/dist/routes-payment-processing.js +238 -0
- package/dist/routes-payments.d.ts +309 -0
- package/dist/routes-payments.d.ts.map +1 -0
- package/dist/routes-payments.js +94 -0
- package/dist/routes-public.d.ts +1948 -0
- package/dist/routes-public.d.ts.map +1 -0
- package/dist/routes-public.js +275 -0
- package/dist/routes-reference-data.d.ts +977 -0
- package/dist/routes-reference-data.d.ts.map +1 -0
- package/dist/routes-reference-data.js +191 -0
- package/dist/routes-reports.d.ts +344 -0
- package/dist/routes-reports.d.ts.map +1 -0
- package/dist/routes-reports.js +93 -0
- package/dist/routes-runtime.d.ts +71 -0
- package/dist/routes-runtime.d.ts.map +1 -0
- package/dist/routes-runtime.js +59 -0
- package/dist/routes-settlement.d.ts +67 -0
- package/dist/routes-settlement.d.ts.map +1 -0
- package/dist/routes-settlement.js +23 -0
- package/dist/routes-shared.d.ts +35 -0
- package/dist/routes-shared.d.ts.map +1 -0
- package/dist/routes-shared.js +10 -0
- package/dist/routes-supplier-invoices.d.ts +778 -0
- package/dist/routes-supplier-invoices.d.ts.map +1 -0
- package/dist/routes-supplier-invoices.js +159 -0
- package/dist/routes-vouchers.d.ts +228 -0
- package/dist/routes-vouchers.d.ts.map +1 -0
- package/dist/routes-vouchers.js +54 -0
- package/dist/routes.d.ts +5577 -0
- package/dist/routes.d.ts.map +1 -0
- package/dist/routes.js +44 -0
- package/dist/schema/booking-billing.d.ts +1006 -0
- package/dist/schema/booking-billing.d.ts.map +1 -0
- package/dist/schema/booking-billing.js +106 -0
- package/dist/schema/enums.d.ts +48 -0
- package/dist/schema/enums.d.ts.map +1 -0
- package/dist/schema/enums.js +237 -0
- package/dist/schema/invoice-documents.d.ts +1245 -0
- package/dist/schema/invoice-documents.d.ts.map +1 -0
- package/dist/schema/invoice-documents.js +140 -0
- package/dist/schema/payment-instruments.d.ts +418 -0
- package/dist/schema/payment-instruments.d.ts.map +1 -0
- package/dist/schema/payment-instruments.js +45 -0
- package/dist/schema/payment-processing.d.ts +563 -0
- package/dist/schema/payment-processing.d.ts.map +1 -0
- package/dist/schema/payment-processing.js +65 -0
- package/dist/schema/payment-sessions.d.ts +728 -0
- package/dist/schema/payment-sessions.d.ts.map +1 -0
- package/dist/schema/payment-sessions.js +79 -0
- package/dist/schema/receivables.d.ts +1474 -0
- package/dist/schema/receivables.d.ts.map +1 -0
- package/dist/schema/receivables.js +179 -0
- package/dist/schema/relations.d.ts +82 -0
- package/dist/schema/relations.d.ts.map +1 -0
- package/dist/schema/relations.js +144 -0
- package/dist/schema/supplier-invoices.d.ts +1619 -0
- package/dist/schema/supplier-invoices.d.ts.map +1 -0
- package/dist/schema/supplier-invoices.js +228 -0
- package/dist/schema/tax.d.ts +712 -0
- package/dist/schema/tax.d.ts.map +1 -0
- package/dist/schema/tax.js +98 -0
- package/dist/schema/vouchers.d.ts +444 -0
- package/dist/schema/vouchers.d.ts.map +1 -0
- package/dist/schema/vouchers.js +64 -0
- package/dist/schema.d.ts +12 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +11 -0
- package/dist/service-accountant-shares.d.ts +106 -0
- package/dist/service-accountant-shares.d.ts.map +1 -0
- package/dist/service-accountant-shares.js +331 -0
- package/dist/service-action-ledger-accounting.d.ts +104 -0
- package/dist/service-action-ledger-accounting.d.ts.map +1 -0
- package/dist/service-action-ledger-accounting.js +386 -0
- package/dist/service-action-ledger-booking-payments.d.ts +48 -0
- package/dist/service-action-ledger-booking-payments.d.ts.map +1 -0
- package/dist/service-action-ledger-booking-payments.js +178 -0
- package/dist/service-action-ledger-bookings.d.ts +44 -0
- package/dist/service-action-ledger-bookings.d.ts.map +1 -0
- package/dist/service-action-ledger-bookings.js +81 -0
- package/dist/service-action-ledger-payment-authorizations.d.ts +48 -0
- package/dist/service-action-ledger-payment-authorizations.d.ts.map +1 -0
- package/dist/service-action-ledger-payment-authorizations.js +209 -0
- package/dist/service-action-ledger-payment-sessions.d.ts +83 -0
- package/dist/service-action-ledger-payment-sessions.d.ts.map +1 -0
- package/dist/service-action-ledger-payment-sessions.js +294 -0
- package/dist/service-action-ledger-supplier-invoices.d.ts +27 -0
- package/dist/service-action-ledger-supplier-invoices.d.ts.map +1 -0
- package/dist/service-action-ledger-supplier-invoices.js +111 -0
- package/dist/service-action-ledger-supplier-payments.d.ts +21 -0
- package/dist/service-action-ledger-supplier-payments.d.ts.map +1 -0
- package/dist/service-action-ledger-supplier-payments.js +97 -0
- package/dist/service-action-ledger.d.ts +7 -0
- package/dist/service-action-ledger.d.ts.map +1 -0
- package/dist/service-action-ledger.js +6 -0
- package/dist/service-aggregates.d.ts +96 -0
- package/dist/service-aggregates.d.ts.map +1 -0
- package/dist/service-aggregates.js +294 -0
- package/dist/service-booking-billing.d.ts +2322 -0
- package/dist/service-booking-billing.d.ts.map +1 -0
- package/dist/service-booking-billing.js +8 -0
- package/dist/service-booking-create.d.ts +410 -0
- package/dist/service-booking-create.d.ts.map +1 -0
- package/dist/service-booking-create.js +1256 -0
- package/dist/service-booking-guarantees.d.ts +725 -0
- package/dist/service-booking-guarantees.d.ts.map +1 -0
- package/dist/service-booking-guarantees.js +153 -0
- package/dist/service-booking-item-billing.d.ts +1062 -0
- package/dist/service-booking-item-billing.d.ts.map +1 -0
- package/dist/service-booking-item-billing.js +77 -0
- package/dist/service-booking-payment-schedules.d.ts +557 -0
- package/dist/service-booking-payment-schedules.d.ts.map +1 -0
- package/dist/service-booking-payment-schedules.js +372 -0
- package/dist/service-bookings-dual-create.d.ts +308 -0
- package/dist/service-bookings-dual-create.d.ts.map +1 -0
- package/dist/service-bookings-dual-create.js +131 -0
- package/dist/service-boundary-sql.d.ts +6 -0
- package/dist/service-boundary-sql.d.ts.map +1 -0
- package/dist/service-boundary-sql.js +15 -0
- package/dist/service-cost-categories.d.ts +26 -0
- package/dist/service-cost-categories.d.ts.map +1 -0
- package/dist/service-cost-categories.js +76 -0
- package/dist/service-documents.d.ts +80 -0
- package/dist/service-documents.d.ts.map +1 -0
- package/dist/service-documents.js +228 -0
- package/dist/service-invoice-artifacts.d.ts +246 -0
- package/dist/service-invoice-artifacts.d.ts.map +1 -0
- package/dist/service-invoice-artifacts.js +277 -0
- package/dist/service-invoice-core.d.ts +405 -0
- package/dist/service-invoice-core.d.ts.map +1 -0
- package/dist/service-invoice-core.js +290 -0
- package/dist/service-invoice-credit-notes.d.ts +973 -0
- package/dist/service-invoice-credit-notes.d.ts.map +1 -0
- package/dist/service-invoice-credit-notes.js +142 -0
- package/dist/service-invoice-from-booking.d.ts +41 -0
- package/dist/service-invoice-from-booking.d.ts.map +1 -0
- package/dist/service-invoice-from-booking.js +267 -0
- package/dist/service-invoice-line-items.d.ts +432 -0
- package/dist/service-invoice-line-items.d.ts.map +1 -0
- package/dist/service-invoice-line-items.js +102 -0
- package/dist/service-invoice-numbering.d.ts +227 -0
- package/dist/service-invoice-numbering.d.ts.map +1 -0
- package/dist/service-invoice-numbering.js +260 -0
- package/dist/service-invoice-payments.d.ts +673 -0
- package/dist/service-invoice-payments.d.ts.map +1 -0
- package/dist/service-invoice-payments.js +398 -0
- package/dist/service-invoices.d.ts +2501 -0
- package/dist/service-invoices.d.ts.map +1 -0
- package/dist/service-invoices.js +12 -0
- package/dist/service-issue.d.ts +207 -0
- package/dist/service-issue.d.ts.map +1 -0
- package/dist/service-issue.js +431 -0
- package/dist/service-payment-authorizations.d.ts +164 -0
- package/dist/service-payment-authorizations.d.ts.map +1 -0
- package/dist/service-payment-authorizations.js +227 -0
- package/dist/service-payment-instruments.d.ts +116 -0
- package/dist/service-payment-instruments.d.ts.map +1 -0
- package/dist/service-payment-instruments.js +99 -0
- package/dist/service-payment-processing.d.ts +676 -0
- package/dist/service-payment-processing.d.ts.map +1 -0
- package/dist/service-payment-processing.js +10 -0
- package/dist/service-payment-session-completion.d.ts +48 -0
- package/dist/service-payment-session-completion.d.ts.map +1 -0
- package/dist/service-payment-session-completion.js +238 -0
- package/dist/service-payment-sessions.d.ts +361 -0
- package/dist/service-payment-sessions.d.ts.map +1 -0
- package/dist/service-payment-sessions.js +280 -0
- package/dist/service-profitability.d.ts +114 -0
- package/dist/service-profitability.d.ts.map +1 -0
- package/dist/service-profitability.js +794 -0
- package/dist/service-public.d.ts +553 -0
- package/dist/service-public.d.ts.map +1 -0
- package/dist/service-public.js +583 -0
- package/dist/service-reference-data.d.ts +272 -0
- package/dist/service-reference-data.d.ts.map +1 -0
- package/dist/service-reference-data.js +280 -0
- package/dist/service-rendition-wait.d.ts +38 -0
- package/dist/service-rendition-wait.d.ts.map +1 -0
- package/dist/service-rendition-wait.js +67 -0
- package/dist/service-reports.d.ts +37 -0
- package/dist/service-reports.d.ts.map +1 -0
- package/dist/service-reports.js +62 -0
- package/dist/service-settlement.d.ts +46 -0
- package/dist/service-settlement.d.ts.map +1 -0
- package/dist/service-settlement.js +185 -0
- package/dist/service-shared.d.ts +541 -0
- package/dist/service-shared.d.ts.map +1 -0
- package/dist/service-shared.js +764 -0
- package/dist/service-supplier-invoices.d.ts +871 -0
- package/dist/service-supplier-invoices.d.ts.map +1 -0
- package/dist/service-supplier-invoices.js +744 -0
- package/dist/service-supplier-payments.d.ts +69 -0
- package/dist/service-supplier-payments.d.ts.map +1 -0
- package/dist/service-supplier-payments.js +136 -0
- package/dist/service-vouchers-migration.d.ts +44 -0
- package/dist/service-vouchers-migration.d.ts.map +1 -0
- package/dist/service-vouchers-migration.js +148 -0
- package/dist/service-vouchers.d.ts +157 -0
- package/dist/service-vouchers.d.ts.map +1 -0
- package/dist/service-vouchers.js +191 -0
- package/dist/service.d.ts +6490 -0
- package/dist/service.d.ts.map +1 -0
- package/dist/service.js +29 -0
- package/dist/validation-billing.d.ts +2 -0
- package/dist/validation-billing.d.ts.map +1 -0
- package/dist/validation-billing.js +1 -0
- package/dist/validation-payments.d.ts +2 -0
- package/dist/validation-payments.d.ts.map +1 -0
- package/dist/validation-payments.js +1 -0
- package/dist/validation-public.d.ts +2 -0
- package/dist/validation-public.d.ts.map +1 -0
- package/dist/validation-public.js +1 -0
- package/dist/validation-shared.d.ts +2 -0
- package/dist/validation-shared.d.ts.map +1 -0
- package/dist/validation-shared.js +1 -0
- package/dist/validation-vouchers.d.ts +2 -0
- package/dist/validation-vouchers.d.ts.map +1 -0
- package/dist/validation-vouchers.js +1 -0
- package/dist/validation.d.ts +2 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +1 -0
- package/package.json +121 -0
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import { bookingItems, bookings, bookingTravelers } from "@voyant-travel/bookings";
|
|
2
|
+
import { and, asc, desc, eq, gt, inArray } from "drizzle-orm";
|
|
3
|
+
import { buildBankTransferInstructions, defaultPaymentPlan, fallbackInvoiceNumber, lineDescription, normalizeExactAmountCents, OUTSTANDING_INVOICE_STATUSES, OUTSTANDING_SCHEDULE_STATUSES, resolveCheckoutIntent, resolveCheckoutSubject, resolveDocumentType, resolvePaymentSessionTarget, } from "./checkout-service-plan.js";
|
|
4
|
+
import { bookingPaymentSchedules, invoiceLineItems, invoiceNumberSeries, invoices, } from "./schema.js";
|
|
5
|
+
import { financeService } from "./service.js";
|
|
6
|
+
export { resolvePaymentSessionTarget, } from "./checkout-service-plan.js";
|
|
7
|
+
async function loadBookingContext(db, bookingId) {
|
|
8
|
+
const [booking] = await db.select().from(bookings).where(eq(bookings.id, bookingId)).limit(1);
|
|
9
|
+
if (!booking)
|
|
10
|
+
return null;
|
|
11
|
+
const [items, participants, schedules, outstandingInvoices] = await Promise.all([
|
|
12
|
+
db
|
|
13
|
+
.select()
|
|
14
|
+
.from(bookingItems)
|
|
15
|
+
.where(eq(bookingItems.bookingId, bookingId))
|
|
16
|
+
.orderBy(bookingItems.createdAt),
|
|
17
|
+
db
|
|
18
|
+
.select()
|
|
19
|
+
.from(bookingTravelers)
|
|
20
|
+
.where(eq(bookingTravelers.bookingId, bookingId))
|
|
21
|
+
.orderBy(desc(bookingTravelers.isPrimary), bookingTravelers.createdAt),
|
|
22
|
+
db
|
|
23
|
+
.select()
|
|
24
|
+
.from(bookingPaymentSchedules)
|
|
25
|
+
.where(and(eq(bookingPaymentSchedules.bookingId, bookingId), inArray(bookingPaymentSchedules.status, OUTSTANDING_SCHEDULE_STATUSES)))
|
|
26
|
+
.orderBy(asc(bookingPaymentSchedules.dueDate), asc(bookingPaymentSchedules.createdAt)),
|
|
27
|
+
db
|
|
28
|
+
.select()
|
|
29
|
+
.from(invoices)
|
|
30
|
+
.where(and(eq(invoices.bookingId, bookingId), inArray(invoices.status, OUTSTANDING_INVOICE_STATUSES), gt(invoices.balanceDueCents, 0)))
|
|
31
|
+
.orderBy(desc(invoices.createdAt)),
|
|
32
|
+
]);
|
|
33
|
+
return {
|
|
34
|
+
booking,
|
|
35
|
+
items,
|
|
36
|
+
participants,
|
|
37
|
+
schedules,
|
|
38
|
+
outstandingInvoices,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
async function ensurePaymentPlanIfNeeded(db, bookingId, existingSchedules, input, options) {
|
|
42
|
+
if (existingSchedules.length > 0 || !input.ensureDefaultPaymentPlan) {
|
|
43
|
+
return existingSchedules;
|
|
44
|
+
}
|
|
45
|
+
const created = await financeService.applyDefaultBookingPaymentPlan(db, bookingId, {
|
|
46
|
+
...defaultPaymentPlan(options),
|
|
47
|
+
...(input.paymentPlan ?? {}),
|
|
48
|
+
});
|
|
49
|
+
return created ?? [];
|
|
50
|
+
}
|
|
51
|
+
async function allocateDocumentNumber(db, bookingNumber, documentType, amountCents) {
|
|
52
|
+
const [series] = await db
|
|
53
|
+
.select()
|
|
54
|
+
.from(invoiceNumberSeries)
|
|
55
|
+
.where(eq(invoiceNumberSeries.scope, documentType))
|
|
56
|
+
.orderBy(desc(invoiceNumberSeries.active), asc(invoiceNumberSeries.createdAt))
|
|
57
|
+
.limit(1);
|
|
58
|
+
if (!series) {
|
|
59
|
+
return fallbackInvoiceNumber(bookingNumber, documentType, amountCents);
|
|
60
|
+
}
|
|
61
|
+
const allocated = await financeService.allocateInvoiceNumber(db, series.id);
|
|
62
|
+
if (allocated.status === "allocated") {
|
|
63
|
+
return allocated.formattedNumber;
|
|
64
|
+
}
|
|
65
|
+
return fallbackInvoiceNumber(bookingNumber, documentType, amountCents);
|
|
66
|
+
}
|
|
67
|
+
function pickSchedule(schedules, scheduleId) {
|
|
68
|
+
if (scheduleId) {
|
|
69
|
+
return schedules.find((schedule) => schedule.id === scheduleId) ?? null;
|
|
70
|
+
}
|
|
71
|
+
return schedules[0] ?? null;
|
|
72
|
+
}
|
|
73
|
+
function pickInvoice(outstandingInvoices, invoiceId) {
|
|
74
|
+
if (invoiceId) {
|
|
75
|
+
return outstandingInvoices.find((invoice) => invoice.id === invoiceId) ?? null;
|
|
76
|
+
}
|
|
77
|
+
return outstandingInvoices[0] ?? null;
|
|
78
|
+
}
|
|
79
|
+
export async function previewCheckoutCollection(db, bookingId, input, options = {}) {
|
|
80
|
+
const context = await loadBookingContext(db, bookingId);
|
|
81
|
+
if (!context)
|
|
82
|
+
return null;
|
|
83
|
+
const schedules = await ensurePaymentPlanIfNeeded(db, bookingId, context.schedules, input, options);
|
|
84
|
+
let paymentSessionTarget = resolvePaymentSessionTarget(input.method, input.stage, input.paymentSessionTarget, options);
|
|
85
|
+
let documentType = resolveDocumentType(input.method, paymentSessionTarget, options);
|
|
86
|
+
let selectedSchedule = pickSchedule(schedules, input.scheduleId);
|
|
87
|
+
let selectedInvoice = pickInvoice(context.outstandingInvoices, input.invoiceId);
|
|
88
|
+
const requestedAmountCents = normalizeExactAmountCents(input.amountCents);
|
|
89
|
+
let amountCents = 0;
|
|
90
|
+
if (requestedAmountCents !== null) {
|
|
91
|
+
amountCents = requestedAmountCents;
|
|
92
|
+
if (paymentSessionTarget === "schedule" &&
|
|
93
|
+
selectedSchedule &&
|
|
94
|
+
selectedSchedule.amountCents === requestedAmountCents) {
|
|
95
|
+
selectedInvoice = null;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
paymentSessionTarget = "invoice";
|
|
99
|
+
documentType = resolveDocumentType(input.method, paymentSessionTarget, options);
|
|
100
|
+
selectedInvoice =
|
|
101
|
+
selectedInvoice && selectedInvoice.balanceDueCents === requestedAmountCents
|
|
102
|
+
? selectedInvoice
|
|
103
|
+
: null;
|
|
104
|
+
selectedSchedule = null;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
else if (paymentSessionTarget === "invoice") {
|
|
108
|
+
amountCents =
|
|
109
|
+
selectedInvoice?.balanceDueCents ??
|
|
110
|
+
selectedSchedule?.amountCents ??
|
|
111
|
+
context.booking.sellAmountCents ??
|
|
112
|
+
0;
|
|
113
|
+
}
|
|
114
|
+
else if (paymentSessionTarget === "schedule") {
|
|
115
|
+
amountCents = selectedSchedule?.amountCents ?? context.booking.sellAmountCents ?? 0;
|
|
116
|
+
}
|
|
117
|
+
let recommendedAction = "none";
|
|
118
|
+
if (input.method === "bank_transfer") {
|
|
119
|
+
recommendedAction = "create_bank_transfer_document";
|
|
120
|
+
}
|
|
121
|
+
else if (paymentSessionTarget === "invoice") {
|
|
122
|
+
recommendedAction = selectedInvoice
|
|
123
|
+
? "create_payment_session"
|
|
124
|
+
: "create_invoice_then_payment_session";
|
|
125
|
+
}
|
|
126
|
+
else if (paymentSessionTarget === "schedule") {
|
|
127
|
+
recommendedAction = "create_payment_session";
|
|
128
|
+
}
|
|
129
|
+
return {
|
|
130
|
+
bookingId,
|
|
131
|
+
method: input.method,
|
|
132
|
+
stage: input.stage,
|
|
133
|
+
paymentSessionTarget,
|
|
134
|
+
documentType,
|
|
135
|
+
willCreateDefaultPaymentPlan: context.schedules.length === 0 && schedules.length > 0,
|
|
136
|
+
selectedSchedule,
|
|
137
|
+
selectedInvoice,
|
|
138
|
+
amountCents,
|
|
139
|
+
currency: context.booking.sellCurrency,
|
|
140
|
+
recommendedAction,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
async function createCollectionInvoice(db, context, plan, notes) {
|
|
144
|
+
const amountCents = plan.amountCents;
|
|
145
|
+
const issueDate = new Date().toISOString().slice(0, 10);
|
|
146
|
+
const dueDate = plan.selectedSchedule?.dueDate ?? issueDate;
|
|
147
|
+
const documentType = plan.documentType ?? "invoice";
|
|
148
|
+
const invoiceNumber = await allocateDocumentNumber(db, context.booking.bookingNumber, documentType, amountCents);
|
|
149
|
+
const [invoice] = await db
|
|
150
|
+
.insert(invoices)
|
|
151
|
+
.values({
|
|
152
|
+
invoiceNumber,
|
|
153
|
+
bookingId: context.booking.id,
|
|
154
|
+
personId: context.booking.personId,
|
|
155
|
+
organizationId: context.booking.organizationId,
|
|
156
|
+
invoiceType: documentType,
|
|
157
|
+
status: "issued",
|
|
158
|
+
currency: context.booking.sellCurrency,
|
|
159
|
+
baseCurrency: context.booking.baseCurrency,
|
|
160
|
+
fxRateSetId: null,
|
|
161
|
+
subtotalCents: amountCents,
|
|
162
|
+
baseSubtotalCents: context.booking.baseSellAmountCents,
|
|
163
|
+
taxCents: 0,
|
|
164
|
+
baseTaxCents: null,
|
|
165
|
+
totalCents: amountCents,
|
|
166
|
+
baseTotalCents: context.booking.baseSellAmountCents,
|
|
167
|
+
paidCents: 0,
|
|
168
|
+
basePaidCents: 0,
|
|
169
|
+
balanceDueCents: amountCents,
|
|
170
|
+
baseBalanceDueCents: context.booking.baseSellAmountCents,
|
|
171
|
+
commissionAmountCents: null,
|
|
172
|
+
issueDate,
|
|
173
|
+
dueDate,
|
|
174
|
+
notes: notes ?? plan.selectedSchedule?.notes ?? null,
|
|
175
|
+
})
|
|
176
|
+
.returning();
|
|
177
|
+
if (!invoice) {
|
|
178
|
+
throw new Error("Failed to create collection invoice");
|
|
179
|
+
}
|
|
180
|
+
await db.insert(invoiceLineItems).values({
|
|
181
|
+
invoiceId: invoice.id,
|
|
182
|
+
bookingItemId: plan.selectedSchedule?.bookingItemId ?? null,
|
|
183
|
+
description: lineDescription(context.booking, plan.selectedSchedule, plan.stage),
|
|
184
|
+
quantity: 1,
|
|
185
|
+
unitPriceCents: amountCents,
|
|
186
|
+
totalCents: amountCents,
|
|
187
|
+
taxRate: null,
|
|
188
|
+
sortOrder: 0,
|
|
189
|
+
});
|
|
190
|
+
return invoice;
|
|
191
|
+
}
|
|
192
|
+
export async function initiateCheckoutCollection(db, bookingId, input, options = {}, runtime = {}) {
|
|
193
|
+
const context = await loadBookingContext(db, bookingId);
|
|
194
|
+
if (!context)
|
|
195
|
+
return null;
|
|
196
|
+
const plan = await previewCheckoutCollection(db, bookingId, input, options);
|
|
197
|
+
if (!plan)
|
|
198
|
+
return null;
|
|
199
|
+
if (plan.amountCents <= 0) {
|
|
200
|
+
throw new Error("No outstanding amount available for collection");
|
|
201
|
+
}
|
|
202
|
+
let invoice = plan.selectedInvoice;
|
|
203
|
+
let paymentSession = null;
|
|
204
|
+
let invoiceNotification = null;
|
|
205
|
+
let paymentSessionNotification = null;
|
|
206
|
+
let bankTransferInstructions = null;
|
|
207
|
+
let providerStart = null;
|
|
208
|
+
if (input.method === "bank_transfer") {
|
|
209
|
+
invoice = await createCollectionInvoice(db, context, plan, input.notes ?? null);
|
|
210
|
+
bankTransferInstructions = buildBankTransferInstructions(invoice, runtime.bankTransferDetails ?? null, input.notes ?? null);
|
|
211
|
+
if (runtime.notificationDispatcher?.sendInvoiceNotification && input.invoiceNotification) {
|
|
212
|
+
invoiceNotification = await runtime.notificationDispatcher.sendInvoiceNotification(db, invoice.id, withPaymentLinkBaseUrl(input.invoiceNotification, runtime.publicCheckoutBaseUrl));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
else if (plan.paymentSessionTarget === "invoice") {
|
|
216
|
+
if (!invoice) {
|
|
217
|
+
invoice = await createCollectionInvoice(db, context, { ...plan, documentType: "invoice" }, input.notes ?? null);
|
|
218
|
+
}
|
|
219
|
+
paymentSession = await financeService.createPaymentSessionFromInvoice(db, invoice.id, {
|
|
220
|
+
...(input.paymentSession ?? {}),
|
|
221
|
+
notes: input.notes ?? input.paymentSession?.notes ?? null,
|
|
222
|
+
});
|
|
223
|
+
if (!paymentSession) {
|
|
224
|
+
throw new Error("Failed to create payment session from invoice");
|
|
225
|
+
}
|
|
226
|
+
if (runtime.notificationDispatcher?.sendInvoiceNotification && input.invoiceNotification) {
|
|
227
|
+
invoiceNotification = await runtime.notificationDispatcher.sendInvoiceNotification(db, invoice.id, withPaymentLinkBaseUrl(input.invoiceNotification, runtime.publicCheckoutBaseUrl));
|
|
228
|
+
}
|
|
229
|
+
if (runtime.notificationDispatcher?.sendPaymentSessionNotification &&
|
|
230
|
+
input.paymentSessionNotification) {
|
|
231
|
+
paymentSessionNotification =
|
|
232
|
+
await runtime.notificationDispatcher.sendPaymentSessionNotification(db, paymentSession.id, withPaymentLinkBaseUrl(input.paymentSessionNotification, runtime.publicCheckoutBaseUrl));
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
if (!plan.selectedSchedule) {
|
|
237
|
+
throw new Error("No outstanding payment schedule available for collection");
|
|
238
|
+
}
|
|
239
|
+
paymentSession = await financeService.createPaymentSessionFromBookingSchedule(db, plan.selectedSchedule.id, {
|
|
240
|
+
...(input.paymentSession ?? {}),
|
|
241
|
+
notes: input.notes ?? input.paymentSession?.notes ?? null,
|
|
242
|
+
});
|
|
243
|
+
if (!paymentSession) {
|
|
244
|
+
throw new Error("Failed to create payment session from booking schedule");
|
|
245
|
+
}
|
|
246
|
+
if (runtime.notificationDispatcher?.sendPaymentSessionNotification &&
|
|
247
|
+
input.paymentSessionNotification) {
|
|
248
|
+
paymentSessionNotification =
|
|
249
|
+
await runtime.notificationDispatcher.sendPaymentSessionNotification(db, paymentSession.id, withPaymentLinkBaseUrl(input.paymentSessionNotification, runtime.publicCheckoutBaseUrl));
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (input.startProvider) {
|
|
253
|
+
if (input.method !== "card") {
|
|
254
|
+
throw new Error("Provider start is only available for card collections");
|
|
255
|
+
}
|
|
256
|
+
if (!paymentSession) {
|
|
257
|
+
throw new Error("No payment session available for provider start");
|
|
258
|
+
}
|
|
259
|
+
const starter = runtime.paymentStarters?.[input.startProvider.provider];
|
|
260
|
+
if (!starter) {
|
|
261
|
+
throw new Error(`Payment provider "${input.startProvider.provider}" is not configured`);
|
|
262
|
+
}
|
|
263
|
+
providerStart = await starter({
|
|
264
|
+
db,
|
|
265
|
+
bookingId,
|
|
266
|
+
plan,
|
|
267
|
+
invoice: invoice ?? null,
|
|
268
|
+
paymentSession,
|
|
269
|
+
input,
|
|
270
|
+
startProvider: input.startProvider,
|
|
271
|
+
bindings: runtime.bindings ?? {},
|
|
272
|
+
});
|
|
273
|
+
if (providerStart.paymentSessionId !== paymentSession.id) {
|
|
274
|
+
const updatedSession = await financeService.getPaymentSessionById(db, providerStart.paymentSessionId);
|
|
275
|
+
paymentSession = updatedSession ?? paymentSession;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
const updatedSession = await financeService.getPaymentSessionById(db, paymentSession.id);
|
|
279
|
+
paymentSession = updatedSession ?? paymentSession;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return {
|
|
283
|
+
plan,
|
|
284
|
+
invoice: invoice ?? null,
|
|
285
|
+
paymentSession,
|
|
286
|
+
invoiceNotification,
|
|
287
|
+
paymentSessionNotification,
|
|
288
|
+
bankTransferInstructions,
|
|
289
|
+
providerStart,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
function withPaymentLinkBaseUrl(input, publicCheckoutBaseUrl) {
|
|
293
|
+
if (input.paymentLinkBaseUrl || !publicCheckoutBaseUrl)
|
|
294
|
+
return input;
|
|
295
|
+
return { ...input, paymentLinkBaseUrl: publicCheckoutBaseUrl };
|
|
296
|
+
}
|
|
297
|
+
export async function bootstrapCheckoutCollection(db, input, options = {}, runtime = {}) {
|
|
298
|
+
const subject = resolveCheckoutSubject(input);
|
|
299
|
+
const initiated = await initiateCheckoutCollection(db, subject.bookingId, {
|
|
300
|
+
method: input.method,
|
|
301
|
+
stage: input.stage,
|
|
302
|
+
scheduleId: input.scheduleId,
|
|
303
|
+
invoiceId: input.invoiceId,
|
|
304
|
+
amountCents: input.amountCents,
|
|
305
|
+
ensureDefaultPaymentPlan: input.ensureDefaultPaymentPlan,
|
|
306
|
+
paymentSessionTarget: input.paymentSessionTarget,
|
|
307
|
+
paymentPlan: input.paymentPlan,
|
|
308
|
+
paymentSession: input.paymentSession,
|
|
309
|
+
paymentSessionNotification: input.paymentSessionNotification,
|
|
310
|
+
invoiceNotification: input.invoiceNotification,
|
|
311
|
+
startProvider: input.startProvider,
|
|
312
|
+
notes: input.notes,
|
|
313
|
+
}, options, runtime);
|
|
314
|
+
if (!initiated) {
|
|
315
|
+
return null;
|
|
316
|
+
}
|
|
317
|
+
return {
|
|
318
|
+
bookingId: subject.bookingId,
|
|
319
|
+
sessionId: subject.sessionId,
|
|
320
|
+
sourceType: subject.sourceType,
|
|
321
|
+
intent: resolveCheckoutIntent(input),
|
|
322
|
+
...initiated,
|
|
323
|
+
};
|
|
324
|
+
}
|