Skip to content

Commit

Permalink
Merge pull request #1 from ClearTax/add-callback-handling
Browse files Browse the repository at this point in the history
Add callback handling
  • Loading branch information
Ekta34 authored Oct 2, 2024
2 parents 23b5ad9 + 7a7ec52 commit 0ea5ba4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
20 changes: 16 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const EnvironmentArr = ['development', 'sandbox', 'production'];

const domainOriginMapping = {
development: 'myinvoice-dev.my.cleartax.com',
sandbox: 'myinvoice-sandbox.my.cleartax.com',
production: 'myinvoice.my.cleartax.com',
development: 'https://myinvoice-dev.my.cleartax.com',
sandbox: 'https://myinvoice-sandbox.my.cleartax.com',
production: 'https://myinvoice.my.cleartax.com',
};

function renderClearCustomerPortal({
Expand All @@ -14,6 +14,7 @@ function renderClearCustomerPortal({
title,
environment = 'sandbox',
style,
callbackFunc,
}) {
if (!token) {
throw new Error('token is required parameter.');
Expand All @@ -28,7 +29,7 @@ function renderClearCustomerPortal({

const domainOrigin = domainOriginMapping[environment];

iframe.src = `https://${domainOrigin}/?token=${token}&iframe=true${tinQuery}`; // fixed URL
iframe.src = `${domainOrigin}/?token=${token}&iframe=true${tinQuery}`; // fixed URL

// Apply user-defined properties
if (width) iframe.width = width;
Expand All @@ -41,6 +42,17 @@ function renderClearCustomerPortal({
});
}

iframe.addEventListener('load', function () {
setTimeout(() => {
if (callbackFunc) {
iframe.contentWindow.postMessage(
{ type: 'callback', iframeCallback: callbackFunc.toString() },
domainOrigin
);
}
}, 100);
});

// Return the iframe element
return iframe;
}
Expand Down
5 changes: 5 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// types/index.d.ts
export type EnvironmentType = 'development' | 'sandbox' | 'production';

export interface CallbackData {
statusCode: number;
}

export interface ICreateIframeOptions {
token: string;
width?: string;
Expand All @@ -9,6 +13,7 @@ export interface ICreateIframeOptions {
environment?: EnvironmentType;
title?: string;
style?: { [key: string]: string }; // Optional inline style object
callbackFunc: (data: CallbackData) => void;
}

export default function createIframe(
Expand Down

0 comments on commit 0ea5ba4

Please sign in to comment.