@miguelmorales13/nestkit 0.22.0 → 0.24.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 +171 -2
- package/dist/auth/index.cjs +1 -1
- package/dist/auth/index.js +1 -1
- package/dist/auth/oauth/index.cjs +1 -1
- package/dist/auth/oauth/index.js +1 -1
- package/dist/bootstrap/index.cjs +4 -4
- package/dist/bootstrap/index.js +3 -3
- package/dist/{chunk-FNUZGYUF.cjs → chunk-ALIKVFOV.cjs} +2 -2
- package/dist/{chunk-UNEVXCFW.js → chunk-PFHHLB7B.js} +1 -1
- package/dist/errors/index.cjs +5 -5
- package/dist/errors/index.js +4 -4
- package/dist/index.cjs +13 -13
- package/dist/index.js +15 -15
- 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/{chunk-A2DGK4ZL.js → chunk-2DUEQKOI.js} +3 -3
- package/dist/{chunk-MSTXS3MU.cjs → chunk-LSDY6ETG.cjs} +3 -3
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
|
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import type { FcmOptions, FirebaseServiceAccount } from '../push.options.js';
|
|
2
|
+
import type { PushChannelPort, PushDevice, PushFailure, ResolvedPushMessage } from '../push.port.js';
|
|
3
|
+
/**
|
|
4
|
+
* Firebase Cloud Messaging over its HTTP v1 API, with `fetch`.
|
|
5
|
+
*
|
|
6
|
+
* Not `firebase-admin`: it pulls half of Google Cloud into the bundle for
|
|
7
|
+
* what is one POST with a bearer token. The token is an OAuth2 access token
|
|
8
|
+
* obtained by signing a JWT with the service account's private key — the
|
|
9
|
+
* same thing the SDK does, in thirty visible lines — and cached until it is
|
|
10
|
+
* about to expire.
|
|
11
|
+
*
|
|
12
|
+
* The message shape is FCM's, one block per platform, because that is where
|
|
13
|
+
* the platforms disagree: what is a native button on the web is a
|
|
14
|
+
* registered category on iOS and does not exist on Android unless the app
|
|
15
|
+
* draws the notification itself. Everything the app might need to do that
|
|
16
|
+
* travels in `data`, as strings, since FCM carries nothing else there.
|
|
17
|
+
*/
|
|
18
|
+
export declare class FcmChannel implements PushChannelPort {
|
|
19
|
+
readonly kind: "fcm";
|
|
20
|
+
private readonly account;
|
|
21
|
+
private readonly fetchImpl;
|
|
22
|
+
private token?;
|
|
23
|
+
private tokenInFlight?;
|
|
24
|
+
constructor(options: FcmOptions);
|
|
25
|
+
get projectId(): string;
|
|
26
|
+
send(device: PushDevice, message: ResolvedPushMessage): Promise<{
|
|
27
|
+
id: string | null;
|
|
28
|
+
} | Omit<PushFailure, 'device'>>;
|
|
29
|
+
sendToTopic(topic: string, message: ResolvedPushMessage): Promise<{
|
|
30
|
+
id: string | null;
|
|
31
|
+
}>;
|
|
32
|
+
subscribeToTopic(tokens: string[], topic: string): Promise<void>;
|
|
33
|
+
unsubscribeFromTopic(tokens: string[], topic: string): Promise<void>;
|
|
34
|
+
/** The FCM v1 message for a target. Exported through the instance so a test can pin the exact bytes. */
|
|
35
|
+
buildMessage(target: {
|
|
36
|
+
token: string;
|
|
37
|
+
} | {
|
|
38
|
+
topic: string;
|
|
39
|
+
}, message: ResolvedPushMessage): Record<string, unknown>;
|
|
40
|
+
private post;
|
|
41
|
+
private topicBatch;
|
|
42
|
+
/**
|
|
43
|
+
* The OAuth2 access token, minted from a self-signed JWT and kept until
|
|
44
|
+
* a minute before it expires. Concurrent sends share one in-flight
|
|
45
|
+
* request rather than each minting their own.
|
|
46
|
+
*/
|
|
47
|
+
private accessToken;
|
|
48
|
+
private mintAccessToken;
|
|
49
|
+
}
|
|
50
|
+
/** Parsed JSON, the JSON in a string, or a path to it — every way the credential shows up. */
|
|
51
|
+
export declare function parseServiceAccount(input: FirebaseServiceAccount | string): FirebaseServiceAccount;
|
|
52
|
+
/**
|
|
53
|
+
* One message, four dialects.
|
|
54
|
+
*
|
|
55
|
+
* - `data` carries `link`, `actions` and `category` on every platform, so
|
|
56
|
+
* an app that renders its own notification has the whole picture.
|
|
57
|
+
* - Android's system notification has no buttons; only `channel_id`, sound
|
|
58
|
+
* and the picture map onto it. Buttons there mean `dataOnly` and the app
|
|
59
|
+
* drawing the notification.
|
|
60
|
+
* - iOS gets `category` (the buttons the app registered under that name),
|
|
61
|
+
* `badge`, `sound`, and `mutable-content` when there is a picture, which
|
|
62
|
+
* is what lets a Notification Service Extension download it. A
|
|
63
|
+
* `dataOnly` message is a background push: `content-available`, priority
|
|
64
|
+
* 5 and push-type `background`, or APNs drops it.
|
|
65
|
+
* - Web gets native `actions`, plus `fcm_options.link` when the link is
|
|
66
|
+
* `https` — FCM refuses anything else there — and the same link in
|
|
67
|
+
* `data` for the service worker's `notificationclick`.
|
|
68
|
+
*/
|
|
69
|
+
export declare function toFcmMessage(target: {
|
|
70
|
+
token: string;
|
|
71
|
+
} | {
|
|
72
|
+
topic: string;
|
|
73
|
+
}, m: ResolvedPushMessage): Record<string, unknown>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { WebPushOptions } from '../push.options.js';
|
|
2
|
+
import type { PushChannelPort, PushDevice, PushFailure, ResolvedPushMessage } from '../push.port.js';
|
|
3
|
+
/** The two calls this adapter makes; what a test hands in instead of the library. */
|
|
4
|
+
export interface WebPushLike {
|
|
5
|
+
setVapidDetails(subject: string, publicKey: string, privateKey: string): void;
|
|
6
|
+
sendNotification(subscription: {
|
|
7
|
+
endpoint: string;
|
|
8
|
+
keys: {
|
|
9
|
+
p256dh: string;
|
|
10
|
+
auth: string;
|
|
11
|
+
};
|
|
12
|
+
}, payload: string, options?: {
|
|
13
|
+
TTL?: number;
|
|
14
|
+
urgency?: string;
|
|
15
|
+
topic?: string;
|
|
16
|
+
}): Promise<{
|
|
17
|
+
statusCode: number;
|
|
18
|
+
}>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The payload a Web Push subscription receives, as JSON. The service
|
|
22
|
+
* worker renders it — `showNotification(title, { body, icon, image, badge,
|
|
23
|
+
* actions, data, tag })` maps one to one — and handles `notificationclick`
|
|
24
|
+
* with `data.link` and `data.actions`. See the README for the worker.
|
|
25
|
+
*/
|
|
26
|
+
export interface WebPushPayload {
|
|
27
|
+
title: string;
|
|
28
|
+
body: string;
|
|
29
|
+
icon?: string;
|
|
30
|
+
image?: string;
|
|
31
|
+
badge?: number;
|
|
32
|
+
tag?: string;
|
|
33
|
+
link?: string;
|
|
34
|
+
actions?: {
|
|
35
|
+
action: string;
|
|
36
|
+
title: string;
|
|
37
|
+
icon?: string;
|
|
38
|
+
link?: string;
|
|
39
|
+
}[];
|
|
40
|
+
data?: Record<string, unknown>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Browsers, through the Web Push protocol with VAPID keys — the `web-push`
|
|
44
|
+
* package, loaded lazily so an app with only native devices never does.
|
|
45
|
+
*
|
|
46
|
+
* `404`/`410` are the push service saying the subscription is gone (the
|
|
47
|
+
* person revoked it, or the browser rotated it): `stale`. Everything else
|
|
48
|
+
* is a failure to report, not a reason to delete.
|
|
49
|
+
*/
|
|
50
|
+
export declare class WebPushChannel implements PushChannelPort {
|
|
51
|
+
private readonly options;
|
|
52
|
+
readonly kind: "webpush";
|
|
53
|
+
private libPromise?;
|
|
54
|
+
constructor(options: WebPushOptions, lib?: WebPushLike);
|
|
55
|
+
private lib;
|
|
56
|
+
private configure;
|
|
57
|
+
/** The JSON the worker gets. On the instance so a test can pin it. */
|
|
58
|
+
buildPayload(message: ResolvedPushMessage): WebPushPayload;
|
|
59
|
+
send(device: PushDevice, message: ResolvedPushMessage): Promise<{
|
|
60
|
+
id: string | null;
|
|
61
|
+
} | Omit<PushFailure, 'device'>>;
|
|
62
|
+
}
|