Skip to content

Commit

Permalink
Merge pull request #22 from xsolla/PAYMENTS-15270_form-activate-getst…
Browse files Browse the repository at this point in the history
…atus

feat(PAYMENTS-15270): form - activate & getStatus
  • Loading branch information
Grendaizo90 authored Aug 16, 2023
2 parents 2bdbc45 + edf95d7 commit a3db57b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/core/status/form-status.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum FormStatus {
undefined = 'undefined',
pending = 'pending',
active = 'active',
}
21 changes: 21 additions & 0 deletions src/features/headless-checkout/headless-checkout.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Message } from '../../core/message.interface';
import { Handler } from '../../core/post-messages-client/handler.type';
import { LocalizeService } from '../../core/i18n/localize.service';
import { getFinanceDetailsHandler } from './post-messages-handlers/get-finance-details.handler';
import { FormStatus } from '../../core/status/form-status.enum';

const mockMessage: Message = {
name: EventName.initPayment,
Expand Down Expand Up @@ -189,4 +190,24 @@ describe('HeadlessCheckout', () => {
await headlessCheckout.getUserBalance();
expect(spy).toHaveBeenCalled();
});

it('Should have correct form status', async () => {
expect(headlessCheckout.form.getStatus()).toEqual(FormStatus.undefined);

spyOn(postMessagesClient, 'send').and.callFake(async (msg, callback) => {
await Promise.resolve().then(() => callback(msg));
return Promise.resolve(undefined);
});

const formInitPromise = headlessCheckout.form.init({
paymentMethodId: 1380,
returnUrl: '',
});

expect(headlessCheckout.form.getStatus()).toEqual(FormStatus.pending);

await formInitPromise.then();

expect(headlessCheckout.form.getStatus()).toEqual(FormStatus.active);
});
});
19 changes: 19 additions & 0 deletions src/features/headless-checkout/headless-checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { getPaymentStatusHandler } from './post-messages-handlers/get-payment-st
import { headlessCheckoutAppUrl } from './environment';
import { FinanceDetails } from '../../core/finance-details/finance-details.interface';
import { getFinanceDetailsHandler } from './post-messages-handlers/get-finance-details.handler';
import { FormStatus } from '../../core/status/form-status.enum';

@singleton()
export class HeadlessCheckout {
Expand Down Expand Up @@ -64,6 +65,8 @@ export class HeadlessCheckout {
* @returns {Form} form details
*/
init: async (configuration: FormConfiguration): Promise<Form> => {
this.formStatus = FormStatus.pending;

const msg: Message = {
name: EventName.initForm,
data: {
Expand All @@ -77,6 +80,7 @@ export class HeadlessCheckout {
this.formSpy.formFields = (args as { fields: Field[] }).fields;
}
this.formSpy.formWasInit = true;
this.formStatus = FormStatus.active;
})
) as Promise<Form>;
},
Expand All @@ -92,8 +96,23 @@ export class HeadlessCheckout {
}
);
},

getStatus: (): FormStatus => {
if (this.formSpy.formWasInit) {
return FormStatus.active;
}

return this.formStatus === FormStatus.pending
? FormStatus.pending
: FormStatus.undefined;
},

activate: (): void => {
this.formStatus = FormStatus.active;
},
};

private formStatus: FormStatus = FormStatus.undefined;
private isWebView?: boolean;
private isSandbox?: boolean;
private coreIframe!: HTMLIFrameElement;
Expand Down

0 comments on commit a3db57b

Please sign in to comment.