Skip to content

Commit

Permalink
Making getRegulatoryDefaults a util function that also sets props bas…
Browse files Browse the repository at this point in the history
…ed on whether component is a Dropin or not
  • Loading branch information
sponglord committed Feb 16, 2024
1 parent 43aa025 commit 0bf70b9
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 23 deletions.
3 changes: 2 additions & 1 deletion packages/lib/src/components/Dropin/Dropin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import { hasOwnProperty } from '../../utils/hasOwnProperty';
import SRPanelProvider from '../../core/Errors/SRPanelProvider';
import splitPaymentMethods from './elements/splitPaymentMethods';
import type { ICore } from '../../core/types';
import { IDropin } from './types';

const SUPPORTED_INSTANT_PAYMENTS = ['paywithgoogle', 'googlepay', 'applepay'];

class DropinElement extends UIElement<DropinConfiguration> {
class DropinElement extends UIElement<DropinConfiguration> implements IDropin {
protected static defaultProps = defaultProps;

public dropinRef = null;
Expand Down
5 changes: 5 additions & 0 deletions packages/lib/src/components/Dropin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,8 @@ export interface DropinComponentState {
isDisabling: boolean;
orderStatus: OrderStatus;
}

export interface IDropin {
activePaymentMethod: () => null;
closeActivePaymentMethod: () => void;
}
7 changes: 5 additions & 2 deletions packages/lib/src/components/internal/UIElement/UIElement.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { h } from 'preact';
import BaseElement from '../BaseElement/BaseElement';
import PayButton from '../PayButton';
import { cleanupFinalResult, sanitizeResponse, verifyPaymentDidNotFail } from './utils';
import { assertIsDropin, cleanupFinalResult, getRegulatoryDefaults, sanitizeResponse, verifyPaymentDidNotFail } from './utils';
import AdyenCheckoutError from '../../../core/Errors/AdyenCheckoutError';
import { hasOwnProperty } from '../../../utils/hasOwnProperty';
import { Resources } from '../../../core/Context/Resources';
Expand All @@ -22,6 +22,7 @@ import type {
} from '../../../types/global-types';
import { ANALYTICS_SUBMIT_STR } from '../../../core/Analytics/constants';
import { AnalyticsInitialEvent, SendAnalyticsObject } from '../../../core/Analytics/types';
import { IDropin } from '../../Dropin/types';

import './UIElement.scss';

Expand Down Expand Up @@ -83,9 +84,11 @@ export abstract class UIElement<P extends UIElementProps = UIElementProps> exten
...componentProps
};

const isDropin = assertIsDropin(this as unknown as IDropin);

this.props = this.formatProps({
...this.constructor['defaultProps'], // component defaults
...this.core.getRegulatoryDefaults(), // regulatory defaults
...getRegulatoryDefaults(this.core.options.countryCode, isDropin), // regulatory defaults
...finalProps // the rest (inc. merchant defined config)
});
}
Expand Down
23 changes: 23 additions & 0 deletions packages/lib/src/components/internal/UIElement/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { UIElementStatus } from './types';
import { RawPaymentResponse, PaymentResponseData } from '../../../types/global-types';
import { IDropin } from '../../Dropin/types';

const ALLOWED_PROPERTIES = ['action', 'resultCode', 'sessionData', 'order', 'sessionResult', 'donationToken', 'error'];

Expand Down Expand Up @@ -57,3 +58,25 @@ export function verifyPaymentDidNotFail(response: PaymentResponseData): Promise<

return Promise.resolve(response);
}

export function assertIsDropin(dropin: IDropin) {
if (!dropin) return false;

const isDropin = typeof dropin.activePaymentMethod === 'object' && typeof dropin.closeActivePaymentMethod === 'function';
return isDropin;
}

export function getRegulatoryDefaults(countryCode: string, isDropin: boolean): Record<string, any> {
switch (countryCode) {
// Finnish regulations state that no payment method can be open by default
case 'FI':
return isDropin
? {
openFirstPaymentMethod: false,
openFirstStoredPaymentMethod: false
}
: {};
default:
return {};
}
}
6 changes: 1 addition & 5 deletions packages/lib/src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { resolveEnvironment, resolveCDNEnvironment, resolveAnalyticsEnvironment
import Analytics from './Analytics';
import { AdditionalDetailsStateData, PaymentAction, PaymentResponseData } from '../types/global-types';
import { CoreConfiguration, ICore } from './types';
import { getDefaultPropsByCountryCode, processGlobalOptions } from './utils';
import { processGlobalOptions } from './utils';
import Session from './CheckoutSession';
import { hasOwnProperty } from '../utils/hasOwnProperty';
import { Resources } from './Context/Resources';
Expand Down Expand Up @@ -264,10 +264,6 @@ class Core implements ICore {
};
}

public getRegulatoryDefaults(): Record<string, any> {
return getDefaultPropsByCountryCode(this.options.countryCode);
}

public storeElementReference(element: UIElement) {
if (element) {
this.components.push(element);
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 @@ -40,8 +40,6 @@ export interface ICore {

storeElementReference(element: UIElement): void;

getRegulatoryDefaults(): Record<string, any>;

options: CoreConfiguration;
paymentMethodsResponse: PaymentMethods;
session?: Session;
Expand Down
13 changes: 0 additions & 13 deletions packages/lib/src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,3 @@ export function processGlobalOptions(globalOptions) {
return r;
}, {});
}

export function getDefaultPropsByCountryCode(countryCode: string): Record<string, any> {
switch (countryCode) {
// Finnish regulations state that no payment method can be open by default
case 'FI':
return {
openFirstPaymentMethod: false,
openFirstStoredPaymentMethod: false
};
default:
return {};
}
}

0 comments on commit 0bf70b9

Please sign in to comment.