@tonder.io/ionic-lite-sdk 1.0.2-beta.TEC-1207.acd2837 → 1.0.2-beta.develop.05dedd2

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.
@@ -1,5 +1,10 @@
1
1
  /** Which backend charges and finishes one payment. It is chosen once and never changes. */
2
2
  export type PaymentEngine = "core" | "legacy";
3
+ /** Where the customer and saved-card calls go: tokens are not interchangeable, so both always share one engine. */
4
+ export interface ISupportTarget {
5
+ engine: PaymentEngine;
6
+ baseUrl: string;
7
+ }
3
8
  export interface ICoreRouting {
4
9
  checkout_id?: string;
5
10
  is_route_finished?: boolean;
@@ -69,7 +69,7 @@ export type RegisterCustomerCardRequest = {
69
69
  skyflow_id: string;
70
70
  };
71
71
  export type TokensSkyflowRequest = {
72
- baseUrl: string;
72
+ coreBaseUrl: string;
73
73
  apiKey: string;
74
74
  vault_id: string;
75
75
  vault_url: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tonder.io/ionic-lite-sdk",
3
- "version": "1.0.2-beta.TEC-1207.acd2837",
3
+ "version": "1.0.2-beta.develop.05dedd2",
4
4
  "description": "Tonder ionic lite SDK",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,5 +1,6 @@
1
1
  import { ThreeDSHandler } from "./3dsHandler";
2
2
  import { fetchBusiness } from "../data/businessApi";
3
+ import { ISupportTarget } from "../types/core";
3
4
  import { injectMercadoPagoSecurity } from "../helpers/mercadopago";
4
5
  import { TONDER_URL_BY_MODE } from "../shared/constants/tonderUrl";
5
6
  import {
@@ -284,11 +285,20 @@ export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOp
284
285
  );
285
286
  }
286
287
 
288
+ // Customer and saved cards follow the merchant's flag and always share one engine: the
289
+ // customer's token and the secure token are only valid where they were issued.
290
+ protected _supportTarget(): ISupportTarget {
291
+ return this.merchantData?.core_payments_enabled === true
292
+ ? { engine: "core", baseUrl: this.coreBaseUrl }
293
+ : { engine: "legacy", baseUrl: this.baseUrl };
294
+ }
295
+
287
296
  async _getCustomer(signal: AbortSignal | null = null) {
288
297
  if (!!this.#customerData) return this.#customerData!;
289
298
 
299
+ await this._fetchMerchantData();
290
300
  this.#customerData = await registerOrFetchCustomer(
291
- this.baseUrl,
301
+ this._supportTarget(),
292
302
  this.apiKeyTonder,
293
303
  this.customer!,
294
304
  signal,
@@ -308,7 +318,21 @@ export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOp
308
318
  }
309
319
  }
310
320
  this.process3ds.setEngine("legacy");
311
- return this.#legacyCheckout(request);
321
+ return this.#legacyCheckout(await this.#withLegacyCustomer(request));
322
+ }
323
+
324
+ // The legacy order takes the customer's token as `client`, and a core customer's token is
325
+ // not valid on the monolith: a payment that ends on the legacy flow (an alternative method,
326
+ // or the kill switch's retry) with the flag ON registers its customer there first.
327
+ async #withLegacyCustomer(request: CheckoutRequest): Promise<CheckoutRequest> {
328
+ if (this._supportTarget().engine !== "core") return request;
329
+ const customer = await registerOrFetchCustomer(
330
+ { engine: "legacy", baseUrl: this.baseUrl },
331
+ this.apiKeyTonder,
332
+ this.customer!,
333
+ this.abortController.signal,
334
+ );
335
+ return { ...request, customer };
312
336
  }
313
337
 
314
338
  // Card payments only (new, saved and Card on File: the core decides the latter by itself);
@@ -549,7 +573,7 @@ export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOp
549
573
  authToken: string,
550
574
  businessId: string | number,
551
575
  ): Promise<ICustomerCardsResponse> {
552
- return await fetchCustomerCards(this.baseUrl, authToken, this.secureToken, businessId);
576
+ return await fetchCustomerCards(this._supportTarget(), authToken, this.secureToken, businessId);
553
577
  }
554
578
 
555
579
  async _saveCustomerCard(
@@ -559,7 +583,7 @@ export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOp
559
583
  appOrigin: boolean = false,
560
584
  ): Promise<ISaveCardInternalResponse> {
561
585
  return await saveCustomerCard(
562
- this.baseUrl,
586
+ this._supportTarget(),
563
587
  authToken,
564
588
  this.secureToken,
565
589
  businessId,
@@ -574,7 +598,7 @@ export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOp
574
598
  skyflowId: string,
575
599
  ): Promise<string> {
576
600
  return await removeCustomerCard(
577
- this.baseUrl,
601
+ this._supportTarget(),
578
602
  authToken,
579
603
  this.secureToken,
580
604
  skyflowId,
@@ -582,7 +606,7 @@ export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOp
582
606
  );
583
607
  }
584
608
  async _fetchCustomerPaymentMethods(): Promise<IPaymentMethodResponse> {
585
- return await fetchCustomerPaymentMethods(this.baseUrl, this.apiKeyTonder);
609
+ return await fetchCustomerPaymentMethods(this.coreBaseUrl, this.apiKeyTonder);
586
610
  }
587
611
 
588
612
  protected _hasCardOnFileKeys(): boolean {
@@ -595,6 +619,11 @@ export class BaseInlineCheckout<T extends CustomizationOptions = CustomizationOp
595
619
  merchantId: this.merchantData?.cardonfile_keys!.public_key!,
596
620
  apiKey: this.apiKeyTonder,
597
621
  isTestEnvironment: this.mode !== "production",
622
+ // The core's customer token must not be stored in the subscription: its owner is the customer's id.
623
+ subscriptionOwner: () =>
624
+ this._supportTarget().engine === "core" && this.#customerData?.id !== undefined
625
+ ? String(this.#customerData.id)
626
+ : undefined,
598
627
  });
599
628
  await this.cardOnFileInstance.initialize();
600
629
  }
@@ -12,6 +12,8 @@ import {getCustomerAPMs} from "../data/api";
12
12
  import {BaseInlineCheckout} from "./BaseInlineCheckout";
13
13
  import {getSkyflowTokens, initSkyflowInstance, mountRevealFields, mountSkyflowFields} from "../helpers/skyflow";
14
14
  import {startCheckoutRouter} from "../data/checkoutApi";
15
+ import {registerOrFetchCustomer} from "../data/customerApi";
16
+ import {saveCustomerCard, removeCustomerCard} from "../data/cardApi";
15
17
  import {getOpenpayDeviceSessionID} from "../data/openPayApi";
16
18
  import {getPaymentMethodDetails} from "../shared/catalog/paymentMethodsCatalog";
17
19
  import {APM, IEvents, IInlineLiteCheckoutOptions, InCollectorContainer, TonderAPM} from "../types/commons";
@@ -487,7 +489,7 @@ export class LiteCheckout extends BaseInlineCheckout implements ILiteCheckout{
487
489
  this.skyflowInstance = await initSkyflowInstance({
488
490
  vault_id: vault_id,
489
491
  vault_url: vault_url,
490
- baseUrl: this.baseUrl,
492
+ coreBaseUrl: this.coreBaseUrl,
491
493
  apiKey: this.apiKeyTonder,
492
494
  mode: this.mode,
493
495
  })
@@ -531,7 +533,7 @@ export class LiteCheckout extends BaseInlineCheckout implements ILiteCheckout{
531
533
  vault_id: vault_id,
532
534
  vault_url: vault_url,
533
535
  data,
534
- baseUrl: this.baseUrl,
536
+ coreBaseUrl: this.coreBaseUrl,
535
537
  apiKey: this.apiKeyTonder,
536
538
  });
537
539
  }
@@ -704,24 +706,13 @@ export class LiteCheckout extends BaseInlineCheckout implements ILiteCheckout{
704
706
  email: string,
705
707
  ): Promise<CustomerRegisterResponse | ErrorResponse> {
706
708
  try {
707
- const url = `${this.baseUrl}/api/v1/customer/`;
708
- const data = { email: email };
709
- const response = await fetch(url, {
710
- method: "POST",
711
- headers: {
712
- "Content-Type": "application/json",
713
- Authorization: `Token ${this.apiKeyTonder}`,
714
- },
715
- signal: this.abortController.signal,
716
- body: JSON.stringify(data),
717
- });
718
-
719
- if (response.ok)
720
- return (await response.json()) as CustomerRegisterResponse;
721
- throw await buildPublicAppError({
722
- response,
723
- errorCode: ErrorKeyEnum.CUSTOMER_OPERATION_ERROR,
724
- });
709
+ await this._fetchMerchantData();
710
+ return await registerOrFetchCustomer(
711
+ this._supportTarget(),
712
+ this.apiKeyTonder,
713
+ { email },
714
+ this.abortController.signal,
715
+ );
725
716
  } catch (e) {
726
717
  this.reportSdkError(e, {
727
718
  feature: "customer-register",
@@ -984,25 +975,13 @@ export class LiteCheckout extends BaseInlineCheckout implements ILiteCheckout{
984
975
  try {
985
976
  await this._fetchMerchantData();
986
977
 
987
- const response = await fetch(
988
- `${this.baseUrl}/api/v1/business/${getBusinessId(this.merchantData)}/cards/`,
989
- {
990
- method: "POST",
991
- headers: {
992
- Authorization: `Bearer ${secureToken}`,
993
- "User-token": customerToken,
994
- "Content-Type": "application/json",
995
- },
996
- body: JSON.stringify({ ...data }),
997
- },
998
- );
999
-
1000
- if (response.ok)
1001
- return (await response.json()) as RegisterCustomerCardResponse;
1002
- throw await buildPublicAppError({
1003
- response,
1004
- errorCode: ErrorKeyEnum.SAVE_CARD_ERROR,
1005
- });
978
+ return (await saveCustomerCard(
979
+ this._supportTarget(),
980
+ customerToken,
981
+ secureToken,
982
+ getBusinessId(this.merchantData),
983
+ { ...data } as ISaveCardSkyflowRequest,
984
+ )) as RegisterCustomerCardResponse;
1006
985
  } catch (error) {
1007
986
  this.reportSdkError(error, {
1008
987
  feature: "register-customer-card",
@@ -1026,6 +1005,17 @@ export class LiteCheckout extends BaseInlineCheckout implements ILiteCheckout{
1026
1005
  ): Promise<Boolean | ErrorResponse> {
1027
1006
  try {
1028
1007
  await this._fetchMerchantData();
1008
+ if (this._supportTarget().engine === "core") {
1009
+ // The core takes the secure token and the customer's token, never the customer's token as `Token`.
1010
+ await removeCustomerCard(
1011
+ this._supportTarget(),
1012
+ customerToken,
1013
+ this.secureToken,
1014
+ skyflowId,
1015
+ getBusinessId(this.merchantData),
1016
+ );
1017
+ return true;
1018
+ }
1029
1019
  const response = await fetch(
1030
1020
  `${this.baseUrl}/api/v1/business/${getBusinessId(this.merchantData)}/cards/${skyflowId}`,
1031
1021
  {
@@ -1064,7 +1054,7 @@ export class LiteCheckout extends BaseInlineCheckout implements ILiteCheckout{
1064
1054
  async getActiveAPMs(): Promise<APM[]> {
1065
1055
  try {
1066
1056
  const apms_response = await getCustomerAPMs(
1067
- this.baseUrl,
1057
+ this.coreBaseUrl,
1068
1058
  this.apiKeyTonder,
1069
1059
  );
1070
1060
  const apms_results =
package/src/data/api.ts CHANGED
@@ -1,11 +1,13 @@
1
+ import { API_ENDPOINTS } from "../shared/constants/apiEndpoints";
2
+ import { fromCorePaymentMethods, buildSupportError } from "../helpers/coreSupport";
1
3
  import { ErrorKeyEnum } from "../shared/enum/ErrorKeyEnum";
2
4
  import {
3
5
  buildPublicAppError,
4
6
  } from "../shared/utils/appError";
5
7
 
6
- export async function getCustomerAPMs(baseUrlTonder: string, publicApiKeyTonder: string, query: string = "?status=active&page_size=10000&country=México", signal: AbortSignal | null | undefined = null) {
8
+ export async function getCustomerAPMs(coreBaseUrl: string, publicApiKeyTonder: string, query: string = "?status=active&page_size=10000&country=México", signal: AbortSignal | null | undefined = null) {
7
9
  const response = await fetch(
8
- `${baseUrlTonder}/api/v1/payment_methods${query}`,
10
+ `${coreBaseUrl}${API_ENDPOINTS.CORE_PAYMENT_METHODS}${query}`,
9
11
  {
10
12
  method: 'GET',
11
13
  headers: {
@@ -15,9 +17,6 @@ export async function getCustomerAPMs(baseUrlTonder: string, publicApiKeyTonder:
15
17
  signal
16
18
  });
17
19
 
18
- if (response.ok) return await response.json();
19
- throw await buildPublicAppError({
20
- response,
21
- errorCode: ErrorKeyEnum.FETCH_PAYMENT_METHODS_ERROR,
22
- });
20
+ if (response.ok) return fromCorePaymentMethods(await response.json());
21
+ throw await buildSupportError(response, ErrorKeyEnum.FETCH_PAYMENT_METHODS_ERROR, "core");
23
22
  }
@@ -3,16 +3,26 @@ import { ErrorKeyEnum } from "../shared/enum/ErrorKeyEnum";
3
3
  import {
4
4
  buildPublicAppError,
5
5
  } from "../shared/utils/appError";
6
+ import { API_ENDPOINTS } from "../shared/constants/apiEndpoints";
7
+ import {fromCoreCards, fromCoreSavedCard, buildSupportError} from "../helpers/coreSupport";
8
+ import {ISupportTarget} from "../types/core";
6
9
  import {ICustomerCardsResponse, ISaveCardInternalResponse, ISaveCardSkyflowRequest} from "../types/card";
7
10
 
11
+ // The core takes the business from the credential, never from the path.
12
+ function cardsUrl(target: ISupportTarget, businessId: string | number, skyflowId = ""): string {
13
+ return target.engine === "core"
14
+ ? `${target.baseUrl}${API_ENDPOINTS.CORE_CARDS}${skyflowId && `/${skyflowId}`}`
15
+ : `${target.baseUrl}/api/v1/business/${businessId}/cards/${skyflowId}`;
16
+ }
17
+
8
18
  export async function fetchCustomerCards(
9
- baseUrl: string,
19
+ target: ISupportTarget,
10
20
  customerToken: string,
11
21
  secureToken: string,
12
22
  businessId: string | number,
13
23
  signal = null,
14
24
  ): Promise<ICustomerCardsResponse> {
15
- const url = `${baseUrl}/api/v1/business/${businessId}/cards/`;
25
+ const url = cardsUrl(target, businessId);
16
26
  const response = await fetch(url, {
17
27
  method: "GET",
18
28
  headers: {
@@ -22,7 +32,10 @@ export async function fetchCustomerCards(
22
32
  },
23
33
  signal,
24
34
  });
25
- if (response.ok) return await response.json();
35
+ if (response.ok) {
36
+ const body = await response.json();
37
+ return target.engine === "core" ? fromCoreCards(body) : body;
38
+ }
26
39
  if (response.status === 401) {
27
40
  return {
28
41
  user_id: 0,
@@ -30,21 +43,18 @@ export async function fetchCustomerCards(
30
43
  };
31
44
  }
32
45
 
33
- throw await buildPublicAppError({
34
- response,
35
- errorCode: ErrorKeyEnum.FETCH_CARDS_ERROR,
36
- });
46
+ throw await buildSupportError(response, ErrorKeyEnum.FETCH_CARDS_ERROR, target.engine);
37
47
  }
38
48
 
39
49
  export async function saveCustomerCard(
40
- baseUrl: string,
50
+ target: ISupportTarget,
41
51
  customerToken: string,
42
52
  secureToken: string,
43
53
  businessId: string | number,
44
54
  data: ISaveCardSkyflowRequest,
45
55
  appOrigin: boolean = false,
46
56
  ): Promise<ISaveCardInternalResponse> {
47
- const url = `${baseUrl}/api/v1/business/${businessId}/cards/`;
57
+ const url = cardsUrl(target, businessId);
48
58
  const response = await fetch(url, {
49
59
  method: "POST",
50
60
  headers: {
@@ -56,22 +66,22 @@ export async function saveCustomerCard(
56
66
  body: JSON.stringify(data),
57
67
  });
58
68
 
59
- if (response.ok) return await response.json();
69
+ if (response.ok) {
70
+ const body = await response.json();
71
+ return target.engine === "core" ? fromCoreSavedCard(body) : body;
72
+ }
60
73
 
61
- throw await buildPublicAppError({
62
- response,
63
- errorCode: ErrorKeyEnum.SAVE_CARD_ERROR,
64
- });
74
+ throw await buildSupportError(response, ErrorKeyEnum.SAVE_CARD_ERROR, target.engine);
65
75
  }
66
76
 
67
77
  export async function removeCustomerCard(
68
- baseUrl: string,
78
+ target: ISupportTarget,
69
79
  customerToken: string,
70
80
  secureToken: string,
71
81
  skyflowId = "",
72
82
  businessId: string | number,
73
83
  ): Promise<string> {
74
- const url = `${baseUrl}/api/v1/business/${businessId}/cards/${skyflowId}`;
84
+ const url = cardsUrl(target, businessId, skyflowId);
75
85
 
76
86
  const response = await fetch(url, {
77
87
  method: "DELETE",
@@ -85,8 +95,5 @@ export async function removeCustomerCard(
85
95
  if (response.status === 204) return MESSAGES_EN[ErrorKeyEnum.CARD_REMOVED_SUCCESSFULLY];
86
96
  if (response.ok && "json" in response) return await response.json();
87
97
 
88
- throw await buildPublicAppError({
89
- response,
90
- errorCode: ErrorKeyEnum.REMOVE_CARD_ERROR,
91
- });
98
+ throw await buildSupportError(response, ErrorKeyEnum.REMOVE_CARD_ERROR, target.engine);
92
99
  }
@@ -1,16 +1,22 @@
1
1
  import {CustomerRegisterResponse} from "../types/responses";
2
+ import {fromCoreCustomer, buildSupportError} from "../helpers/coreSupport";
3
+ import {ISupportTarget} from "../types/core";
4
+ import { API_ENDPOINTS } from "../shared/constants/apiEndpoints";
2
5
  import { ErrorKeyEnum } from "../shared/enum/ErrorKeyEnum";
3
6
  import {
4
7
  buildPublicAppError,
5
8
  } from "../shared/utils/appError";
6
9
 
7
10
  export async function registerOrFetchCustomer(
8
- baseUrl: string,
11
+ target: ISupportTarget,
9
12
  apiKey: string,
10
13
  customer: Record<string, any>,
11
14
  signal: AbortSignal | null = null,
12
15
  ): Promise<CustomerRegisterResponse> {
13
- const url = `${baseUrl}/api/v1/customer/`;
16
+ const url =
17
+ target.engine === "core"
18
+ ? `${target.baseUrl}${API_ENDPOINTS.CORE_CUSTOMER}`
19
+ : `${target.baseUrl}/api/v1/customer/`;
14
20
  const data = {
15
21
  email: customer.email,
16
22
  first_name: customer?.firstName,
@@ -28,11 +34,9 @@ export async function registerOrFetchCustomer(
28
34
  });
29
35
 
30
36
  if (response.ok) {
31
- return await response.json();
37
+ const body = await response.json();
38
+ return target.engine === "core" ? fromCoreCustomer(body) : body;
32
39
  }
33
40
 
34
- throw await buildPublicAppError({
35
- response,
36
- errorCode: ErrorKeyEnum.CUSTOMER_OPERATION_ERROR,
37
- });
41
+ throw await buildSupportError(response, ErrorKeyEnum.CUSTOMER_OPERATION_ERROR, target.engine);
38
42
  }
@@ -2,10 +2,12 @@ import {
2
2
  buildPublicAppError,
3
3
  } from "../shared/utils/appError";
4
4
  import { ErrorKeyEnum } from "../shared/enum/ErrorKeyEnum";
5
+ import {fromCorePaymentMethods, buildSupportError} from "../helpers/coreSupport";
5
6
  import {IPaymentMethodResponse} from "../types/paymentMethod";
7
+ import { API_ENDPOINTS } from "../shared/constants/apiEndpoints";
6
8
 
7
9
  export async function fetchCustomerPaymentMethods(
8
- baseUrl: string,
10
+ coreBaseUrl: string,
9
11
  apiKey: string,
10
12
  params = {
11
13
  status: "active",
@@ -16,7 +18,7 @@ export async function fetchCustomerPaymentMethods(
16
18
  const queryString = new URLSearchParams(params).toString();
17
19
 
18
20
  const response = await fetch(
19
- `${baseUrl}/api/v1/payment_methods?${queryString}`,
21
+ `${coreBaseUrl}${API_ENDPOINTS.CORE_PAYMENT_METHODS}?${queryString}`,
20
22
  {
21
23
  method: "GET",
22
24
  headers: {
@@ -27,9 +29,6 @@ export async function fetchCustomerPaymentMethods(
27
29
  },
28
30
  );
29
31
 
30
- if (response.ok) return await response.json();
31
- throw await buildPublicAppError({
32
- response,
33
- errorCode: ErrorKeyEnum.FETCH_PAYMENT_METHODS_ERROR,
34
- });
32
+ if (response.ok) return fromCorePaymentMethods(await response.json());
33
+ throw await buildSupportError(response, ErrorKeyEnum.FETCH_PAYMENT_METHODS_ERROR, "core");
35
34
  }
@@ -1,14 +1,16 @@
1
+ import { buildSupportError } from "../helpers/coreSupport";
2
+ import { API_ENDPOINTS } from "../shared/constants/apiEndpoints";
1
3
  import { ErrorKeyEnum } from "../shared/enum/ErrorKeyEnum";
2
4
  import {
3
5
  buildPublicAppError,
4
6
  } from "../shared/utils/appError";
5
7
 
6
8
  export async function getVaultToken(
7
- baseUrl: string,
9
+ coreBaseUrl: string,
8
10
  apiKey: string,
9
11
  signal = null,
10
12
  ) {
11
- const response = await fetch(`${baseUrl}/api/v1/vault-token/`, {
13
+ const response = await fetch(`${coreBaseUrl}${API_ENDPOINTS.CORE_VAULT_TOKEN}`, {
12
14
  method: "GET",
13
15
  headers: {
14
16
  Authorization: `Token ${apiKey}`,
@@ -21,8 +23,5 @@ export async function getVaultToken(
21
23
  return responseBody.token;
22
24
  }
23
25
 
24
- throw await buildPublicAppError({
25
- response,
26
- errorCode: ErrorKeyEnum.VAULT_TOKEN_ERROR,
27
- });
26
+ throw await buildSupportError(response, ErrorKeyEnum.VAULT_TOKEN_ERROR, "core");
28
27
  }
@@ -105,13 +105,17 @@ export class CardOnFile {
105
105
  private readonly merchantId: string;
106
106
  private readonly apiKey: string;
107
107
  private readonly isTestEnvironment: boolean;
108
+ private readonly subscriptionOwner?: () => string | undefined;
108
109
  private acquirerInstance: AcquirerInstance | null = null;
109
110
 
110
111
  constructor(config: {
111
112
  merchantId: string;
112
113
  apiKey: string;
113
114
  isTestEnvironment?: boolean;
115
+ /** Replaces the `customerId` of `process()` in the subscription metadata when it answers one. */
116
+ subscriptionOwner?: () => string | undefined;
114
117
  }) {
118
+ this.subscriptionOwner = config.subscriptionOwner;
115
119
  this.isTestEnvironment = config.isTestEnvironment ?? true;
116
120
  // Use centralized API endpoint constants
117
121
  const mode = this.isTestEnvironment ? "stage" : "production";
@@ -344,7 +348,7 @@ export class CardOnFile {
344
348
  token: tokenResponse.token,
345
349
  contactDetails: params.contactDetails,
346
350
  metadata: {
347
- customerId: params.customerId,
351
+ customerId: this.subscriptionOwner?.() ?? params.customerId,
348
352
  },
349
353
  currency: params.currency,
350
354
  });
@@ -0,0 +1,81 @@
1
+ import { PaymentEngine } from "../types/core";
2
+ import { AppError, buildPublicAppError, buildPublicAppErrorFromAnswer } from "../shared/utils/appError";
3
+ import { CustomerRegisterResponse } from "../types/responses";
4
+ import { ICustomerCardsResponse, ISaveCardInternalResponse } from "../types/card";
5
+ import { IPaymentMethodResponse } from "../types/paymentMethod";
6
+
7
+ /*
8
+ * The core's support routes answer its own public contract. These functions turn each answer
9
+ * into what the monolith's route answered, so everything the SDK returns to merchants keeps
10
+ * its fields and types.
11
+ */
12
+
13
+ export function fromCoreCustomer(body: any): CustomerRegisterResponse {
14
+ return {
15
+ id: body.customer_id,
16
+ email: body.email,
17
+ auth_token: body.auth_token,
18
+ first_name: body.first_name,
19
+ last_name: body.last_name,
20
+ };
21
+ }
22
+
23
+ export function fromCoreCards(body: any): ICustomerCardsResponse {
24
+ return {
25
+ user_id: body.customer_id,
26
+ cards: (body.data ?? []).map((card: any) => ({
27
+ fields: {
28
+ card_number: card.card_number,
29
+ expiration_month: card.expiration_month,
30
+ expiration_year: card.expiration_year,
31
+ skyflow_id: card.id,
32
+ card_scheme: card.card_scheme,
33
+ subscription_id: card.subscription_id ?? undefined,
34
+ },
35
+ })),
36
+ } as ICustomerCardsResponse;
37
+ }
38
+
39
+ export function fromCoreSavedCard(body: any): ISaveCardInternalResponse {
40
+ return {
41
+ skyflow_id: body.id,
42
+ user_id: body.customer_id,
43
+ ...(body.card_bin ? { card_bin: body.card_bin } : {}),
44
+ };
45
+ }
46
+
47
+ /** The core's list is never paged: the monolith's paged envelope is rebuilt around it. */
48
+ export function fromCorePaymentMethods(body: any): IPaymentMethodResponse {
49
+ const results = (body.data ?? []).map((method: any) => ({ ...method, pk: method.id }));
50
+ return { count: results.length, next: null, previous: null, results };
51
+ }
52
+
53
+ /**
54
+ * The core's error is `{ error: { code, ... }, request_id }`: its `code` becomes the body's own
55
+ * `code`, where an error's system error is read from, as it was with the monolith's body.
56
+ */
57
+ export function fromCoreErrorBody(body: any): unknown {
58
+ if (!body?.error?.code) return body;
59
+ return {
60
+ code: body.error.code,
61
+ message: body.error.message,
62
+ details: body.error.details,
63
+ request_id: body.request_id,
64
+ };
65
+ }
66
+
67
+ /** The error of a refused support call, from the monolith's body or the core's. */
68
+ export async function buildSupportError(
69
+ response: Response,
70
+ errorCode: string,
71
+ engine: PaymentEngine,
72
+ ): Promise<AppError> {
73
+ if (engine !== "core") return buildPublicAppError({ response, errorCode });
74
+ const body = await response.clone().json().catch(() => undefined);
75
+ return buildPublicAppErrorFromAnswer({
76
+ errorCode,
77
+ status: response.status,
78
+ statusText: response.statusText,
79
+ body: fromCoreErrorBody(body),
80
+ });
81
+ }
@@ -32,7 +32,7 @@ import { ErrorKeyEnum } from "../shared/enum/ErrorKeyEnum";
32
32
  * Use `mountCardFields()` to render Skyflow Elements and collect card data securely.
33
33
  */
34
34
  export async function getSkyflowTokens({
35
- baseUrl,
35
+ coreBaseUrl,
36
36
  apiKey,
37
37
  vault_id,
38
38
  vault_url,
@@ -41,7 +41,7 @@ export async function getSkyflowTokens({
41
41
  const skyflow = Skyflow.init({
42
42
  vaultID: vault_id,
43
43
  vaultURL: vault_url,
44
- getBearerToken: async () => await getVaultToken(baseUrl, apiKey),
44
+ getBearerToken: async () => await getVaultToken(coreBaseUrl, apiKey),
45
45
  options: {
46
46
  logLevel: Skyflow.LogLevel.ERROR,
47
47
  env: Skyflow.Env.DEV, // ⚠️ This should be Env.PROD, but currently it cannot be changed because getSkyflowTokens must be deprecated first, since .setValue does not work in PROD.
@@ -132,7 +132,7 @@ async function getFields(
132
132
  }
133
133
 
134
134
  export async function initSkyflowInstance({
135
- baseUrl,
135
+ coreBaseUrl,
136
136
  apiKey,
137
137
  vault_id,
138
138
  vault_url,
@@ -142,7 +142,7 @@ export async function initSkyflowInstance({
142
142
  return Skyflow.init({
143
143
  vaultID: vault_id,
144
144
  vaultURL: vault_url,
145
- getBearerToken: async () => await getVaultToken(baseUrl, apiKey),
145
+ getBearerToken: async () => await getVaultToken(coreBaseUrl, apiKey),
146
146
  options: {
147
147
  logLevel: Skyflow.LogLevel.ERROR,
148
148
  env: skyflowEnv,
@@ -17,6 +17,10 @@ export const API_ENDPOINTS = Object.freeze({
17
17
  ACQ_SUBSCRIPTION_CREATE: "/acq-kushki/subscription/create",
18
18
  CORE_BUSINESS_CONFIG: "/transactions/v1/business/config",
19
19
  CORE_PROCESS: "/transactions/v1/process",
20
+ CORE_CUSTOMER: "/transactions/v1/customer",
21
+ CORE_CARDS: "/transactions/v1/cards",
22
+ CORE_PAYMENT_METHODS: "/transactions/v1/payment_methods",
23
+ CORE_VAULT_TOKEN: "/transactions/v1/vault-token",
20
24
  });
21
25
 
22
26
  /**
package/src/types/core.ts CHANGED
@@ -1,6 +1,12 @@
1
1
  /** Which backend charges and finishes one payment. It is chosen once and never changes. */
2
2
  export type PaymentEngine = "core" | "legacy";
3
3
 
4
+ /** Where the customer and saved-card calls go: tokens are not interchangeable, so both always share one engine. */
5
+ export interface ISupportTarget {
6
+ engine: PaymentEngine;
7
+ baseUrl: string;
8
+ }
9
+
4
10
  export interface ICoreRouting {
5
11
  checkout_id?: string;
6
12
  is_route_finished?: boolean;
@@ -79,7 +79,7 @@ export type RegisterCustomerCardRequest = {
79
79
  }
80
80
 
81
81
  export type TokensSkyflowRequest = {
82
- baseUrl: string;
82
+ coreBaseUrl: string;
83
83
  apiKey: string;
84
84
  vault_id: string,
85
85
  vault_url: string,