Skip to content

Commit

Permalink
improve: [DEV-1350] Replicate secure token logic
Browse files Browse the repository at this point in the history
improve: [DEV-1350] Replicate secure token logic
  • Loading branch information
David Hernandez committed Oct 22, 2024
1 parent 3d920a5 commit 75e830c
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 45 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tonder.io/ionic-lite-sdk",
"version": "0.0.42-beta.1",
"version": "0.0.42-beta.9",
"description": "Tonder ionic lite SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
36 changes: 31 additions & 5 deletions src/classes/BaseInlineCheckout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
startCheckoutRouter,
} from "../data/checkoutApi";
import { getOpenpayDeviceSessionID } from "../data/openPayApi";
import { getBrowserInfo } from "../helpers/utils";
import {
buildErrorResponse,
buildErrorResponseFromCatch,
formatPublicErrorResponse,
getBrowserInfo
} from "../helpers/utils";
import { registerOrFetchCustomer } from "../data/customerApi";
import { get } from "lodash";
import {
Expand All @@ -18,12 +23,20 @@ import {
saveCustomerCard,
} from "../data/cardApi";
import { fetchCustomerPaymentMethods } from "../data/paymentMethodApi";
import {Business, IConfigureCheckout, IInlineCheckoutBaseOptions, CustomizationOptions} from "../types/commons";
import {
Business,
IConfigureCheckout,
IInlineCheckoutBaseOptions,
CustomizationOptions,
} from "../types/commons";
import {ICustomer} from "../types/customer";
import {ICardFields, IItem, IProcessPaymentRequest, IStartCheckoutResponse} from "../types/checkout";
import {ICustomerCardsResponse, ISaveCardResponse, ISaveCardSkyflowRequest} from "../types/card";
import {IPaymentMethodResponse} from "../types/paymentMethod";
import {ITransaction} from "../types/transaction";
import {GetSecureTokenResponse} from "../types/responses";
import {getSecureToken} from "../data/tokenApi";
import {MESSAGES} from "../shared/constants/messages";
export class BaseInlineCheckout {
baseUrl = "";
cartTotal: string | number = "0";
Expand Down Expand Up @@ -131,6 +144,19 @@ export class BaseInlineCheckout {
});
}

async getSecureToken(secretApikey: string): Promise<GetSecureTokenResponse> {
try {
return await getSecureToken(this.baseUrl, secretApikey)
} catch (error) {
throw formatPublicErrorResponse(
{
message: MESSAGES.secureTokenError,
},
error,
);
}
}

async _initializeCheckout() {
const business_response = await this._fetchMerchantData();

Expand Down Expand Up @@ -300,19 +326,18 @@ export class BaseInlineCheckout {
authToken: string,
businessId: string | number,
): Promise<ICustomerCardsResponse> {
return await fetchCustomerCards(this.baseUrl, authToken, businessId);
return await fetchCustomerCards(this.baseUrl, authToken, this.secureToken, businessId);
}

async _saveCustomerCard(
authToken: string,
secureToken: string,
businessId: string | number,
skyflowTokens: ISaveCardSkyflowRequest,
): Promise<ISaveCardResponse> {
return await saveCustomerCard(
this.baseUrl,
this.secureToken,
authToken,
this.secureToken,
businessId,
skyflowTokens,
);
Expand All @@ -326,6 +351,7 @@ export class BaseInlineCheckout {
return await removeCustomerCard(
this.baseUrl,
authToken,
this.secureToken,
skyflowId,
businessId,
);
Expand Down
22 changes: 1 addition & 21 deletions src/classes/liteCheckout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
CreateOrderResponse,
CreatePaymentResponse,
CustomerRegisterResponse,
GetBusinessResponse, IErrorResponse, RegisterCustomerCardResponse, StartCheckoutResponse, GetSecureTokenResponse
GetBusinessResponse, IErrorResponse, RegisterCustomerCardResponse, StartCheckoutResponse
} from "../types/responses";
import {
CreateOrderRequest,
Expand Down Expand Up @@ -80,7 +80,6 @@ export class LiteCheckout extends BaseInlineCheckout implements ILiteCheckout{
}

public async saveCustomerCard(
secureToken: string,
card: ISaveCardRequest,
): Promise<ISaveCardResponse> {
try {
Expand All @@ -97,7 +96,6 @@ export class LiteCheckout extends BaseInlineCheckout implements ILiteCheckout{
});

return await this._saveCustomerCard(
secureToken,
auth_token,
business?.pk,
skyflowTokens,
Expand Down Expand Up @@ -570,22 +568,4 @@ export class LiteCheckout extends BaseInlineCheckout implements ILiteCheckout{
return [];
}
}

async getSecureToken(token: string): Promise<GetSecureTokenResponse | ErrorResponse> {
try {
const response = await fetch(`${this.baseUrl}/api/secure-token/`, {
method: 'POST',
headers: {
'Authorization': `Token ${token}`,
'Content-Type': 'application/json'
},
signal: this.abortController.signal
});

if (response.ok) return await response.json() as GetSecureTokenResponse;
throw await buildErrorResponse(response);
} catch (error) {
throw buildErrorResponseFromCatch(error);
}
}
}
28 changes: 17 additions & 11 deletions src/data/cardApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {ICustomerCardsResponse, ISaveCardResponse, ISaveCardSkyflowRequest} from
export async function fetchCustomerCards(
baseUrl: string,
customerToken: string,
secureToken: string,
businessId: string | number,
signal = null,
): Promise<ICustomerCardsResponse> {
Expand All @@ -16,24 +17,30 @@ export async function fetchCustomerCards(
const response = await fetch(url, {
method: "GET",
headers: {
Authorization: `Token ${customerToken}`,
Authorization: `Bearer ${secureToken}`,
"Content-Type": "application/json",
'User-token': customerToken,
},
signal,
});
if (response.ok) return await response.json();
const res_json = await response.json();
if (response.status === 401) {
return {
user_id: 0,
cards: [],
};
}

throw await buildErrorResponse(response, res_json);
throw await buildErrorResponse(response);
} catch (error) {
throw buildErrorResponseFromCatch(error);
}
}

export async function saveCustomerCard(
baseUrl: string,
secureToken: string,
customerToken: string,
secureToken: string,
businessId: string | number,
data: ISaveCardSkyflowRequest,
): Promise<ISaveCardResponse> {
Expand All @@ -51,9 +58,7 @@ export async function saveCustomerCard(

if (response.ok) return await response.json();

const res_json = await response.json();

throw await buildErrorResponse(response, res_json);
throw await buildErrorResponse(response);
} catch (error) {
throw buildErrorResponseFromCatch(error);
}
Expand All @@ -62,6 +67,7 @@ export async function saveCustomerCard(
export async function removeCustomerCard(
baseUrl: string,
customerToken: string,
secureToken: string,
skyflowId = "",
businessId: string | number,
): Promise<string> {
Expand All @@ -71,16 +77,16 @@ export async function removeCustomerCard(
const response = await fetch(url, {
method: "DELETE",
headers: {
Authorization: `Token ${customerToken}`,
Authorization: `Bearer ${secureToken}`,
"Content-Type": "application/json",
'User-token': customerToken,
},
});

if (response.status === 204) return MESSAGES.cardSaved;
if (response.status === 204) return MESSAGES.removedCard;
if (response.ok && "json" in response) return await response.json();
const res_json = await response.json();

throw await buildErrorResponse(response, res_json);
throw await buildErrorResponse(response);
} catch (error) {
throw buildErrorResponseFromCatch(error);
}
Expand Down
3 changes: 1 addition & 2 deletions src/data/paymentMethodApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ export async function fetchCustomerPaymentMethods(
);

if (response.ok) return await response.json();
const res_json = await response.json();
throw await buildErrorResponse(response, res_json);
throw await buildErrorResponse(response);
} catch (error) {
throw buildErrorResponseFromCatch(error);
}
Expand Down
27 changes: 27 additions & 0 deletions src/data/tokenApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { GetSecureTokenResponse } from "../types/responses";
import {
buildErrorResponse,
buildErrorResponseFromCatch,
} from "../helpers/utils";

export async function getSecureToken(
baseUrl: string,
token: string,
signal = null,
): Promise<GetSecureTokenResponse> {
try {
const response = await fetch(`${baseUrl}/api/secure-token/`, {
method: "POST",
headers: {
Authorization: `Token ${token}`,
"Content-Type": "application/json",
},
signal,
});

if (response.ok) return (await response.json()) as GetSecureTokenResponse;
throw await buildErrorResponse(response);
} catch (error) {
throw buildErrorResponseFromCatch(error);
}
}
3 changes: 2 additions & 1 deletion src/shared/constants/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export const MESSAGES = Object.freeze({
errorCheckout: "No se ha podido procesar el pago",
cardSaved: "Tarjeta registrada con éxito.",
getPaymentMethodsError: "Ha ocurrido un error obteniendo las métodos de pago del customer. Inténtalo nuevamente.",
getBusinessError: "Ha ocurrido un error obteniendo los datos del comercio. Inténtalo nuevamente."
getBusinessError: "Ha ocurrido un error obteniendo los datos del comercio. Inténtalo nuevamente.",
secureTokenError: "Ha ocurrido un error obteniendo el token de seguridad.",
})
15 changes: 13 additions & 2 deletions src/types/liteInlineCheckout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
CreateOrderResponse,
CreatePaymentResponse,
CustomerRegisterResponse,
GetBusinessResponse,
GetBusinessResponse, GetSecureTokenResponse,
RegisterCustomerCardResponse,
StartCheckoutResponse,
} from "./responses";
Expand Down Expand Up @@ -86,7 +86,7 @@ export interface ILiteCheckout {
*
* @public
*/
saveCustomerCard(secureToken: string, card: ISaveCardRequest): Promise<ISaveCardResponse>;
saveCustomerCard(card: ISaveCardRequest): Promise<ISaveCardResponse>;

/**
* Removes a card from a customer's account.
Expand All @@ -109,6 +109,17 @@ export interface ILiteCheckout {
*/
getCustomerPaymentMethods(): Promise<IPaymentMethod[]>;

/**
* Retrieves security token to access the saved cards functionality.
* @param {string} secretApikey
* @returns {Promise<import("./index").GetSecureTokenResponse>} A promise that resolves with the token.
*
* @throws {import("./index").IPublicError} Throws an error object if the operation fails.
*
* @public
*/
getSecureToken(secretApikey: string): Promise<GetSecureTokenResponse>

/**
* @deprecated This method is deprecated and will be removed in a future release.
* It is no longer necessary to use this method, now automatically handled
Expand Down

0 comments on commit 75e830c

Please sign in to comment.