@develit-services/bank 0.0.16 → 0.0.18

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.
Files changed (31) hide show
  1. package/dist/database/schema.cjs +2 -1
  2. package/dist/database/schema.d.cts +1 -1
  3. package/dist/database/schema.d.mts +1 -1
  4. package/dist/database/schema.d.ts +1 -1
  5. package/dist/database/schema.mjs +1 -1
  6. package/dist/export/worker.cjs +102 -40
  7. package/dist/export/worker.d.cts +27 -12
  8. package/dist/export/worker.d.mts +27 -12
  9. package/dist/export/worker.d.ts +27 -12
  10. package/dist/export/worker.mjs +102 -40
  11. package/dist/export/wrangler.cjs +1 -1
  12. package/dist/export/wrangler.d.cts +2 -1
  13. package/dist/export/wrangler.d.mts +2 -1
  14. package/dist/export/wrangler.d.ts +2 -1
  15. package/dist/export/wrangler.mjs +1 -1
  16. package/dist/shared/{bank.BoVUuQ1D.d.mts → bank.BkLncGmL.d.ts} +18 -10
  17. package/dist/shared/{bank.BT_Yf43j.d.cts → bank.BuNG2S8h.d.cts} +182 -7
  18. package/dist/shared/{bank.BT_Yf43j.d.mts → bank.BuNG2S8h.d.mts} +182 -7
  19. package/dist/shared/{bank.BT_Yf43j.d.ts → bank.BuNG2S8h.d.ts} +182 -7
  20. package/dist/shared/{bank.BeH-ZCJJ.mjs → bank.C9jAvwna.mjs} +24 -1
  21. package/dist/shared/{bank.DZS4d3MJ.mjs → bank.CNpjRxvL.mjs} +103 -59
  22. package/dist/shared/{bank.Ce58djRL.cjs → bank.DQn99N-C.cjs} +103 -59
  23. package/dist/shared/{bank.BwleO_ON.d.cts → bank.DWVLVMDa.d.mts} +18 -10
  24. package/dist/shared/{bank.DlquvmPs.d.ts → bank.DmypH3Au.d.cts} +18 -10
  25. package/dist/shared/{bank.CrAQe4PH.cjs → bank.P9gDUycr.cjs} +26 -0
  26. package/dist/types.cjs +6 -4
  27. package/dist/types.d.cts +47 -15
  28. package/dist/types.d.mts +47 -15
  29. package/dist/types.d.ts +47 -15
  30. package/dist/types.mjs +4 -4
  31. package/package.json +1 -1
@@ -1,5 +1,5 @@
1
1
  import { uuidv4, useResult, createInternalError } from '@develit-io/backend-sdk';
2
- import { s as schema } from './bank.BeH-ZCJJ.mjs';
2
+ import { s as schema, C as CONNECTOR_KEYS } from './bank.C9jAvwna.mjs';
3
3
  import { and, eq } from 'drizzle-orm';
4
4
  import { z } from 'zod';
5
5
  import { COUNTRY_CODES_2, CURRENCY_CODES } from '@develit-io/general-codes';
@@ -103,10 +103,11 @@ const useFinbricksFetch = async (config, init) => {
103
103
  return await res.json();
104
104
  };
105
105
  class FinbricksClient {
106
- constructor(baseUrl, merchantId, privateKeyPem) {
106
+ constructor(baseUrl, merchantId, privateKeyPem, redirectUri) {
107
107
  this.BASE_URL = baseUrl;
108
108
  this.MERCHANT_ID = merchantId;
109
109
  this.PRIVATE_KEY_PEM = privateKeyPem;
110
+ this.REDIRECT_URI = redirectUri;
110
111
  }
111
112
  async request(params) {
112
113
  return useFinbricksFetch({
@@ -222,23 +223,39 @@ const mapFinbricksTransactionToPayment = (tx, account) => {
222
223
  };
223
224
 
224
225
  class FinbricksConnector extends IBankConnector {
225
- constructor(provider, { BASE_URI, MERCHANT_ID, PRIVATE_KEY_PEM }) {
226
+ constructor(provider, {
227
+ BASE_URI,
228
+ MERCHANT_ID,
229
+ PRIVATE_KEY_PEM,
230
+ REDIRECT_URI,
231
+ KV
232
+ }) {
226
233
  super();
227
234
  this.connectorKey = "FINBRICKS";
235
+ this._clientIds = {};
228
236
  this.PROVIDER = provider;
229
- this.finbricks = new FinbricksClient(BASE_URI, MERCHANT_ID, PRIVATE_KEY_PEM);
237
+ this.kv = KV;
238
+ this.finbricks = new FinbricksClient(
239
+ BASE_URI,
240
+ MERCHANT_ID,
241
+ PRIVATE_KEY_PEM,
242
+ REDIRECT_URI
243
+ );
230
244
  }
231
245
  static {
232
246
  this.FETCH_INTERVAL = 60 * 60 * 5;
233
247
  }
234
- async connectUserAccount() {
248
+ async getAuthUri({
249
+ accountId,
250
+ ott
251
+ }) {
235
252
  const clientId = uuidv4();
236
253
  const body = {
237
254
  merchantId: this.finbricks.MERCHANT_ID,
238
255
  clientId,
239
256
  paymentProvider: this.PROVIDER,
240
257
  scope: "AISP_PISP",
241
- callbackUrl: "https://example.com/callback"
258
+ callbackUrl: `${this.finbricks.REDIRECT_URI}?clientId=${clientId}&accountId=${accountId}&ott=${ott}`
242
259
  };
243
260
  const [response, error] = await useResult(
244
261
  this.finbricks.request({
@@ -252,41 +269,49 @@ class FinbricksConnector extends IBankConnector {
252
269
  message: "Finbricks: failed to connect user"
253
270
  });
254
271
  }
255
- return {
256
- redirectUrl: response.redirectUrl,
257
- clientId
258
- };
259
- }
260
- async authenticate({ token: clientId }) {
261
- if (!!clientId) {
262
- const isExpired = await this.isClientIdExpired(clientId);
263
- if (!isExpired) {
264
- this.lastValidClientId = clientId;
265
- return clientId;
266
- }
272
+ return response.redirectUrl;
273
+ }
274
+ get storageKey() {
275
+ return `${this.connectorKey}:auth`;
276
+ }
277
+ async getClientIds() {
278
+ let saved = await this.kv.get(this.storageKey);
279
+ if (!saved) {
280
+ const empty = Object.fromEntries(CONNECTOR_KEYS.map((v) => [v, ""]));
281
+ empty["FIO"] = "d32a83cf-d352-41f3-96d7-4d2961336ddc";
282
+ await this.kv.put(this.storageKey, JSON.stringify(empty));
283
+ saved = JSON.stringify(empty);
267
284
  }
268
- const newClientId = uuidv4();
269
- const body = {
270
- merchantId: this.finbricks.MERCHANT_ID,
271
- paymentProvider: this.PROVIDER,
272
- clientId: newClientId,
273
- scope: "AISP_PISP",
274
- callbackUrl: `www.example.com/callback`
275
- };
276
- const [res, error] = await useResult(
277
- this.finbricks.request({
278
- endpoint: "/v2/auth/authenticate",
279
- method: "POST",
280
- body
281
- })
282
- );
283
- if (error || !res) {
284
- throw createInternalError(error, {
285
- message: `Finbricks: authentication request failed (${this.PROVIDER})`
285
+ this._clientIds = JSON.parse(saved);
286
+ return this._clientIds;
287
+ }
288
+ async setClientIds(value) {
289
+ this._clientIds = value;
290
+ void this.kv.put(this.storageKey, JSON.stringify(value));
291
+ }
292
+ async authorizeAccount({
293
+ urlParams
294
+ }) {
295
+ const params = new URLSearchParams(urlParams);
296
+ const clientId = params.get("clientId");
297
+ if (!clientId)
298
+ throw createInternalError(null, {
299
+ message: "Finbricks: clientId is missing"
300
+ });
301
+ let savedClientIds = await this.getClientIds();
302
+ await this.setClientIds({ ...savedClientIds, [this.PROVIDER]: clientId });
303
+ const expired = await this.isClientIdExpired({ clientId });
304
+ if (expired) {
305
+ throw createInternalError(null, {
306
+ message: "Finbricks: clientId is invalid ore expired",
307
+ code: "FINBRICKS_CLIENT_ID_EXPIRED",
308
+ status: 401
286
309
  });
287
310
  }
288
- this.lastValidClientId = newClientId;
289
- return res.redirectUrl;
311
+ return true;
312
+ }
313
+ async authenticate(code) {
314
+ return "";
290
315
  }
291
316
  async revokeAuthentication(clientId) {
292
317
  const [_, error] = await useResult(
@@ -305,9 +330,10 @@ class FinbricksConnector extends IBankConnector {
305
330
  message: `Finbricks: failed to revoke authentication for clientId ${clientId}`
306
331
  });
307
332
  }
308
- this.lastValidClientId = "";
309
333
  }
310
- async isClientIdExpired(clientId) {
334
+ async isClientIdExpired({
335
+ clientId
336
+ }) {
311
337
  const [response, error] = await useResult(
312
338
  this.finbricks.request({
313
339
  endpoint: FINBRICKS_ENDPOINTS.AUTH_TOKEN_STATUS,
@@ -331,7 +357,7 @@ class FinbricksConnector extends IBankConnector {
331
357
  return validTo.getTime() < Date.now();
332
358
  }
333
359
  async listAccounts() {
334
- if (!this.lastValidClientId) {
360
+ if (!this._clientIds) {
335
361
  throw createInternalError(null, {
336
362
  message: "Finbricks: missing valid clientId",
337
363
  code: "FINBRICKS_CLIENT_ID_MISSING"
@@ -344,7 +370,7 @@ class FinbricksConnector extends IBankConnector {
344
370
  query: {
345
371
  merchantId: this.finbricks.MERCHANT_ID,
346
372
  paymentProvider: this.PROVIDER,
347
- clientId: this.lastValidClientId
373
+ clientId: this._clientIds[this.PROVIDER]
348
374
  }
349
375
  })
350
376
  );
@@ -379,7 +405,7 @@ class FinbricksConnector extends IBankConnector {
379
405
  batchPaymentIdentification: {
380
406
  merchantId: this.finbricks.MERCHANT_ID,
381
407
  debtorAccountIban: payments[0].debtorIban,
382
- clientId: this.lastValidClientId,
408
+ clientId: this._clientIds[this.PROVIDER],
383
409
  callbackUrl: "https://example.com/callback",
384
410
  merchantBatchId: batchId
385
411
  },
@@ -447,7 +473,7 @@ class FinbricksConnector extends IBankConnector {
447
473
  endpoint: FINBRICKS_ENDPOINTS.ACCOUNT_TRANSACTIONS,
448
474
  query: {
449
475
  merchantId: this.finbricks.MERCHANT_ID,
450
- clientId: this.lastValidClientId,
476
+ clientId: this._clientIds[this.PROVIDER],
451
477
  paymentProvider: this.PROVIDER,
452
478
  bankAccountId: account.identification.number,
453
479
  dateFrom,
@@ -536,16 +562,17 @@ class ErsteConnector extends IBankConnector {
536
562
  this.AUTH_URI = config.AUTH_URI;
537
563
  this.PAYMENTS_URI = config.PAYMENTS_URI;
538
564
  this.ACCOUNTS_URI = config.ACCOUNTS_URI;
539
- }
540
- connectUserAccount() {
541
- throw new Error("Method not implemented.");
565
+ this.kv = config.KV;
542
566
  }
543
567
  static {
544
568
  this.FETCH_INTERVAL = 60 * 60 * 5;
545
569
  }
546
- get adminCodeCreationURI() {
570
+ async getAuthUri({
571
+ accountId,
572
+ ott
573
+ }) {
547
574
  const params = new URLSearchParams({
548
- redirect_uri: `${this.REDIRECT_URI}`,
575
+ redirect_uri: `${this.REDIRECT_URI}?accountId=${accountId}&ott=${ott}`,
549
576
  client_id: this.CLIENT_ID,
550
577
  response_type: "code",
551
578
  access_type: "offline",
@@ -554,13 +581,26 @@ class ErsteConnector extends IBankConnector {
554
581
  });
555
582
  return `${this.AUTH_URI}/auth?${params.toString()}`;
556
583
  }
557
- async authenticate({ token, refreshToken }) {
558
- const grantType = refreshToken ? "refresh_token" : "authorization_code";
584
+ async authorizeAccount({
585
+ urlParams
586
+ }) {
587
+ const params = new URLSearchParams(urlParams);
588
+ const code = params.get("code");
589
+ if (!code)
590
+ throw createInternalError(null, {
591
+ message: "Missing code in params"
592
+ });
593
+ await this.kv.put(`${this.connectorKey}:auth`, code);
594
+ return true;
595
+ }
596
+ async authenticate(tokens) {
597
+ const grantType = tokens?.refreshToken ? "refresh_token" : "authorization_code";
559
598
  let bodyParams = {
560
599
  client_id: this.CLIENT_ID,
561
600
  client_secret: this.CLIENT_SECRET,
562
601
  grant_type: grantType
563
602
  };
603
+ const token = await this.kv.get(`${this.connectorKey}:auth`);
564
604
  if (token) {
565
605
  bodyParams = {
566
606
  ...bodyParams,
@@ -568,7 +608,7 @@ class ErsteConnector extends IBankConnector {
568
608
  redirect_uri: this.REDIRECT_URI
569
609
  };
570
610
  }
571
- if (refreshToken) bodyParams.refresh_token = refreshToken;
611
+ if (tokens?.refreshToken) bodyParams.refresh_token = tokens?.refreshToken;
572
612
  const body = new URLSearchParams(bodyParams);
573
613
  const result = await fetch(`${this.AUTH_URI}/token`, {
574
614
  method: "POST",
@@ -782,9 +822,10 @@ class ErsteConnector extends IBankConnector {
782
822
  }
783
823
 
784
824
  class MockConnector extends IBankConnector {
785
- constructor() {
786
- super(...arguments);
825
+ constructor(kv) {
826
+ super();
787
827
  this.connectorKey = "MOCK";
828
+ this.kv = kv;
788
829
  }
789
830
  static {
790
831
  this.FETCH_INTERVAL = 60 * 60 * 5;
@@ -795,11 +836,14 @@ class MockConnector extends IBankConnector {
795
836
  async listAccounts() {
796
837
  return [];
797
838
  }
798
- async connectUserAccount() {
799
- return {
800
- clientId: uuidv4(),
801
- redirectUrl: ""
802
- };
839
+ async getAuthUri({
840
+ accountId,
841
+ ott
842
+ }) {
843
+ return `<base-uri>/auth?ott=ott&accountId=${accountId}`;
844
+ }
845
+ async authorizeAccount() {
846
+ return true;
803
847
  }
804
848
  async preparePayment(payment) {
805
849
  const bankRefId = uuidv4();
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const backendSdk = require('@develit-io/backend-sdk');
4
- const database_schema = require('./bank.CrAQe4PH.cjs');
4
+ const database_schema = require('./bank.P9gDUycr.cjs');
5
5
  const drizzleOrm = require('drizzle-orm');
6
6
  const zod = require('zod');
7
7
  const generalCodes = require('@develit-io/general-codes');
@@ -105,10 +105,11 @@ const useFinbricksFetch = async (config, init) => {
105
105
  return await res.json();
106
106
  };
107
107
  class FinbricksClient {
108
- constructor(baseUrl, merchantId, privateKeyPem) {
108
+ constructor(baseUrl, merchantId, privateKeyPem, redirectUri) {
109
109
  this.BASE_URL = baseUrl;
110
110
  this.MERCHANT_ID = merchantId;
111
111
  this.PRIVATE_KEY_PEM = privateKeyPem;
112
+ this.REDIRECT_URI = redirectUri;
112
113
  }
113
114
  async request(params) {
114
115
  return useFinbricksFetch({
@@ -224,23 +225,39 @@ const mapFinbricksTransactionToPayment = (tx, account) => {
224
225
  };
225
226
 
226
227
  class FinbricksConnector extends IBankConnector {
227
- constructor(provider, { BASE_URI, MERCHANT_ID, PRIVATE_KEY_PEM }) {
228
+ constructor(provider, {
229
+ BASE_URI,
230
+ MERCHANT_ID,
231
+ PRIVATE_KEY_PEM,
232
+ REDIRECT_URI,
233
+ KV
234
+ }) {
228
235
  super();
229
236
  this.connectorKey = "FINBRICKS";
237
+ this._clientIds = {};
230
238
  this.PROVIDER = provider;
231
- this.finbricks = new FinbricksClient(BASE_URI, MERCHANT_ID, PRIVATE_KEY_PEM);
239
+ this.kv = KV;
240
+ this.finbricks = new FinbricksClient(
241
+ BASE_URI,
242
+ MERCHANT_ID,
243
+ PRIVATE_KEY_PEM,
244
+ REDIRECT_URI
245
+ );
232
246
  }
233
247
  static {
234
248
  this.FETCH_INTERVAL = 60 * 60 * 5;
235
249
  }
236
- async connectUserAccount() {
250
+ async getAuthUri({
251
+ accountId,
252
+ ott
253
+ }) {
237
254
  const clientId = backendSdk.uuidv4();
238
255
  const body = {
239
256
  merchantId: this.finbricks.MERCHANT_ID,
240
257
  clientId,
241
258
  paymentProvider: this.PROVIDER,
242
259
  scope: "AISP_PISP",
243
- callbackUrl: "https://example.com/callback"
260
+ callbackUrl: `${this.finbricks.REDIRECT_URI}?clientId=${clientId}&accountId=${accountId}&ott=${ott}`
244
261
  };
245
262
  const [response, error] = await backendSdk.useResult(
246
263
  this.finbricks.request({
@@ -254,41 +271,49 @@ class FinbricksConnector extends IBankConnector {
254
271
  message: "Finbricks: failed to connect user"
255
272
  });
256
273
  }
257
- return {
258
- redirectUrl: response.redirectUrl,
259
- clientId
260
- };
261
- }
262
- async authenticate({ token: clientId }) {
263
- if (!!clientId) {
264
- const isExpired = await this.isClientIdExpired(clientId);
265
- if (!isExpired) {
266
- this.lastValidClientId = clientId;
267
- return clientId;
268
- }
274
+ return response.redirectUrl;
275
+ }
276
+ get storageKey() {
277
+ return `${this.connectorKey}:auth`;
278
+ }
279
+ async getClientIds() {
280
+ let saved = await this.kv.get(this.storageKey);
281
+ if (!saved) {
282
+ const empty = Object.fromEntries(database_schema.CONNECTOR_KEYS.map((v) => [v, ""]));
283
+ empty["FIO"] = "d32a83cf-d352-41f3-96d7-4d2961336ddc";
284
+ await this.kv.put(this.storageKey, JSON.stringify(empty));
285
+ saved = JSON.stringify(empty);
269
286
  }
270
- const newClientId = backendSdk.uuidv4();
271
- const body = {
272
- merchantId: this.finbricks.MERCHANT_ID,
273
- paymentProvider: this.PROVIDER,
274
- clientId: newClientId,
275
- scope: "AISP_PISP",
276
- callbackUrl: `www.example.com/callback`
277
- };
278
- const [res, error] = await backendSdk.useResult(
279
- this.finbricks.request({
280
- endpoint: "/v2/auth/authenticate",
281
- method: "POST",
282
- body
283
- })
284
- );
285
- if (error || !res) {
286
- throw backendSdk.createInternalError(error, {
287
- message: `Finbricks: authentication request failed (${this.PROVIDER})`
287
+ this._clientIds = JSON.parse(saved);
288
+ return this._clientIds;
289
+ }
290
+ async setClientIds(value) {
291
+ this._clientIds = value;
292
+ void this.kv.put(this.storageKey, JSON.stringify(value));
293
+ }
294
+ async authorizeAccount({
295
+ urlParams
296
+ }) {
297
+ const params = new URLSearchParams(urlParams);
298
+ const clientId = params.get("clientId");
299
+ if (!clientId)
300
+ throw backendSdk.createInternalError(null, {
301
+ message: "Finbricks: clientId is missing"
302
+ });
303
+ let savedClientIds = await this.getClientIds();
304
+ await this.setClientIds({ ...savedClientIds, [this.PROVIDER]: clientId });
305
+ const expired = await this.isClientIdExpired({ clientId });
306
+ if (expired) {
307
+ throw backendSdk.createInternalError(null, {
308
+ message: "Finbricks: clientId is invalid ore expired",
309
+ code: "FINBRICKS_CLIENT_ID_EXPIRED",
310
+ status: 401
288
311
  });
289
312
  }
290
- this.lastValidClientId = newClientId;
291
- return res.redirectUrl;
313
+ return true;
314
+ }
315
+ async authenticate(code) {
316
+ return "";
292
317
  }
293
318
  async revokeAuthentication(clientId) {
294
319
  const [_, error] = await backendSdk.useResult(
@@ -307,9 +332,10 @@ class FinbricksConnector extends IBankConnector {
307
332
  message: `Finbricks: failed to revoke authentication for clientId ${clientId}`
308
333
  });
309
334
  }
310
- this.lastValidClientId = "";
311
335
  }
312
- async isClientIdExpired(clientId) {
336
+ async isClientIdExpired({
337
+ clientId
338
+ }) {
313
339
  const [response, error] = await backendSdk.useResult(
314
340
  this.finbricks.request({
315
341
  endpoint: FINBRICKS_ENDPOINTS.AUTH_TOKEN_STATUS,
@@ -333,7 +359,7 @@ class FinbricksConnector extends IBankConnector {
333
359
  return validTo.getTime() < Date.now();
334
360
  }
335
361
  async listAccounts() {
336
- if (!this.lastValidClientId) {
362
+ if (!this._clientIds) {
337
363
  throw backendSdk.createInternalError(null, {
338
364
  message: "Finbricks: missing valid clientId",
339
365
  code: "FINBRICKS_CLIENT_ID_MISSING"
@@ -346,7 +372,7 @@ class FinbricksConnector extends IBankConnector {
346
372
  query: {
347
373
  merchantId: this.finbricks.MERCHANT_ID,
348
374
  paymentProvider: this.PROVIDER,
349
- clientId: this.lastValidClientId
375
+ clientId: this._clientIds[this.PROVIDER]
350
376
  }
351
377
  })
352
378
  );
@@ -381,7 +407,7 @@ class FinbricksConnector extends IBankConnector {
381
407
  batchPaymentIdentification: {
382
408
  merchantId: this.finbricks.MERCHANT_ID,
383
409
  debtorAccountIban: payments[0].debtorIban,
384
- clientId: this.lastValidClientId,
410
+ clientId: this._clientIds[this.PROVIDER],
385
411
  callbackUrl: "https://example.com/callback",
386
412
  merchantBatchId: batchId
387
413
  },
@@ -449,7 +475,7 @@ class FinbricksConnector extends IBankConnector {
449
475
  endpoint: FINBRICKS_ENDPOINTS.ACCOUNT_TRANSACTIONS,
450
476
  query: {
451
477
  merchantId: this.finbricks.MERCHANT_ID,
452
- clientId: this.lastValidClientId,
478
+ clientId: this._clientIds[this.PROVIDER],
453
479
  paymentProvider: this.PROVIDER,
454
480
  bankAccountId: account.identification.number,
455
481
  dateFrom,
@@ -538,16 +564,17 @@ class ErsteConnector extends IBankConnector {
538
564
  this.AUTH_URI = config.AUTH_URI;
539
565
  this.PAYMENTS_URI = config.PAYMENTS_URI;
540
566
  this.ACCOUNTS_URI = config.ACCOUNTS_URI;
541
- }
542
- connectUserAccount() {
543
- throw new Error("Method not implemented.");
567
+ this.kv = config.KV;
544
568
  }
545
569
  static {
546
570
  this.FETCH_INTERVAL = 60 * 60 * 5;
547
571
  }
548
- get adminCodeCreationURI() {
572
+ async getAuthUri({
573
+ accountId,
574
+ ott
575
+ }) {
549
576
  const params = new URLSearchParams({
550
- redirect_uri: `${this.REDIRECT_URI}`,
577
+ redirect_uri: `${this.REDIRECT_URI}?accountId=${accountId}&ott=${ott}`,
551
578
  client_id: this.CLIENT_ID,
552
579
  response_type: "code",
553
580
  access_type: "offline",
@@ -556,13 +583,26 @@ class ErsteConnector extends IBankConnector {
556
583
  });
557
584
  return `${this.AUTH_URI}/auth?${params.toString()}`;
558
585
  }
559
- async authenticate({ token, refreshToken }) {
560
- const grantType = refreshToken ? "refresh_token" : "authorization_code";
586
+ async authorizeAccount({
587
+ urlParams
588
+ }) {
589
+ const params = new URLSearchParams(urlParams);
590
+ const code = params.get("code");
591
+ if (!code)
592
+ throw backendSdk.createInternalError(null, {
593
+ message: "Missing code in params"
594
+ });
595
+ await this.kv.put(`${this.connectorKey}:auth`, code);
596
+ return true;
597
+ }
598
+ async authenticate(tokens) {
599
+ const grantType = tokens?.refreshToken ? "refresh_token" : "authorization_code";
561
600
  let bodyParams = {
562
601
  client_id: this.CLIENT_ID,
563
602
  client_secret: this.CLIENT_SECRET,
564
603
  grant_type: grantType
565
604
  };
605
+ const token = await this.kv.get(`${this.connectorKey}:auth`);
566
606
  if (token) {
567
607
  bodyParams = {
568
608
  ...bodyParams,
@@ -570,7 +610,7 @@ class ErsteConnector extends IBankConnector {
570
610
  redirect_uri: this.REDIRECT_URI
571
611
  };
572
612
  }
573
- if (refreshToken) bodyParams.refresh_token = refreshToken;
613
+ if (tokens?.refreshToken) bodyParams.refresh_token = tokens?.refreshToken;
574
614
  const body = new URLSearchParams(bodyParams);
575
615
  const result = await fetch(`${this.AUTH_URI}/token`, {
576
616
  method: "POST",
@@ -784,9 +824,10 @@ class ErsteConnector extends IBankConnector {
784
824
  }
785
825
 
786
826
  class MockConnector extends IBankConnector {
787
- constructor() {
788
- super(...arguments);
827
+ constructor(kv) {
828
+ super();
789
829
  this.connectorKey = "MOCK";
830
+ this.kv = kv;
790
831
  }
791
832
  static {
792
833
  this.FETCH_INTERVAL = 60 * 60 * 5;
@@ -797,11 +838,14 @@ class MockConnector extends IBankConnector {
797
838
  async listAccounts() {
798
839
  return [];
799
840
  }
800
- async connectUserAccount() {
801
- return {
802
- clientId: backendSdk.uuidv4(),
803
- redirectUrl: ""
804
- };
841
+ async getAuthUri({
842
+ accountId,
843
+ ott
844
+ }) {
845
+ return `<base-uri>/auth?ott=ott&accountId=${accountId}`;
846
+ }
847
+ async authorizeAccount() {
848
+ return true;
805
849
  }
806
850
  async preparePayment(payment) {
807
851
  const bankRefId = backendSdk.uuidv4();
@@ -1,6 +1,6 @@
1
1
  import { Environment } from '@develit-io/backend-sdk';
2
2
  import { BANK_CODES, CURRENCY_CODES, CODES } from '@develit-io/general-codes';
3
- import { a as PaymentInsertType, t as tables } from './bank.BT_Yf43j.cjs';
3
+ import { a as PaymentInsertType, t as tables } from './bank.BuNG2S8h.mjs';
4
4
  import { DrizzleD1Database } from 'drizzle-orm/d1';
5
5
 
6
6
  declare const PAYMENT_TYPES: readonly ["SEPA", "SWIFT", "IFSC", "DOMESTIC"];
@@ -28,7 +28,10 @@ type CountryCode = (typeof COUNTRY_CODES)[number];
28
28
  interface BankAccountWithLastSync extends ConfigBankAccount {
29
29
  lastSyncAt: SyncLog;
30
30
  }
31
- type ConnectorKey = 'ERSTE' | 'FINBRICKS' | 'MOCK' | 'CREDITAS' | 'MOCK_COBS' | 'FIO' | 'MONETA';
31
+ declare const CONNECTOR_KEYS: readonly ["ERSTE", "FINBRICKS", "MOCK", "CREDITAS", "MOCK_COBS", "FIO", "MONETA"];
32
+ type ConnectorKey = (typeof CONNECTOR_KEYS)[number];
33
+ declare const TOKEN_TYPES: readonly ["ACCOUNT_AUTHORIZATION"];
34
+ type TokenType = (typeof TOKEN_TYPES)[number];
32
35
  interface ConfigBankAccount {
33
36
  connectorKey: ConnectorKey;
34
37
  id: string;
@@ -77,14 +80,19 @@ type AuthInput = {
77
80
  token: string;
78
81
  refreshToken?: string;
79
82
  };
80
- type AccountConnection = {
81
- clientId: string;
82
- redirectUrl: string;
83
- };
84
83
  declare abstract class IBankConnector {
85
84
  abstract connectorKey: ConnectorKey;
86
- abstract authenticate(code: AuthInput): Promise<string>;
87
- abstract connectUserAccount(): Promise<AccountConnection>;
85
+ abstract kv: KVNamespace;
86
+ abstract authenticate(tokens?: {
87
+ refreshToken?: string;
88
+ }): Promise<string>;
89
+ abstract getAuthUri({ accountId, ott, }: {
90
+ accountId: string;
91
+ ott: string;
92
+ }): Promise<string>;
93
+ abstract authorizeAccount({ urlParams, }: {
94
+ urlParams: string;
95
+ }): Promise<boolean>;
88
96
  abstract listAccounts(): Promise<[]>;
89
97
  abstract preparePayment(payment: IncomingPaymentMessage): Promise<PaymentPreparedInsertType>;
90
98
  abstract initiateBatchFromPayments({ payments, }: {
@@ -110,5 +118,5 @@ interface IncomingPaymentMessage extends PaymentInsertType {
110
118
  interface OutgoingPaymentMessage extends PaymentInsertType {
111
119
  }
112
120
 
113
- export { IBankConnector as I, BATCH_STATUSES as o, PAYMENT_TYPES as q, PAYMENT_STATUSES as s, PAYMENT_DIRECTIONS as t, COUNTRY_CODES as v };
114
- export type { AccountConnection as A, BankAccountWithLastSync as B, ConfigEnvironmentBankAccount as C, OutgoingPaymentMessage as O, PaymentPreparedInsertType as P, SyncLog as S, ConfigBankAccount as a, ConnectorKey as b, CurrencyCode as c, BankCode as d, CountryCode as e, AuthInput as f, IncomingPaymentMessage as g, InitiatedBatch as h, InitiatedPayment as i, PaymentStatus as j, BatchStatus as k, BatchMetadata as l, Currency as m, PaymentFailedInsertType as n, paymentInitializedInsertType as p, PaymentType as r, PaymentDirection as u };
121
+ export { IBankConnector as I, TOKEN_TYPES as T, BATCH_STATUSES as n, PAYMENT_TYPES as o, PAYMENT_STATUSES as r, PAYMENT_DIRECTIONS as s, COUNTRY_CODES as u, CONNECTOR_KEYS as v };
122
+ export type { AuthInput as A, BankAccountWithLastSync as B, ConfigEnvironmentBankAccount as C, OutgoingPaymentMessage as O, PaymentPreparedInsertType as P, SyncLog as S, ConfigBankAccount as a, ConnectorKey as b, CurrencyCode as c, BankCode as d, CountryCode as e, IncomingPaymentMessage as f, InitiatedBatch as g, InitiatedPayment as h, PaymentStatus as i, BatchStatus as j, BatchMetadata as k, Currency as l, PaymentFailedInsertType as m, paymentInitializedInsertType as p, PaymentType as q, PaymentDirection as t, TokenType as w };