@pradip1995/segment-animation-order 0.2.1 → 0.2.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pradip1995/segment-animation-order",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "Motion/Lumière order confirmed, failed, and details pages",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -1,6 +1,6 @@
1
1
  "use server"
2
2
 
3
- import { cancelOrder, downloadInvoice } from "@pradip1995/commerce-core/data/orders"
3
+ import { cancelOrder } from "@pradip1995/commerce-core/data/orders"
4
4
  import { cookies } from "next/headers"
5
5
 
6
6
  type Result = { success: boolean; error: string | null }
@@ -21,19 +21,84 @@ export async function cancelOrderAction(orderId: string): Promise<Result> {
21
21
  }
22
22
  }
23
23
 
24
+ async function readInvoiceError(response: Response): Promise<string> {
25
+ const fallback = `Failed to download invoice: ${response.status} ${response.statusText}`
26
+ try {
27
+ const contentType = response.headers.get("content-type") || ""
28
+ if (contentType.includes("application/json")) {
29
+ const body = (await response.json()) as {
30
+ message?: string
31
+ error?: string
32
+ }
33
+ if (body.message) return body.message
34
+ if (body.error) return body.error
35
+ } else {
36
+ const text = (await response.text()).trim()
37
+ if (text) return text.slice(0, 240)
38
+ }
39
+ } catch {
40
+ /* ignore parse errors */
41
+ }
42
+ return fallback
43
+ }
44
+
45
+ /**
46
+ * Download invoice PDF. Calls the invoice plugin directly so we can surface
47
+ * the real API error (e.g. order not eligible) instead of a bare 400.
48
+ */
24
49
  export async function downloadInvoiceAction(
25
50
  orderId: string
26
51
  ): Promise<{ success: boolean; data?: string; error?: string }> {
27
52
  if (!orderId) return { success: false, error: "Order ID is required" }
53
+
28
54
  try {
29
- const result = await downloadInvoice(orderId)
30
- if (!result?.success || !result.data) {
31
- return {
32
- success: false,
33
- error: result?.error || "Failed to download invoice",
34
- }
55
+ const cookieStore = await cookies()
56
+ const token = cookieStore.get("_medusa_jwt")?.value
57
+ const guestToken = !token
58
+ ? cookieStore.get("_medusa_guest_jwt")?.value ||
59
+ cookieStore.get("_medusa_guest_token")?.value
60
+ : null
61
+
62
+ const headers: HeadersInit = {
63
+ "x-publishable-api-key": process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY || "",
64
+ }
65
+
66
+ let url = `/store/invoice/download/${orderId}`
67
+ if (token) {
68
+ headers.Cookie = `_medusa_jwt=${token}`
69
+ headers.Authorization = `Bearer ${token}`
70
+ } else if (guestToken) {
71
+ url = `/store/guest/invoice/download/${orderId}`
72
+ headers.Cookie = `_medusa_guest_jwt=${guestToken}`
73
+ headers.Authorization = `Bearer ${guestToken}`
74
+ }
75
+
76
+ const backendUrl =
77
+ process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL || "http://localhost:9000"
78
+ const response = await fetch(`${backendUrl}${url}`, {
79
+ method: "GET",
80
+ headers,
81
+ cache: "no-store",
82
+ })
83
+
84
+ if (!response.ok) {
85
+ return { success: false, error: await readInvoiceError(response) }
86
+ }
87
+
88
+ const contentType = response.headers.get("content-type") || ""
89
+ if (!contentType.includes("application/pdf")) {
90
+ return { success: false, error: await readInvoiceError(response) }
91
+ }
92
+
93
+ const arrayBuffer = await response.arrayBuffer()
94
+ if (arrayBuffer.byteLength === 0) {
95
+ return { success: false, error: "Generated PDF is empty" }
96
+ }
97
+
98
+ return {
99
+ success: true,
100
+ data: Buffer.from(arrayBuffer).toString("base64"),
35
101
  }
36
- return { success: true, data: result.data }
37
102
  } catch (err) {
38
103
  return {
39
104
  success: false,
@@ -69,7 +69,13 @@ function canReorder(order: HttpTypes.StoreOrder): boolean {
69
69
 
70
70
  function canDownloadInvoice(order: HttpTypes.StoreOrder): boolean {
71
71
  const status = (order.status || "").toLowerCase()
72
- return status !== "canceled" && status !== "cancelled"
72
+ if (status === "canceled" || status === "cancelled") return false
73
+ // Invoice only after the order is delivered or marked completed.
74
+ if (status === "completed" || status === "complete") return true
75
+ const fulfillment = (order.fulfillment_status || "").toLowerCase()
76
+ return ["delivered", "partially_delivered", "completed", "complete"].includes(
77
+ fulfillment
78
+ )
73
79
  }
74
80
 
75
81
  function triggerPdfDownload(base64: string, filename: string) {