@easypayment/medusa-paypal-ui 1.0.39 → 1.0.41
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/README.md +399 -288
- package/dist/index.cjs +250 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +70 -3
- package/dist/index.d.ts +70 -3
- package/dist/index.mjs +246 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/PayPalPaymentSection.tsx +284 -0
- package/src/hooks/usePayPalPaymentMethods.ts +129 -0
- package/src/index.ts +10 -8
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* usePayPalPaymentMethods
|
|
5
|
+
*
|
|
6
|
+
* Thin hook that fetches /store/paypal/config and returns the information
|
|
7
|
+
* storefronts need to filter and label PayPal payment methods in their
|
|
8
|
+
* RadioGroup — without having to handle the fetch, caching, or error
|
|
9
|
+
* handling themselves.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { useEffect, useMemo, useRef, useState } from "react"
|
|
13
|
+
import { createPayPalStoreApi } from "../client/paypal"
|
|
14
|
+
|
|
15
|
+
type Args = {
|
|
16
|
+
baseUrl: string
|
|
17
|
+
publishableApiKey?: string
|
|
18
|
+
cartId?: string
|
|
19
|
+
enabled?: boolean
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type Result = {
|
|
23
|
+
paypalEnabled: boolean
|
|
24
|
+
paypalTitle: string
|
|
25
|
+
cardEnabled: boolean
|
|
26
|
+
cardTitle: string
|
|
27
|
+
loading: boolean
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const _cache = new Map<string, { result: Result; at: number }>()
|
|
31
|
+
const CACHE_TTL = 5 * 60 * 1000
|
|
32
|
+
|
|
33
|
+
function cacheKey(baseUrl: string, cartId?: string) {
|
|
34
|
+
return `ppm::${baseUrl}::${cartId ?? ""}`
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const DEFAULT_RESULT: Result = {
|
|
38
|
+
paypalEnabled: true,
|
|
39
|
+
paypalTitle: "PayPal",
|
|
40
|
+
cardEnabled: true,
|
|
41
|
+
cardTitle: "Credit or Debit Card",
|
|
42
|
+
loading: false,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function usePayPalPaymentMethods({
|
|
46
|
+
baseUrl,
|
|
47
|
+
publishableApiKey,
|
|
48
|
+
cartId,
|
|
49
|
+
enabled = true,
|
|
50
|
+
}: Args): Result {
|
|
51
|
+
const api = useMemo(
|
|
52
|
+
() => createPayPalStoreApi({ baseUrl, publishableApiKey }),
|
|
53
|
+
[baseUrl, publishableApiKey]
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
const key = cacheKey(baseUrl, cartId)
|
|
57
|
+
const hit = _cache.get(key)
|
|
58
|
+
const seed = hit && Date.now() - hit.at < CACHE_TTL ? hit.result : null
|
|
59
|
+
|
|
60
|
+
const [result, setResult] = useState<Result>(seed ?? { ...DEFAULT_RESULT, loading: enabled })
|
|
61
|
+
const fetchIdRef = useRef(0)
|
|
62
|
+
|
|
63
|
+
useEffect(() => {
|
|
64
|
+
if (!enabled) {
|
|
65
|
+
setResult((prev) => ({ ...prev, loading: false }))
|
|
66
|
+
return
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const k = cacheKey(baseUrl, cartId)
|
|
70
|
+
const cached = _cache.get(k)
|
|
71
|
+
if (cached && Date.now() - cached.at < CACHE_TTL) {
|
|
72
|
+
setResult(cached.result)
|
|
73
|
+
return
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const id = ++fetchIdRef.current
|
|
77
|
+
let mounted = true
|
|
78
|
+
const controller = new AbortController()
|
|
79
|
+
|
|
80
|
+
setResult((prev) => ({ ...prev, loading: true }))
|
|
81
|
+
|
|
82
|
+
;(async () => {
|
|
83
|
+
try {
|
|
84
|
+
const cfg = await api.getConfig(cartId, controller.signal)
|
|
85
|
+
if (!mounted || id !== fetchIdRef.current) return
|
|
86
|
+
|
|
87
|
+
const next: Result = {
|
|
88
|
+
paypalEnabled: cfg.paypal_enabled !== false,
|
|
89
|
+
paypalTitle:
|
|
90
|
+
typeof cfg.paypal_title === "string" && cfg.paypal_title
|
|
91
|
+
? cfg.paypal_title
|
|
92
|
+
: "PayPal",
|
|
93
|
+
cardEnabled: cfg.card_enabled !== false,
|
|
94
|
+
cardTitle:
|
|
95
|
+
typeof cfg.card_title === "string" && cfg.card_title
|
|
96
|
+
? cfg.card_title
|
|
97
|
+
: "Credit or Debit Card",
|
|
98
|
+
loading: false,
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
_cache.set(k, { result: next, at: Date.now() })
|
|
102
|
+
setResult(next)
|
|
103
|
+
} catch (e: any) {
|
|
104
|
+
if (e?.name === "AbortError") return
|
|
105
|
+
if (!mounted || id !== fetchIdRef.current) return
|
|
106
|
+
|
|
107
|
+
if (e?.message?.includes("403") || e?.message?.includes("Forbidden")) {
|
|
108
|
+
const disabled: Result = {
|
|
109
|
+
...DEFAULT_RESULT,
|
|
110
|
+
paypalEnabled: false,
|
|
111
|
+
cardEnabled: false,
|
|
112
|
+
loading: false,
|
|
113
|
+
}
|
|
114
|
+
setResult(disabled)
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
setResult({ ...DEFAULT_RESULT, loading: false })
|
|
119
|
+
}
|
|
120
|
+
})()
|
|
121
|
+
|
|
122
|
+
return () => {
|
|
123
|
+
mounted = false
|
|
124
|
+
controller.abort()
|
|
125
|
+
}
|
|
126
|
+
}, [api, baseUrl, cartId, enabled])
|
|
127
|
+
|
|
128
|
+
return result
|
|
129
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
export * from "./client/types"
|
|
2
|
-
export * from "./client/paypal"
|
|
3
|
-
export * from "./hooks/usePayPalConfig"
|
|
4
|
-
export * from "./components/PayPalProvider"
|
|
5
|
-
export * from "./components/PayPalCurrencyNotice"
|
|
6
|
-
export * from "./components/PayPalSmartButtons"
|
|
7
|
-
export * from "./components/PayPalAdvancedCard"
|
|
8
|
-
export * from "./adapters/MedusaNextPayPalAdapter"
|
|
1
|
+
export * from "./client/types"
|
|
2
|
+
export * from "./client/paypal"
|
|
3
|
+
export * from "./hooks/usePayPalConfig"
|
|
4
|
+
export * from "./components/PayPalProvider"
|
|
5
|
+
export * from "./components/PayPalCurrencyNotice"
|
|
6
|
+
export * from "./components/PayPalSmartButtons"
|
|
7
|
+
export * from "./components/PayPalAdvancedCard"
|
|
8
|
+
export * from "./adapters/MedusaNextPayPalAdapter"
|
|
9
|
+
export * from "./components/PayPalPaymentSection"
|
|
10
|
+
export * from "./hooks/usePayPalPaymentMethods"
|