Skip to content
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

feat(PAYMENTS-15242): add form initialization #16

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/event-name.enum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const enum EventName {
error = 'error',
initPayment = 'initPayment',
initForm = 'initForm',
getPaymentMethodsList = 'getPaymentMethodsList',
getPaymentQuickMethods = 'getPaymentQuickMethods',
getSavedMethods = 'getSavedMethods',
Expand Down
24 changes: 24 additions & 0 deletions src/core/form/field.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { XpsBoolean } from '../xps-boolean.enum';

export interface Field {
name: string;
type: string;
value: string;
title: string;
example: string;
options?: unknown[];
isMandatory: XpsBoolean;
isReadonly?: XpsBoolean;
isVisible: XpsBoolean;
tooltip?: string;
regex?: string;
validation_error_msg?: string;
label_hint?: string;
javascript: {
change: {
mutableFields: string[];
staticParams: { [key: string]: string };
params: string[];
};
};
}
5 changes: 5 additions & 0 deletions src/core/form/form-configuration.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface FormConfiguration {
paymentMethodId: number;
returnUrl: string;
country?: string;
}
5 changes: 5 additions & 0 deletions src/core/form/form.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Field } from './field.interface';

export interface Form {
fields: Field[];
}
14 changes: 14 additions & 0 deletions src/core/guards/init-form-event-message.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { EventName } from '../../core/event-name.enum';
import { Message } from '../../core/message.interface';
import { Form } from '../form/form.interface';
import { isEventMessage } from './event-message.guard';

export const isInitFormEventMessage = (
messageData: unknown
): messageData is Message<Form> => {
if (isEventMessage(messageData)) {
return messageData.name === EventName.initForm;
}

return false;
};
4 changes: 4 additions & 0 deletions src/core/xps-boolean.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const enum XpsBoolean {
true = '1',
false = '0',
}
24 changes: 24 additions & 0 deletions src/features/headless-checkout/headless-checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { UserBalance } from '../../core/user-balance.interface';
import { getUserBalanceHandler } from './post-messages-handlers/get-user-balance.handler';
import { HeadlessCheckoutSpy } from '../../core/headless-checkout-spy/headless-checkout-spy';
import { getRegularMethodsHandler } from './post-messages-handlers/get-regular-methods.handler';
import { FormConfiguration } from '../../core/form/form-configuration.interface';
import { initFormHandler } from './post-messages-handlers/init-form.handler';
import { Form } from '../../core/form/form.interface';

@singleton()
export class HeadlessCheckout {
Expand Down Expand Up @@ -46,6 +49,27 @@ export class HeadlessCheckout {
},
};

public form = {
/**
* Initialize payment form
* @param configuration Form configuration
* @returns {Form} form details
*/
init: async (configuration: FormConfiguration): Promise<Form> => {
const msg: Message = {
name: EventName.initForm,
data: {
configuration,
},
};

return this.postMessagesClient.send<Form>(
msg,
initFormHandler
) as Promise<Form>;
},
};

private isWebView?: boolean;
private coreIframe!: HTMLIFrameElement;
private errorsSubscription?: () => void;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Form } from '../../../core/form/form.interface';
import { isInitFormEventMessage } from '../../../core/guards/init-form-event-message.guard';
import { Message } from '../../../core/message.interface';
import { Handler } from '../../../core/post-messages-client/handler.type';

export const initFormHandler: Handler<Form> = (
message: Message
): { isHandled: boolean; value?: Form } | null => {
if (isInitFormEventMessage(message)) {
return {
isHandled: true,
value: message.data,
};
}
return null;
};