@easypayment/medusa-payment-paypal 1.3.2 → 1.3.3

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.
@@ -1,6 +1,17 @@
1
- import { randomBytes } from "crypto"
2
1
  import { PAYPAL_PARTNER_ATTRIBUTION_ID } from "../utils/partner"
3
2
 
3
+ /**
4
+ * The seller nonce the hosted partner service (`partnerServiceUrl`) embeds in
5
+ * every partner-referral it creates. The onboarding token exchange sends it as
6
+ * the PKCE `code_verifier`, so it MUST equal the value the referral was minted
7
+ * with — the link request does not send a nonce, so a locally generated one
8
+ * makes every exchange fail with invalid_grant. It is not a secret: the
9
+ * single-use authCode and sharedId are what protect the exchange.
10
+ * Only override PAYPAL_SELLER_NONCE together with your own partner service.
11
+ */
12
+ export const PARTNER_SELLER_NONCE =
13
+ "a1233wtergfsdt4365tzrshgfbaewa36AGa1233wtergfsdt4365tzrshgfbaewa36AG"
14
+
4
15
  /** Partner-level configuration, resolved from env vars with built-in defaults. */
5
16
  export type PayPalModuleConfig = {
6
17
  partnerServiceUrl: string
@@ -19,33 +30,13 @@ const STATIC_CFG: PayPalModuleConfig = {
19
30
  partnerJsUrl:
20
31
  "https://www.paypal.com/webapps/merchantboarding/js/lib/lightbox/partner.js",
21
32
  backendUrl: "http://localhost:9000",
22
- sellerNonce: "",
33
+ sellerNonce: PARTNER_SELLER_NONCE,
23
34
  bnCode: PAYPAL_PARTNER_ATTRIBUTION_ID,
24
35
  partnerMerchantIdSandbox: "K6QLN2LPGQRHL",
25
36
  partnerMerchantIdLive: "GT5R877JNBPLL",
26
37
  alertWebhookUrls: [],
27
38
  }
28
39
 
29
- let warnedGeneratedNonce = false
30
-
31
- function generateSellerNonce(): string {
32
- // The nonce doubles as the OAuth code_verifier for the onboarding exchange,
33
- // so every instance must agree on it. It is persisted into settings on first
34
- // boot, but two instances booting simultaneously can race to persist
35
- // different generated values — an onboarding link minted against the losing
36
- // nonce then fails its token exchange with an opaque invalid_grant. Pinning
37
- // PAYPAL_SELLER_NONCE removes the race entirely.
38
- if (!warnedGeneratedNonce) {
39
- warnedGeneratedNonce = true
40
- console.warn(
41
- "[PayPal] PAYPAL_SELLER_NONCE is not set — generating a random seller nonce. " +
42
- "For multi-instance deployments, set PAYPAL_SELLER_NONCE to a fixed random string " +
43
- "so all instances share the same onboarding code_verifier."
44
- )
45
- }
46
- return randomBytes(32).toString("hex")
47
- }
48
-
49
40
  /** Resolve the module configuration from the environment. */
50
41
  export function getPayPalConfig(): PayPalModuleConfig {
51
42
  const backendUrl = process.env.MEDUSA_BACKEND_URL || STATIC_CFG.backendUrl
@@ -55,7 +46,7 @@ export function getPayPalConfig(): PayPalModuleConfig {
55
46
  partnerServiceUrl:
56
47
  process.env.PAYPAL_PARTNER_SERVICE_URL || STATIC_CFG.partnerServiceUrl,
57
48
  backendUrl,
58
- sellerNonce: process.env.PAYPAL_SELLER_NONCE || generateSellerNonce(),
49
+ sellerNonce: process.env.PAYPAL_SELLER_NONCE || STATIC_CFG.sellerNonce,
59
50
  partnerMerchantIdSandbox:
60
51
  process.env.PAYPAL_PARTNER_MERCHANT_ID_SANDBOX ||
61
52
  STATIC_CFG.partnerMerchantIdSandbox,
@@ -0,0 +1,41 @@
1
+ type ResolveStorefrontUrlInput = {
2
+ /** `api_details.storefront_url` from the admin settings. */
3
+ configured?: unknown
4
+ /** `STOREFRONT_URL` / `STORE_URL`. */
5
+ envUrl?: unknown
6
+ /** The request's `Origin` header. */
7
+ origin?: unknown
8
+ /** `projectConfig.http.storeCors`: a comma-separated list of origins. */
9
+ storeCors?: unknown
10
+ }
11
+
12
+ const clean = (value: unknown) =>
13
+ typeof value === "string" ? value.trim().replace(/\/+$/, "") : ""
14
+
15
+ /**
16
+ * The storefront base URL used for PayPal's return/cancel URLs: admin setting,
17
+ * then env, then the request's Origin.
18
+ *
19
+ * The Origin header is caller-controlled, and the result becomes a redirect
20
+ * target, so it only counts when it exactly matches a STORE_CORS entry. Wildcard
21
+ * and regex entries never match, which leaves the URL unset rather than trusted.
22
+ */
23
+ export function resolveStorefrontUrl({
24
+ configured,
25
+ envUrl,
26
+ origin,
27
+ storeCors,
28
+ }: ResolveStorefrontUrlInput): string | undefined {
29
+ const explicit = clean(configured) || clean(envUrl)
30
+ if (explicit) {
31
+ return explicit
32
+ }
33
+
34
+ const requestOrigin = clean(origin)
35
+ if (!requestOrigin || typeof storeCors !== "string") {
36
+ return undefined
37
+ }
38
+
39
+ const allowed = storeCors.split(",").map(clean)
40
+ return allowed.includes(requestOrigin) ? requestOrigin : undefined
41
+ }