@easypayment/medusa-payment-paypal 1.3.7 → 1.3.9
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 +172 -76
- package/.medusa/server/src/admin/index.mjs +107 -13
- package/.medusa/server/src/api/admin/paypal/logs/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/logs/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/merchant-status/route.d.ts +11 -0
- package/.medusa/server/src/api/admin/paypal/merchant-status/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/paypal/merchant-status/route.js +32 -0
- package/.medusa/server/src/api/admin/paypal/merchant-status/route.js.map +1 -0
- package/.medusa/server/src/api/store/paypal/create-order/route.d.ts +4 -1
- package/.medusa/server/src/api/store/paypal/create-order/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/create-order/route.js +101 -102
- package/.medusa/server/src/api/store/paypal/create-order/route.js.map +1 -1
- package/.medusa/server/src/modules/paypal/service.d.ts +34 -0
- package/.medusa/server/src/modules/paypal/service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/service.js +161 -34
- package/.medusa/server/src/modules/paypal/service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/types/config.d.ts +12 -0
- package/.medusa/server/src/modules/paypal/types/config.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/types/config.js +25 -1
- package/.medusa/server/src/modules/paypal/types/config.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/merchant-status.d.ts +50 -0
- package/.medusa/server/src/modules/paypal/utils/merchant-status.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/merchant-status.js +106 -0
- package/.medusa/server/src/modules/paypal/utils/merchant-status.js.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/order-context.d.ts +61 -0
- package/.medusa/server/src/modules/paypal/utils/order-context.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/order-context.js +127 -0
- package/.medusa/server/src/modules/paypal/utils/order-context.js.map +1 -0
- package/CHANGELOG.md +78 -0
- package/README.md +108 -61
- package/package.json +2 -2
- package/src/admin/routes/settings/paypal/_components/MerchantStatusNotices.tsx +116 -0
- package/src/admin/routes/settings/paypal/additional-settings/page.tsx +32 -6
- package/src/admin/routes/settings/paypal/advanced-card-payments/page.tsx +2 -0
- package/src/admin/routes/settings/paypal/connection/page.tsx +7 -0
- package/src/api/admin/paypal/logs/route.ts +1 -4
- package/src/api/admin/paypal/merchant-status/route.ts +36 -0
- package/src/api/store/paypal/create-order/route.ts +176 -157
- package/src/modules/paypal/service.ts +222 -43
- package/src/modules/paypal/types/config.ts +35 -2
- package/src/modules/paypal/utils/merchant-status.ts +185 -0
- package/src/modules/paypal/utils/order-context.ts +163 -0
|
@@ -10,6 +10,10 @@ import { getPayPalConfig } from "./types/config"
|
|
|
10
10
|
import { getPayPalApiBase } from "./utils/paypal-auth"
|
|
11
11
|
import { normalizeCurrencyCode } from "./utils/currencies"
|
|
12
12
|
import { PAYPAL_PARTNER_ATTRIBUTION_ID } from "./utils/partner"
|
|
13
|
+
import {
|
|
14
|
+
evaluateMerchantStatus,
|
|
15
|
+
type MerchantStatusReport,
|
|
16
|
+
} from "./utils/merchant-status"
|
|
13
17
|
import {
|
|
14
18
|
getCachedSettings,
|
|
15
19
|
invalidateSettingsCache,
|
|
@@ -302,10 +306,111 @@ class PayPalModuleService extends MedusaService({
|
|
|
302
306
|
return null
|
|
303
307
|
}
|
|
304
308
|
|
|
309
|
+
/**
|
|
310
|
+
* Seller onboarding status read by the partner service with the PARTNER's
|
|
311
|
+
* credentials, as PayPal's partner review requires. The plugin only holds
|
|
312
|
+
* the seller's credentials, so it cannot make this call itself.
|
|
313
|
+
*/
|
|
314
|
+
private async fetchMerchantIntegrationViaPartner(
|
|
315
|
+
env: Environment,
|
|
316
|
+
merchantId: string
|
|
317
|
+
) {
|
|
318
|
+
const url = this.cfg.partnerStatusUrl
|
|
319
|
+
if (!url) {
|
|
320
|
+
throw new Error("Partner status service URL is not configured.")
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
const form = new URLSearchParams()
|
|
324
|
+
form.set("sandbox", env === "live" ? "no" : "yes")
|
|
325
|
+
form.set("merchant_id", merchantId)
|
|
326
|
+
form.set("from", "medusa")
|
|
327
|
+
|
|
328
|
+
const res = await paypalFetch(url, {
|
|
329
|
+
method: "POST",
|
|
330
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
331
|
+
body: form.toString(),
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
const text = await res.text().catch(() => "")
|
|
335
|
+
let json: any
|
|
336
|
+
try {
|
|
337
|
+
json = text ? JSON.parse(text) : null
|
|
338
|
+
} catch {
|
|
339
|
+
// A non-JSON answer (e.g. an HTML error page) is handled as a failure.
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
const body = json?.body
|
|
343
|
+
if (
|
|
344
|
+
!res.ok ||
|
|
345
|
+
json?.result !== "success" ||
|
|
346
|
+
!body ||
|
|
347
|
+
typeof body !== "object"
|
|
348
|
+
) {
|
|
349
|
+
const reason =
|
|
350
|
+
body && typeof body === "object"
|
|
351
|
+
? [body.name, body.message].filter(Boolean).join(": ")
|
|
352
|
+
: text.slice(0, 200)
|
|
353
|
+
throw new Error(
|
|
354
|
+
`Partner status lookup failed (${json?.http_code ?? res.status})${
|
|
355
|
+
reason ? `: ${reason}` : ""
|
|
356
|
+
}${json?.debug_id ? ` debug_id=${json.debug_id}` : ""}`
|
|
357
|
+
)
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
return body as Record<string, any>
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Seller onboarding status
|
|
365
|
+
* (`/v1/customer/partners/{partner-id}/merchant-integrations/{merchant-id}`).
|
|
366
|
+
*
|
|
367
|
+
* Asked through the partner service first (partner credentials). When that is
|
|
368
|
+
* unavailable or PayPal refuses the partner token for this seller, falls back
|
|
369
|
+
* to the seller's own credentials — unless `sellerFallback` is false, which
|
|
370
|
+
* callers pass when no usable seller token exists for `env`.
|
|
371
|
+
*/
|
|
372
|
+
private async lookupMerchantIntegration(
|
|
373
|
+
env: Environment,
|
|
374
|
+
merchantId: string,
|
|
375
|
+
opts: { sellerAccessToken?: string; sellerFallback?: boolean } = {}
|
|
376
|
+
): Promise<{ details: Record<string, any>; source: "partner" | "seller" }> {
|
|
377
|
+
try {
|
|
378
|
+
const details = await this.fetchMerchantIntegrationViaPartner(
|
|
379
|
+
env,
|
|
380
|
+
merchantId
|
|
381
|
+
)
|
|
382
|
+
return { details, source: "partner" }
|
|
383
|
+
} catch (e: any) {
|
|
384
|
+
if (opts.sellerFallback === false) throw e
|
|
385
|
+
console.warn(
|
|
386
|
+
"[PayPal] partner seller-status lookup failed, using seller credentials:",
|
|
387
|
+
e?.message || e
|
|
388
|
+
)
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const details = await this.fetchMerchantIntegrationWithSellerCredentials(
|
|
392
|
+
env,
|
|
393
|
+
merchantId,
|
|
394
|
+
opts.sellerAccessToken
|
|
395
|
+
)
|
|
396
|
+
return { details, source: "seller" }
|
|
397
|
+
}
|
|
398
|
+
|
|
305
399
|
private async fetchMerchantIntegrationDetails(
|
|
306
400
|
env: Environment,
|
|
307
401
|
merchantId: string,
|
|
308
402
|
accessTokenOverride?: string
|
|
403
|
+
) {
|
|
404
|
+
const { details } = await this.lookupMerchantIntegration(env, merchantId, {
|
|
405
|
+
sellerAccessToken: accessTokenOverride,
|
|
406
|
+
})
|
|
407
|
+
return details
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
private async fetchMerchantIntegrationWithSellerCredentials(
|
|
411
|
+
env: Environment,
|
|
412
|
+
merchantId: string,
|
|
413
|
+
accessTokenOverride?: string
|
|
309
414
|
) {
|
|
310
415
|
const partnerMerchantId = await this.getPartnerMerchantId(env)
|
|
311
416
|
if (!partnerMerchantId) {
|
|
@@ -562,6 +667,7 @@ class PayPalModuleService extends MedusaService({
|
|
|
562
667
|
async setEnvironment(env: Environment) {
|
|
563
668
|
// Tokens are minted per environment/credentials — drop them on any switch.
|
|
564
669
|
PayPalModuleService.clientTokenCache.clear()
|
|
670
|
+
PayPalModuleService.merchantStatusCache.clear()
|
|
565
671
|
const nextEnv: Environment = env === "sandbox" ? "sandbox" : "live"
|
|
566
672
|
const row = await this.getCurrentRow()
|
|
567
673
|
const previousEnv = (row?.environment as Environment) || "live"
|
|
@@ -933,7 +1039,7 @@ class PayPalModuleService extends MedusaService({
|
|
|
933
1039
|
}
|
|
934
1040
|
|
|
935
1041
|
let sellerEmail = this.extractSellerEmail(credJson, tokenJson)
|
|
936
|
-
|
|
1042
|
+
const sellerMerchantId =
|
|
937
1043
|
String(
|
|
938
1044
|
credJson.payer_id ||
|
|
939
1045
|
credJson.merchant_id ||
|
|
@@ -942,37 +1048,10 @@ class PayPalModuleService extends MedusaService({
|
|
|
942
1048
|
""
|
|
943
1049
|
).trim() || null
|
|
944
1050
|
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
String(tokenJson.payer_id || "").trim(),
|
|
950
|
-
String(tokenJson.merchant_id || "").trim(),
|
|
951
|
-
]
|
|
952
|
-
.filter(Boolean)
|
|
953
|
-
.filter((v, i, arr) => arr.indexOf(v) === i)
|
|
954
|
-
|
|
955
|
-
for (const merchantId of merchantCandidates) {
|
|
956
|
-
try {
|
|
957
|
-
const merchantDetails = await this.fetchMerchantIntegrationDetails(
|
|
958
|
-
env,
|
|
959
|
-
merchantId,
|
|
960
|
-
sellerAccessToken
|
|
961
|
-
)
|
|
962
|
-
sellerMerchantId = sellerMerchantId || merchantId
|
|
963
|
-
sellerEmail = this.extractSellerEmail(merchantDetails)
|
|
964
|
-
if (sellerEmail) {
|
|
965
|
-
break
|
|
966
|
-
}
|
|
967
|
-
} catch (e: any) {
|
|
968
|
-
console.warn(
|
|
969
|
-
`[PayPal] Merchant integration lookup for ${merchantId} failed:`,
|
|
970
|
-
e?.message || e
|
|
971
|
-
)
|
|
972
|
-
}
|
|
973
|
-
}
|
|
974
|
-
}
|
|
975
|
-
|
|
1051
|
+
// Save first: the status lookup below may need the app access token, which
|
|
1052
|
+
// is minted from the stored credentials (and cached). The token PayPal
|
|
1053
|
+
// issued for the authorization code cannot read the seller's status — the
|
|
1054
|
+
// attempt that used to be made with it here always failed with 401.
|
|
976
1055
|
await this.saveSellerCredentials({
|
|
977
1056
|
clientId,
|
|
978
1057
|
clientSecret,
|
|
@@ -982,11 +1061,9 @@ class PayPalModuleService extends MedusaService({
|
|
|
982
1061
|
|
|
983
1062
|
if (!sellerEmail && sellerMerchantId) {
|
|
984
1063
|
try {
|
|
985
|
-
const appAccessToken = await this.getAppAccessToken()
|
|
986
1064
|
const details = await this.fetchMerchantIntegrationDetails(
|
|
987
1065
|
env,
|
|
988
|
-
sellerMerchantId
|
|
989
|
-
appAccessToken
|
|
1066
|
+
sellerMerchantId
|
|
990
1067
|
)
|
|
991
1068
|
sellerEmail = this.extractSellerEmail(details)
|
|
992
1069
|
if (sellerEmail) {
|
|
@@ -1017,13 +1094,27 @@ class PayPalModuleService extends MedusaService({
|
|
|
1017
1094
|
sellerEmail?: string | null
|
|
1018
1095
|
environment?: Environment
|
|
1019
1096
|
}) {
|
|
1020
|
-
// New credentials invalidate any client token minted for the old account.
|
|
1021
|
-
PayPalModuleService.clientTokenCache.clear()
|
|
1022
1097
|
const row = await this.getCurrentRow()
|
|
1023
1098
|
const currentEnv = await this.getCurrentEnvironment()
|
|
1024
1099
|
const env = (input.environment || currentEnv) as Environment
|
|
1025
1100
|
|
|
1026
1101
|
const existingCreds = row ? this.getEnvCreds(row, env) : {}
|
|
1102
|
+
// The same credentials saved again (e.g. to add the seller's email once it
|
|
1103
|
+
// is known) keep the cached app access token: PayPal expects a token to be
|
|
1104
|
+
// re-used until it expires, not re-minted on every save.
|
|
1105
|
+
const credentialsUnchanged =
|
|
1106
|
+
!!row &&
|
|
1107
|
+
env === currentEnv &&
|
|
1108
|
+
existingCreds.clientId === input.clientId &&
|
|
1109
|
+
existingCreds.clientSecret === input.clientSecret
|
|
1110
|
+
const tokenReset = credentialsUnchanged
|
|
1111
|
+
? {}
|
|
1112
|
+
: { app_access_token: null, app_access_token_expires_at: null }
|
|
1113
|
+
if (!credentialsUnchanged) {
|
|
1114
|
+
// New credentials invalidate any client token minted for the old account.
|
|
1115
|
+
PayPalModuleService.clientTokenCache.clear()
|
|
1116
|
+
}
|
|
1117
|
+
PayPalModuleService.merchantStatusCache.clear()
|
|
1027
1118
|
const nextSellerMerchantId =
|
|
1028
1119
|
(input.sellerMerchantId || "").trim() ||
|
|
1029
1120
|
existingCreds.sellerMerchantId ||
|
|
@@ -1086,8 +1177,7 @@ class PayPalModuleService extends MedusaService({
|
|
|
1086
1177
|
seller_client_secret: storedClientSecret,
|
|
1087
1178
|
seller_merchant_id: nextSellerMerchantId,
|
|
1088
1179
|
seller_email: nextSellerEmail,
|
|
1089
|
-
|
|
1090
|
-
app_access_token_expires_at: null,
|
|
1180
|
+
...tokenReset,
|
|
1091
1181
|
metadata: {
|
|
1092
1182
|
...(row.metadata || {}),
|
|
1093
1183
|
credentials: creds,
|
|
@@ -1121,10 +1211,15 @@ class PayPalModuleService extends MedusaService({
|
|
|
1121
1211
|
})
|
|
1122
1212
|
|
|
1123
1213
|
try {
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1214
|
+
// The credentials were just stored, so for the active environment the
|
|
1215
|
+
// cached app access token is used rather than minting a second one.
|
|
1216
|
+
const activeEnv = await this.getCurrentEnvironment()
|
|
1217
|
+
const hydrated = await this.fetchSellerProfileFromDirectCredentials(
|
|
1218
|
+
env,
|
|
1219
|
+
env === activeEnv
|
|
1220
|
+
? undefined
|
|
1221
|
+
: { clientId: input.clientId, clientSecret: input.clientSecret }
|
|
1222
|
+
)
|
|
1128
1223
|
|
|
1129
1224
|
if (hydrated.sellerEmail || hydrated.sellerMerchantId) {
|
|
1130
1225
|
await this.saveSellerCredentials({
|
|
@@ -1451,6 +1546,89 @@ class PayPalModuleService extends MedusaService({
|
|
|
1451
1546
|
}
|
|
1452
1547
|
}
|
|
1453
1548
|
|
|
1549
|
+
private static merchantStatusCache = new Map<
|
|
1550
|
+
string,
|
|
1551
|
+
{ at: number; value: MerchantStatusReport }
|
|
1552
|
+
>()
|
|
1553
|
+
private static MERCHANT_STATUS_TTL_MS = 5 * 60 * 1000
|
|
1554
|
+
|
|
1555
|
+
/**
|
|
1556
|
+
* Whether the connected seller can take payments, from PayPal's seller
|
|
1557
|
+
* onboarding status: email confirmed, payments receivable, PayPal payments
|
|
1558
|
+
* (`PPCP_STANDARD` subscribed + `PAY_WITH_PAYPAL` active) and Advanced Card
|
|
1559
|
+
* payments (`PPCP_CUSTOM` subscribed + `CUSTOM_CARD_PROCESSING` active), plus
|
|
1560
|
+
* the notices to show the merchant when any of those is not met.
|
|
1561
|
+
*
|
|
1562
|
+
* Makes an outbound call, so it is cached for a few minutes per seller and
|
|
1563
|
+
* kept out of `getStatus()` (which must stay a side-effect-free read).
|
|
1564
|
+
* Never throws: a failed lookup is reported as `available: false`.
|
|
1565
|
+
*/
|
|
1566
|
+
async getMerchantStatus(
|
|
1567
|
+
envOverride?: Environment,
|
|
1568
|
+
opts: { refresh?: boolean } = {}
|
|
1569
|
+
): Promise<MerchantStatusReport> {
|
|
1570
|
+
const activeEnv = await this.getCurrentEnvironment()
|
|
1571
|
+
const env = envOverride ?? activeEnv
|
|
1572
|
+
const unavailable = (
|
|
1573
|
+
reason: "not_connected" | "merchant_id_unknown" | "lookup_failed",
|
|
1574
|
+
message?: string
|
|
1575
|
+
): MerchantStatusReport => ({
|
|
1576
|
+
environment: env,
|
|
1577
|
+
available: false,
|
|
1578
|
+
reason,
|
|
1579
|
+
...(message ? { message } : {}),
|
|
1580
|
+
notices: [],
|
|
1581
|
+
})
|
|
1582
|
+
|
|
1583
|
+
try {
|
|
1584
|
+
const row = await this.getCurrentRow()
|
|
1585
|
+
const c = row ? this.getEnvCreds(row, env) : {}
|
|
1586
|
+
if (!row || !c.clientId || !c.clientSecret) {
|
|
1587
|
+
return unavailable("not_connected")
|
|
1588
|
+
}
|
|
1589
|
+
const merchantId =
|
|
1590
|
+
c.sellerMerchantId ||
|
|
1591
|
+
(env === activeEnv ? row.seller_merchant_id : null) ||
|
|
1592
|
+
null
|
|
1593
|
+
if (!merchantId) {
|
|
1594
|
+
return unavailable("merchant_id_unknown")
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
const cacheKey = `${env}::${merchantId}`
|
|
1598
|
+
const cached = PayPalModuleService.merchantStatusCache.get(cacheKey)
|
|
1599
|
+
if (
|
|
1600
|
+
cached &&
|
|
1601
|
+
!opts.refresh &&
|
|
1602
|
+
Date.now() - cached.at < PayPalModuleService.MERCHANT_STATUS_TTL_MS
|
|
1603
|
+
) {
|
|
1604
|
+
return cached.value
|
|
1605
|
+
}
|
|
1606
|
+
|
|
1607
|
+
const { details, source } = await this.lookupMerchantIntegration(
|
|
1608
|
+
env,
|
|
1609
|
+
String(merchantId),
|
|
1610
|
+
// The cached app token belongs to the active environment only.
|
|
1611
|
+
{ sellerFallback: env === activeEnv }
|
|
1612
|
+
)
|
|
1613
|
+
|
|
1614
|
+
const value: MerchantStatusReport = {
|
|
1615
|
+
environment: env,
|
|
1616
|
+
available: true,
|
|
1617
|
+
source,
|
|
1618
|
+
checked_at: new Date().toISOString(),
|
|
1619
|
+
...evaluateMerchantStatus(details),
|
|
1620
|
+
}
|
|
1621
|
+
PayPalModuleService.merchantStatusCache.set(cacheKey, {
|
|
1622
|
+
at: Date.now(),
|
|
1623
|
+
value,
|
|
1624
|
+
})
|
|
1625
|
+
return value
|
|
1626
|
+
} catch (e: any) {
|
|
1627
|
+
console.warn("[PayPal] seller status lookup failed:", e?.message || e)
|
|
1628
|
+
return unavailable("lookup_failed", e?.message || String(e))
|
|
1629
|
+
}
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1454
1632
|
/**
|
|
1455
1633
|
* Remove the active environment's credentials. The other environment (if
|
|
1456
1634
|
* connected) is untouched, so the row stays "connected" when it still holds
|
|
@@ -1458,6 +1636,7 @@ class PayPalModuleService extends MedusaService({
|
|
|
1458
1636
|
*/
|
|
1459
1637
|
async disconnect() {
|
|
1460
1638
|
PayPalModuleService.clientTokenCache.clear()
|
|
1639
|
+
PayPalModuleService.merchantStatusCache.clear()
|
|
1461
1640
|
const row = await this.getCurrentRow()
|
|
1462
1641
|
if (!row) return
|
|
1463
1642
|
const env = await this.getCurrentEnvironment()
|
|
@@ -15,6 +15,12 @@ export const PARTNER_SELLER_NONCE =
|
|
|
15
15
|
/** Partner-level configuration, resolved from env vars with built-in defaults. */
|
|
16
16
|
export type PayPalModuleConfig = {
|
|
17
17
|
partnerServiceUrl: string
|
|
18
|
+
/**
|
|
19
|
+
* Partner-service endpoint that reads a seller's onboarding status with the
|
|
20
|
+
* partner's credentials. Empty when it cannot be derived from a custom
|
|
21
|
+
* `partnerServiceUrl` and `PAYPAL_PARTNER_STATUS_URL` is not set.
|
|
22
|
+
*/
|
|
23
|
+
partnerStatusUrl: string
|
|
18
24
|
partnerJsUrl: string
|
|
19
25
|
backendUrl: string
|
|
20
26
|
sellerNonce: string
|
|
@@ -24,9 +30,32 @@ export type PayPalModuleConfig = {
|
|
|
24
30
|
alertWebhookUrls?: string[]
|
|
25
31
|
}
|
|
26
32
|
|
|
33
|
+
/**
|
|
34
|
+
* The seller-status endpoint lives next to the onboarding endpoint on the
|
|
35
|
+
* partner service (`seller-onboarding.php` → `seller-status.php`). Returns ""
|
|
36
|
+
* for a partner service URL that does not follow that layout.
|
|
37
|
+
*/
|
|
38
|
+
export function derivePartnerStatusUrl(partnerServiceUrl: string): string {
|
|
39
|
+
try {
|
|
40
|
+
const url = new URL(partnerServiceUrl)
|
|
41
|
+
if (!/\/seller-onboarding\.php$/.test(url.pathname)) return ""
|
|
42
|
+
url.pathname = url.pathname.replace(
|
|
43
|
+
/seller-onboarding\.php$/,
|
|
44
|
+
"seller-status.php"
|
|
45
|
+
)
|
|
46
|
+
url.search = ""
|
|
47
|
+
url.hash = ""
|
|
48
|
+
return url.toString()
|
|
49
|
+
} catch {
|
|
50
|
+
return ""
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
27
54
|
const STATIC_CFG: PayPalModuleConfig = {
|
|
28
55
|
partnerServiceUrl:
|
|
29
56
|
"https://mbjtechnolabs.com/medusa-seller-onboarding/seller-onboarding.php?from=medusa",
|
|
57
|
+
partnerStatusUrl:
|
|
58
|
+
"https://mbjtechnolabs.com/medusa-seller-onboarding/seller-status.php",
|
|
30
59
|
partnerJsUrl:
|
|
31
60
|
"https://www.paypal.com/webapps/merchantboarding/js/lib/lightbox/partner.js",
|
|
32
61
|
backendUrl: "http://localhost:9000",
|
|
@@ -40,11 +69,15 @@ const STATIC_CFG: PayPalModuleConfig = {
|
|
|
40
69
|
/** Resolve the module configuration from the environment. */
|
|
41
70
|
export function getPayPalConfig(): PayPalModuleConfig {
|
|
42
71
|
const backendUrl = process.env.MEDUSA_BACKEND_URL || STATIC_CFG.backendUrl
|
|
72
|
+
const partnerServiceUrl =
|
|
73
|
+
process.env.PAYPAL_PARTNER_SERVICE_URL || STATIC_CFG.partnerServiceUrl
|
|
43
74
|
|
|
44
75
|
return {
|
|
45
76
|
...STATIC_CFG,
|
|
46
|
-
partnerServiceUrl
|
|
47
|
-
|
|
77
|
+
partnerServiceUrl,
|
|
78
|
+
partnerStatusUrl:
|
|
79
|
+
(process.env.PAYPAL_PARTNER_STATUS_URL || "").trim() ||
|
|
80
|
+
derivePartnerStatusUrl(partnerServiceUrl),
|
|
48
81
|
backendUrl,
|
|
49
82
|
sellerNonce: process.env.PAYPAL_SELLER_NONCE || STATIC_CFG.sellerNonce,
|
|
50
83
|
partnerMerchantIdSandbox:
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turn PayPal's seller onboarding status
|
|
3
|
+
* (`GET /v1/customer/partners/{partner-id}/merchant-integrations/{merchant-id}`)
|
|
4
|
+
* into what the admin needs to know: can this seller receive payments, are
|
|
5
|
+
* PayPal payments and Advanced Card payments enabled, and what should the
|
|
6
|
+
* merchant be told when they are not.
|
|
7
|
+
*
|
|
8
|
+
* The checks and the two "Attention" messages are the ones PayPal's partner
|
|
9
|
+
* integration review asks for (2026-11-17).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export type MerchantNoticeLevel = "error" | "warning"
|
|
13
|
+
|
|
14
|
+
export type MerchantNotice = {
|
|
15
|
+
code:
|
|
16
|
+
| "email_not_confirmed"
|
|
17
|
+
| "payments_not_receivable"
|
|
18
|
+
| "paypal_payments_not_enabled"
|
|
19
|
+
| "card_payments_not_enabled"
|
|
20
|
+
level: MerchantNoticeLevel
|
|
21
|
+
message: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export type MerchantFeatureStatus = {
|
|
25
|
+
enabled: boolean
|
|
26
|
+
/** `products[name].vetting_status`, or null when the product is absent. */
|
|
27
|
+
vetting_status: string | null
|
|
28
|
+
/** `capabilities[name].status`, or null when the capability is absent. */
|
|
29
|
+
capability_status: string | null
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export type MerchantStatus = {
|
|
33
|
+
merchant_id: string | null
|
|
34
|
+
payments_receivable: boolean | null
|
|
35
|
+
primary_email_confirmed: boolean | null
|
|
36
|
+
paypal_payments: MerchantFeatureStatus
|
|
37
|
+
card_payments: MerchantFeatureStatus
|
|
38
|
+
notices: MerchantNotice[]
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** What `PayPalModuleService.getMerchantStatus()` reports to the admin. */
|
|
42
|
+
export type MerchantStatusReport =
|
|
43
|
+
| ({
|
|
44
|
+
environment: string
|
|
45
|
+
available: true
|
|
46
|
+
/** Whose credentials read the status: the partner's or the seller's. */
|
|
47
|
+
source: "partner" | "seller"
|
|
48
|
+
checked_at: string
|
|
49
|
+
} & MerchantStatus)
|
|
50
|
+
| {
|
|
51
|
+
environment: string
|
|
52
|
+
available: false
|
|
53
|
+
reason: "not_connected" | "merchant_id_unknown" | "lookup_failed"
|
|
54
|
+
message?: string
|
|
55
|
+
notices: MerchantNotice[]
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export const EMAIL_NOT_CONFIRMED_MESSAGE =
|
|
59
|
+
"Attention: Please confirm your email address on https://www.paypal.com/businessprofile/settings in order to receive payments! You currently cannot receive payments."
|
|
60
|
+
|
|
61
|
+
export const PAYMENTS_NOT_RECEIVABLE_MESSAGE =
|
|
62
|
+
"Attention: You currently cannot receive payments due to restriction on your PayPal account. Please reach out to PayPal Customer Support or connect to https://www.paypal.com for more information."
|
|
63
|
+
|
|
64
|
+
function upper(value: unknown): string | null {
|
|
65
|
+
const text = String(value ?? "")
|
|
66
|
+
.trim()
|
|
67
|
+
.toUpperCase()
|
|
68
|
+
return text || null
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function findByName(list: unknown, name: string): Record<string, any> | null {
|
|
72
|
+
if (!Array.isArray(list)) return null
|
|
73
|
+
const match = list.find(
|
|
74
|
+
(entry) => entry && typeof entry === "object" && upper(entry.name) === name
|
|
75
|
+
)
|
|
76
|
+
return (match as Record<string, any>) || null
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function featureStatus(
|
|
80
|
+
details: Record<string, any>,
|
|
81
|
+
productName: string,
|
|
82
|
+
capabilityName: string
|
|
83
|
+
): MerchantFeatureStatus {
|
|
84
|
+
const vetting = upper(
|
|
85
|
+
findByName(details.products, productName)?.vetting_status
|
|
86
|
+
)
|
|
87
|
+
const capability = upper(
|
|
88
|
+
findByName(details.capabilities, capabilityName)?.status
|
|
89
|
+
)
|
|
90
|
+
return {
|
|
91
|
+
enabled: vetting === "SUBSCRIBED" && capability === "ACTIVE",
|
|
92
|
+
vetting_status: vetting,
|
|
93
|
+
capability_status: capability,
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Merchant-facing explanation for a feature that is not enabled. */
|
|
98
|
+
function notEnabledMessage(label: string, status: MerchantFeatureStatus) {
|
|
99
|
+
switch (status.vetting_status) {
|
|
100
|
+
case null:
|
|
101
|
+
return `${label} are not enabled on your PayPal account. Please connect your PayPal account again to apply, or contact PayPal Customer Support.`
|
|
102
|
+
case "NEED_MORE_DATA":
|
|
103
|
+
return `${label} are not enabled yet: PayPal needs more information from you. Please log in to your PayPal account and provide the requested details.`
|
|
104
|
+
case "IN_REVIEW":
|
|
105
|
+
case "PENDING":
|
|
106
|
+
return `${label} are not enabled yet: PayPal is still reviewing your application. This can take a few days; you do not need to do anything for now.`
|
|
107
|
+
case "DENIED":
|
|
108
|
+
return `${label} are not available: PayPal declined your application. Please contact PayPal Customer Support for more information.`
|
|
109
|
+
case "SUBSCRIBED":
|
|
110
|
+
return `${label} are approved on your PayPal account but not active yet (status: ${
|
|
111
|
+
status.capability_status || "not available"
|
|
112
|
+
}). Please check your PayPal account for pending actions, or contact PayPal Customer Support.`
|
|
113
|
+
default:
|
|
114
|
+
return `${label} are not enabled on your PayPal account (status: ${status.vetting_status}). Please check your PayPal account, or contact PayPal Customer Support.`
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Evaluate a merchant-integrations response. Never throws. */
|
|
119
|
+
export function evaluateMerchantStatus(input: unknown): MerchantStatus {
|
|
120
|
+
const details =
|
|
121
|
+
input && typeof input === "object" ? (input as Record<string, any>) : {}
|
|
122
|
+
|
|
123
|
+
const paymentsReceivable =
|
|
124
|
+
typeof details.payments_receivable === "boolean"
|
|
125
|
+
? details.payments_receivable
|
|
126
|
+
: null
|
|
127
|
+
const emailConfirmed =
|
|
128
|
+
typeof details.primary_email_confirmed === "boolean"
|
|
129
|
+
? details.primary_email_confirmed
|
|
130
|
+
: null
|
|
131
|
+
|
|
132
|
+
const paypalPayments = featureStatus(
|
|
133
|
+
details,
|
|
134
|
+
"PPCP_STANDARD",
|
|
135
|
+
"PAY_WITH_PAYPAL"
|
|
136
|
+
)
|
|
137
|
+
const cardPayments = featureStatus(
|
|
138
|
+
details,
|
|
139
|
+
"PPCP_CUSTOM",
|
|
140
|
+
"CUSTOM_CARD_PROCESSING"
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
const notices: MerchantNotice[] = []
|
|
144
|
+
if (emailConfirmed === false) {
|
|
145
|
+
notices.push({
|
|
146
|
+
code: "email_not_confirmed",
|
|
147
|
+
level: "error",
|
|
148
|
+
message: EMAIL_NOT_CONFIRMED_MESSAGE,
|
|
149
|
+
})
|
|
150
|
+
}
|
|
151
|
+
if (paymentsReceivable === false) {
|
|
152
|
+
notices.push({
|
|
153
|
+
code: "payments_not_receivable",
|
|
154
|
+
level: "error",
|
|
155
|
+
message: PAYMENTS_NOT_RECEIVABLE_MESSAGE,
|
|
156
|
+
})
|
|
157
|
+
}
|
|
158
|
+
if (!paypalPayments.enabled) {
|
|
159
|
+
notices.push({
|
|
160
|
+
code: "paypal_payments_not_enabled",
|
|
161
|
+
level: "error",
|
|
162
|
+
message: notEnabledMessage("PayPal payments", paypalPayments),
|
|
163
|
+
})
|
|
164
|
+
}
|
|
165
|
+
if (!cardPayments.enabled) {
|
|
166
|
+
notices.push({
|
|
167
|
+
code: "card_payments_not_enabled",
|
|
168
|
+
// Cards are an optional extra on top of PayPal payments.
|
|
169
|
+
level: "warning",
|
|
170
|
+
message: notEnabledMessage(
|
|
171
|
+
"Advanced Credit and Debit Card payments",
|
|
172
|
+
cardPayments
|
|
173
|
+
),
|
|
174
|
+
})
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
merchant_id: String(details.merchant_id || "").trim() || null,
|
|
179
|
+
payments_receivable: paymentsReceivable,
|
|
180
|
+
primary_email_confirmed: emailConfirmed,
|
|
181
|
+
paypal_payments: paypalPayments,
|
|
182
|
+
card_payments: cardPayments,
|
|
183
|
+
notices,
|
|
184
|
+
}
|
|
185
|
+
}
|