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

Add 'redirectFromTop' config prop to allow top level redirect when Checkout loaded in an iframe #2325

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/forty-badgers-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@adyen/adyen-web': minor
---

Add 'redirectFromTop' config prop to allow top level redirect when Checkout loaded in an iframe
17 changes: 17 additions & 0 deletions packages/lib/src/components/Redirect/Redirect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { h } from 'preact';
import Redirect from './Redirect';
import RedirectShopper from './components/RedirectShopper';

jest.mock('../../utils/detectInIframe', () => {
return jest.fn().mockImplementation(() => {
return true;
});
});

describe('Redirect', () => {
describe('isValid', () => {
test('Is always valid', () => {
Expand All @@ -19,6 +25,17 @@ describe('Redirect', () => {

expect(wrapper.find('form')).toHaveLength(1);
expect(wrapper.find('form').prop('action')).toBe('http://www.adyen.com');
expect(wrapper.find('form').prop('target')).toBe(undefined);
setTimeout(() => expect(window.HTMLFormElement.prototype.submit).toHaveBeenCalled(), 0);
});

test('Accepts a POST redirect status, setting target to _top, when the config prop tells it to', () => {
window.HTMLFormElement.prototype.submit = jest.fn();

const wrapper = mount(<RedirectShopper url="http://www.adyen.com" method="POST" data={{}} redirectFromTop={true} />);

expect(wrapper.find('form')).toHaveLength(1);
expect(wrapper.find('form').prop('target')).toBe('_top');
setTimeout(() => expect(window.HTMLFormElement.prototype.submit).toHaveBeenCalled(), 0);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Component, h } from 'preact';
import detectInIframe from '../../../../utils/detectInIframe';

interface RedirectShopperProps {
beforeRedirect: (resolve, reject, url) => Promise<void>;
url: string;
method: 'GET' | 'POST';
data?: any;
redirectFromTop?: boolean;
sponglord marked this conversation as resolved.
Show resolved Hide resolved
}

class RedirectShopper extends Component<RedirectShopperProps> {
Expand All @@ -19,7 +21,12 @@ class RedirectShopper extends Component<RedirectShopperProps> {
if (this.postForm) {
this.postForm.submit();
} else {
window.location.assign(this.props.url);
if (this.props.redirectFromTop && detectInIframe()) {
// if in an iframe and the config prop allows it - try to redirect from the top level window
window.top.location.assign?.(this.props.url);
} else {
window.location.assign(this.props.url);
}
}
};

Expand All @@ -44,6 +51,7 @@ class RedirectShopper extends Component<RedirectShopperProps> {
ref={ref => {
this.postForm = ref;
}}
{...(this.props.redirectFromTop && detectInIframe() && { target: '_top' })}
>
{Object.keys(data).map(key => (
<input type="hidden" name={key} key={key} value={data[key]} />
Expand Down
1 change: 1 addition & 0 deletions packages/lib/src/core/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const GENERIC_OPTIONS = [
'session',
'clientKey',
'showPayButton',
'redirectFromTop',
'installmentOptions',

// Events
Expand Down
2 changes: 2 additions & 0 deletions packages/lib/src/utils/detectInIframe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Returns true if the page is being run in an iframe
export default () => window.location !== window.parent.location;
Loading