@easypayment/medusa-payment-paypal 1.3.5 → 1.3.7
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/.medusa/server/src/modules/paypal/utils/paypal-fetch.d.ts +16 -2
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.js +73 -2
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/request-logger.d.ts +2 -0
- package/.medusa/server/src/modules/paypal/utils/request-logger.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/request-logger.js +9 -1
- package/.medusa/server/src/modules/paypal/utils/request-logger.js.map +1 -1
- package/CHANGELOG.md +39 -0
- package/package.json +1 -1
- package/src/admin/routes/settings/paypal/connection/page.tsx +148 -58
- package/src/modules/paypal/utils/paypal-fetch.ts +78 -2
- package/src/modules/paypal/utils/request-logger.ts +43 -43
|
@@ -5,9 +5,77 @@
|
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.CircuitOpenError = exports.PAYPAL_HTTP_TIMEOUT_MS = void 0;
|
|
8
|
+
exports.withPartnerAttribution = withPartnerAttribution;
|
|
8
9
|
exports.paypalFetch = paypalFetch;
|
|
9
10
|
exports.paypalFetchWithRetry = paypalFetchWithRetry;
|
|
11
|
+
const partner_1 = require("./partner");
|
|
10
12
|
const request_logger_1 = require("./request-logger");
|
|
13
|
+
// ---------------------------------------------------------------------------
|
|
14
|
+
// Partner attribution (BN code)
|
|
15
|
+
// ---------------------------------------------------------------------------
|
|
16
|
+
const ATTRIBUTION_HEADER = "PayPal-Partner-Attribution-Id";
|
|
17
|
+
/** True for PayPal's own API hosts (`api-m.paypal.com`, the sandbox, …). */
|
|
18
|
+
function isPayPalHost(input) {
|
|
19
|
+
try {
|
|
20
|
+
const hostname = new URL(String(input)).hostname.toLowerCase();
|
|
21
|
+
return hostname === "paypal.com" || hostname.endsWith(".paypal.com");
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Guarantee the BN code on every request to PayPal.
|
|
29
|
+
*
|
|
30
|
+
* Call sites used to add the header themselves, and several (order lookups,
|
|
31
|
+
* webhook verification, the invoice subscriber) did not, so those calls went
|
|
32
|
+
* out unattributed. Enforcing it here means a new call site cannot forget.
|
|
33
|
+
* A header the caller already set is kept, and non-PayPal hosts (the partner
|
|
34
|
+
* onboarding service, alert webhooks) are left alone. The original shape of
|
|
35
|
+
* `headers` is preserved so the request log shows them as the caller wrote
|
|
36
|
+
* them.
|
|
37
|
+
*/
|
|
38
|
+
function withPartnerAttribution(input, init = {}) {
|
|
39
|
+
if (!isPayPalHost(input))
|
|
40
|
+
return init;
|
|
41
|
+
const headers = init.headers;
|
|
42
|
+
const wanted = ATTRIBUTION_HEADER.toLowerCase();
|
|
43
|
+
if (!headers) {
|
|
44
|
+
return {
|
|
45
|
+
...init,
|
|
46
|
+
headers: { [ATTRIBUTION_HEADER]: partner_1.PAYPAL_PARTNER_ATTRIBUTION_ID },
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
if (typeof Headers !== "undefined" && headers instanceof Headers) {
|
|
50
|
+
if (headers.has(ATTRIBUTION_HEADER))
|
|
51
|
+
return init;
|
|
52
|
+
const next = new Headers(headers);
|
|
53
|
+
next.set(ATTRIBUTION_HEADER, partner_1.PAYPAL_PARTNER_ATTRIBUTION_ID);
|
|
54
|
+
return { ...init, headers: next };
|
|
55
|
+
}
|
|
56
|
+
if (Array.isArray(headers)) {
|
|
57
|
+
if (headers.some((pair) => String(pair?.[0]).toLowerCase() === wanted)) {
|
|
58
|
+
return init;
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
...init,
|
|
62
|
+
headers: [
|
|
63
|
+
...headers,
|
|
64
|
+
[ATTRIBUTION_HEADER, partner_1.PAYPAL_PARTNER_ATTRIBUTION_ID],
|
|
65
|
+
],
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
if (Object.keys(headers).some((name) => name.toLowerCase() === wanted)) {
|
|
69
|
+
return init;
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
...init,
|
|
73
|
+
headers: {
|
|
74
|
+
...headers,
|
|
75
|
+
[ATTRIBUTION_HEADER]: partner_1.PAYPAL_PARTNER_ATTRIBUTION_ID,
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
}
|
|
11
79
|
exports.PAYPAL_HTTP_TIMEOUT_MS = (() => {
|
|
12
80
|
const v = Number(process.env.PAYPAL_HTTP_TIMEOUT_MS);
|
|
13
81
|
return Number.isFinite(v) && v > 0 ? v : 30_000;
|
|
@@ -90,9 +158,12 @@ exports.CircuitOpenError = CircuitOpenError;
|
|
|
90
158
|
* `fetch` with a default timeout (`PAYPAL_HTTP_TIMEOUT_MS`) and a per-host
|
|
91
159
|
* circuit breaker. 5xx responses and network/timeout errors count as failures;
|
|
92
160
|
* once the breaker opens, calls to that host reject immediately with
|
|
93
|
-
* `CircuitOpenError` until the half-open probe succeeds.
|
|
161
|
+
* `CircuitOpenError` until the half-open probe succeeds. Requests to PayPal
|
|
162
|
+
* always carry the partner attribution (BN) header: see
|
|
163
|
+
* `withPartnerAttribution`.
|
|
94
164
|
*/
|
|
95
|
-
function paypalFetch(input,
|
|
165
|
+
function paypalFetch(input, requestInit = {}) {
|
|
166
|
+
const init = withPartnerAttribution(input, requestInit);
|
|
96
167
|
const host = cbHostKey(input);
|
|
97
168
|
if (!cbAllowRequest(host)) {
|
|
98
169
|
return Promise.reject(new CircuitOpenError());
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paypal-fetch.js","sourceRoot":"","sources":["../../../../../../src/modules/paypal/utils/paypal-fetch.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;
|
|
1
|
+
{"version":3,"file":"paypal-fetch.js","sourceRoot":"","sources":["../../../../../../src/modules/paypal/utils/paypal-fetch.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAgCH,wDA2CC;AAgHD,kCAuEC;AAgDD,oDA6BC;AA7UD,uCAAyD;AACzD,qDAAuE;AAEvE,8EAA8E;AAC9E,gCAAgC;AAChC,8EAA8E;AAE9E,MAAM,kBAAkB,GAAG,+BAA+B,CAAA;AAE1D,4EAA4E;AAC5E,SAAS,YAAY,CAAC,KAAmB;IACvC,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAA;QAC9D,OAAO,QAAQ,KAAK,YAAY,IAAI,QAAQ,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAA;IACtE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAA;IACd,CAAC;AACH,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,sBAAsB,CACpC,KAAmB,EACnB,OAAoB,EAAE;IAEtB,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAA;IAErC,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAA;IAC5B,MAAM,MAAM,GAAG,kBAAkB,CAAC,WAAW,EAAE,CAAA;IAE/C,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO;YACL,GAAG,IAAI;YACP,OAAO,EAAE,EAAE,CAAC,kBAAkB,CAAC,EAAE,uCAA6B,EAAE;SACjE,CAAA;IACH,CAAC;IACD,IAAI,OAAO,OAAO,KAAK,WAAW,IAAI,OAAO,YAAY,OAAO,EAAE,CAAC;QACjE,IAAI,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC;YAAE,OAAO,IAAI,CAAA;QAChD,MAAM,IA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B,CAAC;gBAAC,MAAM,CAAC;oBACP,2BAA2B;gBAC7B,CAAC;gBACD,MAAM,KAAK,GAAG,aAAa,CAAC,OAAO,CAAC,CAAA;gBACpC,MAAM,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAA;gBAC9C,SAAQ;YACV,CAAC;YACD,OAAO,GAAG,CAAA;QACZ,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,SAAS,GAAG,GAAG,CAAA;YACf,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,IAAI,OAAO,IAAI,WAAW;gBAAE,MAAM,GAAG,CAAA;YAC1D,MAAM,KAAK,GAAG,aAAa,CAAC,OAAO,CAAC,CAAA;YACpC,MAAM,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAA;QAChD,CAAC;IACH,CAAC;IACD,MAAM,SAAS,CAAA;AACjB,CAAC"}
|
|
@@ -19,6 +19,8 @@
|
|
|
19
19
|
export declare function setRequestLoggingEnabled(enabled: boolean): void;
|
|
20
20
|
/** Whether request/response logging is currently active in this process. */
|
|
21
21
|
export declare function isRequestLoggingEnabled(): boolean;
|
|
22
|
+
/** Deep-copy `value` with the values of sensitive keys replaced. */
|
|
23
|
+
export declare function redactBody(value: unknown, depth?: number): unknown;
|
|
22
24
|
/** Absolute path of the log file for the current day. */
|
|
23
25
|
export declare function getCurrentLogFilePath(): string;
|
|
24
26
|
export type OutboundLog = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-logger.d.ts","sourceRoot":"","sources":["../../../../../../src/modules/paypal/utils/request-logger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAoBH,mFAAmF;AACnF,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAE/D;AAED,4EAA4E;AAC5E,wBAAgB,uBAAuB,IAAI,OAAO,CAEjD;
|
|
1
|
+
{"version":3,"file":"request-logger.d.ts","sourceRoot":"","sources":["../../../../../../src/modules/paypal/utils/request-logger.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAoBH,mFAAmF;AACnF,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI,CAE/D;AAED,4EAA4E;AAC5E,wBAAgB,uBAAuB,IAAI,OAAO,CAEjD;AAyBD,oEAAoE;AACpE,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,SAAI,GAAG,OAAO,CAY7D;AA2FD,yDAAyD;AACzD,wBAAgB,qBAAqB,IAAI,MAAM,CAE9C;AA0CD,MAAM,MAAM,WAAW,GAAG;IACxB,MAAM,EAAE,MAAM,CAAA;IACd,GAAG,EAAE,MAAM,CAAA;IACX,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,eAAe,CAAC,EAAE,OAAO,CAAA;IACzB,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB,CAAA;AAED,uEAAuE;AACvE,wBAAgB,WAAW,CAAC,KAAK,EAAE,WAAW,GAAG,IAAI,CA+BpD;AAED,MAAM,MAAM,UAAU,GAAG;IACvB,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,MAAM,CAAA;IACZ,KAAK,CAAC,EAAE,OAAO,CAAA;IACf,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,WAAW,CAAC,EAAE,OAAO,CAAA;IACrB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB,CAAA;AAED,8DAA8D;AAC9D,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CA6BlD;AAMD,MAAM,MAAM,WAAW,GAAG;IACxB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,QAAQ,EAAE,MAAM,CAAA;CACjB,CAAA;AAED,wDAAwD;AACxD,wBAAgB,YAAY,IAAI,WAAW,EAAE,CAmB5C;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAa3D"}
|
|
@@ -22,6 +22,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
23
|
exports.setRequestLoggingEnabled = setRequestLoggingEnabled;
|
|
24
24
|
exports.isRequestLoggingEnabled = isRequestLoggingEnabled;
|
|
25
|
+
exports.redactBody = redactBody;
|
|
25
26
|
exports.getCurrentLogFilePath = getCurrentLogFilePath;
|
|
26
27
|
exports.logOutbound = logOutbound;
|
|
27
28
|
exports.logInbound = logInbound;
|
|
@@ -53,11 +54,18 @@ function isRequestLoggingEnabled() {
|
|
|
53
54
|
// Body keys whose VALUES must never hit the exported file. Card fields are
|
|
54
55
|
// included even though they normally never reach the backend (the JS SDK sends
|
|
55
56
|
// them straight to PayPal) — defence in depth for the advanced-card flow.
|
|
56
|
-
|
|
57
|
+
//
|
|
58
|
+
// `authorization(?!s$)`: redact `authorization`, `authorization_code`, … but
|
|
59
|
+
// not PayPal's `payments.authorizations` collection, which holds the
|
|
60
|
+
// authorization id, status, amount and expiry a reviewer needs to see and no
|
|
61
|
+
// credential. `client_token` is the JS SDK token, which embeds a live access
|
|
62
|
+
// token.
|
|
63
|
+
const SENSITIVE_BODY_KEY_RE = /secret|password|client_secret|client_token|access_token|refresh_token|authorization(?!s$)|auth_code|code_verifier|api[_-]?key|private[_-]?key|security_code|\bcvv2?\b|card[_-]?number|(^|_)number$|expiry|exp_(month|year)/i;
|
|
57
64
|
// Request/response HEADER names to redact. The BN code header is deliberately
|
|
58
65
|
// NOT here — PayPal needs to see it in the log.
|
|
59
66
|
const SENSITIVE_HEADER_RE = /^(authorization|proxy-authorization|cookie|set-cookie|x-api-key)$/i;
|
|
60
67
|
const REDACTED = "[REDACTED]";
|
|
68
|
+
/** Deep-copy `value` with the values of sensitive keys replaced. */
|
|
61
69
|
function redactBody(value, depth = 0) {
|
|
62
70
|
if (depth > 8 || value === null || typeof value !== "object") {
|
|
63
71
|
return value;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-logger.js","sourceRoot":"","sources":["../../../../../../src/modules/paypal/utils/request-logger.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;GAgBG;;;;;AAqBH,4DAEC;AAGD,0DAEC;
|
|
1
|
+
{"version":3,"file":"request-logger.js","sourceRoot":"","sources":["../../../../../../src/modules/paypal/utils/request-logger.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;GAgBG;;;;;AAqBH,4DAEC;AAGD,0DAEC;AA0BD,gCAYC;AA4FD,sDAEC;AAwDD,kCA+BC;AAcD,gCA6BC;AAaD,oCAmBC;AAMD,wCAaC;AAnVD,4CAAmB;AACnB,gDAAuB;AAEvB,8EAA8E;AAC9E,cAAc;AACd,8EAA8E;AAE9E;;;;GAIG;AACH,MAAM,YAAY,GAAG,oBAAoB,CAAC,IAAI,CAC5C,CAAC,OAAO,CAAC,GAAG,CAAC,kBAAkB,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAC9C,CAAA;AAED,IAAI,cAAc,GAAG,KAAK,CAAA;AAE1B,mFAAmF;AACnF,SAAgB,wBAAwB,CAAC,OAAgB;IACvD,cAAc,GAAG,CAAC,CAAC,OAAO,CAAA;AAC5B,CAAC;AAED,4EAA4E;AAC5E,SAAgB,uBAAuB;IACrC,OAAO,YAAY,IAAI,cAAc,CAAA;AACvC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xC,CAAC,CAAC,IAAI,CAAA;IACV,IAAI,CAAC,MAAM;QAAE,OAAO,IAAI,CAAA;IACxB,yEAAyE;IACzE,IAAI,cAAI,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,cAAI,CAAC,OAAO,CAAC,GAAG,CAAC;QAAE,OAAO,IAAI,CAAA;IAC3D,IAAI,CAAC,YAAE,CAAC,UAAU,CAAC,MAAM,CAAC;QAAE,OAAO,IAAI,CAAA;IACvC,OAAO,MAAM,CAAA;AACf,CAAC"}
|
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,45 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to `@easypayment/medusa-payment-paypal` are documented here.
|
|
4
4
|
|
|
5
|
+
## 1.3.7 — 2026-11-16
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- **Every request to PayPal now carries the BN code.** The
|
|
10
|
+
`PayPal-Partner-Attribution-Id` header was added by each call site, and five
|
|
11
|
+
did not: the order lookups in the storefront create-order and capture-order
|
|
12
|
+
routes, webhook signature verification, and the order `GET` and `PATCH` in
|
|
13
|
+
the invoice subscriber. Those calls went out unattributed. The header is now
|
|
14
|
+
enforced in the shared `paypalFetch` helper for all PayPal hosts, so a call
|
|
15
|
+
site can no longer omit it. Non-PayPal hosts (the partner onboarding service,
|
|
16
|
+
alert webhooks) are unaffected.
|
|
17
|
+
- **Request log no longer hides authorization details.** The log redacted any
|
|
18
|
+
JSON key containing "authorization", which also blanked PayPal's
|
|
19
|
+
`payments.authorizations` block (authorization id, status, amount, expiry) in
|
|
20
|
+
authorize and order-details responses. Credentials such as `authorization`
|
|
21
|
+
and `authorization_code` are still redacted.
|
|
22
|
+
|
|
23
|
+
### Security
|
|
24
|
+
|
|
25
|
+
- **Request log now redacts the JS SDK `client_token`.** It embeds a live
|
|
26
|
+
PayPal access token and was written to the log file in full.
|
|
27
|
+
|
|
28
|
+
## 1.3.6 — 2026-11-16
|
|
29
|
+
|
|
30
|
+
### Fixed
|
|
31
|
+
|
|
32
|
+
- **"Connect to PayPal" redirected the whole admin to PayPal instead of opening
|
|
33
|
+
the popup.** This happened after leaving the PayPal Connection page and
|
|
34
|
+
coming back in the same admin session. The page removed and re-added
|
|
35
|
+
PayPal's `partner.js` on every visit; running it a second time reset
|
|
36
|
+
PayPal's mini-browser to an empty placeholder, and the script that provides
|
|
37
|
+
the real one was never loaded again, so the connect button was left a plain
|
|
38
|
+
link. `partner.js` now runs once per working mini-browser, a broken or
|
|
39
|
+
half-finished load is restarted from a clean slate, and the button is only
|
|
40
|
+
shown once PayPal's click handler is actually bound to it (previously it
|
|
41
|
+
could appear before the popup code had finished loading). As a safety net, a
|
|
42
|
+
click on an unbound button is stopped and rebound rather than followed.
|
|
43
|
+
|
|
5
44
|
## 1.3.5 — 2026-11-16
|
|
6
45
|
|
|
7
46
|
### Fixed
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@easypayment/medusa-payment-paypal",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.7",
|
|
4
4
|
"description": "PayPal & credit card payments for Medusa v2 — PayPal Buttons, hosted card fields, 3D Secure, and an admin dashboard. Built by a PayPal Partner.",
|
|
5
5
|
"author": "EasyPayment Plugins",
|
|
6
6
|
"license": "MIT",
|
|
@@ -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
|
}
|
|
@@ -3,8 +3,81 @@
|
|
|
3
3
|
* optional retry for critical payment operations.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
import { PAYPAL_PARTNER_ATTRIBUTION_ID } from "./partner"
|
|
6
7
|
import { isRequestLoggingEnabled, logOutbound } from "./request-logger"
|
|
7
8
|
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Partner attribution (BN code)
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
|
|
13
|
+
const ATTRIBUTION_HEADER = "PayPal-Partner-Attribution-Id"
|
|
14
|
+
|
|
15
|
+
/** True for PayPal's own API hosts (`api-m.paypal.com`, the sandbox, …). */
|
|
16
|
+
function isPayPalHost(input: string | URL): boolean {
|
|
17
|
+
try {
|
|
18
|
+
const hostname = new URL(String(input)).hostname.toLowerCase()
|
|
19
|
+
return hostname === "paypal.com" || hostname.endsWith(".paypal.com")
|
|
20
|
+
} catch {
|
|
21
|
+
return false
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Guarantee the BN code on every request to PayPal.
|
|
27
|
+
*
|
|
28
|
+
* Call sites used to add the header themselves, and several (order lookups,
|
|
29
|
+
* webhook verification, the invoice subscriber) did not, so those calls went
|
|
30
|
+
* out unattributed. Enforcing it here means a new call site cannot forget.
|
|
31
|
+
* A header the caller already set is kept, and non-PayPal hosts (the partner
|
|
32
|
+
* onboarding service, alert webhooks) are left alone. The original shape of
|
|
33
|
+
* `headers` is preserved so the request log shows them as the caller wrote
|
|
34
|
+
* them.
|
|
35
|
+
*/
|
|
36
|
+
export function withPartnerAttribution(
|
|
37
|
+
input: string | URL,
|
|
38
|
+
init: RequestInit = {}
|
|
39
|
+
): RequestInit {
|
|
40
|
+
if (!isPayPalHost(input)) return init
|
|
41
|
+
|
|
42
|
+
const headers = init.headers
|
|
43
|
+
const wanted = ATTRIBUTION_HEADER.toLowerCase()
|
|
44
|
+
|
|
45
|
+
if (!headers) {
|
|
46
|
+
return {
|
|
47
|
+
...init,
|
|
48
|
+
headers: { [ATTRIBUTION_HEADER]: PAYPAL_PARTNER_ATTRIBUTION_ID },
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (typeof Headers !== "undefined" && headers instanceof Headers) {
|
|
52
|
+
if (headers.has(ATTRIBUTION_HEADER)) return init
|
|
53
|
+
const next = new Headers(headers)
|
|
54
|
+
next.set(ATTRIBUTION_HEADER, PAYPAL_PARTNER_ATTRIBUTION_ID)
|
|
55
|
+
return { ...init, headers: next }
|
|
56
|
+
}
|
|
57
|
+
if (Array.isArray(headers)) {
|
|
58
|
+
if (headers.some((pair) => String(pair?.[0]).toLowerCase() === wanted)) {
|
|
59
|
+
return init
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
...init,
|
|
63
|
+
headers: [
|
|
64
|
+
...headers,
|
|
65
|
+
[ATTRIBUTION_HEADER, PAYPAL_PARTNER_ATTRIBUTION_ID],
|
|
66
|
+
],
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (Object.keys(headers).some((name) => name.toLowerCase() === wanted)) {
|
|
70
|
+
return init
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
...init,
|
|
74
|
+
headers: {
|
|
75
|
+
...(headers as Record<string, string>),
|
|
76
|
+
[ATTRIBUTION_HEADER]: PAYPAL_PARTNER_ATTRIBUTION_ID,
|
|
77
|
+
},
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
8
81
|
export const PAYPAL_HTTP_TIMEOUT_MS = (() => {
|
|
9
82
|
const v = Number(process.env.PAYPAL_HTTP_TIMEOUT_MS)
|
|
10
83
|
return Number.isFinite(v) && v > 0 ? v : 30_000
|
|
@@ -111,12 +184,15 @@ export class CircuitOpenError extends Error {
|
|
|
111
184
|
* `fetch` with a default timeout (`PAYPAL_HTTP_TIMEOUT_MS`) and a per-host
|
|
112
185
|
* circuit breaker. 5xx responses and network/timeout errors count as failures;
|
|
113
186
|
* once the breaker opens, calls to that host reject immediately with
|
|
114
|
-
* `CircuitOpenError` until the half-open probe succeeds.
|
|
187
|
+
* `CircuitOpenError` until the half-open probe succeeds. Requests to PayPal
|
|
188
|
+
* always carry the partner attribution (BN) header: see
|
|
189
|
+
* `withPartnerAttribution`.
|
|
115
190
|
*/
|
|
116
191
|
export function paypalFetch(
|
|
117
192
|
input: string | URL,
|
|
118
|
-
|
|
193
|
+
requestInit: RequestInit = {}
|
|
119
194
|
): Promise<Response> {
|
|
195
|
+
const init = withPartnerAttribution(input, requestInit)
|
|
120
196
|
const host = cbHostKey(input)
|
|
121
197
|
if (!cbAllowRequest(host)) {
|
|
122
198
|
return Promise.reject(new CircuitOpenError())
|