@easypayment/medusa-paypal 0.6.0 → 0.6.1

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,200 +1,200 @@
1
- import React, { useEffect, useRef, useState } from "react"
2
- import PayPalTabs from "../_components/Tabs"
3
-
4
- type AdminFetchOptions = {
5
- method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH"
6
- body?: Record<string, unknown>
7
- query?: Record<string, string>
8
- }
9
-
10
- async function adminFetch<T = unknown>(path: string, opts: AdminFetchOptions = {}): Promise<T> {
11
- const { method = "GET", body, query } = opts
12
- let url = path
13
- if (query && Object.keys(query).length > 0) {
14
- const params = new URLSearchParams(query)
15
- url = `${path}?${params.toString()}`
16
- }
17
- const headers: Record<string, string> = { Accept: "application/json" }
18
- if (body !== undefined) headers["Content-Type"] = "application/json"
19
- if (typeof window !== "undefined") {
20
- const token = (window as any).__medusa__?.token
21
- if (token) headers["Authorization"] = `Bearer ${token}`
22
- }
23
- const res = await fetch(url, {
24
- method, headers, credentials: "include",
25
- body: body !== undefined ? JSON.stringify(body) : undefined,
26
- })
27
- const text = await res.text().catch(() => "")
28
- if (!res.ok) {
29
- if (res.status === 401) throw new Error("Unauthorized (401) - session may have expired. Please reload and log in again.")
30
- if (res.status === 403) throw new Error("Forbidden (403) - you do not have permission to perform this action.")
31
- throw new Error(text || `Request failed with status ${res.status}`)
32
- }
33
- if (!text) return {} as T
34
- try { return JSON.parse(text) as T } catch { return {} as T }
35
- }
36
-
37
- type PaymentAction = "capture" | "authorize"
38
- type LandingPage = "no_preference" | "login" | "billing"
39
-
40
- type AdditionalSettingsForm = {
41
- paymentAction: PaymentAction
42
- brandName: string
43
- landingPage: LandingPage
44
- requireInstantPayment: boolean
45
- sendItemDetails: boolean
46
- invoicePrefix: string
47
- creditCardStatementName: string
48
- }
49
-
50
- const DEFAULT_FORM: AdditionalSettingsForm = {
51
- paymentAction: "capture",
52
- brandName: "PayPal",
53
- landingPage: "no_preference",
54
- requireInstantPayment: false,
55
- sendItemDetails: true,
56
- invoicePrefix: "WC-",
57
- creditCardStatementName: "PayPal",
58
- }
59
-
60
- function mergeWithDefaults(saved?: Partial<AdditionalSettingsForm> | null) {
61
- if (!saved) return { ...DEFAULT_FORM }
62
- const entries = Object.entries(saved).filter(([, value]) => value !== undefined)
63
- return { ...DEFAULT_FORM, ...(Object.fromEntries(entries) as Partial<AdditionalSettingsForm>) }
64
- }
65
-
66
- function SectionCard({ title, description, right, children }: { title: string; description?: React.ReactNode; right?: React.ReactNode; children: React.ReactNode }) {
67
- return (
68
- <div className="rounded-xl border border-ui-border-base bg-ui-bg-base shadow-sm">
69
- <div className="flex items-start justify-between gap-4 border-b border-ui-border-base p-4">
70
- <div>
71
- <div className="text-base font-semibold text-ui-fg-base">{title}</div>
72
- {description ? <div className="mt-1 text-sm text-ui-fg-subtle">{description}</div> : null}
73
- </div>
74
- {right}
75
- </div>
76
- <div className="p-4">{children}</div>
77
- </div>
78
- )
79
- }
80
-
81
- function FieldRow({ label, hint, children }: { label: string; hint?: React.ReactNode; children: React.ReactNode }) {
82
- return (
83
- <div className="grid grid-cols-12 items-start gap-4 py-3">
84
- <div className="col-span-12 md:col-span-4">
85
- <div className="text-sm font-medium text-ui-fg-base">{label}</div>
86
- {hint ? <div className="mt-1 text-xs text-ui-fg-subtle">{hint}</div> : null}
87
- </div>
88
- <div className="col-span-12 md:col-span-8">{children}</div>
89
- </div>
90
- )
91
- }
92
-
93
- export default function AdditionalSettingsTab() {
94
- const [form, setForm] = useState<AdditionalSettingsForm>(() => ({ ...DEFAULT_FORM }))
95
- const [loading, setLoading] = useState(false)
96
- const [saving, setSaving] = useState(false)
97
- const [toast, setToast] = useState<{ type: "success" | "error"; message: string } | null>(null)
98
- const didInit = useRef(false)
99
-
100
- useEffect(() => {
101
- if (didInit.current) return
102
- didInit.current = true
103
- ;(async () => {
104
- try {
105
- setLoading(true)
106
- const json = await adminFetch<any>("/admin/paypal/settings")
107
- const payload = json?.data ?? json
108
- const saved = payload?.additional_settings
109
- if (saved && typeof saved === "object") setForm(mergeWithDefaults(saved))
110
- } catch {
111
- // use defaults
112
- } finally {
113
- setLoading(false)
114
- }
115
- })()
116
- }, [])
117
-
118
- async function onSave() {
119
- try {
120
- setSaving(true)
121
- setToast(null)
122
- const json = await adminFetch<any>("/admin/paypal/settings", { method: "POST", body: { additional_settings: form as unknown as Record<string, unknown> } })
123
- const payload = json?.data ?? json
124
- const saved = payload?.additional_settings
125
- if (saved && typeof saved === "object") setForm(mergeWithDefaults(saved))
126
- setToast({ type: "success", message: "Settings saved" })
127
- window.setTimeout(() => setToast(null), 2500)
128
- } catch (e: unknown) {
129
- setToast({ type: "error", message: e instanceof Error ? e.message : "Failed to save settings" })
130
- window.setTimeout(() => setToast(null), 3500)
131
- } finally {
132
- setSaving(false)
133
- }
134
- }
135
-
136
- return (
137
- <div className="p-6">
138
- <div className="flex flex-col gap-6">
139
- <div className="flex items-start justify-between gap-4">
140
- <div><h1 className="text-xl font-semibold text-ui-fg-base">PayPal Gateway By Easy Payment</h1></div>
141
- </div>
142
- <PayPalTabs />
143
- {toast ? (
144
- <div className="fixed right-6 top-6 z-50 rounded-md border border-ui-border-base bg-ui-bg-base px-4 py-3 text-sm shadow-lg" role="status" aria-live="polite">
145
- <span className={toast.type === "success" ? "text-ui-fg-base" : "text-ui-fg-error"}>{toast.message}</span>
146
- </div>
147
- ) : null}
148
- <SectionCard
149
- title="Additional Settings"
150
- description="These settings control checkout behavior and PayPal experience."
151
- right={(
152
- <div className="flex items-center gap-3">
153
- <button type="button" onClick={onSave} disabled={saving || loading} className="rounded-md bg-ui-button-neutral px-4 py-2 text-sm font-medium text-ui-fg-on-color shadow-sm hover:opacity-90 disabled:opacity-60">
154
- {saving ? "Saving..." : "Save settings"}
155
- </button>
156
- {loading ? <span className="text-sm text-ui-fg-subtle">Loading...</span> : null}
157
- </div>
158
- )}
159
- >
160
- <div className="divide-y divide-ui-border-base">
161
- <FieldRow label="Payment action">
162
- <select value={form.paymentAction} onChange={(e) => setForm((p) => ({ ...p, paymentAction: e.target.value as PaymentAction }))} className="w-full rounded-md border border-ui-border-base bg-ui-bg-base px-3 py-2 text-sm text-ui-fg-base outline-none focus:ring-2 focus:ring-ui-border-interactive">
163
- <option value="capture">Capture</option>
164
- <option value="authorize">Authorize</option>
165
- </select>
166
- </FieldRow>
167
- <FieldRow label="Brand Name">
168
- <input value={form.brandName} onChange={(e) => setForm((p) => ({ ...p, brandName: e.target.value }))} className="w-full rounded-md border border-ui-border-base bg-ui-bg-base px-3 py-2 text-sm text-ui-fg-base outline-none focus:ring-2 focus:ring-ui-border-interactive" placeholder="PayPal" />
169
- </FieldRow>
170
- <FieldRow label="Landing Page">
171
- <select value={form.landingPage} onChange={(e) => setForm((p) => ({ ...p, landingPage: e.target.value as LandingPage }))} className="w-full rounded-md border border-ui-border-base bg-ui-bg-base px-3 py-2 text-sm text-ui-fg-base outline-none focus:ring-2 focus:ring-ui-border-interactive">
172
- <option value="no_preference">No Preference</option>
173
- <option value="login">Login</option>
174
- <option value="billing">Billing</option>
175
- </select>
176
- </FieldRow>
177
- <FieldRow label="Instant Payments">
178
- <label className="inline-flex items-center gap-2">
179
- <input type="checkbox" checked={form.requireInstantPayment} onChange={(e) => setForm((p) => ({ ...p, requireInstantPayment: e.target.checked }))} className="h-4 w-4 rounded border-ui-border-base" />
180
- <span className="text-sm text-ui-fg-base">Require Instant Payment</span>
181
- </label>
182
- </FieldRow>
183
- <FieldRow label="Send Item Details" hint="Include all line item details in the payment request to PayPal so that they can be seen from the PayPal transaction details page.">
184
- <label className="inline-flex items-center gap-2">
185
- <input type="checkbox" checked={form.sendItemDetails} onChange={(e) => setForm((p) => ({ ...p, sendItemDetails: e.target.checked }))} className="h-4 w-4 rounded border-ui-border-base" />
186
- <span className="text-sm text-ui-fg-base">Send line item details to PayPal</span>
187
- </label>
188
- </FieldRow>
189
- <FieldRow label="Invoice prefix">
190
- <input value={form.invoicePrefix} onChange={(e) => setForm((p) => ({ ...p, invoicePrefix: e.target.value }))} className="w-full rounded-md border border-ui-border-base bg-ui-bg-base px-3 py-2 text-sm text-ui-fg-base outline-none focus:ring-2 focus:ring-ui-border-interactive" placeholder="WC-" />
191
- </FieldRow>
192
- <FieldRow label="Credit Card Statement Name">
193
- <input value={form.creditCardStatementName} onChange={(e) => setForm((p) => ({ ...p, creditCardStatementName: e.target.value }))} className="w-full rounded-md border border-ui-border-base bg-ui-bg-base px-3 py-2 text-sm text-ui-fg-base outline-none focus:ring-2 focus:ring-ui-border-interactive" placeholder="PayPal" />
194
- </FieldRow>
195
- </div>
196
- </SectionCard>
197
- </div>
198
- </div>
199
- )
200
- }
1
+ import React, { useEffect, useRef, useState } from "react"
2
+ import PayPalTabs from "../_components/Tabs"
3
+
4
+ type AdminFetchOptions = {
5
+ method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH"
6
+ body?: Record<string, unknown>
7
+ query?: Record<string, string>
8
+ }
9
+
10
+ async function adminFetch<T = unknown>(path: string, opts: AdminFetchOptions = {}): Promise<T> {
11
+ const { method = "GET", body, query } = opts
12
+ let url = path
13
+ if (query && Object.keys(query).length > 0) {
14
+ const params = new URLSearchParams(query)
15
+ url = `${path}?${params.toString()}`
16
+ }
17
+ const headers: Record<string, string> = { Accept: "application/json" }
18
+ if (body !== undefined) headers["Content-Type"] = "application/json"
19
+ if (typeof window !== "undefined") {
20
+ const token = (window as any).__medusa__?.token
21
+ if (token) headers["Authorization"] = `Bearer ${token}`
22
+ }
23
+ const res = await fetch(url, {
24
+ method, headers, credentials: "include",
25
+ body: body !== undefined ? JSON.stringify(body) : undefined,
26
+ })
27
+ const text = await res.text().catch(() => "")
28
+ if (!res.ok) {
29
+ if (res.status === 401) throw new Error("Unauthorized (401) - session may have expired. Please reload and log in again.")
30
+ if (res.status === 403) throw new Error("Forbidden (403) - you do not have permission to perform this action.")
31
+ throw new Error(text || `Request failed with status ${res.status}`)
32
+ }
33
+ if (!text) return {} as T
34
+ try { return JSON.parse(text) as T } catch { return {} as T }
35
+ }
36
+
37
+ type PaymentAction = "capture" | "authorize"
38
+ type LandingPage = "no_preference" | "login" | "billing"
39
+
40
+ type AdditionalSettingsForm = {
41
+ paymentAction: PaymentAction
42
+ brandName: string
43
+ landingPage: LandingPage
44
+ requireInstantPayment: boolean
45
+ sendItemDetails: boolean
46
+ invoicePrefix: string
47
+ creditCardStatementName: string
48
+ }
49
+
50
+ const DEFAULT_FORM: AdditionalSettingsForm = {
51
+ paymentAction: "capture",
52
+ brandName: "PayPal",
53
+ landingPage: "no_preference",
54
+ requireInstantPayment: false,
55
+ sendItemDetails: true,
56
+ invoicePrefix: "WC-",
57
+ creditCardStatementName: "PayPal",
58
+ }
59
+
60
+ function mergeWithDefaults(saved?: Partial<AdditionalSettingsForm> | null) {
61
+ if (!saved) return { ...DEFAULT_FORM }
62
+ const entries = Object.entries(saved).filter(([, value]) => value !== undefined)
63
+ return { ...DEFAULT_FORM, ...(Object.fromEntries(entries) as Partial<AdditionalSettingsForm>) }
64
+ }
65
+
66
+ function SectionCard({ title, description, right, children }: { title: string; description?: React.ReactNode; right?: React.ReactNode; children: React.ReactNode }) {
67
+ return (
68
+ <div className="rounded-xl border border-ui-border-base bg-ui-bg-base shadow-sm">
69
+ <div className="flex items-start justify-between gap-4 border-b border-ui-border-base p-4">
70
+ <div>
71
+ <div className="text-base font-semibold text-ui-fg-base">{title}</div>
72
+ {description ? <div className="mt-1 text-sm text-ui-fg-subtle">{description}</div> : null}
73
+ </div>
74
+ {right}
75
+ </div>
76
+ <div className="p-4">{children}</div>
77
+ </div>
78
+ )
79
+ }
80
+
81
+ function FieldRow({ label, hint, children }: { label: string; hint?: React.ReactNode; children: React.ReactNode }) {
82
+ return (
83
+ <div className="grid grid-cols-12 items-start gap-4 py-3">
84
+ <div className="col-span-12 md:col-span-4">
85
+ <div className="text-sm font-medium text-ui-fg-base">{label}</div>
86
+ {hint ? <div className="mt-1 text-xs text-ui-fg-subtle">{hint}</div> : null}
87
+ </div>
88
+ <div className="col-span-12 md:col-span-8">{children}</div>
89
+ </div>
90
+ )
91
+ }
92
+
93
+ export default function AdditionalSettingsTab() {
94
+ const [form, setForm] = useState<AdditionalSettingsForm>(() => ({ ...DEFAULT_FORM }))
95
+ const [loading, setLoading] = useState(false)
96
+ const [saving, setSaving] = useState(false)
97
+ const [toast, setToast] = useState<{ type: "success" | "error"; message: string } | null>(null)
98
+ const didInit = useRef(false)
99
+
100
+ useEffect(() => {
101
+ if (didInit.current) return
102
+ didInit.current = true
103
+ ;(async () => {
104
+ try {
105
+ setLoading(true)
106
+ const json = await adminFetch<any>("/admin/paypal/settings")
107
+ const payload = json?.data ?? json
108
+ const saved = payload?.additional_settings
109
+ if (saved && typeof saved === "object") setForm(mergeWithDefaults(saved))
110
+ } catch {
111
+ // use defaults
112
+ } finally {
113
+ setLoading(false)
114
+ }
115
+ })()
116
+ }, [])
117
+
118
+ async function onSave() {
119
+ try {
120
+ setSaving(true)
121
+ setToast(null)
122
+ const json = await adminFetch<any>("/admin/paypal/settings", { method: "POST", body: { additional_settings: form as unknown as Record<string, unknown> } })
123
+ const payload = json?.data ?? json
124
+ const saved = payload?.additional_settings
125
+ if (saved && typeof saved === "object") setForm(mergeWithDefaults(saved))
126
+ setToast({ type: "success", message: "Settings saved" })
127
+ window.setTimeout(() => setToast(null), 2500)
128
+ } catch (e: unknown) {
129
+ setToast({ type: "error", message: e instanceof Error ? e.message : "Failed to save settings" })
130
+ window.setTimeout(() => setToast(null), 3500)
131
+ } finally {
132
+ setSaving(false)
133
+ }
134
+ }
135
+
136
+ return (
137
+ <div className="p-6">
138
+ <div className="flex flex-col gap-6">
139
+ <div className="flex items-start justify-between gap-4">
140
+ <div><h1 className="text-xl font-semibold text-ui-fg-base">PayPal Gateway By Easy Payment</h1></div>
141
+ </div>
142
+ <PayPalTabs />
143
+ {toast ? (
144
+ <div className="fixed right-6 top-6 z-50 rounded-md border border-ui-border-base bg-ui-bg-base px-4 py-3 text-sm shadow-lg" role="status" aria-live="polite">
145
+ <span className={toast.type === "success" ? "text-ui-fg-base" : "text-ui-fg-error"}>{toast.message}</span>
146
+ </div>
147
+ ) : null}
148
+ <SectionCard
149
+ title="Additional Settings"
150
+ description="These settings control checkout behavior and PayPal experience."
151
+ right={(
152
+ <div className="flex items-center gap-3">
153
+ <button type="button" onClick={onSave} disabled={saving || loading} className="transition-fg relative inline-flex w-fit items-center justify-center overflow-hidden rounded-md outline-none shadow-buttons-neutral text-ui-fg-base bg-ui-button-neutral after:transition-fg after:absolute after:inset-0 after:content-[''] after:button-neutral-gradient hover:bg-ui-button-neutral-hover hover:after:button-neutral-hover-gradient active:bg-ui-button-neutral-pressed active:after:button-neutral-pressed-gradient focus-visible:shadow-buttons-neutral-focus disabled:bg-ui-bg-disabled disabled:border-ui-border-base disabled:text-ui-fg-disabled disabled:shadow-buttons-neutral disabled:after:hidden txt-compact-small-plus px-3 py-1.5">
154
+ {saving ? "Saving..." : "Save settings"}
155
+ </button>
156
+ {loading ? <span className="text-sm text-ui-fg-subtle">Loading...</span> : null}
157
+ </div>
158
+ )}
159
+ >
160
+ <div className="divide-y divide-ui-border-base">
161
+ <FieldRow label="Payment action">
162
+ <select value={form.paymentAction} onChange={(e) => setForm((p) => ({ ...p, paymentAction: e.target.value as PaymentAction }))} className="w-full rounded-md border border-ui-border-base bg-ui-bg-base px-3 py-2 text-sm text-ui-fg-base outline-none focus:ring-2 focus:ring-ui-border-interactive">
163
+ <option value="capture">Capture</option>
164
+ <option value="authorize">Authorize</option>
165
+ </select>
166
+ </FieldRow>
167
+ <FieldRow label="Brand Name">
168
+ <input value={form.brandName} onChange={(e) => setForm((p) => ({ ...p, brandName: e.target.value }))} className="w-full rounded-md border border-ui-border-base bg-ui-bg-base px-3 py-2 text-sm text-ui-fg-base outline-none focus:ring-2 focus:ring-ui-border-interactive" placeholder="PayPal" />
169
+ </FieldRow>
170
+ <FieldRow label="Landing Page">
171
+ <select value={form.landingPage} onChange={(e) => setForm((p) => ({ ...p, landingPage: e.target.value as LandingPage }))} className="w-full rounded-md border border-ui-border-base bg-ui-bg-base px-3 py-2 text-sm text-ui-fg-base outline-none focus:ring-2 focus:ring-ui-border-interactive">
172
+ <option value="no_preference">No Preference</option>
173
+ <option value="login">Login</option>
174
+ <option value="billing">Billing</option>
175
+ </select>
176
+ </FieldRow>
177
+ <FieldRow label="Instant Payments">
178
+ <label className="inline-flex items-center gap-2">
179
+ <input type="checkbox" checked={form.requireInstantPayment} onChange={(e) => setForm((p) => ({ ...p, requireInstantPayment: e.target.checked }))} className="h-4 w-4 rounded border-ui-border-base" />
180
+ <span className="text-sm text-ui-fg-base">Require Instant Payment</span>
181
+ </label>
182
+ </FieldRow>
183
+ <FieldRow label="Send Item Details" hint="Include all line item details in the payment request to PayPal so that they can be seen from the PayPal transaction details page.">
184
+ <label className="inline-flex items-center gap-2">
185
+ <input type="checkbox" checked={form.sendItemDetails} onChange={(e) => setForm((p) => ({ ...p, sendItemDetails: e.target.checked }))} className="h-4 w-4 rounded border-ui-border-base" />
186
+ <span className="text-sm text-ui-fg-base">Send line item details to PayPal</span>
187
+ </label>
188
+ </FieldRow>
189
+ <FieldRow label="Invoice prefix">
190
+ <input value={form.invoicePrefix} onChange={(e) => setForm((p) => ({ ...p, invoicePrefix: e.target.value }))} className="w-full rounded-md border border-ui-border-base bg-ui-bg-base px-3 py-2 text-sm text-ui-fg-base outline-none focus:ring-2 focus:ring-ui-border-interactive" placeholder="WC-" />
191
+ </FieldRow>
192
+ <FieldRow label="Credit Card Statement Name">
193
+ <input value={form.creditCardStatementName} onChange={(e) => setForm((p) => ({ ...p, creditCardStatementName: e.target.value }))} className="w-full rounded-md border border-ui-border-base bg-ui-bg-base px-3 py-2 text-sm text-ui-fg-base outline-none focus:ring-2 focus:ring-ui-border-interactive" placeholder="PayPal" />
194
+ </FieldRow>
195
+ </div>
196
+ </SectionCard>
197
+ </div>
198
+ </div>
199
+ )
200
+ }