@voyantjs/checkout 0.1.0
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 +109 -0
- package/README.md +24 -0
- package/dist/src/index.d.ts +6 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +3 -0
- package/dist/src/routes.d.ts +355 -0
- package/dist/src/routes.d.ts.map +1 -0
- package/dist/src/routes.js +37 -0
- package/dist/src/service.d.ts +42 -0
- package/dist/src/service.d.ts.map +1 -0
- package/dist/src/service.js +276 -0
- package/dist/src/validation.d.ts +193 -0
- package/dist/src/validation.d.ts.map +1 -0
- package/dist/src/validation.js +25 -0
- package/dist/tests/unit/service.test.d.ts +2 -0
- package/dist/tests/unit/service.test.d.ts.map +1 -0
- package/dist/tests/unit/service.test.js +18 -0
- package/package.json +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# Functional Source License, Version 1.1, Apache 2.0 Future License
|
|
2
|
+
|
|
3
|
+
## Abbreviation
|
|
4
|
+
|
|
5
|
+
FSL-1.1-Apache-2.0
|
|
6
|
+
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 PixelMakers Studio SRL
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly
|
|
28
|
+
display and redistribute the Software for any Permitted Purpose identified
|
|
29
|
+
below.
|
|
30
|
+
|
|
31
|
+
### Permitted Purpose
|
|
32
|
+
|
|
33
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing
|
|
34
|
+
Use means making the Software available to others in a commercial product or
|
|
35
|
+
service that:
|
|
36
|
+
|
|
37
|
+
1. substitutes for the Software;
|
|
38
|
+
|
|
39
|
+
2. substitutes for any other product or service we offer using the Software
|
|
40
|
+
that exists as of the date we make the Software available; or
|
|
41
|
+
|
|
42
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
43
|
+
|
|
44
|
+
Permitted Purposes specifically include using the Software:
|
|
45
|
+
|
|
46
|
+
1. for your internal use and access;
|
|
47
|
+
|
|
48
|
+
2. for non-commercial education;
|
|
49
|
+
|
|
50
|
+
3. for non-commercial research; and
|
|
51
|
+
|
|
52
|
+
4. in connection with professional services that you provide to a licensee
|
|
53
|
+
using the Software in accordance with these Terms and Conditions.
|
|
54
|
+
|
|
55
|
+
### Patents
|
|
56
|
+
|
|
57
|
+
To the extent your use for a Permitted Purpose would necessarily infringe
|
|
58
|
+
our patents, the license grant above includes a license under our patents.
|
|
59
|
+
If you make a claim against any party that the Software infringes or
|
|
60
|
+
contributes to the infringement of any patent, then your patent license to
|
|
61
|
+
the Software ends immediately.
|
|
62
|
+
|
|
63
|
+
### Redistribution
|
|
64
|
+
|
|
65
|
+
The Terms and Conditions apply to all copies, modifications and derivatives
|
|
66
|
+
of the Software.
|
|
67
|
+
|
|
68
|
+
If you redistribute any copies, modifications or derivatives of the
|
|
69
|
+
Software, you must include a copy of or a link to these Terms and Conditions
|
|
70
|
+
and not remove any copyright notices provided in or with the Software.
|
|
71
|
+
|
|
72
|
+
### Disclaimer
|
|
73
|
+
|
|
74
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS
|
|
75
|
+
OR IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A
|
|
76
|
+
PARTICULAR PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
77
|
+
|
|
78
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO
|
|
79
|
+
THE SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL
|
|
80
|
+
DAMAGES, EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
81
|
+
|
|
82
|
+
### Trademarks
|
|
83
|
+
|
|
84
|
+
Except for displaying the License Details and identifying us as the origin
|
|
85
|
+
of the Software, you have no right under these Terms and Conditions to use
|
|
86
|
+
our trademarks, trade names, service marks or product names.
|
|
87
|
+
|
|
88
|
+
---
|
|
89
|
+
|
|
90
|
+
## Grant of Future License
|
|
91
|
+
|
|
92
|
+
We hereby irrevocably grant you an additional license to use the Software
|
|
93
|
+
under the Apache License, Version 2.0 that is effective on the second
|
|
94
|
+
anniversary of the date we make the Software available. On or after that
|
|
95
|
+
date, you may use the Software under the Apache License, Version 2.0, in
|
|
96
|
+
which case the following will apply:
|
|
97
|
+
|
|
98
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
|
99
|
+
use this file except in compliance with the License.
|
|
100
|
+
|
|
101
|
+
You may obtain a copy of the License at
|
|
102
|
+
|
|
103
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
104
|
+
|
|
105
|
+
Unless required by applicable law or agreed to in writing, software
|
|
106
|
+
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
107
|
+
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
108
|
+
License for the specific language governing permissions and limitations
|
|
109
|
+
under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# @voyantjs/checkout
|
|
2
|
+
|
|
3
|
+
Optional checkout and collection orchestration for Voyant.
|
|
4
|
+
|
|
5
|
+
This package sits above `@voyantjs/finance` and `@voyantjs/notifications`. It does not implement payment providers. Instead, it decides which booking schedule or invoice to collect, creates the right finance record, and optionally sends the corresponding notification.
|
|
6
|
+
|
|
7
|
+
## What it does
|
|
8
|
+
|
|
9
|
+
- previews a booking collection plan
|
|
10
|
+
- creates bank-transfer collection documents (`proforma` by default)
|
|
11
|
+
- creates card collection `payment_sessions`
|
|
12
|
+
- supports schedule-backed or invoice-backed card collection
|
|
13
|
+
- optionally sends invoice or payment-session notifications
|
|
14
|
+
|
|
15
|
+
## Routes
|
|
16
|
+
|
|
17
|
+
- `POST /v1/checkout/bookings/:bookingId/collection-plan`
|
|
18
|
+
- `POST /v1/checkout/bookings/:bookingId/initiate-collection`
|
|
19
|
+
|
|
20
|
+
## Notes
|
|
21
|
+
|
|
22
|
+
- payment-provider plugins like Netopia remain optional
|
|
23
|
+
- email-provider choice remains app-owned
|
|
24
|
+
- projects can override the default collection policy when mounting the routes
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { createCheckoutRoutes } from "./routes.js";
|
|
2
|
+
export { initiateCheckoutCollection, previewCheckoutCollection, resolvePaymentSessionTarget, } from "./service.js";
|
|
3
|
+
export type { CheckoutCollectionPlan, CheckoutPolicyOptions, InitiatedCheckoutCollection, } from "./service.js";
|
|
4
|
+
export { checkoutCollectionMethodSchema, checkoutCollectionStageSchema, checkoutInvoiceDocumentTypeSchema, checkoutPaymentSessionTargetSchema, initiateCheckoutCollectionSchema, previewCheckoutCollectionSchema, } from "./validation.js";
|
|
5
|
+
export type { InitiateCheckoutCollectionInput, PreviewCheckoutCollectionInput, } from "./validation.js";
|
|
6
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAClD,OAAO,EACL,0BAA0B,EAC1B,yBAAyB,EACzB,2BAA2B,GAC5B,MAAM,cAAc,CAAA;AACrB,YAAY,EACV,sBAAsB,EACtB,qBAAqB,EACrB,2BAA2B,GAC5B,MAAM,cAAc,CAAA;AACrB,OAAO,EACL,8BAA8B,EAC9B,6BAA6B,EAC7B,iCAAiC,EACjC,kCAAkC,EAClC,gCAAgC,EAChC,+BAA+B,GAChC,MAAM,iBAAiB,CAAA;AACxB,YAAY,EACV,+BAA+B,EAC/B,8BAA8B,GAC/B,MAAM,iBAAiB,CAAA"}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { createCheckoutRoutes } from "./routes.js";
|
|
2
|
+
export { initiateCheckoutCollection, previewCheckoutCollection, resolvePaymentSessionTarget, } from "./service.js";
|
|
3
|
+
export { checkoutCollectionMethodSchema, checkoutCollectionStageSchema, checkoutInvoiceDocumentTypeSchema, checkoutPaymentSessionTargetSchema, initiateCheckoutCollectionSchema, previewCheckoutCollectionSchema, } from "./validation.js";
|
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import type { NotificationProvider } from "@voyantjs/notifications";
|
|
2
|
+
import type { PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
|
3
|
+
import { type CheckoutPolicyOptions } from "./service.js";
|
|
4
|
+
type Env = {
|
|
5
|
+
Bindings: Record<string, unknown>;
|
|
6
|
+
Variables: {
|
|
7
|
+
db: PostgresJsDatabase;
|
|
8
|
+
userId?: string;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
export declare function createCheckoutRoutes(options?: {
|
|
12
|
+
policy?: CheckoutPolicyOptions;
|
|
13
|
+
providers?: ReadonlyArray<NotificationProvider>;
|
|
14
|
+
resolveProviders?: (bindings: Record<string, unknown>) => ReadonlyArray<NotificationProvider>;
|
|
15
|
+
}): import("hono/hono-base").HonoBase<Env, {
|
|
16
|
+
"/v1/checkout/bookings/:bookingId/collection-plan": {
|
|
17
|
+
$post: {
|
|
18
|
+
input: {
|
|
19
|
+
param: {
|
|
20
|
+
bookingId: string;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
output: {
|
|
24
|
+
error: string;
|
|
25
|
+
};
|
|
26
|
+
outputFormat: "json";
|
|
27
|
+
status: 404;
|
|
28
|
+
} | {
|
|
29
|
+
input: {
|
|
30
|
+
param: {
|
|
31
|
+
bookingId: string;
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
output: {
|
|
35
|
+
data: {
|
|
36
|
+
bookingId: string;
|
|
37
|
+
method: "card" | "bank_transfer";
|
|
38
|
+
stage: "initial" | "reminder" | "manual";
|
|
39
|
+
paymentSessionTarget: "schedule" | "invoice" | null;
|
|
40
|
+
documentType: "proforma" | "invoice" | null;
|
|
41
|
+
willCreateDefaultPaymentPlan: boolean;
|
|
42
|
+
selectedSchedule: {
|
|
43
|
+
id: string;
|
|
44
|
+
status: "cancelled" | "expired" | "pending" | "paid" | "due" | "waived";
|
|
45
|
+
notes: string | null;
|
|
46
|
+
createdAt: string;
|
|
47
|
+
updatedAt: string;
|
|
48
|
+
bookingId: string;
|
|
49
|
+
bookingItemId: string | null;
|
|
50
|
+
currency: string;
|
|
51
|
+
dueDate: string;
|
|
52
|
+
scheduleType: "other" | "deposit" | "installment" | "balance" | "hold";
|
|
53
|
+
amountCents: number;
|
|
54
|
+
} | null;
|
|
55
|
+
selectedInvoice: {
|
|
56
|
+
id: string;
|
|
57
|
+
status: "draft" | "sent" | "partially_paid" | "paid" | "overdue" | "void";
|
|
58
|
+
notes: string | null;
|
|
59
|
+
createdAt: string;
|
|
60
|
+
updatedAt: string;
|
|
61
|
+
personId: string | null;
|
|
62
|
+
organizationId: string | null;
|
|
63
|
+
baseCurrency: string | null;
|
|
64
|
+
bookingId: string;
|
|
65
|
+
invoiceNumber: string;
|
|
66
|
+
invoiceType: "invoice" | "proforma" | "credit_note";
|
|
67
|
+
seriesId: string | null;
|
|
68
|
+
sequence: number | null;
|
|
69
|
+
templateId: string | null;
|
|
70
|
+
taxRegimeId: string | null;
|
|
71
|
+
language: string | null;
|
|
72
|
+
currency: string;
|
|
73
|
+
fxRateSetId: string | null;
|
|
74
|
+
subtotalCents: number;
|
|
75
|
+
baseSubtotalCents: number | null;
|
|
76
|
+
taxCents: number;
|
|
77
|
+
baseTaxCents: number | null;
|
|
78
|
+
totalCents: number;
|
|
79
|
+
baseTotalCents: number | null;
|
|
80
|
+
paidCents: number;
|
|
81
|
+
basePaidCents: number | null;
|
|
82
|
+
balanceDueCents: number;
|
|
83
|
+
baseBalanceDueCents: number | null;
|
|
84
|
+
commissionPercent: number | null;
|
|
85
|
+
commissionAmountCents: number | null;
|
|
86
|
+
issueDate: string;
|
|
87
|
+
dueDate: string;
|
|
88
|
+
} | null;
|
|
89
|
+
amountCents: number;
|
|
90
|
+
currency: string;
|
|
91
|
+
recommendedAction: "create_bank_transfer_document" | "create_payment_session" | "create_invoice_then_payment_session" | "none";
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
outputFormat: "json";
|
|
95
|
+
status: import("hono/utils/http-status").ContentfulStatusCode;
|
|
96
|
+
} | {
|
|
97
|
+
input: {
|
|
98
|
+
param: {
|
|
99
|
+
bookingId: string;
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
output: {
|
|
103
|
+
error: string;
|
|
104
|
+
};
|
|
105
|
+
outputFormat: "json";
|
|
106
|
+
status: 400;
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
} & {
|
|
110
|
+
"/v1/checkout/bookings/:bookingId/initiate-collection": {
|
|
111
|
+
$post: {
|
|
112
|
+
input: {
|
|
113
|
+
param: {
|
|
114
|
+
bookingId: string;
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
output: {
|
|
118
|
+
error: string;
|
|
119
|
+
};
|
|
120
|
+
outputFormat: "json";
|
|
121
|
+
status: 404;
|
|
122
|
+
} | {
|
|
123
|
+
input: {
|
|
124
|
+
param: {
|
|
125
|
+
bookingId: string;
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
output: {
|
|
129
|
+
data: {
|
|
130
|
+
plan: {
|
|
131
|
+
bookingId: string;
|
|
132
|
+
method: "card" | "bank_transfer";
|
|
133
|
+
stage: "initial" | "reminder" | "manual";
|
|
134
|
+
paymentSessionTarget: "schedule" | "invoice" | null;
|
|
135
|
+
documentType: "proforma" | "invoice" | null;
|
|
136
|
+
willCreateDefaultPaymentPlan: boolean;
|
|
137
|
+
selectedSchedule: {
|
|
138
|
+
id: string;
|
|
139
|
+
status: "cancelled" | "expired" | "pending" | "paid" | "due" | "waived";
|
|
140
|
+
notes: string | null;
|
|
141
|
+
createdAt: string;
|
|
142
|
+
updatedAt: string;
|
|
143
|
+
bookingId: string;
|
|
144
|
+
bookingItemId: string | null;
|
|
145
|
+
currency: string;
|
|
146
|
+
dueDate: string;
|
|
147
|
+
scheduleType: "other" | "deposit" | "installment" | "balance" | "hold";
|
|
148
|
+
amountCents: number;
|
|
149
|
+
} | null;
|
|
150
|
+
selectedInvoice: {
|
|
151
|
+
id: string;
|
|
152
|
+
status: "draft" | "sent" | "partially_paid" | "paid" | "overdue" | "void";
|
|
153
|
+
notes: string | null;
|
|
154
|
+
createdAt: string;
|
|
155
|
+
updatedAt: string;
|
|
156
|
+
personId: string | null;
|
|
157
|
+
organizationId: string | null;
|
|
158
|
+
baseCurrency: string | null;
|
|
159
|
+
bookingId: string;
|
|
160
|
+
invoiceNumber: string;
|
|
161
|
+
invoiceType: "invoice" | "proforma" | "credit_note";
|
|
162
|
+
seriesId: string | null;
|
|
163
|
+
sequence: number | null;
|
|
164
|
+
templateId: string | null;
|
|
165
|
+
taxRegimeId: string | null;
|
|
166
|
+
language: string | null;
|
|
167
|
+
currency: string;
|
|
168
|
+
fxRateSetId: string | null;
|
|
169
|
+
subtotalCents: number;
|
|
170
|
+
baseSubtotalCents: number | null;
|
|
171
|
+
taxCents: number;
|
|
172
|
+
baseTaxCents: number | null;
|
|
173
|
+
totalCents: number;
|
|
174
|
+
baseTotalCents: number | null;
|
|
175
|
+
paidCents: number;
|
|
176
|
+
basePaidCents: number | null;
|
|
177
|
+
balanceDueCents: number;
|
|
178
|
+
baseBalanceDueCents: number | null;
|
|
179
|
+
commissionPercent: number | null;
|
|
180
|
+
commissionAmountCents: number | null;
|
|
181
|
+
issueDate: string;
|
|
182
|
+
dueDate: string;
|
|
183
|
+
} | null;
|
|
184
|
+
amountCents: number;
|
|
185
|
+
currency: string;
|
|
186
|
+
recommendedAction: "create_bank_transfer_document" | "create_payment_session" | "create_invoice_then_payment_session" | "none";
|
|
187
|
+
};
|
|
188
|
+
invoice: {
|
|
189
|
+
id: string;
|
|
190
|
+
status: "draft" | "sent" | "partially_paid" | "paid" | "overdue" | "void";
|
|
191
|
+
notes: string | null;
|
|
192
|
+
createdAt: string;
|
|
193
|
+
updatedAt: string;
|
|
194
|
+
personId: string | null;
|
|
195
|
+
organizationId: string | null;
|
|
196
|
+
baseCurrency: string | null;
|
|
197
|
+
bookingId: string;
|
|
198
|
+
invoiceNumber: string;
|
|
199
|
+
invoiceType: "invoice" | "proforma" | "credit_note";
|
|
200
|
+
seriesId: string | null;
|
|
201
|
+
sequence: number | null;
|
|
202
|
+
templateId: string | null;
|
|
203
|
+
taxRegimeId: string | null;
|
|
204
|
+
language: string | null;
|
|
205
|
+
currency: string;
|
|
206
|
+
fxRateSetId: string | null;
|
|
207
|
+
subtotalCents: number;
|
|
208
|
+
baseSubtotalCents: number | null;
|
|
209
|
+
taxCents: number;
|
|
210
|
+
baseTaxCents: number | null;
|
|
211
|
+
totalCents: number;
|
|
212
|
+
baseTotalCents: number | null;
|
|
213
|
+
paidCents: number;
|
|
214
|
+
basePaidCents: number | null;
|
|
215
|
+
balanceDueCents: number;
|
|
216
|
+
baseBalanceDueCents: number | null;
|
|
217
|
+
commissionPercent: number | null;
|
|
218
|
+
commissionAmountCents: number | null;
|
|
219
|
+
issueDate: string;
|
|
220
|
+
dueDate: string;
|
|
221
|
+
} | null;
|
|
222
|
+
paymentSession: {
|
|
223
|
+
id: string;
|
|
224
|
+
status: "cancelled" | "expired" | "pending" | "failed" | "paid" | "requires_redirect" | "processing" | "authorized";
|
|
225
|
+
notes: string | null;
|
|
226
|
+
createdAt: string;
|
|
227
|
+
updatedAt: string;
|
|
228
|
+
expiredAt: string | null;
|
|
229
|
+
cancelledAt: string | null;
|
|
230
|
+
completedAt: string | null;
|
|
231
|
+
bookingId: string | null;
|
|
232
|
+
metadata: {
|
|
233
|
+
[x: string]: import("hono/utils/types").JSONValue;
|
|
234
|
+
} | null;
|
|
235
|
+
expiresAt: string | null;
|
|
236
|
+
provider: string | null;
|
|
237
|
+
targetType: "other" | "booking" | "order" | "invoice" | "booking_payment_schedule" | "booking_guarantee";
|
|
238
|
+
targetId: string | null;
|
|
239
|
+
orderId: string | null;
|
|
240
|
+
currency: string;
|
|
241
|
+
invoiceId: string | null;
|
|
242
|
+
amountCents: number;
|
|
243
|
+
bookingPaymentScheduleId: string | null;
|
|
244
|
+
paymentInstrumentId: string | null;
|
|
245
|
+
bookingGuaranteeId: string | null;
|
|
246
|
+
paymentAuthorizationId: string | null;
|
|
247
|
+
paymentCaptureId: string | null;
|
|
248
|
+
paymentMethod: "other" | "voucher" | "wallet" | "bank_transfer" | "credit_card" | "debit_card" | "cash" | "cheque" | "direct_bill" | null;
|
|
249
|
+
paymentId: string | null;
|
|
250
|
+
providerSessionId: string | null;
|
|
251
|
+
providerPaymentId: string | null;
|
|
252
|
+
externalReference: string | null;
|
|
253
|
+
idempotencyKey: string | null;
|
|
254
|
+
clientReference: string | null;
|
|
255
|
+
payerPersonId: string | null;
|
|
256
|
+
payerOrganizationId: string | null;
|
|
257
|
+
payerEmail: string | null;
|
|
258
|
+
payerName: string | null;
|
|
259
|
+
redirectUrl: string | null;
|
|
260
|
+
returnUrl: string | null;
|
|
261
|
+
cancelUrl: string | null;
|
|
262
|
+
callbackUrl: string | null;
|
|
263
|
+
failedAt: string | null;
|
|
264
|
+
failureCode: string | null;
|
|
265
|
+
failureMessage: string | null;
|
|
266
|
+
providerPayload: {
|
|
267
|
+
[x: string]: import("hono/utils/types").JSONValue;
|
|
268
|
+
} | null;
|
|
269
|
+
} | null;
|
|
270
|
+
invoiceNotification: {
|
|
271
|
+
id: string;
|
|
272
|
+
status: "cancelled" | "pending" | "failed" | "sent";
|
|
273
|
+
createdAt: string;
|
|
274
|
+
updatedAt: string;
|
|
275
|
+
personId: string | null;
|
|
276
|
+
organizationId: string | null;
|
|
277
|
+
bookingId: string | null;
|
|
278
|
+
metadata: {
|
|
279
|
+
[x: string]: import("hono/utils/types").JSONValue;
|
|
280
|
+
} | null;
|
|
281
|
+
channel: "email" | "sms";
|
|
282
|
+
provider: string;
|
|
283
|
+
targetType: "other" | "booking" | "invoice" | "booking_payment_schedule" | "booking_guarantee" | "payment_session" | "person" | "organization";
|
|
284
|
+
targetId: string | null;
|
|
285
|
+
templateId: string | null;
|
|
286
|
+
invoiceId: string | null;
|
|
287
|
+
failedAt: string | null;
|
|
288
|
+
errorMessage: string | null;
|
|
289
|
+
fromAddress: string | null;
|
|
290
|
+
templateSlug: string | null;
|
|
291
|
+
paymentSessionId: string | null;
|
|
292
|
+
providerMessageId: string | null;
|
|
293
|
+
toAddress: string;
|
|
294
|
+
subject: string | null;
|
|
295
|
+
htmlBody: string | null;
|
|
296
|
+
textBody: string | null;
|
|
297
|
+
payloadData: {
|
|
298
|
+
[x: string]: import("hono/utils/types").JSONValue;
|
|
299
|
+
} | null;
|
|
300
|
+
scheduledFor: string | null;
|
|
301
|
+
sentAt: string | null;
|
|
302
|
+
} | null;
|
|
303
|
+
paymentSessionNotification: {
|
|
304
|
+
id: string;
|
|
305
|
+
status: "cancelled" | "pending" | "failed" | "sent";
|
|
306
|
+
createdAt: string;
|
|
307
|
+
updatedAt: string;
|
|
308
|
+
personId: string | null;
|
|
309
|
+
organizationId: string | null;
|
|
310
|
+
bookingId: string | null;
|
|
311
|
+
metadata: {
|
|
312
|
+
[x: string]: import("hono/utils/types").JSONValue;
|
|
313
|
+
} | null;
|
|
314
|
+
channel: "email" | "sms";
|
|
315
|
+
provider: string;
|
|
316
|
+
targetType: "other" | "booking" | "invoice" | "booking_payment_schedule" | "booking_guarantee" | "payment_session" | "person" | "organization";
|
|
317
|
+
targetId: string | null;
|
|
318
|
+
templateId: string | null;
|
|
319
|
+
invoiceId: string | null;
|
|
320
|
+
failedAt: string | null;
|
|
321
|
+
errorMessage: string | null;
|
|
322
|
+
fromAddress: string | null;
|
|
323
|
+
templateSlug: string | null;
|
|
324
|
+
paymentSessionId: string | null;
|
|
325
|
+
providerMessageId: string | null;
|
|
326
|
+
toAddress: string;
|
|
327
|
+
subject: string | null;
|
|
328
|
+
htmlBody: string | null;
|
|
329
|
+
textBody: string | null;
|
|
330
|
+
payloadData: {
|
|
331
|
+
[x: string]: import("hono/utils/types").JSONValue;
|
|
332
|
+
} | null;
|
|
333
|
+
scheduledFor: string | null;
|
|
334
|
+
sentAt: string | null;
|
|
335
|
+
} | null;
|
|
336
|
+
};
|
|
337
|
+
};
|
|
338
|
+
outputFormat: "json";
|
|
339
|
+
status: 201;
|
|
340
|
+
} | {
|
|
341
|
+
input: {
|
|
342
|
+
param: {
|
|
343
|
+
bookingId: string;
|
|
344
|
+
};
|
|
345
|
+
};
|
|
346
|
+
output: {
|
|
347
|
+
error: string;
|
|
348
|
+
};
|
|
349
|
+
outputFormat: "json";
|
|
350
|
+
status: 409;
|
|
351
|
+
};
|
|
352
|
+
};
|
|
353
|
+
}, "/", "/v1/checkout/bookings/:bookingId/initiate-collection">;
|
|
354
|
+
export {};
|
|
355
|
+
//# sourceMappingURL=routes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../../src/routes.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AACnE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAGjE,OAAO,EAGL,KAAK,qBAAqB,EAC3B,MAAM,cAAc,CAAA;AAMrB,KAAK,GAAG,GAAG;IACT,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IACjC,SAAS,EAAE;QACT,EAAE,EAAE,kBAAkB,CAAA;QACtB,MAAM,CAAC,EAAE,MAAM,CAAA;KAChB,CAAA;CACF,CAAA;AAED,wBAAgB,oBAAoB,CAAC,OAAO,GAAE;IAC5C,MAAM,CAAC,EAAE,qBAAqB,CAAA;IAC9B,SAAS,CAAC,EAAE,aAAa,CAAC,oBAAoB,CAAC,CAAA;IAC/C,gBAAgB,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,aAAa,CAAC,oBAAoB,CAAC,CAAA;CACzF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;gEA+CL"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { createNotificationService } from "@voyantjs/notifications";
|
|
2
|
+
import { Hono } from "hono";
|
|
3
|
+
import { initiateCheckoutCollection, previewCheckoutCollection, } from "./service.js";
|
|
4
|
+
import { initiateCheckoutCollectionSchema, previewCheckoutCollectionSchema, } from "./validation.js";
|
|
5
|
+
export function createCheckoutRoutes(options = {}) {
|
|
6
|
+
return new Hono()
|
|
7
|
+
.post("/v1/checkout/bookings/:bookingId/collection-plan", async (c) => {
|
|
8
|
+
try {
|
|
9
|
+
const plan = await previewCheckoutCollection(c.get("db"), c.req.param("bookingId"), previewCheckoutCollectionSchema.parse(await c.req.json().catch(() => ({}))), options.policy);
|
|
10
|
+
if (!plan) {
|
|
11
|
+
return c.json({ error: "Booking not found" }, 404);
|
|
12
|
+
}
|
|
13
|
+
return c.json({ data: plan });
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
const message = error instanceof Error ? error.message : "Failed to preview checkout collection";
|
|
17
|
+
return c.json({ error: message }, 400);
|
|
18
|
+
}
|
|
19
|
+
})
|
|
20
|
+
.post("/v1/checkout/bookings/:bookingId/initiate-collection", async (c) => {
|
|
21
|
+
try {
|
|
22
|
+
const dispatcher = createNotificationService(options.resolveProviders?.(c.env) ?? options.providers ?? []);
|
|
23
|
+
const result = await initiateCheckoutCollection(c.get("db"), c.req.param("bookingId"), initiateCheckoutCollectionSchema.parse(await c.req.json()), options.policy, dispatcher);
|
|
24
|
+
if (!result) {
|
|
25
|
+
return c.json({ error: "Booking not found" }, 404);
|
|
26
|
+
}
|
|
27
|
+
return c.json({ data: result }, 201);
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
const message = error instanceof Error ? error.message : "Failed to initiate checkout collection";
|
|
31
|
+
if (message.includes("Booking not found")) {
|
|
32
|
+
return c.json({ error: message }, 404);
|
|
33
|
+
}
|
|
34
|
+
return c.json({ error: message }, 409);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { bookingPaymentSchedules, invoices, type PaymentSession } from "@voyantjs/finance";
|
|
2
|
+
import type { NotificationDelivery, NotificationService } from "@voyantjs/notifications";
|
|
3
|
+
import type { PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
|
4
|
+
import type { InitiateCheckoutCollectionInput, PreviewCheckoutCollectionInput } from "./validation.js";
|
|
5
|
+
export interface CheckoutPolicyOptions {
|
|
6
|
+
defaultCardCollectionTarget?: "schedule" | "invoice";
|
|
7
|
+
defaultReminderCardCollectionTarget?: "schedule" | "invoice";
|
|
8
|
+
defaultBankTransferDocumentType?: "proforma" | "invoice";
|
|
9
|
+
defaultPaymentPlan?: {
|
|
10
|
+
depositMode: "none" | "percentage" | "fixed_amount";
|
|
11
|
+
depositValue: number;
|
|
12
|
+
balanceDueDaysBeforeStart: number;
|
|
13
|
+
clearExistingPending: boolean;
|
|
14
|
+
createGuarantee: boolean;
|
|
15
|
+
guaranteeType: "deposit" | "credit_card" | "preauth" | "card_on_file" | "bank_transfer" | "voucher" | "agency_letter";
|
|
16
|
+
notes?: string | null;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export interface CheckoutCollectionPlan {
|
|
20
|
+
bookingId: string;
|
|
21
|
+
method: "card" | "bank_transfer";
|
|
22
|
+
stage: "initial" | "reminder" | "manual";
|
|
23
|
+
paymentSessionTarget: "schedule" | "invoice" | null;
|
|
24
|
+
documentType: "proforma" | "invoice" | null;
|
|
25
|
+
willCreateDefaultPaymentPlan: boolean;
|
|
26
|
+
selectedSchedule: typeof bookingPaymentSchedules.$inferSelect | null;
|
|
27
|
+
selectedInvoice: typeof invoices.$inferSelect | null;
|
|
28
|
+
amountCents: number;
|
|
29
|
+
currency: string;
|
|
30
|
+
recommendedAction: "create_bank_transfer_document" | "create_payment_session" | "create_invoice_then_payment_session" | "none";
|
|
31
|
+
}
|
|
32
|
+
export interface InitiatedCheckoutCollection {
|
|
33
|
+
plan: CheckoutCollectionPlan;
|
|
34
|
+
invoice: typeof invoices.$inferSelect | null;
|
|
35
|
+
paymentSession: PaymentSession | null;
|
|
36
|
+
invoiceNotification: NotificationDelivery | null;
|
|
37
|
+
paymentSessionNotification: NotificationDelivery | null;
|
|
38
|
+
}
|
|
39
|
+
export declare function resolvePaymentSessionTarget(method: "card" | "bank_transfer", stage: "initial" | "reminder" | "manual", override: "schedule" | "invoice" | undefined, options: CheckoutPolicyOptions): "invoice" | "schedule";
|
|
40
|
+
export declare function previewCheckoutCollection(db: PostgresJsDatabase, bookingId: string, input: PreviewCheckoutCollectionInput, options?: CheckoutPolicyOptions): Promise<CheckoutCollectionPlan | null>;
|
|
41
|
+
export declare function initiateCheckoutCollection(db: PostgresJsDatabase, bookingId: string, input: InitiateCheckoutCollectionInput, options?: CheckoutPolicyOptions, dispatcher?: NotificationService): Promise<InitiatedCheckoutCollection | null>;
|
|
42
|
+
//# sourceMappingURL=service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../src/service.ts"],"names":[],"mappings":"AACA,OAAO,EACL,uBAAuB,EAIvB,QAAQ,EACR,KAAK,cAAc,EACpB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,KAAK,EACV,oBAAoB,EACpB,mBAAmB,EACpB,MAAM,yBAAyB,CAAA;AAGhC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAEjE,OAAO,KAAK,EACV,+BAA+B,EAC/B,8BAA8B,EAC/B,MAAM,iBAAiB,CAAA;AAExB,MAAM,WAAW,qBAAqB;IACpC,2BAA2B,CAAC,EAAE,UAAU,GAAG,SAAS,CAAA;IACpD,mCAAmC,CAAC,EAAE,UAAU,GAAG,SAAS,CAAA;IAC5D,+BAA+B,CAAC,EAAE,UAAU,GAAG,SAAS,CAAA;IACxD,kBAAkB,CAAC,EAAE;QACnB,WAAW,EAAE,MAAM,GAAG,YAAY,GAAG,cAAc,CAAA;QACnD,YAAY,EAAE,MAAM,CAAA;QACpB,yBAAyB,EAAE,MAAM,CAAA;QACjC,oBAAoB,EAAE,OAAO,CAAA;QAC7B,eAAe,EAAE,OAAO,CAAA;QACxB,aAAa,EAAE,SAAS,GAAG,aAAa,GAAG,SAAS,GAAG,cAAc,GAAG,eAAe,GAAG,SAAS,GAAG,eAAe,CAAA;QACrH,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;KACtB,CAAA;CACF;AAUD,MAAM,WAAW,sBAAsB;IACrC,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,MAAM,GAAG,eAAe,CAAA;IAChC,KAAK,EAAE,SAAS,GAAG,UAAU,GAAG,QAAQ,CAAA;IACxC,oBAAoB,EAAE,UAAU,GAAG,SAAS,GAAG,IAAI,CAAA;IACnD,YAAY,EAAE,UAAU,GAAG,SAAS,GAAG,IAAI,CAAA;IAC3C,4BAA4B,EAAE,OAAO,CAAA;IACrC,gBAAgB,EAAE,OAAO,uBAAuB,CAAC,YAAY,GAAG,IAAI,CAAA;IACpE,eAAe,EAAE,OAAO,QAAQ,CAAC,YAAY,GAAG,IAAI,CAAA;IACpD,WAAW,EAAE,MAAM,CAAA;IACnB,QAAQ,EAAE,MAAM,CAAA;IAChB,iBAAiB,EACb,+BAA+B,GAC/B,wBAAwB,GACxB,qCAAqC,GACrC,MAAM,CAAA;CACX;AAED,MAAM,WAAW,2BAA2B;IAC1C,IAAI,EAAE,sBAAsB,CAAA;IAC5B,OAAO,EAAE,OAAO,QAAQ,CAAC,YAAY,GAAG,IAAI,CAAA;IAC5C,cAAc,EAAE,cAAc,GAAG,IAAI,CAAA;IACrC,mBAAmB,EAAE,oBAAoB,GAAG,IAAI,CAAA;IAChD,0BAA0B,EAAE,oBAAoB,GAAG,IAAI,CAAA;CACxD;AAyBD,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,MAAM,GAAG,eAAe,EAChC,KAAK,EAAE,SAAS,GAAG,UAAU,GAAG,QAAQ,EACxC,QAAQ,EAAE,UAAU,GAAG,SAAS,GAAG,SAAS,EAC5C,OAAO,EAAE,qBAAqB,0BAM/B;AAoJD,wBAAsB,yBAAyB,CAC7C,EAAE,EAAE,kBAAkB,EACtB,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,8BAA8B,EACrC,OAAO,GAAE,qBAA0B,GAClC,OAAO,CAAC,sBAAsB,GAAG,IAAI,CAAC,CAmDxC;AAkED,wBAAsB,0BAA0B,CAC9C,EAAE,EAAE,kBAAkB,EACtB,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,+BAA+B,EACtC,OAAO,GAAE,qBAA0B,EACnC,UAAU,CAAC,EAAE,mBAAmB,GAC/B,OAAO,CAAC,2BAA2B,GAAG,IAAI,CAAC,CA4F7C"}
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { bookingItems, bookingParticipants, bookings } from "@voyantjs/bookings";
|
|
2
|
+
import { bookingPaymentSchedules, financeService, invoiceLineItems, invoiceNumberSeries, invoices, } from "@voyantjs/finance";
|
|
3
|
+
import { notificationsService } from "@voyantjs/notifications";
|
|
4
|
+
import { and, asc, desc, eq, gt, inArray } from "drizzle-orm";
|
|
5
|
+
const OUTSTANDING_SCHEDULE_STATUSES = [
|
|
6
|
+
"pending",
|
|
7
|
+
"due",
|
|
8
|
+
];
|
|
9
|
+
const OUTSTANDING_INVOICE_STATUSES = [
|
|
10
|
+
"draft",
|
|
11
|
+
"sent",
|
|
12
|
+
"partially_paid",
|
|
13
|
+
"overdue",
|
|
14
|
+
];
|
|
15
|
+
function defaultPaymentPlan(options) {
|
|
16
|
+
return {
|
|
17
|
+
depositMode: options.defaultPaymentPlan?.depositMode ?? "percentage",
|
|
18
|
+
depositValue: options.defaultPaymentPlan?.depositValue ?? 30,
|
|
19
|
+
balanceDueDaysBeforeStart: options.defaultPaymentPlan?.balanceDueDaysBeforeStart ?? 30,
|
|
20
|
+
clearExistingPending: options.defaultPaymentPlan?.clearExistingPending ?? true,
|
|
21
|
+
createGuarantee: options.defaultPaymentPlan?.createGuarantee ?? false,
|
|
22
|
+
guaranteeType: options.defaultPaymentPlan?.guaranteeType ?? "deposit",
|
|
23
|
+
notes: options.defaultPaymentPlan?.notes ?? null,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export function resolvePaymentSessionTarget(method, stage, override, options) {
|
|
27
|
+
if (method === "bank_transfer")
|
|
28
|
+
return "invoice";
|
|
29
|
+
if (override)
|
|
30
|
+
return override;
|
|
31
|
+
if (stage === "reminder")
|
|
32
|
+
return options.defaultReminderCardCollectionTarget ?? "schedule";
|
|
33
|
+
return options.defaultCardCollectionTarget ?? "schedule";
|
|
34
|
+
}
|
|
35
|
+
function resolveDocumentType(method, target, options) {
|
|
36
|
+
if (method === "bank_transfer") {
|
|
37
|
+
return options.defaultBankTransferDocumentType ?? "proforma";
|
|
38
|
+
}
|
|
39
|
+
if (target === "invoice") {
|
|
40
|
+
return "invoice";
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
function fallbackInvoiceNumber(bookingNumber, documentType, amountCents) {
|
|
45
|
+
const stamp = Date.now().toString(36).toUpperCase();
|
|
46
|
+
const suffix = documentType === "proforma" ? "PF" : "INV";
|
|
47
|
+
return `${bookingNumber}-${suffix}-${amountCents}-${stamp}`;
|
|
48
|
+
}
|
|
49
|
+
function lineDescription(booking, schedule, stage) {
|
|
50
|
+
if (!schedule) {
|
|
51
|
+
return `Booking ${booking.bookingNumber}`;
|
|
52
|
+
}
|
|
53
|
+
const kind = schedule.scheduleType === "deposit" ? "deposit" : schedule.scheduleType;
|
|
54
|
+
if (stage === "reminder") {
|
|
55
|
+
return `Booking ${booking.bookingNumber} ${kind} reminder`;
|
|
56
|
+
}
|
|
57
|
+
return `Booking ${booking.bookingNumber} ${kind}`;
|
|
58
|
+
}
|
|
59
|
+
async function loadBookingContext(db, bookingId) {
|
|
60
|
+
const [booking] = await db.select().from(bookings).where(eq(bookings.id, bookingId)).limit(1);
|
|
61
|
+
if (!booking)
|
|
62
|
+
return null;
|
|
63
|
+
const [items, participants, schedules, outstandingInvoices] = await Promise.all([
|
|
64
|
+
db.select().from(bookingItems).where(eq(bookingItems.bookingId, bookingId)).orderBy(bookingItems.createdAt),
|
|
65
|
+
db
|
|
66
|
+
.select()
|
|
67
|
+
.from(bookingParticipants)
|
|
68
|
+
.where(eq(bookingParticipants.bookingId, bookingId))
|
|
69
|
+
.orderBy(desc(bookingParticipants.isPrimary), bookingParticipants.createdAt),
|
|
70
|
+
db
|
|
71
|
+
.select()
|
|
72
|
+
.from(bookingPaymentSchedules)
|
|
73
|
+
.where(and(eq(bookingPaymentSchedules.bookingId, bookingId), inArray(bookingPaymentSchedules.status, OUTSTANDING_SCHEDULE_STATUSES)))
|
|
74
|
+
.orderBy(asc(bookingPaymentSchedules.dueDate), asc(bookingPaymentSchedules.createdAt)),
|
|
75
|
+
db
|
|
76
|
+
.select()
|
|
77
|
+
.from(invoices)
|
|
78
|
+
.where(and(eq(invoices.bookingId, bookingId), inArray(invoices.status, OUTSTANDING_INVOICE_STATUSES), gt(invoices.balanceDueCents, 0)))
|
|
79
|
+
.orderBy(desc(invoices.createdAt)),
|
|
80
|
+
]);
|
|
81
|
+
return {
|
|
82
|
+
booking,
|
|
83
|
+
items,
|
|
84
|
+
participants,
|
|
85
|
+
schedules,
|
|
86
|
+
outstandingInvoices,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
async function ensurePaymentPlanIfNeeded(db, bookingId, existingSchedules, input, options) {
|
|
90
|
+
if (existingSchedules.length > 0 || !input.ensureDefaultPaymentPlan) {
|
|
91
|
+
return existingSchedules;
|
|
92
|
+
}
|
|
93
|
+
const created = await financeService.applyDefaultBookingPaymentPlan(db, bookingId, {
|
|
94
|
+
...defaultPaymentPlan(options),
|
|
95
|
+
...(input.paymentPlan ?? {}),
|
|
96
|
+
});
|
|
97
|
+
return created ?? [];
|
|
98
|
+
}
|
|
99
|
+
async function allocateDocumentNumber(db, bookingNumber, documentType, amountCents) {
|
|
100
|
+
const [series] = await db
|
|
101
|
+
.select()
|
|
102
|
+
.from(invoiceNumberSeries)
|
|
103
|
+
.where(eq(invoiceNumberSeries.scope, documentType))
|
|
104
|
+
.orderBy(desc(invoiceNumberSeries.active), asc(invoiceNumberSeries.createdAt))
|
|
105
|
+
.limit(1);
|
|
106
|
+
if (!series) {
|
|
107
|
+
return fallbackInvoiceNumber(bookingNumber, documentType, amountCents);
|
|
108
|
+
}
|
|
109
|
+
const allocated = await financeService.allocateInvoiceNumber(db, series.id);
|
|
110
|
+
if (allocated.status === "allocated") {
|
|
111
|
+
return allocated.formattedNumber;
|
|
112
|
+
}
|
|
113
|
+
return fallbackInvoiceNumber(bookingNumber, documentType, amountCents);
|
|
114
|
+
}
|
|
115
|
+
function pickSchedule(schedules, scheduleId) {
|
|
116
|
+
if (scheduleId) {
|
|
117
|
+
return schedules.find((schedule) => schedule.id === scheduleId) ?? null;
|
|
118
|
+
}
|
|
119
|
+
return schedules[0] ?? null;
|
|
120
|
+
}
|
|
121
|
+
function pickInvoice(outstandingInvoices, invoiceId) {
|
|
122
|
+
if (invoiceId) {
|
|
123
|
+
return outstandingInvoices.find((invoice) => invoice.id === invoiceId) ?? null;
|
|
124
|
+
}
|
|
125
|
+
return outstandingInvoices[0] ?? null;
|
|
126
|
+
}
|
|
127
|
+
export async function previewCheckoutCollection(db, bookingId, input, options = {}) {
|
|
128
|
+
const context = await loadBookingContext(db, bookingId);
|
|
129
|
+
if (!context)
|
|
130
|
+
return null;
|
|
131
|
+
const schedules = await ensurePaymentPlanIfNeeded(db, bookingId, context.schedules, input, options);
|
|
132
|
+
const paymentSessionTarget = resolvePaymentSessionTarget(input.method, input.stage, input.paymentSessionTarget, options);
|
|
133
|
+
const documentType = resolveDocumentType(input.method, paymentSessionTarget, options);
|
|
134
|
+
const selectedSchedule = pickSchedule(schedules, input.scheduleId);
|
|
135
|
+
const selectedInvoice = pickInvoice(context.outstandingInvoices, input.invoiceId);
|
|
136
|
+
let amountCents = 0;
|
|
137
|
+
if (paymentSessionTarget === "invoice") {
|
|
138
|
+
amountCents = selectedInvoice?.balanceDueCents ?? selectedSchedule?.amountCents ?? context.booking.sellAmountCents ?? 0;
|
|
139
|
+
}
|
|
140
|
+
else if (paymentSessionTarget === "schedule") {
|
|
141
|
+
amountCents = selectedSchedule?.amountCents ?? context.booking.sellAmountCents ?? 0;
|
|
142
|
+
}
|
|
143
|
+
let recommendedAction = "none";
|
|
144
|
+
if (input.method === "bank_transfer") {
|
|
145
|
+
recommendedAction = "create_bank_transfer_document";
|
|
146
|
+
}
|
|
147
|
+
else if (paymentSessionTarget === "invoice") {
|
|
148
|
+
recommendedAction = selectedInvoice ? "create_payment_session" : "create_invoice_then_payment_session";
|
|
149
|
+
}
|
|
150
|
+
else if (paymentSessionTarget === "schedule") {
|
|
151
|
+
recommendedAction = "create_payment_session";
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
bookingId,
|
|
155
|
+
method: input.method,
|
|
156
|
+
stage: input.stage,
|
|
157
|
+
paymentSessionTarget,
|
|
158
|
+
documentType,
|
|
159
|
+
willCreateDefaultPaymentPlan: context.schedules.length === 0 && schedules.length > 0,
|
|
160
|
+
selectedSchedule,
|
|
161
|
+
selectedInvoice,
|
|
162
|
+
amountCents,
|
|
163
|
+
currency: context.booking.sellCurrency,
|
|
164
|
+
recommendedAction,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
async function createCollectionInvoice(db, context, plan, notes) {
|
|
168
|
+
const amountCents = plan.amountCents;
|
|
169
|
+
const issueDate = new Date().toISOString().slice(0, 10);
|
|
170
|
+
const dueDate = plan.selectedSchedule?.dueDate ?? issueDate;
|
|
171
|
+
const documentType = plan.documentType ?? "invoice";
|
|
172
|
+
const invoiceNumber = await allocateDocumentNumber(db, context.booking.bookingNumber, documentType, amountCents);
|
|
173
|
+
const [invoice] = await db
|
|
174
|
+
.insert(invoices)
|
|
175
|
+
.values({
|
|
176
|
+
invoiceNumber,
|
|
177
|
+
bookingId: context.booking.id,
|
|
178
|
+
personId: context.booking.personId,
|
|
179
|
+
organizationId: context.booking.organizationId,
|
|
180
|
+
invoiceType: documentType,
|
|
181
|
+
status: "sent",
|
|
182
|
+
currency: context.booking.sellCurrency,
|
|
183
|
+
baseCurrency: context.booking.baseCurrency,
|
|
184
|
+
fxRateSetId: null,
|
|
185
|
+
subtotalCents: amountCents,
|
|
186
|
+
baseSubtotalCents: context.booking.baseSellAmountCents,
|
|
187
|
+
taxCents: 0,
|
|
188
|
+
baseTaxCents: null,
|
|
189
|
+
totalCents: amountCents,
|
|
190
|
+
baseTotalCents: context.booking.baseSellAmountCents,
|
|
191
|
+
paidCents: 0,
|
|
192
|
+
basePaidCents: 0,
|
|
193
|
+
balanceDueCents: amountCents,
|
|
194
|
+
baseBalanceDueCents: context.booking.baseSellAmountCents,
|
|
195
|
+
commissionAmountCents: null,
|
|
196
|
+
issueDate,
|
|
197
|
+
dueDate,
|
|
198
|
+
notes: notes ?? plan.selectedSchedule?.notes ?? null,
|
|
199
|
+
})
|
|
200
|
+
.returning();
|
|
201
|
+
if (!invoice) {
|
|
202
|
+
throw new Error("Failed to create collection invoice");
|
|
203
|
+
}
|
|
204
|
+
await db.insert(invoiceLineItems).values({
|
|
205
|
+
invoiceId: invoice.id,
|
|
206
|
+
bookingItemId: plan.selectedSchedule?.bookingItemId ?? null,
|
|
207
|
+
description: lineDescription(context.booking, plan.selectedSchedule, plan.stage),
|
|
208
|
+
quantity: 1,
|
|
209
|
+
unitPriceCents: amountCents,
|
|
210
|
+
totalCents: amountCents,
|
|
211
|
+
taxRate: null,
|
|
212
|
+
sortOrder: 0,
|
|
213
|
+
});
|
|
214
|
+
return invoice;
|
|
215
|
+
}
|
|
216
|
+
export async function initiateCheckoutCollection(db, bookingId, input, options = {}, dispatcher) {
|
|
217
|
+
const context = await loadBookingContext(db, bookingId);
|
|
218
|
+
if (!context)
|
|
219
|
+
return null;
|
|
220
|
+
const plan = await previewCheckoutCollection(db, bookingId, input, options);
|
|
221
|
+
if (!plan)
|
|
222
|
+
return null;
|
|
223
|
+
if (plan.amountCents <= 0) {
|
|
224
|
+
throw new Error("No outstanding amount available for collection");
|
|
225
|
+
}
|
|
226
|
+
let invoice = plan.selectedInvoice;
|
|
227
|
+
let paymentSession = null;
|
|
228
|
+
let invoiceNotification = null;
|
|
229
|
+
let paymentSessionNotification = null;
|
|
230
|
+
if (input.method === "bank_transfer") {
|
|
231
|
+
invoice = await createCollectionInvoice(db, context, plan, input.notes ?? null);
|
|
232
|
+
if (dispatcher && input.invoiceNotification) {
|
|
233
|
+
invoiceNotification = await notificationsService.sendInvoiceNotification(db, dispatcher, invoice.id, input.invoiceNotification);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
else if (plan.paymentSessionTarget === "invoice") {
|
|
237
|
+
if (!invoice) {
|
|
238
|
+
invoice = await createCollectionInvoice(db, context, { ...plan, documentType: "invoice" }, input.notes ?? null);
|
|
239
|
+
}
|
|
240
|
+
paymentSession = await financeService.createPaymentSessionFromInvoice(db, invoice.id, {
|
|
241
|
+
...(input.paymentSession ?? {}),
|
|
242
|
+
notes: input.notes ?? input.paymentSession?.notes ?? null,
|
|
243
|
+
});
|
|
244
|
+
if (!paymentSession) {
|
|
245
|
+
throw new Error("Failed to create payment session from invoice");
|
|
246
|
+
}
|
|
247
|
+
if (dispatcher && input.invoiceNotification) {
|
|
248
|
+
invoiceNotification = await notificationsService.sendInvoiceNotification(db, dispatcher, invoice.id, input.invoiceNotification);
|
|
249
|
+
}
|
|
250
|
+
if (dispatcher && input.paymentSessionNotification) {
|
|
251
|
+
paymentSessionNotification = await notificationsService.sendPaymentSessionNotification(db, dispatcher, paymentSession.id, input.paymentSessionNotification);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
if (!plan.selectedSchedule) {
|
|
256
|
+
throw new Error("No outstanding payment schedule available for collection");
|
|
257
|
+
}
|
|
258
|
+
paymentSession = await financeService.createPaymentSessionFromBookingSchedule(db, plan.selectedSchedule.id, {
|
|
259
|
+
...(input.paymentSession ?? {}),
|
|
260
|
+
notes: input.notes ?? input.paymentSession?.notes ?? null,
|
|
261
|
+
});
|
|
262
|
+
if (!paymentSession) {
|
|
263
|
+
throw new Error("Failed to create payment session from booking schedule");
|
|
264
|
+
}
|
|
265
|
+
if (dispatcher && input.paymentSessionNotification) {
|
|
266
|
+
paymentSessionNotification = await notificationsService.sendPaymentSessionNotification(db, dispatcher, paymentSession.id, input.paymentSessionNotification);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
plan,
|
|
271
|
+
invoice: invoice ?? null,
|
|
272
|
+
paymentSession,
|
|
273
|
+
invoiceNotification,
|
|
274
|
+
paymentSessionNotification,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const checkoutCollectionMethodSchema: z.ZodEnum<{
|
|
3
|
+
bank_transfer: "bank_transfer";
|
|
4
|
+
card: "card";
|
|
5
|
+
}>;
|
|
6
|
+
export declare const checkoutCollectionStageSchema: z.ZodEnum<{
|
|
7
|
+
manual: "manual";
|
|
8
|
+
initial: "initial";
|
|
9
|
+
reminder: "reminder";
|
|
10
|
+
}>;
|
|
11
|
+
export declare const checkoutPaymentSessionTargetSchema: z.ZodEnum<{
|
|
12
|
+
invoice: "invoice";
|
|
13
|
+
schedule: "schedule";
|
|
14
|
+
}>;
|
|
15
|
+
export declare const checkoutInvoiceDocumentTypeSchema: z.ZodEnum<{
|
|
16
|
+
invoice: "invoice";
|
|
17
|
+
proforma: "proforma";
|
|
18
|
+
}>;
|
|
19
|
+
export declare const previewCheckoutCollectionSchema: z.ZodObject<{
|
|
20
|
+
method: z.ZodEnum<{
|
|
21
|
+
bank_transfer: "bank_transfer";
|
|
22
|
+
card: "card";
|
|
23
|
+
}>;
|
|
24
|
+
stage: z.ZodDefault<z.ZodEnum<{
|
|
25
|
+
manual: "manual";
|
|
26
|
+
initial: "initial";
|
|
27
|
+
reminder: "reminder";
|
|
28
|
+
}>>;
|
|
29
|
+
scheduleId: z.ZodOptional<z.ZodString>;
|
|
30
|
+
invoiceId: z.ZodOptional<z.ZodString>;
|
|
31
|
+
ensureDefaultPaymentPlan: z.ZodDefault<z.ZodBoolean>;
|
|
32
|
+
paymentSessionTarget: z.ZodOptional<z.ZodEnum<{
|
|
33
|
+
invoice: "invoice";
|
|
34
|
+
schedule: "schedule";
|
|
35
|
+
}>>;
|
|
36
|
+
paymentPlan: z.ZodOptional<z.ZodObject<{
|
|
37
|
+
depositMode: z.ZodOptional<z.ZodDefault<z.ZodEnum<{
|
|
38
|
+
percentage: "percentage";
|
|
39
|
+
none: "none";
|
|
40
|
+
fixed_amount: "fixed_amount";
|
|
41
|
+
}>>>;
|
|
42
|
+
depositValue: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
|
|
43
|
+
depositDueDate: z.ZodOptional<z.ZodNullable<z.ZodOptional<z.ZodString>>>;
|
|
44
|
+
balanceDueDaysBeforeStart: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
|
|
45
|
+
clearExistingPending: z.ZodOptional<z.ZodDefault<z.ZodBoolean>>;
|
|
46
|
+
createGuarantee: z.ZodOptional<z.ZodDefault<z.ZodBoolean>>;
|
|
47
|
+
guaranteeType: z.ZodOptional<z.ZodDefault<z.ZodEnum<{
|
|
48
|
+
other: "other";
|
|
49
|
+
voucher: "voucher";
|
|
50
|
+
bank_transfer: "bank_transfer";
|
|
51
|
+
credit_card: "credit_card";
|
|
52
|
+
deposit: "deposit";
|
|
53
|
+
preauth: "preauth";
|
|
54
|
+
card_on_file: "card_on_file";
|
|
55
|
+
agency_letter: "agency_letter";
|
|
56
|
+
}>>>;
|
|
57
|
+
notes: z.ZodOptional<z.ZodNullable<z.ZodOptional<z.ZodString>>>;
|
|
58
|
+
}, z.core.$strip>>;
|
|
59
|
+
}, z.core.$strip>;
|
|
60
|
+
export declare const initiateCheckoutCollectionSchema: z.ZodObject<{
|
|
61
|
+
method: z.ZodEnum<{
|
|
62
|
+
bank_transfer: "bank_transfer";
|
|
63
|
+
card: "card";
|
|
64
|
+
}>;
|
|
65
|
+
stage: z.ZodDefault<z.ZodEnum<{
|
|
66
|
+
manual: "manual";
|
|
67
|
+
initial: "initial";
|
|
68
|
+
reminder: "reminder";
|
|
69
|
+
}>>;
|
|
70
|
+
scheduleId: z.ZodOptional<z.ZodString>;
|
|
71
|
+
invoiceId: z.ZodOptional<z.ZodString>;
|
|
72
|
+
ensureDefaultPaymentPlan: z.ZodDefault<z.ZodBoolean>;
|
|
73
|
+
paymentSessionTarget: z.ZodOptional<z.ZodEnum<{
|
|
74
|
+
invoice: "invoice";
|
|
75
|
+
schedule: "schedule";
|
|
76
|
+
}>>;
|
|
77
|
+
paymentPlan: z.ZodOptional<z.ZodObject<{
|
|
78
|
+
depositMode: z.ZodOptional<z.ZodDefault<z.ZodEnum<{
|
|
79
|
+
percentage: "percentage";
|
|
80
|
+
none: "none";
|
|
81
|
+
fixed_amount: "fixed_amount";
|
|
82
|
+
}>>>;
|
|
83
|
+
depositValue: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
|
|
84
|
+
depositDueDate: z.ZodOptional<z.ZodNullable<z.ZodOptional<z.ZodString>>>;
|
|
85
|
+
balanceDueDaysBeforeStart: z.ZodOptional<z.ZodDefault<z.ZodNumber>>;
|
|
86
|
+
clearExistingPending: z.ZodOptional<z.ZodDefault<z.ZodBoolean>>;
|
|
87
|
+
createGuarantee: z.ZodOptional<z.ZodDefault<z.ZodBoolean>>;
|
|
88
|
+
guaranteeType: z.ZodOptional<z.ZodDefault<z.ZodEnum<{
|
|
89
|
+
other: "other";
|
|
90
|
+
voucher: "voucher";
|
|
91
|
+
bank_transfer: "bank_transfer";
|
|
92
|
+
credit_card: "credit_card";
|
|
93
|
+
deposit: "deposit";
|
|
94
|
+
preauth: "preauth";
|
|
95
|
+
card_on_file: "card_on_file";
|
|
96
|
+
agency_letter: "agency_letter";
|
|
97
|
+
}>>>;
|
|
98
|
+
notes: z.ZodOptional<z.ZodNullable<z.ZodOptional<z.ZodString>>>;
|
|
99
|
+
}, z.core.$strip>>;
|
|
100
|
+
paymentSession: z.ZodOptional<z.ZodUnion<readonly [z.ZodObject<{
|
|
101
|
+
provider: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
102
|
+
paymentMethod: z.ZodNullable<z.ZodOptional<z.ZodEnum<{
|
|
103
|
+
other: "other";
|
|
104
|
+
voucher: "voucher";
|
|
105
|
+
wallet: "wallet";
|
|
106
|
+
bank_transfer: "bank_transfer";
|
|
107
|
+
credit_card: "credit_card";
|
|
108
|
+
debit_card: "debit_card";
|
|
109
|
+
cash: "cash";
|
|
110
|
+
cheque: "cheque";
|
|
111
|
+
direct_bill: "direct_bill";
|
|
112
|
+
}>>>;
|
|
113
|
+
payerPersonId: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
114
|
+
payerOrganizationId: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
115
|
+
payerEmail: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
116
|
+
payerName: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
117
|
+
externalReference: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
118
|
+
idempotencyKey: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
119
|
+
clientReference: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
120
|
+
returnUrl: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
121
|
+
cancelUrl: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
122
|
+
callbackUrl: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
123
|
+
expiresAt: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
124
|
+
notes: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
125
|
+
providerPayload: z.ZodNullable<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
|
|
126
|
+
metadata: z.ZodNullable<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
|
|
127
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
128
|
+
provider: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
129
|
+
paymentMethod: z.ZodNullable<z.ZodOptional<z.ZodEnum<{
|
|
130
|
+
other: "other";
|
|
131
|
+
voucher: "voucher";
|
|
132
|
+
wallet: "wallet";
|
|
133
|
+
bank_transfer: "bank_transfer";
|
|
134
|
+
credit_card: "credit_card";
|
|
135
|
+
debit_card: "debit_card";
|
|
136
|
+
cash: "cash";
|
|
137
|
+
cheque: "cheque";
|
|
138
|
+
direct_bill: "direct_bill";
|
|
139
|
+
}>>>;
|
|
140
|
+
payerPersonId: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
141
|
+
payerOrganizationId: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
142
|
+
payerEmail: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
143
|
+
payerName: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
144
|
+
externalReference: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
145
|
+
idempotencyKey: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
146
|
+
clientReference: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
147
|
+
returnUrl: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
148
|
+
cancelUrl: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
149
|
+
callbackUrl: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
150
|
+
expiresAt: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
151
|
+
notes: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
152
|
+
providerPayload: z.ZodNullable<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
|
|
153
|
+
metadata: z.ZodNullable<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
|
|
154
|
+
}, z.core.$strip>]>>;
|
|
155
|
+
paymentSessionNotification: z.ZodOptional<z.ZodObject<{
|
|
156
|
+
templateId: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
157
|
+
templateSlug: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
158
|
+
channel: z.ZodDefault<z.ZodEnum<{
|
|
159
|
+
email: "email";
|
|
160
|
+
sms: "sms";
|
|
161
|
+
}>>;
|
|
162
|
+
provider: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
163
|
+
to: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
164
|
+
from: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
165
|
+
subject: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
166
|
+
html: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
167
|
+
text: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
168
|
+
data: z.ZodNullable<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
|
|
169
|
+
metadata: z.ZodNullable<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
|
|
170
|
+
scheduledFor: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
171
|
+
}, z.core.$strip>>;
|
|
172
|
+
invoiceNotification: z.ZodOptional<z.ZodObject<{
|
|
173
|
+
templateId: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
174
|
+
templateSlug: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
175
|
+
channel: z.ZodDefault<z.ZodEnum<{
|
|
176
|
+
email: "email";
|
|
177
|
+
sms: "sms";
|
|
178
|
+
}>>;
|
|
179
|
+
provider: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
180
|
+
to: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
181
|
+
from: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
182
|
+
subject: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
183
|
+
html: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
184
|
+
text: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
185
|
+
data: z.ZodNullable<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
|
|
186
|
+
metadata: z.ZodNullable<z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>>;
|
|
187
|
+
scheduledFor: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
188
|
+
}, z.core.$strip>>;
|
|
189
|
+
notes: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
190
|
+
}, z.core.$strip>;
|
|
191
|
+
export type PreviewCheckoutCollectionInput = z.infer<typeof previewCheckoutCollectionSchema>;
|
|
192
|
+
export type InitiateCheckoutCollectionInput = z.infer<typeof initiateCheckoutCollectionSchema>;
|
|
193
|
+
//# sourceMappingURL=validation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/validation.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,eAAO,MAAM,8BAA8B;;;EAAoC,CAAA;AAC/E,eAAO,MAAM,6BAA6B;;;;EAA4C,CAAA;AACtF,eAAO,MAAM,kCAAkC;;;EAAkC,CAAA;AACjF,eAAO,MAAM,iCAAiC;;;EAAkC,CAAA;AAIhF,eAAO,MAAM,+BAA+B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAQ1C,CAAA;AAEF,eAAO,MAAM,gCAAgC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAO3C,CAAA;AAEF,MAAM,MAAM,8BAA8B,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,+BAA+B,CAAC,CAAA;AAC5F,MAAM,MAAM,+BAA+B,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,gCAAgC,CAAC,CAAA"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { applyDefaultBookingPaymentPlanSchema, createPaymentSessionFromInvoiceSchema, createPaymentSessionFromScheduleSchema, } from "@voyantjs/finance";
|
|
2
|
+
import { sendInvoiceNotificationSchema, sendPaymentSessionNotificationSchema, } from "@voyantjs/notifications";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
export const checkoutCollectionMethodSchema = z.enum(["card", "bank_transfer"]);
|
|
5
|
+
export const checkoutCollectionStageSchema = z.enum(["initial", "reminder", "manual"]);
|
|
6
|
+
export const checkoutPaymentSessionTargetSchema = z.enum(["schedule", "invoice"]);
|
|
7
|
+
export const checkoutInvoiceDocumentTypeSchema = z.enum(["proforma", "invoice"]);
|
|
8
|
+
const planOverrideSchema = applyDefaultBookingPaymentPlanSchema.partial();
|
|
9
|
+
export const previewCheckoutCollectionSchema = z.object({
|
|
10
|
+
method: checkoutCollectionMethodSchema,
|
|
11
|
+
stage: checkoutCollectionStageSchema.default("initial"),
|
|
12
|
+
scheduleId: z.string().optional(),
|
|
13
|
+
invoiceId: z.string().optional(),
|
|
14
|
+
ensureDefaultPaymentPlan: z.boolean().default(true),
|
|
15
|
+
paymentSessionTarget: checkoutPaymentSessionTargetSchema.optional(),
|
|
16
|
+
paymentPlan: planOverrideSchema.optional(),
|
|
17
|
+
});
|
|
18
|
+
export const initiateCheckoutCollectionSchema = previewCheckoutCollectionSchema.extend({
|
|
19
|
+
paymentSession: z
|
|
20
|
+
.union([createPaymentSessionFromScheduleSchema, createPaymentSessionFromInvoiceSchema])
|
|
21
|
+
.optional(),
|
|
22
|
+
paymentSessionNotification: sendPaymentSessionNotificationSchema.optional(),
|
|
23
|
+
invoiceNotification: sendInvoiceNotificationSchema.optional(),
|
|
24
|
+
notes: z.string().optional().nullable(),
|
|
25
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"service.test.d.ts","sourceRoot":"","sources":["../../../tests/unit/service.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { resolvePaymentSessionTarget } from "../../src/service.js";
|
|
3
|
+
describe("resolvePaymentSessionTarget", () => {
|
|
4
|
+
it("always uses invoice collection for bank transfer", () => {
|
|
5
|
+
expect(resolvePaymentSessionTarget("bank_transfer", "initial", undefined, {})).toBe("invoice");
|
|
6
|
+
expect(resolvePaymentSessionTarget("bank_transfer", "reminder", "schedule", {})).toBe("invoice");
|
|
7
|
+
});
|
|
8
|
+
it("uses stage-aware defaults for card collection", () => {
|
|
9
|
+
expect(resolvePaymentSessionTarget("card", "initial", undefined, {})).toBe("schedule");
|
|
10
|
+
expect(resolvePaymentSessionTarget("card", "reminder", undefined, {
|
|
11
|
+
defaultReminderCardCollectionTarget: "invoice",
|
|
12
|
+
})).toBe("invoice");
|
|
13
|
+
});
|
|
14
|
+
it("honors explicit overrides", () => {
|
|
15
|
+
expect(resolvePaymentSessionTarget("card", "initial", "invoice", {})).toBe("invoice");
|
|
16
|
+
expect(resolvePaymentSessionTarget("card", "reminder", "schedule", {})).toBe("schedule");
|
|
17
|
+
});
|
|
18
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@voyantjs/checkout",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "FSL-1.1-Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./routes": {
|
|
12
|
+
"types": "./dist/routes.d.ts",
|
|
13
|
+
"import": "./dist/routes.js"
|
|
14
|
+
},
|
|
15
|
+
"./service": {
|
|
16
|
+
"types": "./dist/service.d.ts",
|
|
17
|
+
"import": "./dist/service.js"
|
|
18
|
+
},
|
|
19
|
+
"./validation": {
|
|
20
|
+
"types": "./dist/validation.d.ts",
|
|
21
|
+
"import": "./dist/validation.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"drizzle-orm": "^0.45.2",
|
|
26
|
+
"hono": "^4.12.10",
|
|
27
|
+
"zod": "^4.3.6",
|
|
28
|
+
"@voyantjs/bookings": "0.1.0",
|
|
29
|
+
"@voyantjs/finance": "0.1.0",
|
|
30
|
+
"@voyantjs/core": "0.1.0",
|
|
31
|
+
"@voyantjs/hono": "0.1.0",
|
|
32
|
+
"@voyantjs/notifications": "0.1.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"typescript": "^6.0.2",
|
|
36
|
+
"vitest": "^4.1.2",
|
|
37
|
+
"@voyantjs/voyant-typescript-config": "0.1.0"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist"
|
|
41
|
+
],
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {
|
|
46
|
+
"typecheck": "tsc --noEmit",
|
|
47
|
+
"lint": "biome check src/",
|
|
48
|
+
"test": "vitest run",
|
|
49
|
+
"build": "tsc -p tsconfig.json",
|
|
50
|
+
"clean": "rm -rf dist"
|
|
51
|
+
},
|
|
52
|
+
"main": "./dist/index.js",
|
|
53
|
+
"types": "./dist/index.d.ts"
|
|
54
|
+
}
|