@miguelmorales13/nestkit 0.22.0 → 0.25.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 +182 -5
- package/dist/email/adapters/zeptomail.email.d.ts +27 -0
- package/dist/email/email.module.d.ts +2 -1
- package/dist/email/email.options.d.ts +10 -1
- package/dist/email/index.cjs +90 -10
- package/dist/email/index.d.ts +2 -1
- package/dist/email/index.js +89 -9
- package/dist/index.cjs +5 -5
- package/dist/index.js +6 -6
- package/dist/payments/adapters/mercadopago-connect.d.ts +70 -0
- package/dist/payments/adapters/mercadopago.terminal.d.ts +57 -0
- package/dist/payments/index.cjs +153 -1
- package/dist/payments/index.d.ts +3 -1
- package/dist/payments/index.js +152 -0
- package/dist/payments/payments.port.d.ts +24 -0
- package/dist/push/channels/fcm.channel.d.ts +73 -0
- package/dist/push/channels/webpush.channel.d.ts +62 -0
- package/dist/push/index.cjs +449 -0
- package/dist/push/index.d.ts +9 -0
- package/dist/push/index.js +449 -0
- package/dist/push/push.module.d.ts +40 -0
- package/dist/push/push.options.d.ts +40 -0
- package/dist/push/push.port.d.ts +148 -0
- package/dist/push/push.service.d.ts +31 -0
- package/dist/security/index.cjs +35 -0
- package/dist/security/index.d.ts +2 -0
- package/dist/security/index.js +35 -0
- package/dist/security/signed-state.d.ts +42 -0
- package/package.json +20 -4
package/dist/index.js
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SUPABASE_ANON_CLIENT,
|
|
3
|
+
SUPABASE_SERVICE_ROLE_CLIENT,
|
|
4
|
+
SupabaseModule
|
|
5
|
+
} from "./chunk-PA24P76K.js";
|
|
1
6
|
import "./chunk-EPVKCBPT.js";
|
|
2
7
|
import {
|
|
3
8
|
PG_POOL,
|
|
4
9
|
PgModule,
|
|
5
10
|
withTenantScope
|
|
6
11
|
} from "./chunk-VKOPDDCC.js";
|
|
7
|
-
import {
|
|
8
|
-
SUPABASE_ANON_CLIENT,
|
|
9
|
-
SUPABASE_SERVICE_ROLE_CLIENT,
|
|
10
|
-
SupabaseModule
|
|
11
|
-
} from "./chunk-PA24P76K.js";
|
|
12
12
|
import {
|
|
13
13
|
REQUEST_ID_HEADER,
|
|
14
14
|
RequestIdMiddleware,
|
|
15
15
|
TrackingModule
|
|
16
16
|
} from "./chunk-4PFAKLT3.js";
|
|
17
|
-
import "./chunk-ODPSIFOB.js";
|
|
18
17
|
import {
|
|
19
18
|
I18nInterceptor,
|
|
20
19
|
I18nModule,
|
|
@@ -22,6 +21,7 @@ import {
|
|
|
22
21
|
matchLanguage,
|
|
23
22
|
translateOr
|
|
24
23
|
} from "./chunk-6RAPVLJM.js";
|
|
24
|
+
import "./chunk-ODPSIFOB.js";
|
|
25
25
|
import {
|
|
26
26
|
BaseCrudService,
|
|
27
27
|
ListQuery,
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export interface MercadoPagoConnectOptions {
|
|
2
|
+
/** From the Mercado Pago application, not from any connected seller. */
|
|
3
|
+
clientId: string;
|
|
4
|
+
clientSecret: string;
|
|
5
|
+
/** Must match, character for character, the Redirect URI registered on the application. */
|
|
6
|
+
redirectUri: string;
|
|
7
|
+
/**
|
|
8
|
+
* Defaults to `offline_access`. Mercado Pago documents this as the one
|
|
9
|
+
* thing that turns on `refresh_token` for the authorization-code flow —
|
|
10
|
+
* drop it and every connection stops working the moment its access token
|
|
11
|
+
* expires, with no way back short of sending the seller through consent
|
|
12
|
+
* again.
|
|
13
|
+
*/
|
|
14
|
+
scope?: string;
|
|
15
|
+
/** Override for tests. Defaults to `https://api.mercadopago.com`. */
|
|
16
|
+
tokenBaseUrl?: string;
|
|
17
|
+
/** Override for tests. Defaults to `https://auth.mercadopago.com/authorization`. */
|
|
18
|
+
authorizeUrl?: string;
|
|
19
|
+
/** Override for tests. Defaults to the global `fetch`. */
|
|
20
|
+
fetchImpl?: typeof fetch;
|
|
21
|
+
}
|
|
22
|
+
/** A connected seller's tokens, ready to build a {@link MercadoPagoTerminalPayments} for them. */
|
|
23
|
+
export interface MercadoPagoConnectTokens {
|
|
24
|
+
accessToken: string;
|
|
25
|
+
refreshToken: string;
|
|
26
|
+
/** The seller's own Mercado Pago user id — what {@link MercadoPagoTerminalPayments.createStore} takes as `userId`. */
|
|
27
|
+
userId: string;
|
|
28
|
+
scope?: string;
|
|
29
|
+
liveMode?: boolean;
|
|
30
|
+
tokenType?: string;
|
|
31
|
+
expiresAt: Date;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Connects a seller's Mercado Pago account by OAuth: the marketplace flow a
|
|
35
|
+
* single fixed `MERCADOPAGO_ACCESS_TOKEN` (see
|
|
36
|
+
* {@link MercadoPagoTerminalPayments}) cannot cover — one integrator, many
|
|
37
|
+
* connected sellers, each operating their own terminals on their own
|
|
38
|
+
* tokens, none of them typed into a form by hand.
|
|
39
|
+
*
|
|
40
|
+
* This is deliberately a separate class from `MercadoPagoTerminalPayments`
|
|
41
|
+
* rather than an extra constructor mode: connecting a seller and charging a
|
|
42
|
+
* terminal happen at different times, usually in different parts of an
|
|
43
|
+
* application (an onboarding screen versus a checkout), and a seller's
|
|
44
|
+
* access token is the *output* of this class and the *input* to that one.
|
|
45
|
+
*/
|
|
46
|
+
export declare class MercadoPagoConnect {
|
|
47
|
+
private readonly options;
|
|
48
|
+
private readonly tokenBaseUrl;
|
|
49
|
+
private readonly authorizeUrl;
|
|
50
|
+
private readonly fetchImpl;
|
|
51
|
+
constructor(options: MercadoPagoConnectOptions);
|
|
52
|
+
/**
|
|
53
|
+
* The consent-screen URL to send the seller to. `state` comes back
|
|
54
|
+
* untouched on your callback — sign it yourself (see
|
|
55
|
+
* `@miguelmorales13/nestkit/security`'s `signState`) if you need to prove
|
|
56
|
+
* your own application started the flow and tie the callback back to
|
|
57
|
+
* whoever did; Mercado Pago's redirect carries it, nothing more.
|
|
58
|
+
*/
|
|
59
|
+
authorizationUrl(state?: string): string;
|
|
60
|
+
/** Trades the callback's `code` for the seller's tokens. */
|
|
61
|
+
exchangeCode(code: string): Promise<MercadoPagoConnectTokens>;
|
|
62
|
+
/**
|
|
63
|
+
* Renews an access token. Mercado Pago **rotates the refresh token on
|
|
64
|
+
* every renewal** — persist the whole result, not just `accessToken`, or
|
|
65
|
+
* the seller's connection silently stops renewing the first time this
|
|
66
|
+
* runs.
|
|
67
|
+
*/
|
|
68
|
+
refresh(refreshToken: string): Promise<MercadoPagoConnectTokens>;
|
|
69
|
+
private token;
|
|
70
|
+
}
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import type { MercadoPagoPointOptions } from '../payments.options.js';
|
|
2
2
|
import type { ChargeInput, Terminal, TerminalPayment, TerminalPaymentsPort, TerminalWebhookEvent, WebhookRequestLike } from '../payments.port.js';
|
|
3
|
+
/**
|
|
4
|
+
* Whether `terminalId` belongs to a model Mercado Pago will switch to PDV
|
|
5
|
+
* mode over the API. The id carries its model as a prefix
|
|
6
|
+
* (`NEWLAND_N950__SBX0000001`) — exactly the pattern
|
|
7
|
+
* `PATCH /terminals/v1/setup` validates and, for an unsupported model,
|
|
8
|
+
* rejects with a bare `"'$.terminals[0].id' - does not match pattern"` that
|
|
9
|
+
* names no model and suggests nothing is wrong with the id itself.
|
|
10
|
+
*/
|
|
11
|
+
export declare function isIntegrableTerminalModel(terminalId: string): boolean;
|
|
3
12
|
/**
|
|
4
13
|
* Mercado Pago Point over the Orders API.
|
|
5
14
|
*
|
|
@@ -42,6 +51,54 @@ export declare class MercadoPagoTerminalPayments implements TerminalPaymentsPort
|
|
|
42
51
|
* expensive way to discover this.
|
|
43
52
|
*/
|
|
44
53
|
setOperatingMode(terminalIds: string[], mode: 'PDV' | 'STANDALONE'): Promise<Terminal[]>;
|
|
54
|
+
/**
|
|
55
|
+
* Creates the "Store" a terminal has to belong to before the Orders API
|
|
56
|
+
* will accept a charge for it — Mercado Pago's own Point documentation
|
|
57
|
+
* describes a mandatory hierarchy, **Store → POS → Terminal**, and a
|
|
58
|
+
* terminal that only ever paired through the merchant app (standalone,
|
|
59
|
+
* never through this call) sits outside it: charging it fails with
|
|
60
|
+
* `PA_UNAUTHORIZED_RESULT_FROM_POLICIES`, a message that names no missing
|
|
61
|
+
* Store because the policy engine that rejects it does not know that is
|
|
62
|
+
* the reason either.
|
|
63
|
+
*
|
|
64
|
+
* `location` and `businessHours` are Mercado Pago requirements, not
|
|
65
|
+
* choices this method makes for you — filled with real values it is your
|
|
66
|
+
* integration's call whether to ask the merchant for them or default them.
|
|
67
|
+
*
|
|
68
|
+
* **Check {@link listTerminals} first.** A terminal paired through the app
|
|
69
|
+
* can already carry a `storeId`/`posId` of its own — Mercado Pago assigns
|
|
70
|
+
* them the moment it is paired, standalone or not. Calling this again for
|
|
71
|
+
* a terminal that already has one does not fail, but it litters the
|
|
72
|
+
* merchant's Mercado Pago panel with a Store nothing points at.
|
|
73
|
+
*/
|
|
74
|
+
createStore(input: {
|
|
75
|
+
/** The Mercado Pago user id the Store belongs to — the seller's, from their OAuth connection, not the application's. */
|
|
76
|
+
userId: string;
|
|
77
|
+
name: string;
|
|
78
|
+
location: {
|
|
79
|
+
streetNumber: string;
|
|
80
|
+
streetName: string;
|
|
81
|
+
cityName: string;
|
|
82
|
+
stateName: string;
|
|
83
|
+
latitude: number;
|
|
84
|
+
longitude: number;
|
|
85
|
+
};
|
|
86
|
+
/** All seven days. Each is a list of open/close windows, `HH:MM` 24h. */
|
|
87
|
+
businessHours: Record<'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'sunday', {
|
|
88
|
+
open: string;
|
|
89
|
+
close: string;
|
|
90
|
+
}[]>;
|
|
91
|
+
}): Promise<string>;
|
|
92
|
+
/**
|
|
93
|
+
* Creates the "POS" (register/cash-point) inside a Store — the second
|
|
94
|
+
* link in Mercado Pago's Store → POS → Terminal chain, and the one that
|
|
95
|
+
* actually takes a terminal out of `standalone` once it is assigned to it.
|
|
96
|
+
*/
|
|
97
|
+
createPos(input: {
|
|
98
|
+
storeId: string;
|
|
99
|
+
name: string;
|
|
100
|
+
externalId?: string;
|
|
101
|
+
}): Promise<string>;
|
|
45
102
|
/**
|
|
46
103
|
* Forces a final status on an order created with **test credentials**, so a
|
|
47
104
|
* whole flow — order, status change, webhook — can be exercised without a
|
package/dist/payments/index.cjs
CHANGED
|
@@ -100,6 +100,10 @@ async function requestJson(provider, fetchImpl, url, init) {
|
|
|
100
100
|
// src/payments/adapters/mercadopago.terminal.ts
|
|
101
101
|
var PROVIDER = "mercadopago";
|
|
102
102
|
var DEFAULT_BASE_URL = "https://api.mercadopago.com";
|
|
103
|
+
var INTEGRABLE_MODELS = ["NEWLAND_N950", "PAX_A910", "INGENICO_MOVE2500"];
|
|
104
|
+
function isIntegrableTerminalModel(terminalId) {
|
|
105
|
+
return INTEGRABLE_MODELS.some((model) => terminalId.startsWith(`${model}__`));
|
|
106
|
+
}
|
|
103
107
|
function toStatus(status) {
|
|
104
108
|
switch (status) {
|
|
105
109
|
case "created":
|
|
@@ -162,6 +166,9 @@ var MercadoPagoTerminalPayments = class {
|
|
|
162
166
|
// Mercado Pago's list says nothing about connectivity.
|
|
163
167
|
online: void 0,
|
|
164
168
|
operatingMode: terminal.operating_mode === "PDV" ? "pdv" : terminal.operating_mode === "STANDALONE" ? "standalone" : "unknown",
|
|
169
|
+
storeId: terminal.store_id,
|
|
170
|
+
posId: terminal.pos_id === void 0 ? void 0 : String(terminal.pos_id),
|
|
171
|
+
integrable: isIntegrableTerminalModel(terminal.id),
|
|
165
172
|
raw: terminal
|
|
166
173
|
});
|
|
167
174
|
}
|
|
@@ -281,6 +288,15 @@ var MercadoPagoTerminalPayments = class {
|
|
|
281
288
|
* expensive way to discover this.
|
|
282
289
|
*/
|
|
283
290
|
async setOperatingMode(terminalIds, mode) {
|
|
291
|
+
if (mode === "PDV") {
|
|
292
|
+
const unsupported = terminalIds.filter((id) => !isIntegrableTerminalModel(id));
|
|
293
|
+
if (unsupported.length > 0) {
|
|
294
|
+
throw new UnsupportedByProviderException(
|
|
295
|
+
PROVIDER,
|
|
296
|
+
`Mercado Pago only switches Point Smart 1 and Point Smart 2 to PDV mode over the API. Not integrable: ${unsupported.join(", ")}. Other models (Point Air among them) are official Mercado Pago hardware but standalone-only, permanently \u2014 there is no PDV mode for them to switch to.`
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
284
300
|
const response = await requestJson(
|
|
285
301
|
PROVIDER,
|
|
286
302
|
this.fetchImpl,
|
|
@@ -294,9 +310,71 @@ var MercadoPagoTerminalPayments = class {
|
|
|
294
310
|
return (_nullishCoalesce(response.terminals, () => ( []))).map((terminal) => ({
|
|
295
311
|
id: terminal.id,
|
|
296
312
|
operatingMode: terminal.operating_mode === "PDV" ? "pdv" : terminal.operating_mode === "STANDALONE" ? "standalone" : "unknown",
|
|
313
|
+
integrable: isIntegrableTerminalModel(terminal.id),
|
|
297
314
|
raw: terminal
|
|
298
315
|
}));
|
|
299
316
|
}
|
|
317
|
+
/**
|
|
318
|
+
* Creates the "Store" a terminal has to belong to before the Orders API
|
|
319
|
+
* will accept a charge for it — Mercado Pago's own Point documentation
|
|
320
|
+
* describes a mandatory hierarchy, **Store → POS → Terminal**, and a
|
|
321
|
+
* terminal that only ever paired through the merchant app (standalone,
|
|
322
|
+
* never through this call) sits outside it: charging it fails with
|
|
323
|
+
* `PA_UNAUTHORIZED_RESULT_FROM_POLICIES`, a message that names no missing
|
|
324
|
+
* Store because the policy engine that rejects it does not know that is
|
|
325
|
+
* the reason either.
|
|
326
|
+
*
|
|
327
|
+
* `location` and `businessHours` are Mercado Pago requirements, not
|
|
328
|
+
* choices this method makes for you — filled with real values it is your
|
|
329
|
+
* integration's call whether to ask the merchant for them or default them.
|
|
330
|
+
*
|
|
331
|
+
* **Check {@link listTerminals} first.** A terminal paired through the app
|
|
332
|
+
* can already carry a `storeId`/`posId` of its own — Mercado Pago assigns
|
|
333
|
+
* them the moment it is paired, standalone or not. Calling this again for
|
|
334
|
+
* a terminal that already has one does not fail, but it litters the
|
|
335
|
+
* merchant's Mercado Pago panel with a Store nothing points at.
|
|
336
|
+
*/
|
|
337
|
+
async createStore(input) {
|
|
338
|
+
const data = await requestJson(
|
|
339
|
+
PROVIDER,
|
|
340
|
+
this.fetchImpl,
|
|
341
|
+
`${this.baseUrl}/users/${encodeURIComponent(input.userId)}/stores`,
|
|
342
|
+
{
|
|
343
|
+
method: "POST",
|
|
344
|
+
headers: this.headers(),
|
|
345
|
+
body: JSON.stringify({
|
|
346
|
+
name: input.name,
|
|
347
|
+
business_hours: input.businessHours,
|
|
348
|
+
location: {
|
|
349
|
+
street_number: input.location.streetNumber,
|
|
350
|
+
street_name: input.location.streetName,
|
|
351
|
+
city_name: input.location.cityName,
|
|
352
|
+
state_name: input.location.stateName,
|
|
353
|
+
latitude: input.location.latitude,
|
|
354
|
+
longitude: input.location.longitude
|
|
355
|
+
}
|
|
356
|
+
})
|
|
357
|
+
}
|
|
358
|
+
);
|
|
359
|
+
return data.id;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Creates the "POS" (register/cash-point) inside a Store — the second
|
|
363
|
+
* link in Mercado Pago's Store → POS → Terminal chain, and the one that
|
|
364
|
+
* actually takes a terminal out of `standalone` once it is assigned to it.
|
|
365
|
+
*/
|
|
366
|
+
async createPos(input) {
|
|
367
|
+
const data = await requestJson(PROVIDER, this.fetchImpl, `${this.baseUrl}/pos`, {
|
|
368
|
+
method: "POST",
|
|
369
|
+
headers: this.headers(),
|
|
370
|
+
body: JSON.stringify({
|
|
371
|
+
name: input.name,
|
|
372
|
+
store_id: input.storeId,
|
|
373
|
+
external_id: input.externalId
|
|
374
|
+
})
|
|
375
|
+
});
|
|
376
|
+
return data.id;
|
|
377
|
+
}
|
|
300
378
|
/**
|
|
301
379
|
* Forces a final status on an order created with **test credentials**, so a
|
|
302
380
|
* whole flow — order, status change, webhook — can be exercised without a
|
|
@@ -780,6 +858,80 @@ function createTerminalPaymentsWebhookController(options) {
|
|
|
780
858
|
return TerminalPaymentsWebhookControllerHost;
|
|
781
859
|
}
|
|
782
860
|
|
|
861
|
+
// src/payments/adapters/mercadopago-connect.ts
|
|
862
|
+
var PROVIDER3 = "mercadopago";
|
|
863
|
+
var DEFAULT_TOKEN_BASE_URL = "https://api.mercadopago.com";
|
|
864
|
+
var DEFAULT_AUTHORIZE_URL = "https://auth.mercadopago.com/authorization";
|
|
865
|
+
var MercadoPagoConnect = class {
|
|
866
|
+
constructor(options) {
|
|
867
|
+
this.options = options;
|
|
868
|
+
this.tokenBaseUrl = (_nullishCoalesce(options.tokenBaseUrl, () => ( DEFAULT_TOKEN_BASE_URL))).replace(/\/$/, "");
|
|
869
|
+
this.authorizeUrl = _nullishCoalesce(options.authorizeUrl, () => ( DEFAULT_AUTHORIZE_URL));
|
|
870
|
+
this.fetchImpl = _nullishCoalesce(options.fetchImpl, () => ( globalThis.fetch));
|
|
871
|
+
}
|
|
872
|
+
/**
|
|
873
|
+
* The consent-screen URL to send the seller to. `state` comes back
|
|
874
|
+
* untouched on your callback — sign it yourself (see
|
|
875
|
+
* `@miguelmorales13/nestkit/security`'s `signState`) if you need to prove
|
|
876
|
+
* your own application started the flow and tie the callback back to
|
|
877
|
+
* whoever did; Mercado Pago's redirect carries it, nothing more.
|
|
878
|
+
*/
|
|
879
|
+
authorizationUrl(state) {
|
|
880
|
+
const params = new URLSearchParams({
|
|
881
|
+
client_id: this.options.clientId,
|
|
882
|
+
response_type: "code",
|
|
883
|
+
platform_id: "mp",
|
|
884
|
+
redirect_uri: this.options.redirectUri,
|
|
885
|
+
scope: _nullishCoalesce(this.options.scope, () => ( "offline_access")),
|
|
886
|
+
...state ? { state } : {}
|
|
887
|
+
});
|
|
888
|
+
return `${this.authorizeUrl}?${params.toString()}`;
|
|
889
|
+
}
|
|
890
|
+
/** Trades the callback's `code` for the seller's tokens. */
|
|
891
|
+
exchangeCode(code) {
|
|
892
|
+
return this.token({ grant_type: "authorization_code", code, redirect_uri: this.options.redirectUri });
|
|
893
|
+
}
|
|
894
|
+
/**
|
|
895
|
+
* Renews an access token. Mercado Pago **rotates the refresh token on
|
|
896
|
+
* every renewal** — persist the whole result, not just `accessToken`, or
|
|
897
|
+
* the seller's connection silently stops renewing the first time this
|
|
898
|
+
* runs.
|
|
899
|
+
*/
|
|
900
|
+
refresh(refreshToken) {
|
|
901
|
+
return this.token({ grant_type: "refresh_token", refresh_token: refreshToken });
|
|
902
|
+
}
|
|
903
|
+
async token(extra) {
|
|
904
|
+
const data = await requestJson(PROVIDER3, this.fetchImpl, `${this.tokenBaseUrl}/oauth/token`, {
|
|
905
|
+
method: "POST",
|
|
906
|
+
headers: { "content-type": "application/json" },
|
|
907
|
+
body: JSON.stringify({
|
|
908
|
+
client_id: this.options.clientId,
|
|
909
|
+
client_secret: this.options.clientSecret,
|
|
910
|
+
...extra
|
|
911
|
+
})
|
|
912
|
+
});
|
|
913
|
+
if (!data.access_token || !data.refresh_token) {
|
|
914
|
+
throw new TerminalPaymentProviderException(
|
|
915
|
+
PROVIDER3,
|
|
916
|
+
200,
|
|
917
|
+
data,
|
|
918
|
+
"Mercado Pago did not return both an access and a refresh token \u2014 check that scope includes offline_access."
|
|
919
|
+
);
|
|
920
|
+
}
|
|
921
|
+
return {
|
|
922
|
+
accessToken: data.access_token,
|
|
923
|
+
refreshToken: data.refresh_token,
|
|
924
|
+
userId: String(_nullishCoalesce(data.user_id, () => ( ""))),
|
|
925
|
+
scope: data.scope,
|
|
926
|
+
liveMode: data.live_mode,
|
|
927
|
+
tokenType: data.token_type,
|
|
928
|
+
expiresAt: new Date(Date.now() + (_nullishCoalesce(data.expires_in, () => ( 0))) * 1e3)
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
};
|
|
932
|
+
|
|
933
|
+
|
|
934
|
+
|
|
783
935
|
|
|
784
936
|
|
|
785
937
|
|
|
@@ -793,4 +945,4 @@ function createTerminalPaymentsWebhookController(options) {
|
|
|
793
945
|
|
|
794
946
|
|
|
795
947
|
|
|
796
|
-
exports.ClipTerminalPayments = ClipTerminalPayments; exports.MercadoPagoTerminalPayments = MercadoPagoTerminalPayments; exports.PAYMENTS_OPTIONS = PAYMENTS_OPTIONS; exports.PaymentsModule = PaymentsModule; exports.TERMINAL_PAYMENTS = TERMINAL_PAYMENTS; exports.TerminalPaymentProviderException = TerminalPaymentProviderException; exports.UnsupportedByProviderException = UnsupportedByProviderException; exports.assertCents = assertCents; exports.centsToDecimalString = centsToDecimalString; exports.createTerminalPaymentsAdapter = createTerminalPaymentsAdapter; exports.createTerminalPaymentsWebhookController = createTerminalPaymentsWebhookController; exports.decimalToCents = decimalToCents; exports.resolvePaymentsOptions = resolvePaymentsOptions;
|
|
948
|
+
exports.ClipTerminalPayments = ClipTerminalPayments; exports.MercadoPagoConnect = MercadoPagoConnect; exports.MercadoPagoTerminalPayments = MercadoPagoTerminalPayments; exports.PAYMENTS_OPTIONS = PAYMENTS_OPTIONS; exports.PaymentsModule = PaymentsModule; exports.TERMINAL_PAYMENTS = TERMINAL_PAYMENTS; exports.TerminalPaymentProviderException = TerminalPaymentProviderException; exports.UnsupportedByProviderException = UnsupportedByProviderException; exports.assertCents = assertCents; exports.centsToDecimalString = centsToDecimalString; exports.createTerminalPaymentsAdapter = createTerminalPaymentsAdapter; exports.createTerminalPaymentsWebhookController = createTerminalPaymentsWebhookController; exports.decimalToCents = decimalToCents; exports.isIntegrableTerminalModel = isIntegrableTerminalModel; exports.resolvePaymentsOptions = resolvePaymentsOptions;
|
package/dist/payments/index.d.ts
CHANGED
|
@@ -5,8 +5,10 @@ export type { PaymentsOptions, PaymentsProvider, MercadoPagoPointOptions, ClipPi
|
|
|
5
5
|
export { PaymentsModule, createTerminalPaymentsAdapter, resolvePaymentsOptions, } from './payments.module.js';
|
|
6
6
|
export { createTerminalPaymentsWebhookController } from './payments-webhook.controller.js';
|
|
7
7
|
export type { CreateTerminalPaymentsWebhookControllerOptions } from './payments-webhook.controller.js';
|
|
8
|
-
export { MercadoPagoTerminalPayments } from './adapters/mercadopago.terminal.js';
|
|
8
|
+
export { MercadoPagoTerminalPayments, isIntegrableTerminalModel } from './adapters/mercadopago.terminal.js';
|
|
9
9
|
export { ClipTerminalPayments } from './adapters/clip.terminal.js';
|
|
10
10
|
export type { ClipTransaction } from './adapters/clip.terminal.js';
|
|
11
|
+
export { MercadoPagoConnect } from './adapters/mercadopago-connect.js';
|
|
12
|
+
export type { MercadoPagoConnectOptions, MercadoPagoConnectTokens } from './adapters/mercadopago-connect.js';
|
|
11
13
|
export { TerminalPaymentProviderException, UnsupportedByProviderException } from './provider-http.js';
|
|
12
14
|
export { assertCents, centsToDecimalString, decimalToCents } from './money.js';
|
package/dist/payments/index.js
CHANGED
|
@@ -100,6 +100,10 @@ async function requestJson(provider, fetchImpl, url, init) {
|
|
|
100
100
|
// src/payments/adapters/mercadopago.terminal.ts
|
|
101
101
|
var PROVIDER = "mercadopago";
|
|
102
102
|
var DEFAULT_BASE_URL = "https://api.mercadopago.com";
|
|
103
|
+
var INTEGRABLE_MODELS = ["NEWLAND_N950", "PAX_A910", "INGENICO_MOVE2500"];
|
|
104
|
+
function isIntegrableTerminalModel(terminalId) {
|
|
105
|
+
return INTEGRABLE_MODELS.some((model) => terminalId.startsWith(`${model}__`));
|
|
106
|
+
}
|
|
103
107
|
function toStatus(status) {
|
|
104
108
|
switch (status) {
|
|
105
109
|
case "created":
|
|
@@ -162,6 +166,9 @@ var MercadoPagoTerminalPayments = class {
|
|
|
162
166
|
// Mercado Pago's list says nothing about connectivity.
|
|
163
167
|
online: void 0,
|
|
164
168
|
operatingMode: terminal.operating_mode === "PDV" ? "pdv" : terminal.operating_mode === "STANDALONE" ? "standalone" : "unknown",
|
|
169
|
+
storeId: terminal.store_id,
|
|
170
|
+
posId: terminal.pos_id === void 0 ? void 0 : String(terminal.pos_id),
|
|
171
|
+
integrable: isIntegrableTerminalModel(terminal.id),
|
|
165
172
|
raw: terminal
|
|
166
173
|
});
|
|
167
174
|
}
|
|
@@ -281,6 +288,15 @@ var MercadoPagoTerminalPayments = class {
|
|
|
281
288
|
* expensive way to discover this.
|
|
282
289
|
*/
|
|
283
290
|
async setOperatingMode(terminalIds, mode) {
|
|
291
|
+
if (mode === "PDV") {
|
|
292
|
+
const unsupported = terminalIds.filter((id) => !isIntegrableTerminalModel(id));
|
|
293
|
+
if (unsupported.length > 0) {
|
|
294
|
+
throw new UnsupportedByProviderException(
|
|
295
|
+
PROVIDER,
|
|
296
|
+
`Mercado Pago only switches Point Smart 1 and Point Smart 2 to PDV mode over the API. Not integrable: ${unsupported.join(", ")}. Other models (Point Air among them) are official Mercado Pago hardware but standalone-only, permanently \u2014 there is no PDV mode for them to switch to.`
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
284
300
|
const response = await requestJson(
|
|
285
301
|
PROVIDER,
|
|
286
302
|
this.fetchImpl,
|
|
@@ -294,9 +310,71 @@ var MercadoPagoTerminalPayments = class {
|
|
|
294
310
|
return (response.terminals ?? []).map((terminal) => ({
|
|
295
311
|
id: terminal.id,
|
|
296
312
|
operatingMode: terminal.operating_mode === "PDV" ? "pdv" : terminal.operating_mode === "STANDALONE" ? "standalone" : "unknown",
|
|
313
|
+
integrable: isIntegrableTerminalModel(terminal.id),
|
|
297
314
|
raw: terminal
|
|
298
315
|
}));
|
|
299
316
|
}
|
|
317
|
+
/**
|
|
318
|
+
* Creates the "Store" a terminal has to belong to before the Orders API
|
|
319
|
+
* will accept a charge for it — Mercado Pago's own Point documentation
|
|
320
|
+
* describes a mandatory hierarchy, **Store → POS → Terminal**, and a
|
|
321
|
+
* terminal that only ever paired through the merchant app (standalone,
|
|
322
|
+
* never through this call) sits outside it: charging it fails with
|
|
323
|
+
* `PA_UNAUTHORIZED_RESULT_FROM_POLICIES`, a message that names no missing
|
|
324
|
+
* Store because the policy engine that rejects it does not know that is
|
|
325
|
+
* the reason either.
|
|
326
|
+
*
|
|
327
|
+
* `location` and `businessHours` are Mercado Pago requirements, not
|
|
328
|
+
* choices this method makes for you — filled with real values it is your
|
|
329
|
+
* integration's call whether to ask the merchant for them or default them.
|
|
330
|
+
*
|
|
331
|
+
* **Check {@link listTerminals} first.** A terminal paired through the app
|
|
332
|
+
* can already carry a `storeId`/`posId` of its own — Mercado Pago assigns
|
|
333
|
+
* them the moment it is paired, standalone or not. Calling this again for
|
|
334
|
+
* a terminal that already has one does not fail, but it litters the
|
|
335
|
+
* merchant's Mercado Pago panel with a Store nothing points at.
|
|
336
|
+
*/
|
|
337
|
+
async createStore(input) {
|
|
338
|
+
const data = await requestJson(
|
|
339
|
+
PROVIDER,
|
|
340
|
+
this.fetchImpl,
|
|
341
|
+
`${this.baseUrl}/users/${encodeURIComponent(input.userId)}/stores`,
|
|
342
|
+
{
|
|
343
|
+
method: "POST",
|
|
344
|
+
headers: this.headers(),
|
|
345
|
+
body: JSON.stringify({
|
|
346
|
+
name: input.name,
|
|
347
|
+
business_hours: input.businessHours,
|
|
348
|
+
location: {
|
|
349
|
+
street_number: input.location.streetNumber,
|
|
350
|
+
street_name: input.location.streetName,
|
|
351
|
+
city_name: input.location.cityName,
|
|
352
|
+
state_name: input.location.stateName,
|
|
353
|
+
latitude: input.location.latitude,
|
|
354
|
+
longitude: input.location.longitude
|
|
355
|
+
}
|
|
356
|
+
})
|
|
357
|
+
}
|
|
358
|
+
);
|
|
359
|
+
return data.id;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Creates the "POS" (register/cash-point) inside a Store — the second
|
|
363
|
+
* link in Mercado Pago's Store → POS → Terminal chain, and the one that
|
|
364
|
+
* actually takes a terminal out of `standalone` once it is assigned to it.
|
|
365
|
+
*/
|
|
366
|
+
async createPos(input) {
|
|
367
|
+
const data = await requestJson(PROVIDER, this.fetchImpl, `${this.baseUrl}/pos`, {
|
|
368
|
+
method: "POST",
|
|
369
|
+
headers: this.headers(),
|
|
370
|
+
body: JSON.stringify({
|
|
371
|
+
name: input.name,
|
|
372
|
+
store_id: input.storeId,
|
|
373
|
+
external_id: input.externalId
|
|
374
|
+
})
|
|
375
|
+
});
|
|
376
|
+
return data.id;
|
|
377
|
+
}
|
|
300
378
|
/**
|
|
301
379
|
* Forces a final status on an order created with **test credentials**, so a
|
|
302
380
|
* whole flow — order, status change, webhook — can be exercised without a
|
|
@@ -779,8 +857,81 @@ function createTerminalPaymentsWebhookController(options) {
|
|
|
779
857
|
], TerminalPaymentsWebhookControllerHost);
|
|
780
858
|
return TerminalPaymentsWebhookControllerHost;
|
|
781
859
|
}
|
|
860
|
+
|
|
861
|
+
// src/payments/adapters/mercadopago-connect.ts
|
|
862
|
+
var PROVIDER3 = "mercadopago";
|
|
863
|
+
var DEFAULT_TOKEN_BASE_URL = "https://api.mercadopago.com";
|
|
864
|
+
var DEFAULT_AUTHORIZE_URL = "https://auth.mercadopago.com/authorization";
|
|
865
|
+
var MercadoPagoConnect = class {
|
|
866
|
+
constructor(options) {
|
|
867
|
+
this.options = options;
|
|
868
|
+
this.tokenBaseUrl = (options.tokenBaseUrl ?? DEFAULT_TOKEN_BASE_URL).replace(/\/$/, "");
|
|
869
|
+
this.authorizeUrl = options.authorizeUrl ?? DEFAULT_AUTHORIZE_URL;
|
|
870
|
+
this.fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
871
|
+
}
|
|
872
|
+
/**
|
|
873
|
+
* The consent-screen URL to send the seller to. `state` comes back
|
|
874
|
+
* untouched on your callback — sign it yourself (see
|
|
875
|
+
* `@miguelmorales13/nestkit/security`'s `signState`) if you need to prove
|
|
876
|
+
* your own application started the flow and tie the callback back to
|
|
877
|
+
* whoever did; Mercado Pago's redirect carries it, nothing more.
|
|
878
|
+
*/
|
|
879
|
+
authorizationUrl(state) {
|
|
880
|
+
const params = new URLSearchParams({
|
|
881
|
+
client_id: this.options.clientId,
|
|
882
|
+
response_type: "code",
|
|
883
|
+
platform_id: "mp",
|
|
884
|
+
redirect_uri: this.options.redirectUri,
|
|
885
|
+
scope: this.options.scope ?? "offline_access",
|
|
886
|
+
...state ? { state } : {}
|
|
887
|
+
});
|
|
888
|
+
return `${this.authorizeUrl}?${params.toString()}`;
|
|
889
|
+
}
|
|
890
|
+
/** Trades the callback's `code` for the seller's tokens. */
|
|
891
|
+
exchangeCode(code) {
|
|
892
|
+
return this.token({ grant_type: "authorization_code", code, redirect_uri: this.options.redirectUri });
|
|
893
|
+
}
|
|
894
|
+
/**
|
|
895
|
+
* Renews an access token. Mercado Pago **rotates the refresh token on
|
|
896
|
+
* every renewal** — persist the whole result, not just `accessToken`, or
|
|
897
|
+
* the seller's connection silently stops renewing the first time this
|
|
898
|
+
* runs.
|
|
899
|
+
*/
|
|
900
|
+
refresh(refreshToken) {
|
|
901
|
+
return this.token({ grant_type: "refresh_token", refresh_token: refreshToken });
|
|
902
|
+
}
|
|
903
|
+
async token(extra) {
|
|
904
|
+
const data = await requestJson(PROVIDER3, this.fetchImpl, `${this.tokenBaseUrl}/oauth/token`, {
|
|
905
|
+
method: "POST",
|
|
906
|
+
headers: { "content-type": "application/json" },
|
|
907
|
+
body: JSON.stringify({
|
|
908
|
+
client_id: this.options.clientId,
|
|
909
|
+
client_secret: this.options.clientSecret,
|
|
910
|
+
...extra
|
|
911
|
+
})
|
|
912
|
+
});
|
|
913
|
+
if (!data.access_token || !data.refresh_token) {
|
|
914
|
+
throw new TerminalPaymentProviderException(
|
|
915
|
+
PROVIDER3,
|
|
916
|
+
200,
|
|
917
|
+
data,
|
|
918
|
+
"Mercado Pago did not return both an access and a refresh token \u2014 check that scope includes offline_access."
|
|
919
|
+
);
|
|
920
|
+
}
|
|
921
|
+
return {
|
|
922
|
+
accessToken: data.access_token,
|
|
923
|
+
refreshToken: data.refresh_token,
|
|
924
|
+
userId: String(data.user_id ?? ""),
|
|
925
|
+
scope: data.scope,
|
|
926
|
+
liveMode: data.live_mode,
|
|
927
|
+
tokenType: data.token_type,
|
|
928
|
+
expiresAt: new Date(Date.now() + (data.expires_in ?? 0) * 1e3)
|
|
929
|
+
};
|
|
930
|
+
}
|
|
931
|
+
};
|
|
782
932
|
export {
|
|
783
933
|
ClipTerminalPayments,
|
|
934
|
+
MercadoPagoConnect,
|
|
784
935
|
MercadoPagoTerminalPayments,
|
|
785
936
|
PAYMENTS_OPTIONS,
|
|
786
937
|
PaymentsModule,
|
|
@@ -792,5 +943,6 @@ export {
|
|
|
792
943
|
createTerminalPaymentsAdapter,
|
|
793
944
|
createTerminalPaymentsWebhookController,
|
|
794
945
|
decimalToCents,
|
|
946
|
+
isIntegrableTerminalModel,
|
|
795
947
|
resolvePaymentsOptions
|
|
796
948
|
};
|
|
@@ -28,6 +28,30 @@ export interface Terminal {
|
|
|
28
28
|
online?: boolean;
|
|
29
29
|
/** Mercado Pago only: `'pdv'` accepts API charges, `'standalone'` ignores them. */
|
|
30
30
|
operatingMode?: 'pdv' | 'standalone' | 'unknown';
|
|
31
|
+
/**
|
|
32
|
+
* Mercado Pago only: the Store and POS this terminal already belongs to,
|
|
33
|
+
* when it has one. Mercado Pago assigns these the first time the device is
|
|
34
|
+
* paired — even from its own merchant app, even while `operatingMode`
|
|
35
|
+
* stays `standalone` — so a terminal can carry a real `storeId`/`posId`
|
|
36
|
+
* before your integration ever calls {@link MercadoPagoTerminalPayments.createStore}.
|
|
37
|
+
* Reuse them instead of creating a second Store for the same terminal: the
|
|
38
|
+
* Orders API does not need more than one, and a merchant only sees the
|
|
39
|
+
* extra as clutter in their Mercado Pago panel.
|
|
40
|
+
*/
|
|
41
|
+
storeId?: string;
|
|
42
|
+
posId?: string;
|
|
43
|
+
/**
|
|
44
|
+
* Mercado Pago only: whether this terminal's model can be switched to PDV
|
|
45
|
+
* mode over the API at all. Only Point Smart 1 and Point Smart 2 can —
|
|
46
|
+
* confirmed with Mercado Pago support, not merely undocumented: **Point
|
|
47
|
+
* Air ships official Mercado Pago hardware, but is standalone-only by
|
|
48
|
+
* design, permanently.** {@link MercadoPagoTerminalPayments.setOperatingMode}
|
|
49
|
+
* refuses a non-integrable id before it ever reaches the API, but the flag
|
|
50
|
+
* is here too, on the list, because the honest place to stop a merchant
|
|
51
|
+
* from picking a terminal that will never take an API charge is before
|
|
52
|
+
* they pick it — not after the first failed sale.
|
|
53
|
+
*/
|
|
54
|
+
integrable?: boolean;
|
|
31
55
|
/** The provider's own object, for anything this port does not model. */
|
|
32
56
|
raw: unknown;
|
|
33
57
|
}
|