-
Notifications
You must be signed in to change notification settings - Fork 60
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: sends modal event data to modal wrapper for use in hooks #1170
Open
danzhaaspaypal
wants to merge
9
commits into
feature/DTCRCMERC-3611-modal-lander-v6
Choose a base branch
from
feature/modal-wrapper-events
base: feature/DTCRCMERC-3611-modal-lander-v6
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f40050f
sends modal events with data to modal wrapper for use in hooks
danzhaaspaypal abfb667
adds required props to hook event
danzhaaspaypal 58abf9c
refactors onApply for handling in wrapper
danzhaaspaypal 3283443
renames postmessenger event names and refactors
danzhaaspaypal e91a56d
fixes props validation
danzhaaspaypal 71b7c29
corrects offerTypes to offerType
danzhaaspaypal 4551ab3
Merge branch 'feature/DTCRCMERC-3611-modal-lander-v6' of https://gith…
danzhaaspaypal 5593c7b
addresses comments
danzhaaspaypal f0a7ea8
Merge branch 'feature/DTCRCMERC-3611-modal-lander-v6' of https://gith…
danzhaaspaypal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { uniqueID } from '@krakenjs/belter/src'; | ||
|
||
// these constants are defined in PostMessenger | ||
const POSTMESSENGER_EVENT_TYPES = { | ||
ACK: 'ack', | ||
MESSAGE: 'message' | ||
}; | ||
const POSTMESSENGER_ACK_PAYLOAD = { | ||
ok: 'true' | ||
}; | ||
|
||
export const POSTMESSENGER_EVENT_NAMES = { | ||
CALCULATE: 'paypal-messages-modal-calculate', | ||
CLOSE: 'paypal-messages-modal-close', | ||
SHOW: 'paypal-messages-modal-show' | ||
}; | ||
|
||
export function sendEvent(payload, trustedOrigin) { | ||
if (!trustedOrigin && !document.referrer) { | ||
return; | ||
} | ||
|
||
const isTest = process.env.NODE_ENV === 'test'; | ||
const targetWindow = !isTest && window.parent === window ? window.opener : window.parent; | ||
|
||
// referrer origin is used by integrations not passing in props.origin manually | ||
// eslint-disable-next-line compat/compat | ||
const referrerOrigin = !isTest ? new window.URL(document.referrer)?.origin : undefined; | ||
|
||
targetWindow.postMessage(payload, trustedOrigin || referrerOrigin); | ||
} | ||
|
||
// This function provides data security by preventing accidentally exposing sensitive data; we are adding | ||
// an extra layer of validation here by only allowing explicitly approved fields to be included | ||
function createSafePayload(unscreenedPayload) { | ||
const allowedFields = [ | ||
'linkName' // close event | ||
]; | ||
|
||
const safePayload = {}; | ||
const entries = Object.entries(unscreenedPayload); | ||
entries.forEach(entry => { | ||
const [key, value] = entry; | ||
if (allowedFields.includes(key)) { | ||
safePayload[key] = value; | ||
} | ||
}); | ||
|
||
return safePayload; | ||
} | ||
|
||
export function createPostMessengerEvent(typeArg, eventName, eventPayloadArg) { | ||
let type; | ||
let eventPayload; | ||
|
||
if (typeArg === 'ack') { | ||
type = POSTMESSENGER_EVENT_TYPES.ACK; | ||
eventPayload = POSTMESSENGER_ACK_PAYLOAD; | ||
} else if (typeArg === 'message') { | ||
type = POSTMESSENGER_EVENT_TYPES.MESSAGE; | ||
// createSafePayload | ||
eventPayload = createSafePayload(eventPayloadArg); | ||
} | ||
|
||
return { | ||
eventName, | ||
id: uniqueID(), | ||
type, | ||
eventPayload | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing this will break some existing integrations as this value is used by integrations not passing in the
props.origin
manually today. Does this by chance work in place of the manualorigin
query param we introduced for our needs to simplify the new integration?`There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh good catch! I can make the document.referrer's origin a fallback in the case the origin isn't passed manually so both cases are covered