@easypayment/medusa-paypal 0.6.6 → 0.6.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.
@@ -319,7 +319,7 @@ function AdvancedCardPaymentsTab() {
319
319
  )
320
320
  ] }) });
321
321
  }
322
- function PayPalGooglePayPage() {
322
+ function PayPalApplePayPage() {
323
323
  return /* @__PURE__ */ jsxRuntime.jsx(reactRouterDom.Navigate, { to: "/settings/paypal/connection", replace: true });
324
324
  }
325
325
  const config = adminSdk.defineRouteConfig({
@@ -916,7 +916,7 @@ function PayPalConnectionPage() {
916
916
  ` })
917
917
  ] });
918
918
  }
919
- function PayPalApplePayPage() {
919
+ function PayPalGooglePayPage() {
920
920
  return /* @__PURE__ */ jsxRuntime.jsx(reactRouterDom.Navigate, { to: "/settings/paypal/connection", replace: true });
921
921
  }
922
922
  function PayPalPayLaterMessagingPage() {
@@ -1211,16 +1211,16 @@ const routeModule = {
1211
1211
  path: "/settings/paypal/advanced-card-payments"
1212
1212
  },
1213
1213
  {
1214
- Component: PayPalGooglePayPage,
1215
- path: "/settings/paypal/google-pay"
1214
+ Component: PayPalApplePayPage,
1215
+ path: "/settings/paypal/apple-pay"
1216
1216
  },
1217
1217
  {
1218
1218
  Component: PayPalConnectionPage,
1219
1219
  path: "/settings/paypal/connection"
1220
1220
  },
1221
1221
  {
1222
- Component: PayPalApplePayPage,
1223
- path: "/settings/paypal/apple-pay"
1222
+ Component: PayPalGooglePayPage,
1223
+ path: "/settings/paypal/google-pay"
1224
1224
  },
1225
1225
  {
1226
1226
  Component: PayPalPayLaterMessagingPage,
@@ -318,7 +318,7 @@ function AdvancedCardPaymentsTab() {
318
318
  )
319
319
  ] }) });
320
320
  }
321
- function PayPalGooglePayPage() {
321
+ function PayPalApplePayPage() {
322
322
  return /* @__PURE__ */ jsx(Navigate, { to: "/settings/paypal/connection", replace: true });
323
323
  }
324
324
  const config = defineRouteConfig({
@@ -915,7 +915,7 @@ function PayPalConnectionPage() {
915
915
  ` })
916
916
  ] });
917
917
  }
918
- function PayPalApplePayPage() {
918
+ function PayPalGooglePayPage() {
919
919
  return /* @__PURE__ */ jsx(Navigate, { to: "/settings/paypal/connection", replace: true });
920
920
  }
921
921
  function PayPalPayLaterMessagingPage() {
@@ -1210,16 +1210,16 @@ const routeModule = {
1210
1210
  path: "/settings/paypal/advanced-card-payments"
1211
1211
  },
1212
1212
  {
1213
- Component: PayPalGooglePayPage,
1214
- path: "/settings/paypal/google-pay"
1213
+ Component: PayPalApplePayPage,
1214
+ path: "/settings/paypal/apple-pay"
1215
1215
  },
1216
1216
  {
1217
1217
  Component: PayPalConnectionPage,
1218
1218
  path: "/settings/paypal/connection"
1219
1219
  },
1220
1220
  {
1221
- Component: PayPalApplePayPage,
1222
- path: "/settings/paypal/apple-pay"
1221
+ Component: PayPalGooglePayPage,
1222
+ path: "/settings/paypal/google-pay"
1223
1223
  },
1224
1224
  {
1225
1225
  Component: PayPalPayLaterMessagingPage,
@@ -0,0 +1,13 @@
1
+ import type { MedusaContainer } from "@medusajs/framework/types";
2
+ export default function paypalOrderInvoiceSubscriber({ event, container, }: {
3
+ event: {
4
+ data: {
5
+ id: string;
6
+ };
7
+ };
8
+ container: MedusaContainer;
9
+ }): Promise<void>;
10
+ export declare const config: {
11
+ event: string;
12
+ };
13
+ //# sourceMappingURL=paypal-order-invoice.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paypal-order-invoice.d.ts","sourceRoot":"","sources":["../../../../src/subscribers/paypal-order-invoice.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,2BAA2B,CAAA;AAKhE,wBAA8B,4BAA4B,CAAC,EACzD,KAAK,EACL,SAAS,GACV,EAAE;IACD,KAAK,EAAE;QAAE,IAAI,EAAE;YAAE,EAAE,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE,CAAA;IAC/B,SAAS,EAAE,eAAe,CAAA;CAC3B,iBA6FA;AAED,eAAO,MAAM,MAAM;;CAElB,CAAA"}
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.config = void 0;
4
+ exports.default = paypalOrderInvoiceSubscriber;
5
+ const paypal_auth_1 = require("../modules/paypal/utils/paypal-auth");
6
+ const provider_ids_1 = require("../modules/paypal/utils/provider-ids");
7
+ async function paypalOrderInvoiceSubscriber({ event, container, }) {
8
+ const orderId = event?.data?.id;
9
+ if (!orderId)
10
+ return;
11
+ try {
12
+ const query = container.resolve("query");
13
+ const paypal = container.resolve("paypal_onboarding");
14
+ // Fetch the order with payment session data
15
+ const { data: orders } = await query.graph({
16
+ entity: "order",
17
+ fields: [
18
+ "id",
19
+ "display_id",
20
+ "payment_collections.payment_sessions.id",
21
+ "payment_collections.payment_sessions.data",
22
+ "payment_collections.payment_sessions.provider_id",
23
+ "payment_collections.payment_sessions.status",
24
+ ],
25
+ filters: { id: orderId },
26
+ });
27
+ const order = orders?.[0];
28
+ if (!order)
29
+ return;
30
+ // Find the PayPal session
31
+ const sessions = order.payment_collections?.flatMap((pc) => pc.payment_sessions || []) || [];
32
+ const paypalSession = sessions
33
+ .filter((s) => (0, provider_ids_1.isPayPalProviderId)(s.provider_id))
34
+ .sort((a, b) => new Date(b.created_at || 0).getTime() - new Date(a.created_at || 0).getTime())[0];
35
+ if (!paypalSession)
36
+ return;
37
+ const paypalData = (paypalSession.data?.paypal || {});
38
+ const paypalOrderId = String(paypalData.order_id || "");
39
+ if (!paypalOrderId)
40
+ return;
41
+ // Get invoice prefix from settings
42
+ const settings = await paypal.getSettings().catch(() => ({}));
43
+ const settingsData = settings && typeof settings === "object" && "data" in settings
44
+ ? (settings.data ?? {})
45
+ : {};
46
+ const additionalSettings = (settingsData.additional_settings || {});
47
+ const invoicePrefix = typeof additionalSettings.invoicePrefix === "string"
48
+ ? additionalSettings.invoicePrefix
49
+ : "";
50
+ // Build industry-standard invoice ID: prefix + order display_id
51
+ // e.g. "WC-139" or "ORD-139"
52
+ const displayId = String(order.display_id || "");
53
+ const invoiceId = `${invoicePrefix}${displayId}`.trim();
54
+ if (!invoiceId)
55
+ return;
56
+ // Patch the PayPal order with the correct invoice_id
57
+ const creds = await paypal.getActiveCredentials();
58
+ const { accessToken, base } = await (0, paypal_auth_1.getPayPalAccessToken)(creds);
59
+ const patchResp = await fetch(`${base}/v2/checkout/orders/${paypalOrderId}`, {
60
+ method: "PATCH",
61
+ headers: {
62
+ Authorization: `Bearer ${accessToken}`,
63
+ "Content-Type": "application/json",
64
+ },
65
+ body: JSON.stringify([
66
+ {
67
+ op: "replace",
68
+ path: "/purchase_units/@reference_id=='default'/invoice_id",
69
+ value: invoiceId,
70
+ },
71
+ ]),
72
+ });
73
+ if (patchResp.ok || patchResp.status === 204) {
74
+ console.info(`[PayPal] invoice_id updated to "${invoiceId}" for PayPal order ${paypalOrderId} (Medusa order ${orderId})`);
75
+ }
76
+ else {
77
+ const text = await patchResp.text().catch(() => "");
78
+ console.warn(`[PayPal] invoice_id patch failed (${patchResp.status}) for order ${paypalOrderId}: ${text}`);
79
+ }
80
+ }
81
+ catch (e) {
82
+ // Non-fatal — never block order placement
83
+ console.warn("[PayPal] paypalOrderInvoiceSubscriber error:", e?.message || e);
84
+ }
85
+ }
86
+ exports.config = {
87
+ event: "order.placed",
88
+ };
89
+ //# sourceMappingURL=paypal-order-invoice.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"paypal-order-invoice.js","sourceRoot":"","sources":["../../../../src/subscribers/paypal-order-invoice.ts"],"names":[],"mappings":";;;AAKA,+CAmGC;AAtGD,qEAA0E;AAC1E,uEAAyE;AAE1D,KAAK,UAAU,4BAA4B,CAAC,EACzD,KAAK,EACL,SAAS,GAIV;IACC,MAAM,OAAO,GAAG,KAAK,EAAE,IAAI,EAAE,EAAE,CAAA;IAC/B,IAAI,CAAC,OAAO;QAAE,OAAM;IAEpB,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,SAAS,CAAC,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oBACb,IAAI,EAAE,qDAAqD;oBAC3D,KAAK,EAAE,SAAS;iBACjB;aACF,CAAC;SACH,CAAC,CAAA;QAEF,IAAI,SAAS,CAAC,EAAE,IAAI,SAAS,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;YAC7C,OAAO,CAAC,IAAI,CACV,mCAAmC,SAAS,sBAAsB,aAAa,kBAAkB,OAAO,GAAG,CAC5G,CAAA;QACH,CAAC;aAAM,CAAC;YACN,MAAM,IAAI,GAAG,MAAM,SAAS,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,CAAA;YACnD,OAAO,CAAC,IAAI,CACV,qCAAqC,SAAS,CAAC,MAAM,eAAe,aAAa,KAAK,IAAI,EAAE,CAC7F,CAAA;QACH,CAAC;IACH,CAAC;IAAC,OAAO,CAAM,EAAE,CAAC;QAChB,0CAA0C;QAC1C,OAAO,CAAC,IAAI,CAAC,8CAA8C,EAAE,CAAC,EAAE,OAAO,IAAI,CAAC,CAAC,CAAA;IAC/E,CAAC;AACH,CAAC;AAEY,QAAA,MAAM,GAAG;IACpB,KAAK,EAAE,cAAc;CACtB,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easypayment/medusa-paypal",
3
- "version": "0.6.6",
3
+ "version": "0.6.7",
4
4
  "description": "Industry-standard PayPal integration for Medusa v2",
5
5
  "license": "MIT",
6
6
  "main": "./.medusa/server/src/index.js",
@@ -0,0 +1,109 @@
1
+ import type { MedusaContainer } from "@medusajs/framework/types"
2
+ import type PayPalModuleService from "../modules/paypal/service"
3
+ import { getPayPalAccessToken } from "../modules/paypal/utils/paypal-auth"
4
+ import { isPayPalProviderId } from "../modules/paypal/utils/provider-ids"
5
+
6
+ export default async function paypalOrderInvoiceSubscriber({
7
+ event,
8
+ container,
9
+ }: {
10
+ event: { data: { id: string } }
11
+ container: MedusaContainer
12
+ }) {
13
+ const orderId = event?.data?.id
14
+ if (!orderId) return
15
+
16
+ try {
17
+ const query = container.resolve("query") as any
18
+ const paypal = container.resolve<PayPalModuleService>("paypal_onboarding")
19
+
20
+ // Fetch the order with payment session data
21
+ const { data: orders } = await query.graph({
22
+ entity: "order",
23
+ fields: [
24
+ "id",
25
+ "display_id",
26
+ "payment_collections.payment_sessions.id",
27
+ "payment_collections.payment_sessions.data",
28
+ "payment_collections.payment_sessions.provider_id",
29
+ "payment_collections.payment_sessions.status",
30
+ ],
31
+ filters: { id: orderId },
32
+ })
33
+
34
+ const order = orders?.[0]
35
+ if (!order) return
36
+
37
+ // Find the PayPal session
38
+ const sessions = order.payment_collections?.flatMap(
39
+ (pc: any) => pc.payment_sessions || []
40
+ ) || []
41
+
42
+ const paypalSession = sessions
43
+ .filter((s: any) => isPayPalProviderId(s.provider_id))
44
+ .sort((a: any, b: any) =>
45
+ new Date(b.created_at || 0).getTime() - new Date(a.created_at || 0).getTime()
46
+ )[0]
47
+
48
+ if (!paypalSession) return
49
+
50
+ const paypalData = (paypalSession.data?.paypal || {}) as Record<string, any>
51
+ const paypalOrderId = String(paypalData.order_id || "")
52
+ if (!paypalOrderId) return
53
+
54
+ // Get invoice prefix from settings
55
+ const settings = await paypal.getSettings().catch(() => ({}))
56
+ const settingsData =
57
+ settings && typeof settings === "object" && "data" in settings
58
+ ? ((settings as { data?: Record<string, any> }).data ?? {})
59
+ : {}
60
+ const additionalSettings = (settingsData.additional_settings || {}) as Record<string, any>
61
+ const invoicePrefix =
62
+ typeof additionalSettings.invoicePrefix === "string"
63
+ ? additionalSettings.invoicePrefix
64
+ : ""
65
+
66
+ // Build industry-standard invoice ID: prefix + order display_id
67
+ // e.g. "WC-139" or "ORD-139"
68
+ const displayId = String(order.display_id || "")
69
+ const invoiceId = `${invoicePrefix}${displayId}`.trim()
70
+ if (!invoiceId) return
71
+
72
+ // Patch the PayPal order with the correct invoice_id
73
+ const creds = await paypal.getActiveCredentials()
74
+ const { accessToken, base } = await getPayPalAccessToken(creds)
75
+
76
+ const patchResp = await fetch(`${base}/v2/checkout/orders/${paypalOrderId}`, {
77
+ method: "PATCH",
78
+ headers: {
79
+ Authorization: `Bearer ${accessToken}`,
80
+ "Content-Type": "application/json",
81
+ },
82
+ body: JSON.stringify([
83
+ {
84
+ op: "replace",
85
+ path: "/purchase_units/@reference_id=='default'/invoice_id",
86
+ value: invoiceId,
87
+ },
88
+ ]),
89
+ })
90
+
91
+ if (patchResp.ok || patchResp.status === 204) {
92
+ console.info(
93
+ `[PayPal] invoice_id updated to "${invoiceId}" for PayPal order ${paypalOrderId} (Medusa order ${orderId})`
94
+ )
95
+ } else {
96
+ const text = await patchResp.text().catch(() => "")
97
+ console.warn(
98
+ `[PayPal] invoice_id patch failed (${patchResp.status}) for order ${paypalOrderId}: ${text}`
99
+ )
100
+ }
101
+ } catch (e: any) {
102
+ // Non-fatal — never block order placement
103
+ console.warn("[PayPal] paypalOrderInvoiceSubscriber error:", e?.message || e)
104
+ }
105
+ }
106
+
107
+ export const config = {
108
+ event: "order.placed",
109
+ }