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

feat: add compatibility with window alias strategy #202

Merged
merged 1 commit into from
May 21, 2024
Merged
Changes from all commits
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
13 changes: 12 additions & 1 deletion src/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { destroyElement } from "@krakenjs/belter/src";

import { getVersion } from "./global";
import { getSDKScript, getNamespace } from "./script";
import { getLogger } from "./logger";

export type SetupComponent<T> = {|
name: string,
Expand All @@ -21,7 +22,13 @@ export function setupSDK(components: $ReadOnlyArray<SetupComponent<mixed>>) {
const existingVersion = existingNamespace && existingNamespace.version;

if (existingNamespace) {
if (existingNamespace[INTERNAL_DESTROY_KEY]) {
if (existingVersion.startsWith("6.")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a scenario where a merchant's code could be using v6 on window.paypal, but then this changes it to window.paypal.v6 and breaks their code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, I think the tradeoff for v6 defaulting to window.paypal is that we have to teach merchants using both SDKs to access v6 through window.paypal.v6.

getLogger().info("setup_sdk_v6_integration_found", {
v5Version: version,
v6Version: existingVersion,
});
delete window[namespace];
} else if (existingNamespace[INTERNAL_DESTROY_KEY]) {
existingNamespace[INTERNAL_DESTROY_KEY](
new Error(
`New SDK instance loaded, existing instance destroyed (${namespace} / ${version})`
Expand All @@ -42,6 +49,10 @@ export function setupSDK(components: $ReadOnlyArray<SetupComponent<mixed>>) {
window[namespace] = window[namespace] || {};
window[namespace].version = version;

if (namespace === "paypal" && window.__paypal_sdk__?.v6) {
window.paypal.v6 = window.__paypal_sdk__.v6;
}

const destroyers = [];

for (const { name, requirer, setupHandler } of components) {
Expand Down
Loading