@easypayment/medusa-paypal 0.6.7 → 0.6.8

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,15 +1,12 @@
1
- import type { MedusaContainer } from "@medusajs/framework/types"
1
+ import type { SubscriberArgs, SubscriberConfig } from "@medusajs/framework"
2
2
  import type PayPalModuleService from "../modules/paypal/service"
3
3
  import { getPayPalAccessToken } from "../modules/paypal/utils/paypal-auth"
4
4
  import { isPayPalProviderId } from "../modules/paypal/utils/provider-ids"
5
5
 
6
- export default async function paypalOrderInvoiceSubscriber({
6
+ export default async function paypalOrderInvoiceHandler({
7
7
  event,
8
8
  container,
9
- }: {
10
- event: { data: { id: string } }
11
- container: MedusaContainer
12
- }) {
9
+ }: SubscriberArgs<{ id: string }>) {
13
10
  const orderId = event?.data?.id
14
11
  if (!orderId) return
15
12
 
@@ -27,6 +24,7 @@ export default async function paypalOrderInvoiceSubscriber({
27
24
  "payment_collections.payment_sessions.data",
28
25
  "payment_collections.payment_sessions.provider_id",
29
26
  "payment_collections.payment_sessions.status",
27
+ "payment_collections.payment_sessions.created_at",
30
28
  ],
31
29
  filters: { id: orderId },
32
30
  })
@@ -35,21 +33,29 @@ export default async function paypalOrderInvoiceSubscriber({
35
33
  if (!order) return
36
34
 
37
35
  // Find the PayPal session
38
- const sessions = order.payment_collections?.flatMap(
36
+ const sessions = (order.payment_collections || []).flatMap(
39
37
  (pc: any) => pc.payment_sessions || []
40
- ) || []
38
+ )
41
39
 
42
40
  const paypalSession = sessions
43
41
  .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()
42
+ .sort(
43
+ (a: any, b: any) =>
44
+ new Date(b.created_at || 0).getTime() -
45
+ new Date(a.created_at || 0).getTime()
46
46
  )[0]
47
47
 
48
- if (!paypalSession) return
48
+ if (!paypalSession) {
49
+ console.info("[PayPal] invoice subscriber: no PayPal session found for order", orderId)
50
+ return
51
+ }
49
52
 
50
- const paypalData = (paypalSession.data?.paypal || {}) as Record<string, any>
53
+ const paypalData = ((paypalSession.data || {}).paypal || {}) as Record<string, any>
51
54
  const paypalOrderId = String(paypalData.order_id || "")
52
- if (!paypalOrderId) return
55
+ if (!paypalOrderId) {
56
+ console.info("[PayPal] invoice subscriber: no PayPal order_id in session for order", orderId)
57
+ return
58
+ }
53
59
 
54
60
  // Get invoice prefix from settings
55
61
  const settings = await paypal.getSettings().catch(() => ({}))
@@ -64,7 +70,7 @@ export default async function paypalOrderInvoiceSubscriber({
64
70
  : ""
65
71
 
66
72
  // Build industry-standard invoice ID: prefix + order display_id
67
- // e.g. "WC-139" or "ORD-139"
73
+ // e.g. "WC-140" or "ORD-140"
68
74
  const displayId = String(order.display_id || "")
69
75
  const invoiceId = `${invoicePrefix}${displayId}`.trim()
70
76
  if (!invoiceId) return
@@ -90,20 +96,20 @@ export default async function paypalOrderInvoiceSubscriber({
90
96
 
91
97
  if (patchResp.ok || patchResp.status === 204) {
92
98
  console.info(
93
- `[PayPal] invoice_id updated to "${invoiceId}" for PayPal order ${paypalOrderId} (Medusa order ${orderId})`
99
+ `[PayPal] invoice_id updated to "${invoiceId}" for PayPal order ${paypalOrderId} (Medusa order #${displayId})`
94
100
  )
95
101
  } else {
96
102
  const text = await patchResp.text().catch(() => "")
97
103
  console.warn(
98
- `[PayPal] invoice_id patch failed (${patchResp.status}) for order ${paypalOrderId}: ${text}`
104
+ `[PayPal] invoice_id patch failed (${patchResp.status}) for PayPal order ${paypalOrderId}: ${text}`
99
105
  )
100
106
  }
101
107
  } catch (e: any) {
102
108
  // Non-fatal — never block order placement
103
- console.warn("[PayPal] paypalOrderInvoiceSubscriber error:", e?.message || e)
109
+ console.warn("[PayPal] paypalOrderInvoiceHandler error:", e?.message || e)
104
110
  }
105
111
  }
106
112
 
107
- export const config = {
113
+ export const config: SubscriberConfig = {
108
114
  event: "order.placed",
109
115
  }