@miguelmorales13/nestkit 0.28.0 → 0.29.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/README.md +69 -0
- package/dist/chunk-33KFVAUZ.cjs +151 -0
- package/dist/chunk-LU3HO7B3.js +151 -0
- package/dist/index.cjs +2 -2
- package/dist/index.js +1 -1
- package/dist/payments/connect/credentials-cipher.d.ts +14 -0
- package/dist/payments/connect/mercadopago-refund.d.ts +118 -0
- package/dist/payments/connect/mercadopago-signature.d.ts +6 -0
- package/dist/payments/connect/online-checkout.d.ts +69 -0
- package/dist/payments/connect/rejection.d.ts +7 -0
- package/dist/payments/connect/status.d.ts +20 -0
- package/dist/payments/connect/terminal-connections.d.ts +291 -0
- package/dist/payments/index.cjs +901 -3
- package/dist/payments/index.d.ts +10 -0
- package/dist/payments/index.js +899 -1
- package/dist/security/index.cjs +27 -152
- package/dist/security/index.js +13 -138
- package/package.json +2 -2
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { Logger } from '@nestjs/common';
|
|
2
|
+
import type { I18nMessage } from '../../i18n/message.js';
|
|
3
|
+
import { MercadoPagoConnect } from '../adapters/mercadopago-connect.js';
|
|
4
|
+
import type { TerminalPayment, TerminalPaymentStatus, TerminalPaymentsPort } from '../payments.port.js';
|
|
5
|
+
import type { CredentialsCipher } from './credentials-cipher.js';
|
|
6
|
+
import { type RefundMessages, type RefundResult } from './mercadopago-refund.js';
|
|
7
|
+
import type { OnlineCheckoutCredentials } from './online-checkout.js';
|
|
8
|
+
export type ConnectionProvider = 'mercadopago' | 'clip';
|
|
9
|
+
/**
|
|
10
|
+
* Credentials of a merchant's Mercado Pago account, as stored (encrypted JSON). The field names are the persisted
|
|
11
|
+
* format — keep them if you already have rows.
|
|
12
|
+
*/
|
|
13
|
+
export interface MercadoPagoCredentials {
|
|
14
|
+
accessToken: string;
|
|
15
|
+
refreshToken: string;
|
|
16
|
+
mpUserId: string;
|
|
17
|
+
/** ISO. Refreshed on its own 5 minutes before it expires. */
|
|
18
|
+
expira: string;
|
|
19
|
+
/** Store / POS that Mercado Pago requires before accepting an order — filled when a terminal is chosen. */
|
|
20
|
+
mpStoreId?: string;
|
|
21
|
+
mpPosId?: string;
|
|
22
|
+
}
|
|
23
|
+
export interface ClipCredentials {
|
|
24
|
+
apiKey: string;
|
|
25
|
+
secretKey: string;
|
|
26
|
+
}
|
|
27
|
+
export type ConnectionCredentials = MercadoPagoCredentials | ClipCredentials;
|
|
28
|
+
/** One connected terminal account of a tenant (a store, a branch…). What your database row looks like. */
|
|
29
|
+
export interface ConnectionRecord {
|
|
30
|
+
id: string;
|
|
31
|
+
/** The tenant that owns it. Every call is scoped by this. */
|
|
32
|
+
scopeId: string;
|
|
33
|
+
provider: ConnectionProvider;
|
|
34
|
+
encryptedCredentials: string;
|
|
35
|
+
/** Mercado Pago `terminal_id` or the Clip reader's serial number. */
|
|
36
|
+
terminalId: string | null;
|
|
37
|
+
terminalName: string | null;
|
|
38
|
+
/** Hand-written name to tell two terminals of one provider apart. */
|
|
39
|
+
alias: string | null;
|
|
40
|
+
/** Clip only: goes in the webhook URL (Clip does not sign its notices). Unique. */
|
|
41
|
+
webhookToken: string | null;
|
|
42
|
+
active: boolean;
|
|
43
|
+
primary: boolean;
|
|
44
|
+
createdAt: Date;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Where connections live. Implement it over any table; `scopeId` is your tenant (store, branch, company). Services
|
|
48
|
+
* that also run without a tenant in context (webhooks) need an implementation that is allowed to cross tenants for
|
|
49
|
+
* `findByWebhookToken` and `listActiveByProvider`.
|
|
50
|
+
*/
|
|
51
|
+
export interface TerminalConnectionStore {
|
|
52
|
+
/** Every connection of the tenant, oldest first. */
|
|
53
|
+
list(scopeId: string): Promise<ConnectionRecord[]>;
|
|
54
|
+
find(id: string): Promise<ConnectionRecord | null>;
|
|
55
|
+
insert(data: Omit<ConnectionRecord, 'id' | 'createdAt'>): Promise<ConnectionRecord>;
|
|
56
|
+
update(id: string, patch: Partial<Omit<ConnectionRecord, 'id' | 'scopeId' | 'createdAt'>>): Promise<void>;
|
|
57
|
+
/** `false` when nothing matched `id` inside `scopeId`. */
|
|
58
|
+
delete(scopeId: string, id: string): Promise<boolean>;
|
|
59
|
+
/** Exactly one primary per tenant: turn the others off and `id` on, atomically. `false` if `id` is not the tenant's. */
|
|
60
|
+
setPrimary(scopeId: string, id: string): Promise<boolean>;
|
|
61
|
+
findByWebhookToken(token: string): Promise<ConnectionRecord | null>;
|
|
62
|
+
/** Active connections of a provider across ALL tenants (to map a Mercado Pago notice to its tenants). */
|
|
63
|
+
listActiveByProvider(provider: ConnectionProvider): Promise<ConnectionRecord[]>;
|
|
64
|
+
}
|
|
65
|
+
/** A connection without its secrets, ready to show. */
|
|
66
|
+
export interface ConnectionView {
|
|
67
|
+
id: string;
|
|
68
|
+
provider: ConnectionProvider;
|
|
69
|
+
terminalId: string | null;
|
|
70
|
+
terminalName: string | null;
|
|
71
|
+
alias: string | null;
|
|
72
|
+
active: boolean;
|
|
73
|
+
primary: boolean;
|
|
74
|
+
/** Connected and with a terminal chosen: a point of sale can send charges. */
|
|
75
|
+
ready: boolean;
|
|
76
|
+
/** Clip only, and only if asked for: the URL to paste in Clip's Postback Webhook. */
|
|
77
|
+
webhookUrl?: string;
|
|
78
|
+
}
|
|
79
|
+
export interface TerminalSummary {
|
|
80
|
+
id: string;
|
|
81
|
+
name: string;
|
|
82
|
+
online: boolean | null;
|
|
83
|
+
mode: 'pdv' | 'standalone' | 'unknown' | null;
|
|
84
|
+
}
|
|
85
|
+
export interface TerminalCharge {
|
|
86
|
+
id: string;
|
|
87
|
+
provider: string;
|
|
88
|
+
status: TerminalPaymentStatus;
|
|
89
|
+
amountCents: number;
|
|
90
|
+
paidCents: number | null;
|
|
91
|
+
}
|
|
92
|
+
export type ConnectionMessageKey = 'missingAccessToken' | 'missingCredentials' | 'minimumAmount';
|
|
93
|
+
type Message = string | I18nMessage;
|
|
94
|
+
export interface TerminalConnectionsOptions {
|
|
95
|
+
store: TerminalConnectionStore;
|
|
96
|
+
/** Without it the whole feature reports `available === false`. Build it with `CredentialsCipher.fromHex`. */
|
|
97
|
+
cipher: CredentialsCipher | null | (() => CredentialsCipher | null);
|
|
98
|
+
/**
|
|
99
|
+
* The `fetch` for each provider (timeouts, retries, alerts). Cobrar sends an idempotency key for Mercado Pago, so
|
|
100
|
+
* retrying is safe there; Clip's charge is not idempotent, so retry only reads and cancellations.
|
|
101
|
+
*/
|
|
102
|
+
fetchImpl?: (provider: ConnectionProvider, scopeId?: string) => typeof fetch;
|
|
103
|
+
/** Test doubles / sandboxes. */
|
|
104
|
+
mercadoPagoBaseUrl?: string;
|
|
105
|
+
clipBaseUrl?: string;
|
|
106
|
+
/** Mercado Pago marketplace OAuth for this scope; `null` when the app is not configured. */
|
|
107
|
+
mercadoPagoConnect?: (scopeId?: string) => MercadoPagoConnect | null;
|
|
108
|
+
/** Secret that verifies Mercado Pago webhooks (`x-signature`). Without it notices are ignored. */
|
|
109
|
+
mercadoPagoWebhookSecret?: string | (() => string | undefined);
|
|
110
|
+
/** Secret that signs the OAuth `state`. */
|
|
111
|
+
stateSecret?: string | (() => string | undefined);
|
|
112
|
+
/** Public URL of your API, for the Clip webhook URL. */
|
|
113
|
+
publicWebhookUrl?: (webhookToken: string) => string;
|
|
114
|
+
/** Name for the Mercado Pago Store/POS created when the terminal has none. */
|
|
115
|
+
describeScope?: (scopeId: string) => Promise<{
|
|
116
|
+
name: string;
|
|
117
|
+
}>;
|
|
118
|
+
/** Mercado Pago requires a location to create a Store. Defaults to Mexico City. */
|
|
119
|
+
storeLocation?: (name: string) => Record<string, unknown>;
|
|
120
|
+
/** Minimum charge per provider, in cents. Defaults: Mercado Pago 500, Clip 100. */
|
|
121
|
+
minimumCents?: Partial<Record<ConnectionProvider, number>>;
|
|
122
|
+
/** What the Mercado Pago terminal displays for the charge. */
|
|
123
|
+
chargeDescription?: string;
|
|
124
|
+
providerNames?: Partial<Record<ConnectionProvider, string>>;
|
|
125
|
+
/** Override the texts people read (translate them, e.g. with nestkit's `t()`). */
|
|
126
|
+
messages?: Partial<{
|
|
127
|
+
missingAccessToken: () => Message;
|
|
128
|
+
missingCredentials: () => Message;
|
|
129
|
+
minimumAmount: (p: {
|
|
130
|
+
provider: string;
|
|
131
|
+
minimum: string;
|
|
132
|
+
}) => Message;
|
|
133
|
+
}>;
|
|
134
|
+
refundMessages?: Partial<RefundMessages>;
|
|
135
|
+
/** Called, de-duplicated, when a provider notice moved a charge to a status you have not seen. */
|
|
136
|
+
onPaymentChanged?: (scopeId: string, paymentId: string, charge: TerminalCharge) => void;
|
|
137
|
+
/** Called for every verified Mercado Pago order notice, before it is looked up (someone may be waiting on a refund). */
|
|
138
|
+
onMercadoPagoOrder?: (scopeId: string, orderId: string) => void;
|
|
139
|
+
logger?: Pick<Logger, 'log' | 'warn' | 'debug' | 'error'>;
|
|
140
|
+
}
|
|
141
|
+
/** Open all day, every day: a sensible default so a merchant is not asked for hours just to connect a terminal. */
|
|
142
|
+
export declare const ALWAYS_OPEN: {
|
|
143
|
+
[k: string]: {
|
|
144
|
+
open: string;
|
|
145
|
+
close: string;
|
|
146
|
+
}[];
|
|
147
|
+
};
|
|
148
|
+
/** Mexico City, for stores that give no address. */
|
|
149
|
+
export declare const defaultStoreLocation: (name: string) => {
|
|
150
|
+
streetNumber: string;
|
|
151
|
+
streetName: string;
|
|
152
|
+
cityName: string;
|
|
153
|
+
stateName: string;
|
|
154
|
+
latitude: number;
|
|
155
|
+
longitude: number;
|
|
156
|
+
};
|
|
157
|
+
export interface OAuthStatePayload {
|
|
158
|
+
scopeId: string;
|
|
159
|
+
actorId: string;
|
|
160
|
+
/** Binds the callback to the browser that started the flow (cookie value). */
|
|
161
|
+
csrf: string;
|
|
162
|
+
destination?: string;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Everything a point of sale needs to take card payments on a merchant's own Mercado Pago / Clip terminals:
|
|
166
|
+
* connect (Clip keys, Mercado Pago OAuth), choose and provision the terminal, charge, look up, cancel, react to
|
|
167
|
+
* provider notices, refund, and hand out the merchant's credentials for online checkout. Storage-agnostic: bring a
|
|
168
|
+
* {@link TerminalConnectionStore}. Plain class, no Nest dependency: wrap it in your own service for plan checks,
|
|
169
|
+
* audit and events.
|
|
170
|
+
*/
|
|
171
|
+
export declare class TerminalConnections {
|
|
172
|
+
private readonly o;
|
|
173
|
+
private readonly log;
|
|
174
|
+
private readonly lastStatus;
|
|
175
|
+
private mpCache;
|
|
176
|
+
constructor(o: TerminalConnectionsOptions);
|
|
177
|
+
/** Resolved on every use so a key set after construction (env, secret manager) is picked up. */
|
|
178
|
+
private get cipher();
|
|
179
|
+
get available(): boolean;
|
|
180
|
+
private requireCipher;
|
|
181
|
+
private name;
|
|
182
|
+
private fetchFor;
|
|
183
|
+
private sorted;
|
|
184
|
+
view(r: ConnectionRecord, includeWebhook?: boolean): ConnectionView;
|
|
185
|
+
list(scopeId: string, includeWebhook?: boolean): Promise<ConnectionView[]>;
|
|
186
|
+
/** Providers with an active account: which one the merchant can charge online with. */
|
|
187
|
+
connectedProviders(scopeId: string): Promise<ConnectionProvider[]>;
|
|
188
|
+
private one;
|
|
189
|
+
/** Connects Clip (API key + secret key) after checking them; adds a new terminal row. */
|
|
190
|
+
connectClip(scopeId: string, credentials: ClipCredentials): Promise<{
|
|
191
|
+
id: string;
|
|
192
|
+
terminals: TerminalSummary[];
|
|
193
|
+
}>;
|
|
194
|
+
/**
|
|
195
|
+
* URL to send the owner to so they authorize. `state` binds the callback to this tenant, and `csrf` (to be set as an
|
|
196
|
+
* httpOnly cookie by your controller) binds it to the browser that started the flow (OAuth CSRF, RFC 6749 §10.12).
|
|
197
|
+
* Check your plan/permissions BEFORE calling this: the public callback cannot tell the original tab anything.
|
|
198
|
+
*/
|
|
199
|
+
mercadoPagoAuthorizationUrl(scopeId: string, actorId: string, destination?: string): {
|
|
200
|
+
url: string;
|
|
201
|
+
csrf: string;
|
|
202
|
+
};
|
|
203
|
+
private stateSecret;
|
|
204
|
+
private readOAuthState;
|
|
205
|
+
/** Where to go back after the callback. An unreadable `state` goes back to `fallback`. */
|
|
206
|
+
oauthDestination(state: string | undefined, fallback?: string): string;
|
|
207
|
+
/**
|
|
208
|
+
* The Mercado Pago callback (no session: only `code`/`state` plus the cookie set at the start). Exchanges the code,
|
|
209
|
+
* adds a new terminal row with the encrypted credentials and says which tenant/user/row it ended in.
|
|
210
|
+
* `cookieOptional` for native apps, whose system browser does not hold the cookie of the app's HTTP session: there
|
|
211
|
+
* the signed `state` (tenant, user, validity) is all that binds the return.
|
|
212
|
+
*/
|
|
213
|
+
completeMercadoPagoOAuth(code: string, state: string | undefined, nonceCookie: string | undefined, isCookieOptional?: (destination: string | undefined) => boolean): Promise<{
|
|
214
|
+
scopeId: string;
|
|
215
|
+
actorId: string;
|
|
216
|
+
id: string;
|
|
217
|
+
terminals: number;
|
|
218
|
+
}>;
|
|
219
|
+
/** Inserts a new row (never overwrites); the tenant's first terminal becomes primary on its own. */
|
|
220
|
+
private save;
|
|
221
|
+
terminals(scopeId: string, id: string): Promise<TerminalSummary[]>;
|
|
222
|
+
/**
|
|
223
|
+
* The owner picks, from the real list, which terminal it is. For Mercado Pago it also makes sure the Store→POS
|
|
224
|
+
* hierarchy its Point API requires exists and switches the terminal to PDV mode — without blocking if any of that
|
|
225
|
+
* fails (uncertified account, freshly paired terminal): the choice is saved anyway.
|
|
226
|
+
*/
|
|
227
|
+
chooseTerminal(scopeId: string, id: string, terminalId: string, name?: string): Promise<ConnectionView>;
|
|
228
|
+
rename(scopeId: string, id: string, alias: string): Promise<ConnectionView>;
|
|
229
|
+
setPrimary(scopeId: string, id: string): Promise<void>;
|
|
230
|
+
disconnect(scopeId: string, id: string): Promise<void>;
|
|
231
|
+
private provisionMercadoPago;
|
|
232
|
+
/** Sends the charge to the chosen terminal. Returns once queued, not once paid. */
|
|
233
|
+
charge(scopeId: string, id: string, amountCents: number, reference: string): Promise<TerminalCharge>;
|
|
234
|
+
get(scopeId: string, id: string, paymentId: string): Promise<TerminalCharge>;
|
|
235
|
+
cancel(scopeId: string, id: string, paymentId: string): Promise<TerminalCharge>;
|
|
236
|
+
/** The status a point of sale should act on, with the adapter's `unknown` resolved (and logged). */
|
|
237
|
+
normalize(p: TerminalPayment): TerminalCharge;
|
|
238
|
+
/** Clip: `token` identifies the terminal (and the tenant). Never throws: a webhook always answers 200. */
|
|
239
|
+
handleClipNotice(token: string, paymentId: unknown): Promise<void>;
|
|
240
|
+
/** Mercado Pago: verifies `x-signature` and finds the tenant(s) by `user_id`. Never throws. */
|
|
241
|
+
handleMercadoPagoNotice(a: {
|
|
242
|
+
dataIdQuery: string | null;
|
|
243
|
+
xSignature: string | undefined;
|
|
244
|
+
xRequestId: string | undefined;
|
|
245
|
+
body: unknown;
|
|
246
|
+
}): Promise<void>;
|
|
247
|
+
/** One active row per tenant whose Mercado Pago account is `mpUserId`. Short cache. */
|
|
248
|
+
private mercadoPagoRows;
|
|
249
|
+
private lookupAndNotify;
|
|
250
|
+
/**
|
|
251
|
+
* Credentials of the tenant's own account for online checkout (the money goes to them). With no `provider` it takes
|
|
252
|
+
* the primary terminal's. Mercado Pago refreshes its access token here.
|
|
253
|
+
*/
|
|
254
|
+
checkoutCredentials(scopeId: string, provider?: ConnectionProvider | null): Promise<OnlineCheckoutCredentials>;
|
|
255
|
+
/** There is something to ask with: encryption key and an active Mercado Pago account. */
|
|
256
|
+
canRefundMercadoPago(scopeId: string): Promise<boolean>;
|
|
257
|
+
/**
|
|
258
|
+
* The account (among those connected to the tenant) where the order exists. `no_answer` if one did not answer and
|
|
259
|
+
* none had it: it cannot be concluded the order does not exist.
|
|
260
|
+
*/
|
|
261
|
+
private accountOfOrder;
|
|
262
|
+
/**
|
|
263
|
+
* Asks (or asks again with the SAME key) for the refund of `amountCents` of the order, or just looks it up.
|
|
264
|
+
* - `key`: stable per refund; Mercado Pago remembers it 24 h, so retrying never creates a second refund.
|
|
265
|
+
* - `providerRefundId` known or `onlyLookup` (past the key's window): the order is only read, nothing is sent.
|
|
266
|
+
* - `firstAttempt`: if the order is not in the connected account it is rejected; on a retry it stays pending (the
|
|
267
|
+
* first attempt may have gone through).
|
|
268
|
+
* - `claimed`: ids of refunds of that order that already belong to OTHER records (not to be mistaken for this one).
|
|
269
|
+
* Never throws because of the provider: no answer = `pending`.
|
|
270
|
+
*/
|
|
271
|
+
refundMercadoPago(scopeId: string, a: {
|
|
272
|
+
orderId: string;
|
|
273
|
+
amountCents: number;
|
|
274
|
+
key: string;
|
|
275
|
+
providerRefundId: string | null;
|
|
276
|
+
claimed: string[];
|
|
277
|
+
onlyLookup?: boolean;
|
|
278
|
+
firstAttempt?: boolean;
|
|
279
|
+
}): Promise<RefundResult>;
|
|
280
|
+
/** The row, its decrypted (and, for Mercado Pago, refreshed) credentials and a ready adapter. */
|
|
281
|
+
adapterOf(scopeId: string, id: string): Promise<{
|
|
282
|
+
config: ConnectionRecord;
|
|
283
|
+
credentials: ConnectionCredentials;
|
|
284
|
+
adapter: TerminalPaymentsPort;
|
|
285
|
+
}>;
|
|
286
|
+
/** Renews the Mercado Pago access token 5 minutes before it expires; Mercado Pago rotates the refresh token too. */
|
|
287
|
+
private refreshed;
|
|
288
|
+
private mercadoPagoTerminals;
|
|
289
|
+
private adapter;
|
|
290
|
+
}
|
|
291
|
+
export {};
|