Skip to content

Commit

Permalink
feat: added getStatus form method
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Poluhin committed Aug 7, 2023
1 parent 42692f6 commit e2da1ac
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 19 deletions.
11 changes: 1 addition & 10 deletions src/core/actions/status-updated.action.type.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import { Action } from './action.interface';
import { StatusEnum } from '../status/status.enum';

export type StatusUpdatedActionType = 'status_updated';

export enum StatusEnum {
created = 'created',
awaiting = 'awaiting',
processing = 'processing',
authorized = 'authorized',
held = 'held',
done = 'done',
error = 'error',
canceled = 'canceled',
}
export interface StatusUpdatedActionData {
statusState: StatusEnum;
group: string;
Expand Down
1 change: 1 addition & 0 deletions src/core/event-name.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const enum EventName {
getSavedMethods = 'getSavedMethods',
getUserBalance = 'getUserBalance',
getLegalComponentConfig = 'getLegalComponentConfig',
getPaymentStatus = 'getPaymentStatus',
legalComponentPing = 'legalComponentPing',
legalComponentPong = 'legalComponentPong',
nextAction = 'nextAction',
Expand Down
19 changes: 19 additions & 0 deletions src/core/guards/get-payment-status-message.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { EventName } from '../event-name.enum';
import { isGetPaymentStatusMessage } from './get-payment-status-message.guard';

describe('GetPaymentStatus event message guard', () => {
it('Should return true', () => {
const isCorrectEvent = isGetPaymentStatusMessage({
name: EventName.getPaymentStatus,
});

expect(isCorrectEvent).toBeTruthy();
});
it('Should return false', () => {
const isWrongEvent = isGetPaymentStatusMessage({
name: EventName.initPayment,
});

expect(isWrongEvent).toBeFalsy();
});
});
14 changes: 14 additions & 0 deletions src/core/guards/get-payment-status-message.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Message } from '../message.interface';
import { Status } from '../status/status.interface';
import { isEventMessage } from './event-message.guard';
import { EventName } from '../event-name.enum';

export const isGetPaymentStatusMessage = (
messageData: unknown
): messageData is Message<Status> => {
if (isEventMessage(messageData)) {
return messageData.name === EventName.getPaymentStatus;
}

return false;
};
11 changes: 11 additions & 0 deletions src/core/status/status.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export enum StatusEnum {
created = 'created',
awaiting = 'awaiting',
processing = 'processing',
authorized = 'authorized',
held = 'held',
done = 'done',
error = 'error',
canceled = 'canceled',
unknown = 'unknown',
}
66 changes: 66 additions & 0 deletions src/core/status/status.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { StatusEnum } from './status.enum';
import { TitleClass } from './title-class.enum';

export interface Status {
statusState: StatusEnum;
statusMessage: string;
group: string;
postMessageStatus?: string;
virtualCurrencyAmount?: number;
email?: string;
financeInfo?: {
[key: string]: {
key?: string;
pref?: string;
value?: string;
};
};
isCancelUser?: boolean;
backUrlSettings?: {
backUrl: string;
backUrlAction?: string;
backUrlCaption: string;
returnRegion: string;
showBackButton?: boolean;
};
additionalBackButton?: {
link: string;
action: string;
label: string;
region: string;
isEnabled: boolean;
};
needToCheck?: boolean;
autoRedirect?: { time: number };
userReturnStatus?: string;
discount?: number | null;
userId?: string;
invoice?: number;
pid?: number;
projectAmount?: {
amount: string;
currency: string;
};
titleClass?: TitleClass;
isSuccess?: boolean;
isCanceled?: boolean;
paymentCountryIso?: string;
order?: {
lineitems?: Array<{
name: string;
quantity: number;
currency: string;
amount: number;
amount_without_discount: number;
indirect_tax_rate: number;
}>;
shipping?: {
currency: string;
amount: number;
};
contained_taxes?: {
sales_tax?: boolean;
user_vat?: boolean;
};
};
}
5 changes: 5 additions & 0 deletions src/core/status/title-class.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum TitleClass {
done = 'done',
cancel = 'cancel',
cancelUser = 'canceluser',
}
42 changes: 33 additions & 9 deletions src/features/headless-checkout/headless-checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@ import { Message } from '../../core/message.interface';
import { PaymentMethod } from '../../core/payment-method.interface';
import { Handler } from '../../core/post-messages-client/handler.type';
import { PostMessagesClient } from '../../core/post-messages-client/post-messages-client';
import { getErrorHandler } from './post-messages-handlers/error.handler';
import { getQuickMethodsHandler } from './post-messages-handlers/get-quick-methods.handler';
import { setTokenHandler } from './post-messages-handlers/set-token.handler';
import { headlessCheckoutAppUrl } from './environment';
import { webComponents } from '../../core/web-components/web-components.map';
import { SavedMethod } from '../../core/saved-method.interface';
import { getSavedMethodsHandler } from './post-messages-handlers/get-saved-methods.handler';
import { UserBalance } from '../../core/user-balance.interface';
import { getUserBalanceHandler } from './post-messages-handlers/get-user-balance.handler';
import { HeadlessCheckoutSpy } from '../../core/spy/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';
import { NextAction } from '../../core/actions/next-action.interface';
import { nextActionHandler } from './post-messages-handlers/next-action.handler';
import { FormSpy } from '../../core/spy/form-spy/form-spy';
import { Status } from '../../core/status/status.interface';
import { StatusEnum } from '../../core/status/status.enum';
import { getErrorHandler } from './post-messages-handlers/error.handler';
import { initFormHandler } from './post-messages-handlers/init-form.handler';
import { getQuickMethodsHandler } from './post-messages-handlers/get-quick-methods.handler';
import { setTokenHandler } from './post-messages-handlers/set-token.handler';
import { getRegularMethodsHandler } from './post-messages-handlers/get-regular-methods.handler';
import { getSavedMethodsHandler } from './post-messages-handlers/get-saved-methods.handler';
import { getUserBalanceHandler } from './post-messages-handlers/get-user-balance.handler';
import { nextActionHandler } from './post-messages-handlers/next-action.handler';
import { getPaymentStatusHandler } from './post-messages-handlers/get-payment-status/get-payment-status.handler';
import { headlessCheckoutAppUrl } from './environment';

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

getStatus: async (): Promise<Status> => {
const msg: Message = {
name: EventName.getPaymentStatus,
};

const status = await this.postMessagesClient.send<Status>(
msg,
(message) => getPaymentStatusHandler(message)
);

if (!status) {
return {
statusState: StatusEnum.unknown,
statusMessage: 'Unknown status',
group: 'unknown',
};
}

return status;
},
};

private isWebView?: boolean;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { EventName } from '../../../../core/event-name.enum';
import { Message } from '../../../../core/message.interface';
import { getPaymentStatusHandler } from './get-payment-status.handler';

const mockMessage: Message = {
name: EventName.getPaymentStatus,
};
describe('getPaymentStatusHandler', () => {
it('should handle status', () => {
expect(getPaymentStatusHandler(mockMessage)).toEqual({
isHandled: true,
value: undefined,
});
});

it('should return null', () => {
expect(
getPaymentStatusHandler({ name: EventName.getPaymentMethodsList })
).toBeNull();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Handler } from '../../../../core/post-messages-client/handler.type';
import { Message } from '../../../../core/message.interface';
import { Status } from '../../../../core/status/status.interface';
import { isGetPaymentStatusMessage } from '../../../../core/guards/get-payment-status-message.guard';

export const getPaymentStatusHandler: Handler<Status> = (
message: Message
): { isHandled: boolean; value?: Status } | null => {
if (!isGetPaymentStatusMessage(message)) {
return null;
}

return {
isHandled: true,
value: message.data,
};
};

0 comments on commit e2da1ac

Please sign in to comment.