-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds ANCV payment method component #2293
Merged
Merged
Changes from 12 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
801ac62
starting ancv
m1aw c0e2005
adds onOrder
m1aw 0a4919b
Merge branch 'main' into feature/ancv-component
m1aw 1d414a2
fixes missing scss in InputBase
m1aw f6c742c
Merge branch 'fix/input-base-missing-scss' into feature/ancv-component
m1aw b17958b
adds validation and order
m1aw 52a6b40
fix error messages
m1aw 56e3ac8
move ANCV input
m1aw a1a2693
Merge branch 'main' into feature/ancv-component
m1aw 1a054e9
fix spelling mistake
m1aw 0111eaa
update error i18n
m1aw a3ccf40
clean up await props
m1aw 2b7a2c7
add ancv to tests
m1aw 55a5ea4
add changeset for ancv
58aaf73
translations for ancv
m1aw fe2766f
fix playwright test report
m1aw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { h } from 'preact'; | ||
import UIElement from '../UIElement'; | ||
import ANCVInput from './components/ANCVInput'; | ||
import CoreProvider from '../../core/Context/CoreProvider'; | ||
import config from './components/ANCVAwait/config'; | ||
import Await from '../../components/internal/Await'; | ||
import SRPanelProvider from '../../core/Errors/SRPanelProvider'; | ||
import { PaymentResponse, UIElementProps } from '../types'; | ||
import AdyenCheckoutError from '../../core/Errors/AdyenCheckoutError'; | ||
import PayButton from '../internal/PayButton'; | ||
|
||
export interface ANCVProps extends UIElementProps { | ||
paymentData?: any; | ||
data: ANCVDataState; | ||
onOrderRequest?: any; | ||
onOrderCreated?: any; | ||
} | ||
|
||
export interface ANCVDataState { | ||
beneficiaryId: string; | ||
} | ||
|
||
export class ANCVElement extends UIElement<ANCVProps> { | ||
private static type = 'ancv'; | ||
|
||
/** | ||
* Formats the component data output | ||
*/ | ||
formatData() { | ||
return { | ||
paymentMethod: { | ||
type: ANCVElement.type, | ||
beneficiaryId: this.state.data?.beneficiaryId | ||
} | ||
}; | ||
} | ||
|
||
private onOrderRequest = data => { | ||
if (this.props.onOrderRequest) | ||
return new Promise((resolve, reject) => { | ||
this.props.onOrderRequest(resolve, reject, data); | ||
}); | ||
|
||
if (this.props.session) { | ||
return this.props.session.createOrder(); | ||
} | ||
}; | ||
|
||
protected handleOrder = ({ order }: PaymentResponse) => { | ||
this.updateParent({ order }); | ||
if (this.props.session && this.props.onOrderCreated) { | ||
return this.props.onOrderCreated(order); | ||
} | ||
}; | ||
|
||
public createOrder = () => { | ||
if (!this.isValid) { | ||
this.showValidation(); | ||
return false; | ||
} | ||
|
||
this.setStatus('loading'); | ||
|
||
return this.onOrderRequest(this.data) | ||
.then((order: { orderData: string; pspReference: string }) => { | ||
this.setState({ order: { orderData: order.orderData, pspReference: order.pspReference } }); | ||
this.submit(); | ||
}) | ||
.catch(error => { | ||
this.setStatus(error?.message || 'error'); | ||
if (this.props.onError) this.handleError(new AdyenCheckoutError('ERROR', error)); | ||
}); | ||
}; | ||
|
||
// Reimplement payButton similar to GiftCard to allow to set onClick | ||
public payButton = props => { | ||
return <PayButton {...props} />; | ||
}; | ||
|
||
get isValid(): boolean { | ||
return !!this.state.isValid; | ||
} | ||
|
||
get displayName(): string { | ||
return this.props.name; | ||
} | ||
|
||
render() { | ||
if (this.props.paymentData) { | ||
return ( | ||
<CoreProvider i18n={this.props.i18n} loadingContext={this.props.loadingContext} resources={this.resources}> | ||
<SRPanelProvider srPanel={this.props.modules.srPanel}> | ||
<Await | ||
ref={ref => { | ||
this.componentRef = ref; | ||
}} | ||
clientKey={this.props.clientKey} | ||
paymentData={this.props.paymentData} | ||
onError={this.props.onError} | ||
onComplete={this.onComplete} | ||
brandLogo={this.icon} | ||
type={this.constructor['type']} | ||
messageText={this.props.i18n.get('ancv.confirmPayment')} | ||
awaitText={this.props.i18n.get('await.waitForConfirmation')} | ||
showCountdownTimer={config.showCountdownTimer} | ||
throttleTime={config.THROTTLE_TIME} | ||
throttleInterval={config.THROTTLE_INTERVAL} | ||
onActionHandled={this.props.onActionHandled} | ||
/> | ||
</SRPanelProvider> | ||
</CoreProvider> | ||
); | ||
} | ||
|
||
return ( | ||
<CoreProvider i18n={this.props.i18n} loadingContext={this.props.loadingContext} resources={this.resources}> | ||
<ANCVInput | ||
ref={ref => { | ||
this.componentRef = ref; | ||
}} | ||
{...this.props} | ||
onSubmit={this.createOrder} | ||
onChange={this.setState} | ||
payButton={this.payButton} | ||
showPayButton={this.props.showPayButton} | ||
/> | ||
</CoreProvider> | ||
); | ||
} | ||
} | ||
|
||
export default ANCVElement; |
10 changes: 10 additions & 0 deletions
10
packages/lib/src/components/ANCV/components/ANCVAwait/config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const COUNTDOWN_MINUTES = 15; // min | ||
export const THROTTLE_TIME = 60000; // ms | ||
export const THROTTLE_INTERVAL = 10000; // ms | ||
|
||
export default { | ||
COUNTDOWN_MINUTES, | ||
THROTTLE_TIME, | ||
THROTTLE_INTERVAL, | ||
showCountdownTimer: false | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { h } from 'preact'; | ||
import { useEffect, useState } from 'preact/hooks'; | ||
import useCoreContext from '../../../core/Context/useCoreContext'; | ||
import LoadingWrapper from '../../internal/LoadingWrapper'; | ||
import InputText from '../../internal/FormFields/InputText'; | ||
import Field from '../../internal/FormFields/Field'; | ||
import useForm from '../../../utils/useForm'; | ||
import { UIElementProps } from '../../types'; | ||
import { ancvValidationRules } from '../validate'; | ||
import { ANCVDataState } from '../ANCV'; | ||
|
||
export interface ANCVInputProps extends UIElementProps { | ||
ref?: any; | ||
showPayButton: boolean; | ||
onSubmit: () => void; | ||
} | ||
|
||
type ANCVInputDataState = ANCVDataState; | ||
|
||
function ANCVInput({ showPayButton, payButton, onChange, onSubmit }: ANCVInputProps) { | ||
const { i18n } = useCoreContext(); | ||
|
||
const { handleChangeFor, triggerValidation, data, valid, errors, isValid } = useForm<ANCVInputDataState>({ | ||
schema: ['beneficiaryId'], | ||
rules: ancvValidationRules | ||
}); | ||
|
||
useEffect(() => { | ||
onChange({ data, errors, valid, isValid }, this); | ||
}, [data, valid, errors, isValid]); | ||
|
||
const [status, setStatus] = useState<string>('ready'); | ||
|
||
this.setStatus = setStatus; | ||
this.showValidation = triggerValidation; | ||
|
||
return ( | ||
<LoadingWrapper> | ||
<div className="adyen-checkout__ancv"> | ||
<p className="adyen-checkout-form-instruction">{i18n.get('ancv.form.instruction')}</p> | ||
<Field | ||
errorMessage={!!errors.beneficiaryId && i18n.get(errors.beneficiaryId.errorMessage)} | ||
label={i18n.get('ancv.input.label')} | ||
isValid={valid.beneficiaryId} | ||
name={'beneficiaryId'} | ||
> | ||
<InputText | ||
value={data.beneficiaryId} | ||
name={'beneficiaryId'} | ||
spellcheck={true} | ||
required={true} | ||
onInput={handleChangeFor('beneficiaryId', 'input')} | ||
onBlur={handleChangeFor('beneficiaryId', 'blur')} | ||
/> | ||
</Field> | ||
{showPayButton && payButton({ status, label: i18n.get('confirmPurchase'), onClick: onSubmit })} | ||
</div> | ||
</LoadingWrapper> | ||
); | ||
} | ||
|
||
ANCVInput.defaultProps = {}; | ||
|
||
export default ANCVInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './ANCV'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { ValidatorRules } from '../../utils/Validator/types'; | ||
import { isEmailValid } from '../internal/PersonalDetails/validate'; | ||
|
||
export const isANCVNumber = text => /^\d{11}$/.test(text); | ||
|
||
export const ancvValidationRules: ValidatorRules = { | ||
beneficiaryId: { | ||
validate: value => isEmailValid(value) || isANCVNumber(value), | ||
errorMessage: 'ancv.beneficiaryId.invalid', | ||
modes: ['blur'] | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/lib/storybook/stories/components/ANCV.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Meta, StoryObj } from '@storybook/preact'; | ||
import { PaymentMethodStoryProps } from '../types'; | ||
import { getStoryContextCheckout } from '../../utils/get-story-context-checkout'; | ||
import { Container } from '../Container'; | ||
import { ANCVProps } from '../../../src/components/ANCV/ANCV'; | ||
|
||
type ANCVStory = StoryObj<PaymentMethodStoryProps<ANCVProps>>; | ||
|
||
const meta: Meta<PaymentMethodStoryProps<ANCVProps>> = { | ||
title: 'Components/ANCV' | ||
}; | ||
|
||
export const ANCV: ANCVStory = { | ||
render: (args, context) => { | ||
const checkout = getStoryContextCheckout(context); | ||
return <Container type={'ancv'} componentConfiguration={args.componentConfiguration} checkout={checkout} />; | ||
}, | ||
args: { | ||
countryCode: 'NL', | ||
amount: 2000, | ||
useSessions: false | ||
} | ||
}; | ||
export default meta; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic should be move elsewhere.