Skip to content

Commit

Permalink
fixing sonarcloud lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ribeiroguilherme committed Jan 25, 2024
1 parent 0c5319f commit ea50de1
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 101 deletions.
28 changes: 4 additions & 24 deletions packages/lib/src/components/ApplePay/ApplePay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ import { httpPost } from '../../core/Services/http';
import { APPLEPAY_SESSION_ENDPOINT } from './config';
import { preparePaymentRequest } from './payment-request';
import { resolveSupportedVersion, mapBrands, formatApplePayContactToAdyenAddressFormat } from './utils';
import {
ApplePayConfiguration,
ApplePayElementData,
ApplePayPaymentOrderDetails,
ApplePaySessionRequest
} from './types';
import { ApplePayConfiguration, ApplePayElementData, ApplePayPaymentOrderDetails, ApplePaySessionRequest } from './types';
import AdyenCheckoutError from '../../core/Errors/AdyenCheckoutError';
import { TxVariants } from '../tx-variants';
import { PaymentResponseData, RawPaymentResponse } from '../../types/global-types';
Expand Down Expand Up @@ -71,13 +66,7 @@ class ApplePayElement extends UIElement<ApplePayConfiguration> {
};

private startSession() {
const {
version,
onValidateMerchant,
onPaymentMethodSelected,
onShippingMethodSelected,
onShippingContactSelected
} = this.props;
const { version, onValidateMerchant, onPaymentMethodSelected, onShippingMethodSelected, onShippingContactSelected } = this.props;

const paymentRequest = preparePaymentRequest({
companyName: this.props.configuration.merchantName,
Expand Down Expand Up @@ -244,24 +233,15 @@ class ApplePayElement extends UIElement<ApplePayConfiguration> {
*/
public override async isAvailable(): Promise<void> {
if (document.location.protocol !== 'https:') {
return Promise.reject(
new AdyenCheckoutError(
'IMPLEMENTATION_ERROR',
'Trying to start an Apple Pay session from an insecure document'
)
);
return Promise.reject(new AdyenCheckoutError('IMPLEMENTATION_ERROR', 'Trying to start an Apple Pay session from an insecure document'));
}

if (!this.props.onValidateMerchant && !this.props.clientKey) {
return Promise.reject(new AdyenCheckoutError('IMPLEMENTATION_ERROR', 'clientKey was not provided'));
}

try {
if (
window.ApplePaySession &&
ApplePaySession.canMakePayments() &&
ApplePaySession.supportsVersion(this.props.version)
) {
if (window.ApplePaySession && ApplePaySession.canMakePayments() && ApplePaySession.supportsVersion(this.props.version)) {
return Promise.resolve();
}
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion packages/lib/src/components/ApplePay/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export interface ApplePayElementData {
type: string;
applePayToken: string;
billingAddress?: AddressData;
shippingAddress?: AddressData;
deliveryAddress?: AddressData;
};
}

Expand Down
19 changes: 4 additions & 15 deletions packages/lib/src/components/GooglePay/GooglePay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,10 @@ class GooglePay extends UIElement<GooglePayConfiguration> {
}

formatProps(props): GooglePayConfiguration {
// const allowedCardNetworks = props.brands?.length ? mapBrands(props.brands) : props.allowedCardNetworks;
const buttonSizeMode = props.buttonSizeMode ?? (props.isDropin ? 'fill' : 'static');
const buttonLocale = getGooglePayLocale(props.buttonLocale ?? props.i18n?.locale);

const callbackIntents: google.payments.api.CallbackIntent[] = [
...props.callbackIntents,
'PAYMENT_AUTHORIZATION'
];
const callbackIntents: google.payments.api.CallbackIntent[] = [...props.callbackIntents, 'PAYMENT_AUTHORIZATION'];

return {
...props,
Expand Down Expand Up @@ -86,16 +82,9 @@ class GooglePay extends UIElement<GooglePayConfiguration> {
*
* @see https://developers.google.com/pay/api/web/reference/client#onPaymentAuthorized
**/
private onPaymentAuthorized = async (
paymentData: google.payments.api.PaymentData
): Promise<google.payments.api.PaymentAuthorizationResult> => {
const billingAddress: AddressData = formatGooglePayContactToAdyenAddressFormat(
paymentData.paymentMethodData.info.billingAddress
);
const deliveryAddress: AddressData = formatGooglePayContactToAdyenAddressFormat(
paymentData.shippingAddress,
true
);
private onPaymentAuthorized = async (paymentData: google.payments.api.PaymentData): Promise<google.payments.api.PaymentAuthorizationResult> => {
const billingAddress: AddressData = formatGooglePayContactToAdyenAddressFormat(paymentData.paymentMethodData.info.billingAddress);
const deliveryAddress: AddressData = formatGooglePayContactToAdyenAddressFormat(paymentData.shippingAddress, true);

this.setState({
authorizedEvent: paymentData,
Expand Down
16 changes: 0 additions & 16 deletions packages/lib/src/components/GooglePay/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,6 @@ export function formatGooglePayContactToAdyenAddressFormat(
};
}

// export function mapBrands(brands) {
// const brandMapping = {
// mc: 'MASTERCARD',
// amex: 'AMEX',
// visa: 'VISA',
// interac: 'INTERAC',
// discover: 'DISCOVER'
// };
// return brands.reduce((accumulator, item) => {
// if (!!brandMapping[item] && !accumulator.includes(brandMapping[item])) {
// accumulator.push(brandMapping[item]);
// }
// return accumulator;
// }, []);
// }

const supportedLocales = [
'en',
'ar',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ import { AddressData } from '../../../types/global-types';
/**
* This function formats PayPal contact format to Adyen address format
*/
export const formatPaypalOrderContatcToAdyenFormat = (
paymentContact: any,
isDeliveryAddress?: boolean
): AddressData | null => {
export const formatPaypalOrderContatcToAdyenFormat = (paymentContact: any, isDeliveryAddress?: boolean): AddressData | null => {
const getStreet = (addressPart1 = null, addressPart2 = null): string | null => {
if (addressPart1 && addressPart2) return `${addressPart1}, ${addressPart2}`;
if (addressPart1) return addressPart1;
if (addressPart2) return addressPart2;
return null;
};

if (!paymentContact || !paymentContact.address) return null;
if (paymentContact?.address === undefined) return null;

const { address, name } = paymentContact;
const street = getStreet(address.address_line_1, address.address_line_2);
Expand Down
16 changes: 11 additions & 5 deletions packages/lib/src/components/internal/UIElement/UIElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import { Resources } from '../../../core/Context/Resources';
import { NewableComponent } from '../../../core/core.registry';
import { ComponentMethodsRef, IUIElement, PayButtonFunctionProps, UIElementProps, UIElementStatus } from './types';
import {
AdditionalDetailsStateData,
CheckoutAdvancedFlowResponse,
Order,
PaymentAction,
PaymentData,
PaymentMethodsResponse,
CheckoutAdvancedFlowResponse,
PaymentResponseData,
RawPaymentResponse,
AdditionalDetailsStateData
RawPaymentResponse
} from '../../../types/global-types';
import './UIElement.scss';
import { CheckoutSessionDetailsResponse, CheckoutSessionPaymentResponse } from '../../../core/CheckoutSession/types';
Expand Down Expand Up @@ -249,7 +249,7 @@ export abstract class UIElement<P extends UIElementProps = UIElementProps> exten

private async submitAdditionalDetailsUsingSessionsFlow(data: any): Promise<CheckoutSessionDetailsResponse> {
try {
return this.core.session.submitDetails(data);
return await this.core.session.submitDetails(data);
} catch (error) {
this.handleError(error);
return Promise.reject(error);
Expand Down Expand Up @@ -402,10 +402,16 @@ export abstract class UIElement<P extends UIElementProps = UIElementProps> exten
return <PayButton {...props} amount={this.props.amount} secondaryAmount={this.props.secondaryAmount} onClick={this.submit} />;
};

/**
* Used in the Partial Orders flow.
* When the Order is updated, the merchant can request new payment methods based on the new remaining amount
*
* @private
*/
private async handleAdvanceFlowPaymentMethodsUpdate(order: Order) {
return new Promise<PaymentMethodsResponse>((resolve, reject) => {
if (!this.props.onPaymentMethodsRequest) {
return reject();
return reject(new Error('onPaymentMethodsRequest is not implemented'));
}

this.props.onPaymentMethodsRequest(
Expand Down
24 changes: 6 additions & 18 deletions packages/lib/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,12 @@ class Core implements ICore {
this.setOptions(props);

this.loadingContext = resolveEnvironment(this.options.environment, this.options.environmentUrls?.api);
this.cdnContext = resolveCDNEnvironment(
this.options.resourceEnvironment || this.options.environment,
this.options.environmentUrls?.api
);
this.session =
this.options.session && new Session(this.options.session, this.options.clientKey, this.loadingContext);
this.cdnContext = resolveCDNEnvironment(this.options.resourceEnvironment || this.options.environment, this.options.environmentUrls?.api);
this.session = this.options.session && new Session(this.options.session, this.options.clientKey, this.loadingContext);

const clientKeyType = this.options.clientKey?.substr(0, 4);
if ((clientKeyType === 'test' || clientKeyType === 'live') && !this.loadingContext.includes(clientKeyType)) {
throw new Error(
`Error: you are using a '${clientKeyType}' clientKey against the '${this.options.environment}' environment`
);
throw new Error(`Error: you are using a '${clientKeyType}' clientKey against the '${this.options.environment}' environment`);
}

// Expose version number for npm builds
Expand Down Expand Up @@ -168,9 +162,7 @@ class Core implements ICore {
'a "resultCode": have you passed in the whole response object by mistake?'
);
}
throw new Error(
'createFromAction::Invalid Action - the passed action object does not have a "type" property'
);
throw new Error('createFromAction::Invalid Action - the passed action object does not have a "type" property');
}

if (action.type) {
Expand Down Expand Up @@ -260,8 +252,7 @@ class Core implements ICore {
* @internal
*/
private handleCreateError(paymentMethod?): never {
const paymentMethodName =
paymentMethod && paymentMethod.name ? paymentMethod.name : 'The passed payment method';
const paymentMethodName = paymentMethod?.name ?? 'The passed payment method';
const errorMessage = paymentMethod
? `${paymentMethodName} is not a valid Checkout Component. What was passed as a txVariant was: ${JSON.stringify(
paymentMethod
Expand All @@ -272,10 +263,7 @@ class Core implements ICore {
}

private createPaymentMethodsList(paymentMethodsResponse?: PaymentMethods): void {
this.paymentMethodsResponse = new PaymentMethods(
this.options.paymentMethodsResponse || paymentMethodsResponse,
this.options
);
this.paymentMethodsResponse = new PaymentMethods(this.options.paymentMethodsResponse || paymentMethodsResponse, this.options);
}

private createCoreModules(): void {
Expand Down
2 changes: 0 additions & 2 deletions packages/lib/src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ export interface CoreConfiguration {
actions: {
resolve: (response: CheckoutAdvancedFlowResponse) => void;
reject: () => void;
// reject: (error?: onSubmitReject) => void;
}
): void;

Expand All @@ -208,7 +207,6 @@ export interface CoreConfiguration {
actions: {
resolve: (response: CheckoutAdvancedFlowResponse) => void;
reject: () => void;
// reject: (error?: onSubmitReject) => void;
}
): void;

Expand Down
15 changes: 0 additions & 15 deletions packages/lib/src/types/global-types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { ADDRESS_SCHEMA } from '../components/internal/Address/constants';
import actionTypes from '../core/ProcessResponse/PaymentAction/actionTypes';
// import { onSubmitReject } from '../core/types';

export type PaymentActionsType = keyof typeof actionTypes;

Expand Down Expand Up @@ -206,19 +205,6 @@ export interface PaymentAmountExtended extends PaymentAmount {
currencyDisplay?: string;
}

export type ShopperDetails = {
shopperName?: {
firstName?: string;
lastName?: string;
};
shopperEmail?: string;
countryCode?: string;
telephoneNumber?: string;
dateOfBirth?: string;
billingAddress?: Partial<AddressData>;
shippingAddress?: Partial<AddressData>;
};

export type AddressField = (typeof ADDRESS_SCHEMA)[number];

export type AddressData = {
Expand Down Expand Up @@ -336,7 +322,6 @@ export type SessionsResponse = {
resultCode: ResultCode;
};

//TODO double check these values
export interface PaymentMethodsRequestData {
order?: Order;
locale?: string;
Expand Down

0 comments on commit ea50de1

Please sign in to comment.