@easypayment/medusa-payment-paypal 1.1.1 → 1.2.0
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 +7 -7
- package/.medusa/server/src/admin/index.mjs +7 -7
- package/.medusa/server/src/api/hooks/paypal/onboard-return/route.d.ts +16 -0
- package/.medusa/server/src/api/hooks/paypal/onboard-return/route.d.ts.map +1 -0
- package/.medusa/server/src/api/hooks/paypal/onboard-return/route.js +20 -0
- package/.medusa/server/src/api/hooks/paypal/onboard-return/route.js.map +1 -0
- package/.medusa/server/src/api/hooks/paypal/webhook/route.d.ts +18 -0
- package/.medusa/server/src/api/hooks/paypal/webhook/route.d.ts.map +1 -0
- package/.medusa/server/src/api/hooks/paypal/webhook/route.js +22 -0
- package/.medusa/server/src/api/hooks/paypal/webhook/route.js.map +1 -0
- package/.medusa/server/src/api/middlewares.d.ts.map +1 -1
- package/.medusa/server/src/api/middlewares.js +24 -1
- package/.medusa/server/src/api/middlewares.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/capture-order/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/capture-order/route.js +66 -13
- package/.medusa/server/src/api/store/paypal/capture-order/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/config/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/config/route.js +8 -0
- package/.medusa/server/src/api/store/paypal/config/route.js.map +1 -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 +63 -9
- package/.medusa/server/src/api/store/paypal/create-order/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal-complete/route.js +1 -1
- package/.medusa/server/src/api/store/paypal-complete/route.js.map +1 -1
- package/.medusa/server/src/api/utils/rate-limit.d.ts +13 -1
- package/.medusa/server/src/api/utils/rate-limit.d.ts.map +1 -1
- package/.medusa/server/src/api/utils/rate-limit.js +18 -7
- package/.medusa/server/src/api/utils/rate-limit.js.map +1 -1
- package/.medusa/server/src/jobs/paypal-webhook-retry.d.ts.map +1 -1
- package/.medusa/server/src/jobs/paypal-webhook-retry.js +4 -0
- package/.medusa/server/src/jobs/paypal-webhook-retry.js.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/base-provider.d.ts +47 -3
- package/.medusa/server/src/modules/paypal/payment-provider/base-provider.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/base-provider.js +73 -0
- package/.medusa/server/src/modules/paypal/payment-provider/base-provider.js.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.js +415 -285
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts +0 -4
- package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/service.js +140 -151
- package/.medusa/server/src/modules/paypal/payment-provider/service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/service.d.ts +7 -0
- package/.medusa/server/src/modules/paypal/service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/service.js +135 -13
- package/.medusa/server/src/modules/paypal/service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/types/config.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/types/config.js +13 -0
- package/.medusa/server/src/modules/paypal/types/config.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/amounts.d.ts +1 -1
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.d.ts +10 -0
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.js +49 -34
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/metrics.d.ts +18 -0
- package/.medusa/server/src/modules/paypal/utils/metrics.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/metrics.js +45 -0
- package/.medusa/server/src/modules/paypal/utils/metrics.js.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.js +45 -28
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.js.map +1 -1
- package/.medusa/server/src/modules/paypal/webhook-processor.d.ts +27 -0
- package/.medusa/server/src/modules/paypal/webhook-processor.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/webhook-processor.js +170 -9
- package/.medusa/server/src/modules/paypal/webhook-processor.js.map +1 -1
- package/.medusa/server/src/subscribers/paypal-webhook-process.d.ts.map +1 -1
- package/.medusa/server/src/subscribers/paypal-webhook-process.js +4 -0
- package/.medusa/server/src/subscribers/paypal-webhook-process.js.map +1 -1
- package/CHANGELOG.md +208 -73
- package/README.md +175 -184
- package/package.json +91 -90
- package/src/api/hooks/paypal/onboard-return/route.ts +15 -0
- package/src/api/hooks/paypal/webhook/route.ts +17 -0
- package/src/api/middlewares.ts +24 -1
- package/src/api/store/paypal/capture-order/route.ts +83 -12
- package/src/api/store/paypal/config/route.ts +8 -0
- package/src/api/store/paypal/create-order/route.ts +73 -9
- package/src/api/store/paypal-complete/route.ts +1 -1
- package/src/api/utils/rate-limit.ts +31 -7
- package/src/jobs/paypal-webhook-retry.ts +4 -0
- package/src/modules/paypal/payment-provider/base-provider.ts +83 -0
- package/src/modules/paypal/payment-provider/card-service.ts +277 -136
- package/src/modules/paypal/payment-provider/service.ts +149 -186
- package/src/modules/paypal/service.ts +1703 -1554
- package/src/modules/paypal/types/config.ts +16 -0
- package/src/modules/paypal/utils/credential-resolver.ts +45 -36
- package/src/modules/paypal/utils/metrics.ts +48 -0
- package/src/modules/paypal/utils/paypal-fetch.ts +58 -29
- package/src/modules/paypal/webhook-processor.ts +195 -10
- package/src/subscribers/paypal-webhook-process.ts +4 -0
|
@@ -311,9 +311,6 @@ function AdvancedCardPaymentsTab() {
|
|
|
311
311
|
function PayPalApplePayPage() {
|
|
312
312
|
return /* @__PURE__ */ jsxRuntime.jsx(reactRouterDom.Navigate, { to: "/settings/paypal/connection", replace: true });
|
|
313
313
|
}
|
|
314
|
-
function PayPalGooglePayPage() {
|
|
315
|
-
return /* @__PURE__ */ jsxRuntime.jsx(reactRouterDom.Navigate, { to: "/settings/paypal/connection", replace: true });
|
|
316
|
-
}
|
|
317
314
|
const config = adminSdk.defineRouteConfig({
|
|
318
315
|
label: "PayPal Connection"
|
|
319
316
|
});
|
|
@@ -1031,6 +1028,9 @@ function PayPalConnectionPage() {
|
|
|
1031
1028
|
` })
|
|
1032
1029
|
] });
|
|
1033
1030
|
}
|
|
1031
|
+
function PayPalGooglePayPage() {
|
|
1032
|
+
return /* @__PURE__ */ jsxRuntime.jsx(reactRouterDom.Navigate, { to: "/settings/paypal/connection", replace: true });
|
|
1033
|
+
}
|
|
1034
1034
|
function PayPalPayLaterMessagingPage() {
|
|
1035
1035
|
return /* @__PURE__ */ jsxRuntime.jsx(reactRouterDom.Navigate, { to: "/settings/paypal/connection", replace: true });
|
|
1036
1036
|
}
|
|
@@ -1260,14 +1260,14 @@ const routeModule = {
|
|
|
1260
1260
|
Component: PayPalApplePayPage,
|
|
1261
1261
|
path: "/settings/paypal/apple-pay"
|
|
1262
1262
|
},
|
|
1263
|
-
{
|
|
1264
|
-
Component: PayPalGooglePayPage,
|
|
1265
|
-
path: "/settings/paypal/google-pay"
|
|
1266
|
-
},
|
|
1267
1263
|
{
|
|
1268
1264
|
Component: PayPalConnectionPage,
|
|
1269
1265
|
path: "/settings/paypal/connection"
|
|
1270
1266
|
},
|
|
1267
|
+
{
|
|
1268
|
+
Component: PayPalGooglePayPage,
|
|
1269
|
+
path: "/settings/paypal/google-pay"
|
|
1270
|
+
},
|
|
1271
1271
|
{
|
|
1272
1272
|
Component: PayPalPayLaterMessagingPage,
|
|
1273
1273
|
path: "/settings/paypal/pay-later-messaging"
|
|
@@ -310,9 +310,6 @@ function AdvancedCardPaymentsTab() {
|
|
|
310
310
|
function PayPalApplePayPage() {
|
|
311
311
|
return /* @__PURE__ */ jsx(Navigate, { to: "/settings/paypal/connection", replace: true });
|
|
312
312
|
}
|
|
313
|
-
function PayPalGooglePayPage() {
|
|
314
|
-
return /* @__PURE__ */ jsx(Navigate, { to: "/settings/paypal/connection", replace: true });
|
|
315
|
-
}
|
|
316
313
|
const config = defineRouteConfig({
|
|
317
314
|
label: "PayPal Connection"
|
|
318
315
|
});
|
|
@@ -1030,6 +1027,9 @@ function PayPalConnectionPage() {
|
|
|
1030
1027
|
` })
|
|
1031
1028
|
] });
|
|
1032
1029
|
}
|
|
1030
|
+
function PayPalGooglePayPage() {
|
|
1031
|
+
return /* @__PURE__ */ jsx(Navigate, { to: "/settings/paypal/connection", replace: true });
|
|
1032
|
+
}
|
|
1033
1033
|
function PayPalPayLaterMessagingPage() {
|
|
1034
1034
|
return /* @__PURE__ */ jsx(Navigate, { to: "/settings/paypal/connection", replace: true });
|
|
1035
1035
|
}
|
|
@@ -1259,14 +1259,14 @@ const routeModule = {
|
|
|
1259
1259
|
Component: PayPalApplePayPage,
|
|
1260
1260
|
path: "/settings/paypal/apple-pay"
|
|
1261
1261
|
},
|
|
1262
|
-
{
|
|
1263
|
-
Component: PayPalGooglePayPage,
|
|
1264
|
-
path: "/settings/paypal/google-pay"
|
|
1265
|
-
},
|
|
1266
1262
|
{
|
|
1267
1263
|
Component: PayPalConnectionPage,
|
|
1268
1264
|
path: "/settings/paypal/connection"
|
|
1269
1265
|
},
|
|
1266
|
+
{
|
|
1267
|
+
Component: PayPalGooglePayPage,
|
|
1268
|
+
path: "/settings/paypal/google-pay"
|
|
1269
|
+
},
|
|
1270
1270
|
{
|
|
1271
1271
|
Component: PayPalPayLaterMessagingPage,
|
|
1272
1272
|
path: "/settings/paypal/pay-later-messaging"
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PayPal onboarding `return_url` under the `/hooks` namespace.
|
|
3
|
+
*
|
|
4
|
+
* PayPal redirects the onboarding popup here as a plain top-level browser
|
|
5
|
+
* navigation, which carries no `x-publishable-api-key` header — and Medusa's
|
|
6
|
+
* store publishable-key middleware rejects every `/store/*` request without
|
|
7
|
+
* that header before the handler runs. Hosting the return bridge under
|
|
8
|
+
* `/hooks` (no publishable-key guard) lets the popup complete the credential
|
|
9
|
+
* exchange and close itself.
|
|
10
|
+
*
|
|
11
|
+
* The legacy `/store/paypal/onboard-return` route is kept for deployments
|
|
12
|
+
* whose edge injects the key; both paths share this exact handler. Newly
|
|
13
|
+
* generated onboarding links point at `/hooks/paypal/onboard-return`.
|
|
14
|
+
*/
|
|
15
|
+
export { GET } from "../../../store/paypal/onboard-return/route";
|
|
16
|
+
//# sourceMappingURL=route.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../../../../../../src/api/hooks/paypal/onboard-return/route.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,EAAE,GAAG,EAAE,MAAM,4CAA4C,CAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GET = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* PayPal onboarding `return_url` under the `/hooks` namespace.
|
|
6
|
+
*
|
|
7
|
+
* PayPal redirects the onboarding popup here as a plain top-level browser
|
|
8
|
+
* navigation, which carries no `x-publishable-api-key` header — and Medusa's
|
|
9
|
+
* store publishable-key middleware rejects every `/store/*` request without
|
|
10
|
+
* that header before the handler runs. Hosting the return bridge under
|
|
11
|
+
* `/hooks` (no publishable-key guard) lets the popup complete the credential
|
|
12
|
+
* exchange and close itself.
|
|
13
|
+
*
|
|
14
|
+
* The legacy `/store/paypal/onboard-return` route is kept for deployments
|
|
15
|
+
* whose edge injects the key; both paths share this exact handler. Newly
|
|
16
|
+
* generated onboarding links point at `/hooks/paypal/onboard-return`.
|
|
17
|
+
*/
|
|
18
|
+
var route_1 = require("../../../store/paypal/onboard-return/route");
|
|
19
|
+
Object.defineProperty(exports, "GET", { enumerable: true, get: function () { return route_1.GET; } });
|
|
20
|
+
//# sourceMappingURL=route.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route.js","sourceRoot":"","sources":["../../../../../../../src/api/hooks/paypal/onboard-return/route.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;GAaG;AACH,oEAAgE;AAAvD,4FAAA,GAAG,OAAA"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PayPal webhook endpoint under the `/hooks` namespace.
|
|
3
|
+
*
|
|
4
|
+
* Medusa applies its store publishable-key middleware to EVERY `/store/*`
|
|
5
|
+
* route, and PayPal's webhook delivery cannot send the
|
|
6
|
+
* `x-publishable-api-key` header — so the original
|
|
7
|
+
* `/store/paypal/webhook` endpoint is rejected with NOT_ALLOWED before the
|
|
8
|
+
* handler runs unless a reverse proxy injects the header. The `/hooks`
|
|
9
|
+
* namespace (the same one Medusa core uses for provider webhooks) has no such
|
|
10
|
+
* guard, so external callers can reach it directly.
|
|
11
|
+
*
|
|
12
|
+
* The `/store/paypal/webhook` route is kept for deployments that already
|
|
13
|
+
* inject the key at their edge; both paths share this exact handler. New
|
|
14
|
+
* webhook registrations (and migrated existing ones) point at
|
|
15
|
+
* `/hooks/paypal/webhook`.
|
|
16
|
+
*/
|
|
17
|
+
export { POST } from "../../../store/paypal/webhook/route";
|
|
18
|
+
//# sourceMappingURL=route.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../../../../../../src/api/hooks/paypal/webhook/route.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAAE,IAAI,EAAE,MAAM,qCAAqC,CAAA"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.POST = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* PayPal webhook endpoint under the `/hooks` namespace.
|
|
6
|
+
*
|
|
7
|
+
* Medusa applies its store publishable-key middleware to EVERY `/store/*`
|
|
8
|
+
* route, and PayPal's webhook delivery cannot send the
|
|
9
|
+
* `x-publishable-api-key` header — so the original
|
|
10
|
+
* `/store/paypal/webhook` endpoint is rejected with NOT_ALLOWED before the
|
|
11
|
+
* handler runs unless a reverse proxy injects the header. The `/hooks`
|
|
12
|
+
* namespace (the same one Medusa core uses for provider webhooks) has no such
|
|
13
|
+
* guard, so external callers can reach it directly.
|
|
14
|
+
*
|
|
15
|
+
* The `/store/paypal/webhook` route is kept for deployments that already
|
|
16
|
+
* inject the key at their edge; both paths share this exact handler. New
|
|
17
|
+
* webhook registrations (and migrated existing ones) point at
|
|
18
|
+
* `/hooks/paypal/webhook`.
|
|
19
|
+
*/
|
|
20
|
+
var route_1 = require("../../../store/paypal/webhook/route");
|
|
21
|
+
Object.defineProperty(exports, "POST", { enumerable: true, get: function () { return route_1.POST; } });
|
|
22
|
+
//# sourceMappingURL=route.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"route.js","sourceRoot":"","sources":["../../../../../../../src/api/hooks/paypal/webhook/route.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;;GAeG;AACH,6DAA0D;AAAjD,6FAAA,IAAI,OAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middlewares.d.ts","sourceRoot":"","sources":["../../../../src/api/middlewares.ts"],"names":[],"mappings":";AAGA,
|
|
1
|
+
{"version":3,"file":"middlewares.d.ts","sourceRoot":"","sources":["../../../../src/api/middlewares.ts"],"names":[],"mappings":";AAGA,wBAyDE"}
|
|
@@ -8,10 +8,33 @@ exports.default = (0, http_1.defineMiddlewares)({
|
|
|
8
8
|
// Keep the exact bytes PayPal signed (`req.rawBody`) so webhook signature
|
|
9
9
|
// verification hashes the original payload, not a re-serialized copy. This
|
|
10
10
|
// entry must precede the "/store/paypal/:path*" catch-all below.
|
|
11
|
+
// Rate-limited (opt-in, like the store routes): every unauthenticated
|
|
12
|
+
// POST here costs a DB dedup read plus an outbound PayPal
|
|
13
|
+
// verify-webhook-signature call, and a flood of garbage requests could
|
|
14
|
+
// trip the shared circuit breaker for legitimate verifications.
|
|
11
15
|
matcher: "/store/paypal/webhook",
|
|
12
16
|
method: ["POST"],
|
|
13
17
|
bodyParser: { preserveRawBody: true },
|
|
14
|
-
middlewares: [
|
|
18
|
+
middlewares: [
|
|
19
|
+
(0, rate_limit_1.createRateLimiter)("webhook", {
|
|
20
|
+
maxEnvVar: "PAYPAL_WEBHOOK_RATE_LIMIT_MAX",
|
|
21
|
+
windowEnvVar: "PAYPAL_WEBHOOK_RATE_LIMIT_WINDOW_MS",
|
|
22
|
+
}),
|
|
23
|
+
],
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
// Same raw-body requirement for the /hooks mirror of the webhook route —
|
|
27
|
+
// the path external PayPal deliveries actually reach, since Medusa's
|
|
28
|
+
// publishable-key middleware blocks unauthenticated POSTs to /store/*.
|
|
29
|
+
matcher: "/hooks/paypal/webhook",
|
|
30
|
+
method: ["POST"],
|
|
31
|
+
bodyParser: { preserveRawBody: true },
|
|
32
|
+
middlewares: [
|
|
33
|
+
(0, rate_limit_1.createRateLimiter)("webhook-hooks", {
|
|
34
|
+
maxEnvVar: "PAYPAL_WEBHOOK_RATE_LIMIT_MAX",
|
|
35
|
+
windowEnvVar: "PAYPAL_WEBHOOK_RATE_LIMIT_WINDOW_MS",
|
|
36
|
+
}),
|
|
37
|
+
],
|
|
15
38
|
},
|
|
16
39
|
{
|
|
17
40
|
// Rate-limit the order-creating route: unauthenticated by necessity, so
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middlewares.js","sourceRoot":"","sources":["../../../../src/api/middlewares.ts"],"names":[],"mappings":";;AAAA,mDAA4D;AAC5D,mDAAsD;AAEtD,kBAAe,IAAA,wBAAiB,EAAC;IAC/B,MAAM,EAAE;QACN;YACE,0EAA0E;YAC1E,2EAA2E;YAC3E,iEAAiE;YACjE,OAAO,EAAE,uBAAuB;YAChC,MAAM,EAAE,CAAC,MAAM,CAAC;YAChB,UAAU,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE;YACrC,WAAW,EAAE,EAAE;
|
|
1
|
+
{"version":3,"file":"middlewares.js","sourceRoot":"","sources":["../../../../src/api/middlewares.ts"],"names":[],"mappings":";;AAAA,mDAA4D;AAC5D,mDAAsD;AAEtD,kBAAe,IAAA,wBAAiB,EAAC;IAC/B,MAAM,EAAE;QACN;YACE,0EAA0E;YAC1E,2EAA2E;YAC3E,iEAAiE;YACjE,sEAAsE;YACtE,0DAA0D;YAC1D,uEAAuE;YACvE,gEAAgE;YAChE,OAAO,EAAE,uBAAuB;YAChC,MAAM,EAAE,CAAC,MAAM,CAAC;YAChB,UAAU,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE;YACrC,WAAW,EAAE;gBACX,IAAA,8BAAiB,EAAC,SAAS,EAAE;oBAC3B,SAAS,EAAE,+BAA+B;oBAC1C,YAAY,EAAE,qCAAqC;iBACpD,CAAC;aACH;SACF;QACD;YACE,yEAAyE;YACzE,qEAAqE;YACrE,uEAAuE;YACvE,OAAO,EAAE,uBAAuB;YAChC,MAAM,EAAE,CAAC,MAAM,CAAC;YAChB,UAAU,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE;YACrC,WAAW,EAAE;gBACX,IAAA,8BAAiB,EAAC,eAAe,EAAE;oBACjC,SAAS,EAAE,+BAA+B;oBAC1C,YAAY,EAAE,qCAAqC;iBACpD,CAAC;aACH;SACF;QACD;YACE,wEAAwE;YACxE,oEAAoE;YACpE,yEAAyE;YACzE,OAAO,EAAE,4BAA4B;YACrC,MAAM,EAAE,CAAC,MAAM,CAAC;YAChB,WAAW,EAAE,CAAC,IAAA,8BAAiB,EAAC,cAAc,CAAC,CAAC;SACjD;QACD;YACE,OAAO,EAAE,6BAA6B;YACtC,MAAM,EAAE,CAAC,MAAM,CAAC;YAChB,WAAW,EAAE,CAAC,IAAA,8BAAiB,EAAC,eAAe,CAAC,CAAC;SAClD;QACD;YACE,OAAO,EAAE,wBAAwB;YACjC,MAAM,EAAE,CAAC,MAAM,CAAC;YAChB,WAAW,EAAE,CAAC,IAAA,8BAAiB,EAAC,iBAAiB,CAAC,CAAC;SACpD;QACD;YACE,OAAO,EAAE,sBAAsB;YAC/B,WAAW,EAAE,EAAE;SAChB;KACF;CACF,CAAC,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../../../../../../src/api/store/paypal/capture-order/route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA;
|
|
1
|
+
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../../../../../../src/api/store/paypal/capture-order/route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA;AA6K7E,wBAAsB,IAAI,CAAC,GAAG,EAAE,aAAa,EAAE,GAAG,EAAE,cAAc,2BAoMjE"}
|
|
@@ -4,10 +4,10 @@ exports.POST = POST;
|
|
|
4
4
|
const utils_1 = require("@medusajs/framework/utils");
|
|
5
5
|
const crypto_1 = require("crypto");
|
|
6
6
|
const paypal_auth_1 = require("../../../../modules/paypal/utils/paypal-auth");
|
|
7
|
+
const partner_1 = require("../../../../modules/paypal/utils/partner");
|
|
7
8
|
const paypal_fetch_1 = require("../../../../modules/paypal/utils/paypal-fetch");
|
|
8
9
|
const status_utils_1 = require("../../../../modules/paypal/payment-provider/status-utils");
|
|
9
10
|
const payment_session_1 = require("../../../../modules/paypal/utils/payment-session");
|
|
10
|
-
const BN_CODE = "MBJTechnolabs_SI_SPB";
|
|
11
11
|
const PAYPAL_ORDER_ID_RE = /^[A-Z0-9]{10,25}$/;
|
|
12
12
|
function resolveIdempotencyKey(req, suffix, fallback) {
|
|
13
13
|
const header = req.headers["idempotency-key"] ||
|
|
@@ -74,7 +74,24 @@ async function attachPayPalAuthorizationToSession(cartId, orderId, authorization
|
|
|
74
74
|
return false;
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
|
-
|
|
77
|
+
/**
|
|
78
|
+
* Return the session's existing capture ONLY when it demonstrably COMPLETED.
|
|
79
|
+
*
|
|
80
|
+
* The session can carry a capture that never settled: the webhook processor
|
|
81
|
+
* patches `capture_id` onto the session for every capture event, including
|
|
82
|
+
* PAYMENT.CAPTURE.DENIED and PENDING. Blindly short-circuiting on any stored
|
|
83
|
+
* capture would report success to the storefront for a payment that was
|
|
84
|
+
* declined — the buyer would then hit "payment processed but order could not
|
|
85
|
+
* be finalized" instead of being able to retry.
|
|
86
|
+
*
|
|
87
|
+
* - stored capture object with a known status: COMPLETED → return it,
|
|
88
|
+
* anything else → null (fall through and attempt a real capture).
|
|
89
|
+
* - bare `capture_id` (or a capture object whose status can't be read):
|
|
90
|
+
* verify against the live PayPal order. If PayPal can't be reached, return
|
|
91
|
+
* the stored data — the previous fail-safe behavior — since attempting a
|
|
92
|
+
* fresh capture against an unreachable PayPal would fail anyway.
|
|
93
|
+
*/
|
|
94
|
+
async function getExistingCompletedCapture(cartId, orderId, scope, fetchLiveOrder) {
|
|
78
95
|
try {
|
|
79
96
|
const session = await (0, payment_session_1.findPayPalSessionForCart)(cartId, scope);
|
|
80
97
|
if (!session)
|
|
@@ -83,10 +100,28 @@ async function getExistingCapture(cartId, orderId, scope) {
|
|
|
83
100
|
const existingOrderId = String(paypalData.order_id || "");
|
|
84
101
|
if (existingOrderId && existingOrderId !== orderId)
|
|
85
102
|
return null;
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
103
|
+
const stored = paypalData.capture;
|
|
104
|
+
if (stored) {
|
|
105
|
+
const storedStatus = (0, status_utils_1.extractCaptureStatus)(stored);
|
|
106
|
+
if (storedStatus === "COMPLETED")
|
|
107
|
+
return stored;
|
|
108
|
+
if (storedStatus)
|
|
109
|
+
return null;
|
|
110
|
+
// no readable status on the stored object — verify live below
|
|
111
|
+
}
|
|
112
|
+
if (!stored && !paypalData.capture_id)
|
|
113
|
+
return null;
|
|
114
|
+
let order;
|
|
115
|
+
try {
|
|
116
|
+
order = await fetchLiveOrder();
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return stored || { id: paypalData.capture_id };
|
|
120
|
+
}
|
|
121
|
+
const liveCapture = order?.purchase_units?.[0]?.payments?.captures?.[0];
|
|
122
|
+
if (liveCapture && (0, status_utils_1.extractCaptureStatus)(liveCapture) === "COMPLETED") {
|
|
123
|
+
return liveCapture;
|
|
124
|
+
}
|
|
90
125
|
return null;
|
|
91
126
|
}
|
|
92
127
|
catch {
|
|
@@ -128,15 +163,28 @@ async function POST(req, res) {
|
|
|
128
163
|
message: "order_id does not match the payment session for this cart",
|
|
129
164
|
});
|
|
130
165
|
}
|
|
131
|
-
const existingCapture = await getExistingCapture(cartId, orderId, scope);
|
|
132
|
-
if (existingCapture) {
|
|
133
|
-
return res.json({ capture: existingCapture });
|
|
134
|
-
}
|
|
135
166
|
const creds = await paypal.getActiveCredentials();
|
|
136
167
|
// Cached app access token (single-flight refresh) — avoids an OAuth
|
|
137
168
|
// round-trip on every capture-order call.
|
|
138
169
|
const base = (0, paypal_auth_1.getPayPalApiBase)(creds.environment);
|
|
139
170
|
const accessToken = await paypal.getAppAccessToken();
|
|
171
|
+
const fetchLiveOrder = async () => {
|
|
172
|
+
const resp = await (0, paypal_fetch_1.paypalFetch)(`${base}/v2/checkout/orders/${encodeURIComponent(orderId)}`, {
|
|
173
|
+
method: "GET",
|
|
174
|
+
headers: {
|
|
175
|
+
Authorization: `Bearer ${accessToken}`,
|
|
176
|
+
"Content-Type": "application/json",
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
if (!resp.ok) {
|
|
180
|
+
throw new Error(`PayPal get order error (${resp.status})`);
|
|
181
|
+
}
|
|
182
|
+
return resp.json();
|
|
183
|
+
};
|
|
184
|
+
const existingCapture = await getExistingCompletedCapture(cartId, orderId, scope, fetchLiveOrder);
|
|
185
|
+
if (existingCapture) {
|
|
186
|
+
return res.json({ capture: existingCapture });
|
|
187
|
+
}
|
|
140
188
|
const settings = await paypal.getSettings().catch(() => ({}));
|
|
141
189
|
const data = settings && typeof settings === "object" && "data" in settings
|
|
142
190
|
? (settings.data ?? {})
|
|
@@ -145,7 +193,12 @@ async function POST(req, res) {
|
|
|
145
193
|
const paymentAction = typeof additionalSettings.paymentAction === "string"
|
|
146
194
|
? additionalSettings.paymentAction
|
|
147
195
|
: "capture";
|
|
148
|
-
|
|
196
|
+
// The suffix must include the order id: PayPal deduplicates by
|
|
197
|
+
// PayPal-Request-Id, so a client reusing one Idempotency-Key header across
|
|
198
|
+
// two different orders would otherwise get order A's cached capture back
|
|
199
|
+
// for order B. Named `paypalRequestId` so it never shadows `requestId`
|
|
200
|
+
// (the log/response correlation UUID declared at the top of POST).
|
|
201
|
+
const paypalRequestId = resolveIdempotencyKey(req, `capture-order-${orderId}`, `pp-capture-${orderId}`);
|
|
149
202
|
const safeOrderId = encodeURIComponent(orderId);
|
|
150
203
|
const endpoint = paymentAction === "authorize"
|
|
151
204
|
? `${base}/v2/checkout/orders/${safeOrderId}/authorize`
|
|
@@ -155,8 +208,8 @@ async function POST(req, res) {
|
|
|
155
208
|
headers: {
|
|
156
209
|
Authorization: `Bearer ${accessToken}`,
|
|
157
210
|
"Content-Type": "application/json",
|
|
158
|
-
"PayPal-Request-Id":
|
|
159
|
-
"PayPal-Partner-Attribution-Id":
|
|
211
|
+
"PayPal-Request-Id": paypalRequestId,
|
|
212
|
+
"PayPal-Partner-Attribution-Id": partner_1.PAYPAL_PARTNER_ATTRIBUTION_ID,
|
|
160
213
|
},
|
|
161
214
|
});
|
|
162
215
|
const ppText = await ppResp.text();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route.js","sourceRoot":"","sources":["../../../../../../../src/api/store/paypal/capture-order/route.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"route.js","sourceRoot":"","sources":["../../../../../../../src/api/store/paypal/capture-order/route.ts"],"names":[],"mappings":";;AA6KA,oBAoMC;AA/WD,qDAAqE;AACrE,mCAAmC;AAEnC,8EAA+E;AAC/E,sEAAmG;AACnG,gFAA2E;AAC3E,2FAA+F;AAC/F,sFAIyD;AAEzD,MAAM,kBAAkB,GAAG,mBAAmB,CAAA;AAO9C,SAAS,qBAAqB,CAAC,GAAkB,EAAE,MAAc,EAAE,QAAgB;IACjF,MAAM,MAAM,GACV,GAAG,CAAC,OAAO,CAAC,iBAAiB,CAAC;QAC9B,GAAG,CAAC,OAAO,CAAC,iBAAiB,CAAC;QAC9B,GAAG,CAAC,OAAO,CAAC,mBAAmB,CAAC;QAChC,GAAG,CAAC,OAAO,CAAC,mBAAmB,CAAC,CAAA;IAClC,MAAM,GAAG,GAAG,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAA;IACtD,IAAI,GAAG,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;QAC9B,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,IAAI,MAAM,EAAE,CAAA;IAC1C,CAAC;IACD,OAAO,QAAQ,IAAI,MAAM,MAAM,IAAI,IAAA,mBAAU,GAAE,EAAE,CAAA;AACnD,CAAC;AAED;;;;;;;GAOG;AACH,KAAK,UAAU,4BAA4B,CACzC,MAAc,EACd,OAAe,EACf,OAAY,EACZ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@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../../../../../../src/api/store/paypal/config/route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA;AAQ7E,wBAAsB,GAAG,CAAC,GAAG,EAAE,aAAa,EAAE,GAAG,EAAE,cAAc,
|
|
1
|
+
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../../../../../../src/api/store/paypal/config/route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA;AAQ7E,wBAAsB,GAAG,CAAC,GAAG,EAAE,aAAa,EAAE,GAAG,EAAE,cAAc,2BA0GhE"}
|
|
@@ -74,6 +74,14 @@ async function GET(req, res) {
|
|
|
74
74
|
button_width: paypalSettings.buttonWidth || "responsive",
|
|
75
75
|
button_height: paypalSettings.buttonHeight ?? 45,
|
|
76
76
|
button_label: paypalSettings.buttonLabel || "paypal",
|
|
77
|
+
// Funding sources to disable in the PayPal JS SDK (e.g. ["card",
|
|
78
|
+
// "paylater"]). The UI package has always read this field; emit it so
|
|
79
|
+
// the setting actually takes effect.
|
|
80
|
+
disable_buttons: Array.isArray(paypalSettings.disableButtons)
|
|
81
|
+
? paypalSettings.disableButtons.map((b) => String(b))
|
|
82
|
+
: Array.isArray(paypalSettings.disable_buttons)
|
|
83
|
+
? paypalSettings.disable_buttons.map((b) => String(b))
|
|
84
|
+
: [],
|
|
77
85
|
});
|
|
78
86
|
}
|
|
79
87
|
catch {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route.js","sourceRoot":"","sources":["../../../../../../../src/api/store/paypal/config/route.ts"],"names":[],"mappings":";;AAQA,
|
|
1
|
+
{"version":3,"file":"route.js","sourceRoot":"","sources":["../../../../../../../src/api/store/paypal/config/route.ts"],"names":[],"mappings":";;AAQA,kBA0GC;AAhHD,4EA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cAAc,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC,CAAU,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;gBAC9D,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,cAAc,CAAC,eAAe,CAAC;oBAC7C,CAAC,CAAC,cAAc,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC,CAAU,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;oBAC/D,CAAC,CAAC,EAAE;SACT,CAAC,CAAA;IACJ,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,OAAO,EAAE,8BAA8B,EAAE,CAAC,CAAA;IAC1E,CAAC;AACH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../../../../../../src/api/store/paypal/create-order/route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA;
|
|
1
|
+
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../../../../../../src/api/store/paypal/create-order/route.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA;AAwF7E,wBAAsB,IAAI,CAAC,GAAG,EAAE,aAAa,EAAE,GAAG,EAAE,cAAc,2BAkcjE"}
|
|
@@ -6,9 +6,9 @@ const crypto_1 = require("crypto");
|
|
|
6
6
|
const amounts_1 = require("../../../../modules/paypal/utils/amounts");
|
|
7
7
|
const currencies_1 = require("../../../../modules/paypal/utils/currencies");
|
|
8
8
|
const paypal_auth_1 = require("../../../../modules/paypal/utils/paypal-auth");
|
|
9
|
+
const partner_1 = require("../../../../modules/paypal/utils/partner");
|
|
9
10
|
const paypal_fetch_1 = require("../../../../modules/paypal/utils/paypal-fetch");
|
|
10
11
|
const payment_session_1 = require("../../../../modules/paypal/utils/payment-session");
|
|
11
|
-
const BN_CODE = "MBJTechnolabs_SI_SPB";
|
|
12
12
|
function resolveIdempotencyKey(req, suffix, fallback) {
|
|
13
13
|
const header = req.headers["idempotency-key"] ||
|
|
14
14
|
req.headers["Idempotency-Key"] ||
|
|
@@ -16,6 +16,10 @@ function resolveIdempotencyKey(req, suffix, fallback) {
|
|
|
16
16
|
req.headers["X-Idempotency-Key"];
|
|
17
17
|
const key = Array.isArray(header) ? header[0] : header;
|
|
18
18
|
if (key && String(key).trim()) {
|
|
19
|
+
// The suffix must include the cart id (callers pass it): PayPal caches by
|
|
20
|
+
// PayPal-Request-Id, so a client reusing one idempotency-key header across
|
|
21
|
+
// two carts would otherwise get cart A's cached order (A's amount) back
|
|
22
|
+
// for cart B.
|
|
19
23
|
return `${String(key).trim()}-${suffix}`;
|
|
20
24
|
}
|
|
21
25
|
return fallback || `pp-${suffix}-${(0, crypto_1.randomUUID)()}`;
|
|
@@ -70,10 +74,11 @@ async function POST(req, res) {
|
|
|
70
74
|
if (!cartId.startsWith("cart_")) {
|
|
71
75
|
return res.status(400).json({ message: "Invalid cart_id format" });
|
|
72
76
|
}
|
|
77
|
+
// A stored order id is only reused after verifying it still matches the
|
|
78
|
+
// cart (amount/currency/state) — see below, once the cart is loaded. Buyers
|
|
79
|
+
// who back out of the PayPal popup and change their cart would otherwise be
|
|
80
|
+
// charged the stale total.
|
|
73
81
|
const existingOrderId = await getExistingPayPalOrderId(req, cartId);
|
|
74
|
-
if (existingOrderId) {
|
|
75
|
-
return res.json({ id: existingOrderId });
|
|
76
|
-
}
|
|
77
82
|
const query = req.scope.resolve("query");
|
|
78
83
|
const { data } = await query.graph({
|
|
79
84
|
entity: "cart",
|
|
@@ -127,6 +132,50 @@ async function POST(req, res) {
|
|
|
127
132
|
const exponent = (0, amounts_1.getCurrencyExponent)(currency);
|
|
128
133
|
const totalMajor = Number(cart.total || 0);
|
|
129
134
|
const value = totalMajor.toFixed(exponent);
|
|
135
|
+
// Reuse the stored PayPal order only when it still matches the cart: same
|
|
136
|
+
// amount, same currency, and still in a reusable (pre-terminal) state. On
|
|
137
|
+
// mismatch fall through and create a fresh order (which replaces the
|
|
138
|
+
// stored id on the session). If PayPal can't be reached for the check,
|
|
139
|
+
// reuse the stored id — matching the previous behavior rather than
|
|
140
|
+
// blocking checkout on a transient error.
|
|
141
|
+
if (existingOrderId) {
|
|
142
|
+
try {
|
|
143
|
+
const checkBase = (0, paypal_auth_1.getPayPalApiBase)(creds.environment);
|
|
144
|
+
const checkToken = await paypal.getAppAccessToken();
|
|
145
|
+
const checkResp = await (0, paypal_fetch_1.paypalFetch)(`${checkBase}/v2/checkout/orders/${encodeURIComponent(existingOrderId)}`, {
|
|
146
|
+
method: "GET",
|
|
147
|
+
headers: {
|
|
148
|
+
Authorization: `Bearer ${checkToken}`,
|
|
149
|
+
"Content-Type": "application/json",
|
|
150
|
+
},
|
|
151
|
+
});
|
|
152
|
+
if (checkResp.ok) {
|
|
153
|
+
const existingOrder = await checkResp.json();
|
|
154
|
+
const existingAmount = existingOrder?.purchase_units?.[0]?.amount;
|
|
155
|
+
const existingStatus = String(existingOrder?.status || "").toUpperCase();
|
|
156
|
+
const reusableStatus = ["CREATED", "APPROVED", "PAYER_ACTION_REQUIRED"].includes(existingStatus);
|
|
157
|
+
const amountMatches = String(existingAmount?.value || "") === value &&
|
|
158
|
+
String(existingAmount?.currency_code || "").toUpperCase() === currency;
|
|
159
|
+
if (reusableStatus && amountMatches) {
|
|
160
|
+
return res.json({ id: existingOrderId });
|
|
161
|
+
}
|
|
162
|
+
if (existingStatus === "COMPLETED") {
|
|
163
|
+
// Already paid — never mint a second order for this cart.
|
|
164
|
+
return res.json({ id: existingOrderId });
|
|
165
|
+
}
|
|
166
|
+
logger.info(`[paypal] create-order: stored order ${existingOrderId} is stale (status=${existingStatus}, amount=${existingAmount?.value} ${existingAmount?.currency_code} vs cart ${value} ${currency}) — creating a fresh order (request_id=${requestId})`);
|
|
167
|
+
}
|
|
168
|
+
else if (checkResp.status !== 404) {
|
|
169
|
+
// Transient PayPal-side failure: reuse rather than block checkout.
|
|
170
|
+
return res.json({ id: existingOrderId });
|
|
171
|
+
}
|
|
172
|
+
// 404: the stored order no longer exists (e.g. environment switched) —
|
|
173
|
+
// fall through and create a fresh one.
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
return res.json({ id: existingOrderId });
|
|
177
|
+
}
|
|
178
|
+
}
|
|
130
179
|
const paymentActionRaw = typeof additionalSettings.paymentAction === "string"
|
|
131
180
|
? additionalSettings.paymentAction
|
|
132
181
|
: "capture";
|
|
@@ -277,17 +326,22 @@ async function POST(req, res) {
|
|
|
277
326
|
// instead of minting a fresh OAuth token on every create-order call.
|
|
278
327
|
const base = (0, paypal_auth_1.getPayPalApiBase)(creds.environment);
|
|
279
328
|
const accessToken = await paypal.getAppAccessToken();
|
|
280
|
-
// Deterministic-per-cart idempotency key for the PayPal
|
|
281
|
-
//
|
|
282
|
-
//
|
|
283
|
-
|
|
329
|
+
// Deterministic-per-(cart, amount, currency) idempotency key for the PayPal
|
|
330
|
+
// call. The amount/currency MUST be part of the key: PayPal replays the
|
|
331
|
+
// cached response for a reused PayPal-Request-Id, so a key derived from the
|
|
332
|
+
// cart id alone would return the ORIGINAL order (at the original total)
|
|
333
|
+
// when the buyer changes the cart and a fresh order is minted — silently
|
|
334
|
+
// re-charging the stale amount and defeating the staleness check above.
|
|
335
|
+
// Kept distinct from `requestId` (the log/response correlation id declared
|
|
336
|
+
// at the top of POST) so the two never shadow each other.
|
|
337
|
+
const paypalRequestId = resolveIdempotencyKey(req, `create-order-${cart.id}-${value}-${currency}`, `pp-create-${cart.id}-${value}-${currency}`);
|
|
284
338
|
const ppResp = await (0, paypal_fetch_1.paypalFetch)(`${base}/v2/checkout/orders`, {
|
|
285
339
|
method: "POST",
|
|
286
340
|
headers: {
|
|
287
341
|
Authorization: `Bearer ${accessToken}`,
|
|
288
342
|
"Content-Type": "application/json",
|
|
289
343
|
"PayPal-Request-Id": paypalRequestId,
|
|
290
|
-
"PayPal-Partner-Attribution-Id":
|
|
344
|
+
"PayPal-Partner-Attribution-Id": partner_1.PAYPAL_PARTNER_ATTRIBUTION_ID,
|
|
291
345
|
},
|
|
292
346
|
body: JSON.stringify({
|
|
293
347
|
intent: paymentAction,
|