@easypayment/medusa-payment-paypal 1.3.5 → 1.3.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.medusa/server/src/admin/index.js +255 -215
- package/.medusa/server/src/admin/index.mjs +255 -215
- package/CHANGELOG.md +16 -0
- package/package.json +1 -1
- package/src/admin/routes/settings/paypal/connection/page.tsx +148 -58
|
@@ -24,13 +24,33 @@ export const config = defineRouteConfig({
|
|
|
24
24
|
const PARTNER_JS_URL =
|
|
25
25
|
"https://www.paypal.com/webapps/merchantboarding/js/lib/lightbox/partner.js"
|
|
26
26
|
|
|
27
|
+
const PARTNER_SCRIPT_ID = "paypal-partner-js"
|
|
28
|
+
|
|
29
|
+
// Script tags partner.js adds while it bootstraps: its ramp config, then the
|
|
30
|
+
// mini-browser implementation (legacy signup.js, or the zoid build).
|
|
31
|
+
const PARTNER_HELPER_SCRIPT_IDS = [
|
|
32
|
+
"rampConfig-js",
|
|
33
|
+
"signup-js",
|
|
34
|
+
"zoidMiniBrowser-js",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
const MINI_BROWSER_POLL_MS = 50
|
|
38
|
+
const MINI_BROWSER_POLL_ATTEMPTS = 200 // ~10s
|
|
39
|
+
|
|
40
|
+
type MiniBrowserApi = {
|
|
41
|
+
init?: () => void
|
|
42
|
+
closeFlow?: () => void
|
|
43
|
+
startFlow?: unknown
|
|
44
|
+
component?: unknown
|
|
45
|
+
}
|
|
46
|
+
|
|
27
47
|
declare global {
|
|
28
48
|
interface Window {
|
|
29
49
|
PAYPAL?: {
|
|
30
50
|
apps?: {
|
|
31
51
|
Signup?: {
|
|
32
|
-
miniBrowser?:
|
|
33
|
-
MiniBrowser?:
|
|
52
|
+
miniBrowser?: MiniBrowserApi
|
|
53
|
+
MiniBrowser?: MiniBrowserApi
|
|
34
54
|
}
|
|
35
55
|
}
|
|
36
56
|
}
|
|
@@ -38,6 +58,72 @@ declare global {
|
|
|
38
58
|
}
|
|
39
59
|
}
|
|
40
60
|
|
|
61
|
+
function getMiniBrowser(): MiniBrowserApi | undefined {
|
|
62
|
+
const signup = window.PAYPAL?.apps?.Signup
|
|
63
|
+
return signup?.MiniBrowser || signup?.miniBrowser
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* partner.js itself only installs a placeholder `MiniBrowser` whose `init` does
|
|
68
|
+
* nothing but re-arm a timer. The implementation that binds the connect button
|
|
69
|
+
* and opens the popup arrives later, from a script partner.js loads in turn.
|
|
70
|
+
* `PAYPAL.apps.Signup` existing is therefore NOT enough to call the page ready.
|
|
71
|
+
*/
|
|
72
|
+
function isMiniBrowserReady(): boolean {
|
|
73
|
+
const mb = getMiniBrowser()
|
|
74
|
+
return typeof mb?.startFlow === "function" || !!mb?.component
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Shared across mounts of this page within one admin session, so a quick
|
|
78
|
+
// leave-and-return reuses the load in flight instead of starting a second one.
|
|
79
|
+
let miniBrowserLoad: Promise<boolean> | null = null
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Load PayPal's mini-browser, resolving to whether it is usable.
|
|
83
|
+
*
|
|
84
|
+
* partner.js must run at most once per working mini-browser: running it again
|
|
85
|
+
* resets `PAYPAL.apps.Signup` to the placeholder, and because its helper script
|
|
86
|
+
* tags are still in the document it never reloads the implementation. That left
|
|
87
|
+
* the connect button a plain link, so a click navigated the whole admin to
|
|
88
|
+
* PayPal instead of opening the popup. So: reuse a ready mini-browser, and
|
|
89
|
+
* otherwise start from a clean slate by dropping every stale tag first.
|
|
90
|
+
*/
|
|
91
|
+
function loadMiniBrowser(): Promise<boolean> {
|
|
92
|
+
if (isMiniBrowserReady()) return Promise.resolve(true)
|
|
93
|
+
if (miniBrowserLoad) return miniBrowserLoad
|
|
94
|
+
|
|
95
|
+
miniBrowserLoad = new Promise<boolean>((resolve) => {
|
|
96
|
+
for (const id of [PARTNER_SCRIPT_ID, ...PARTNER_HELPER_SCRIPT_IDS]) {
|
|
97
|
+
document.getElementById(id)?.remove()
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const script = document.createElement("script")
|
|
101
|
+
script.id = PARTNER_SCRIPT_ID
|
|
102
|
+
script.src = PARTNER_JS_URL
|
|
103
|
+
script.async = true
|
|
104
|
+
document.head.appendChild(script)
|
|
105
|
+
|
|
106
|
+
let attempts = 0
|
|
107
|
+
const poll = () => {
|
|
108
|
+
if (isMiniBrowserReady()) {
|
|
109
|
+
miniBrowserLoad = null
|
|
110
|
+
resolve(true)
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
attempts++
|
|
114
|
+
if (attempts >= MINI_BROWSER_POLL_ATTEMPTS) {
|
|
115
|
+
miniBrowserLoad = null
|
|
116
|
+
resolve(false)
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
setTimeout(poll, MINI_BROWSER_POLL_MS)
|
|
120
|
+
}
|
|
121
|
+
poll()
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
return miniBrowserLoad
|
|
125
|
+
}
|
|
126
|
+
|
|
41
127
|
const SERVICE_URL = "/admin/paypal/onboarding-link"
|
|
42
128
|
|
|
43
129
|
// Generated onboarding URLs are cached in localStorage per environment so a
|
|
@@ -132,6 +218,10 @@ export default function PayPalConnectionPage() {
|
|
|
132
218
|
const runIdRef = useRef(0)
|
|
133
219
|
const currentRunId = useRef(0)
|
|
134
220
|
const completedRef = useRef(false)
|
|
221
|
+
// Which mini-browser instance each connect button element was bound with.
|
|
222
|
+
const boundButtonsRef = useRef(
|
|
223
|
+
new WeakMap<HTMLAnchorElement, MiniBrowserApi>()
|
|
224
|
+
)
|
|
135
225
|
|
|
136
226
|
// Long-lived listeners/timers read the current env through a ref so they never
|
|
137
227
|
// capture a stale value from the render that registered them.
|
|
@@ -288,55 +378,52 @@ export default function PayPalConnectionPage() {
|
|
|
288
378
|
[closeMiniBrowser, stopStatusPolling]
|
|
289
379
|
)
|
|
290
380
|
|
|
291
|
-
//
|
|
381
|
+
// True when PayPal's click handler is on the connect button as it exists now.
|
|
382
|
+
// The button is a new element after a disconnect or a return to this page, and
|
|
383
|
+
// a plain link without that handler navigates the whole admin to PayPal.
|
|
384
|
+
const isConnectButtonBound = useCallback(() => {
|
|
385
|
+
const anchor = paypalButtonRef.current
|
|
386
|
+
const mb = getMiniBrowser()
|
|
387
|
+
return (
|
|
388
|
+
!!anchor &&
|
|
389
|
+
!!mb &&
|
|
390
|
+
isMiniBrowserReady() &&
|
|
391
|
+
boundButtonsRef.current.get(anchor) === mb
|
|
392
|
+
)
|
|
393
|
+
}, [])
|
|
394
|
+
|
|
395
|
+
// Bind PayPal's mini-browser to the connect button once it has a real
|
|
396
|
+
// onboarding URL. Returns whether the button is bound afterwards.
|
|
292
397
|
const initPartner = useCallback(() => {
|
|
293
|
-
const
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
398
|
+
const anchor = paypalButtonRef.current
|
|
399
|
+
const mb = getMiniBrowser()
|
|
400
|
+
if (!anchor || !mb || !isMiniBrowserReady()) return false
|
|
401
|
+
// init() adds another click handler to every [data-paypal-button] each time
|
|
402
|
+
// it runs, so call it once per button element (and per mini-browser).
|
|
403
|
+
if (boundButtonsRef.current.get(anchor) === mb) return true
|
|
404
|
+
if (typeof mb.init !== "function") return false
|
|
405
|
+
try {
|
|
406
|
+
mb.init()
|
|
407
|
+
boundButtonsRef.current.set(anchor, mb)
|
|
408
|
+
return true
|
|
409
|
+
} catch (e) {
|
|
410
|
+
console.error("[paypal] partner.js init failed:", e)
|
|
411
|
+
return false
|
|
303
412
|
}
|
|
304
413
|
}, [])
|
|
305
414
|
|
|
306
|
-
//
|
|
415
|
+
// Start loading PayPal's mini-browser as soon as the page mounts. partner.js
|
|
416
|
+
// needs a [data-paypal-button] in the document to finish bootstrapping; both
|
|
417
|
+
// the connected and the not-connected view render one. Nothing is removed on
|
|
418
|
+
// unmount: see loadMiniBrowser for why partner.js must not run twice.
|
|
307
419
|
useEffect(() => {
|
|
308
|
-
|
|
309
|
-
if (existingScript) return
|
|
310
|
-
|
|
311
|
-
const preloadHref = PARTNER_JS_URL
|
|
312
|
-
let preloadLink: HTMLLinkElement | null = null
|
|
313
|
-
if (
|
|
314
|
-
!document.head.querySelector(`link[rel="preload"][href="${preloadHref}"]`)
|
|
315
|
-
) {
|
|
316
|
-
preloadLink = document.createElement("link")
|
|
317
|
-
preloadLink.rel = "preload"
|
|
318
|
-
preloadLink.href = preloadHref
|
|
319
|
-
preloadLink.as = "script"
|
|
320
|
-
document.head.appendChild(preloadLink)
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
const ppScript = document.createElement("script")
|
|
324
|
-
ppScript.id = "paypal-partner-js"
|
|
325
|
-
ppScript.src = preloadHref
|
|
326
|
-
ppScript.async = true
|
|
327
|
-
document.head.appendChild(ppScript)
|
|
328
|
-
|
|
329
|
-
return () => {
|
|
330
|
-
if (preloadLink?.parentNode)
|
|
331
|
-
preloadLink.parentNode.removeChild(preloadLink)
|
|
332
|
-
if (ppScript.parentNode) ppScript.parentNode.removeChild(ppScript)
|
|
333
|
-
}
|
|
420
|
+
void loadMiniBrowser()
|
|
334
421
|
}, [])
|
|
335
422
|
|
|
336
423
|
// Put the (cached or freshly generated) onboarding URL on the button, then wait
|
|
337
|
-
// for
|
|
338
|
-
//
|
|
339
|
-
//
|
|
424
|
+
// for PayPal's mini-browser to be loaded and bound before showing the button.
|
|
425
|
+
// Revealing it only once bound is what guarantees a click opens the popup
|
|
426
|
+
// (never a new tab, never a full-page redirect).
|
|
340
427
|
const activatePayPal = useCallback(
|
|
341
428
|
(url: string, runId: number) => {
|
|
342
429
|
if (paypalButtonRef.current) {
|
|
@@ -344,27 +431,20 @@ export default function PayPalConnectionPage() {
|
|
|
344
431
|
}
|
|
345
432
|
setFinalUrl(url)
|
|
346
433
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
434
|
+
void (async () => {
|
|
435
|
+
// One retry: a load that was in flight while this page was not mounted
|
|
436
|
+
// finds no connect button, never finishes, and has to be restarted.
|
|
437
|
+
const loaded = (await loadMiniBrowser()) || (await loadMiniBrowser())
|
|
351
438
|
if (runId !== currentRunId.current) return
|
|
352
|
-
if (
|
|
353
|
-
|
|
354
|
-
setConnState("ready")
|
|
355
|
-
return
|
|
356
|
-
}
|
|
357
|
-
attempts++
|
|
358
|
-
if (attempts >= MAX_ATTEMPTS) {
|
|
439
|
+
if (!paypalButtonRef.current) return
|
|
440
|
+
if (!loaded || !initPartner()) {
|
|
359
441
|
showError(
|
|
360
442
|
"PayPal partner script failed to load. Please refresh and try again."
|
|
361
443
|
)
|
|
362
444
|
return
|
|
363
445
|
}
|
|
364
|
-
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
tryInit()
|
|
446
|
+
setConnState("ready")
|
|
447
|
+
})()
|
|
368
448
|
},
|
|
369
449
|
[initPartner, showError]
|
|
370
450
|
)
|
|
@@ -563,6 +643,16 @@ export default function PayPalConnectionPage() {
|
|
|
563
643
|
e.preventDefault()
|
|
564
644
|
return
|
|
565
645
|
}
|
|
646
|
+
// Safety net: without PayPal's handler this click would follow the link and
|
|
647
|
+
// take the whole admin to PayPal. Stop it, bind again, and let the next
|
|
648
|
+
// click open the popup.
|
|
649
|
+
if (!isConnectButtonBound()) {
|
|
650
|
+
e.preventDefault()
|
|
651
|
+
currentRunId.current = ++runIdRef.current
|
|
652
|
+
setConnState("loading")
|
|
653
|
+
activatePayPal(finalUrl, currentRunId.current)
|
|
654
|
+
return
|
|
655
|
+
}
|
|
566
656
|
completedRef.current = false
|
|
567
657
|
startStatusPolling()
|
|
568
658
|
}
|