@easypayment/medusa-paypal-ui 1.0.38 → 1.0.40
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/dist/index.cjs +300 -5
- 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 +296 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/PayPalPaymentSection.tsx +284 -0
- package/src/components/PayPalSmartButtons.tsx +45 -3
- package/src/hooks/usePayPalPaymentMethods.ts +129 -0
- package/src/index.ts +10 -8
package/dist/index.cjs
CHANGED
|
@@ -235,6 +235,44 @@ function PayPalSmartButtons(props) {
|
|
|
235
235
|
]
|
|
236
236
|
}
|
|
237
237
|
),
|
|
238
|
+
config.environment === "sandbox" && /* @__PURE__ */ jsxRuntime.jsxs(
|
|
239
|
+
"div",
|
|
240
|
+
{
|
|
241
|
+
style: {
|
|
242
|
+
display: "flex",
|
|
243
|
+
alignItems: "flex-start",
|
|
244
|
+
gap: 8,
|
|
245
|
+
padding: "10px 14px",
|
|
246
|
+
background: "#eff6ff",
|
|
247
|
+
border: "1px solid #bfdbfe",
|
|
248
|
+
borderRadius: 8,
|
|
249
|
+
fontSize: 13,
|
|
250
|
+
color: "#1e40af",
|
|
251
|
+
lineHeight: 1.5,
|
|
252
|
+
marginBottom: 10
|
|
253
|
+
},
|
|
254
|
+
children: [
|
|
255
|
+
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
256
|
+
"svg",
|
|
257
|
+
{
|
|
258
|
+
width: "16",
|
|
259
|
+
height: "16",
|
|
260
|
+
viewBox: "0 0 24 24",
|
|
261
|
+
fill: "none",
|
|
262
|
+
stroke: "#1e40af",
|
|
263
|
+
strokeWidth: "2",
|
|
264
|
+
strokeLinecap: "round",
|
|
265
|
+
style: { flexShrink: 0, marginTop: 1 },
|
|
266
|
+
children: [
|
|
267
|
+
/* @__PURE__ */ jsxRuntime.jsx("circle", { cx: "12", cy: "12", r: "10" }),
|
|
268
|
+
/* @__PURE__ */ jsxRuntime.jsx("path", { d: "M12 8v4M12 16h.01" })
|
|
269
|
+
]
|
|
270
|
+
}
|
|
271
|
+
),
|
|
272
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { children: "Sandbox mode \u2014 you will not be charged. Use your PayPal sandbox account to complete the payment." })
|
|
273
|
+
]
|
|
274
|
+
}
|
|
275
|
+
),
|
|
238
276
|
/* @__PURE__ */ jsxRuntime.jsx(
|
|
239
277
|
reactPaypalJs.PayPalButtons,
|
|
240
278
|
{
|
|
@@ -275,19 +313,26 @@ function PayPalSmartButtons(props) {
|
|
|
275
313
|
}
|
|
276
314
|
}
|
|
277
315
|
),
|
|
278
|
-
error ? /* @__PURE__ */ jsxRuntime.
|
|
316
|
+
error ? /* @__PURE__ */ jsxRuntime.jsxs(
|
|
279
317
|
"div",
|
|
280
318
|
{
|
|
281
319
|
style: {
|
|
282
320
|
marginTop: 10,
|
|
283
|
-
|
|
321
|
+
display: "flex",
|
|
322
|
+
alignItems: "flex-start",
|
|
323
|
+
gap: 8,
|
|
324
|
+
padding: "10px 14px",
|
|
284
325
|
background: "#fef2f2",
|
|
285
326
|
border: "1px solid #fecaca",
|
|
286
|
-
borderRadius:
|
|
327
|
+
borderRadius: 8,
|
|
287
328
|
fontSize: 13,
|
|
288
|
-
color: "#b91c1c"
|
|
329
|
+
color: "#b91c1c",
|
|
330
|
+
lineHeight: 1.5
|
|
289
331
|
},
|
|
290
|
-
children:
|
|
332
|
+
children: [
|
|
333
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { style: { flexShrink: 0, fontSize: 15 }, children: "\u26A0\uFE0F" }),
|
|
334
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { children: error })
|
|
335
|
+
]
|
|
291
336
|
}
|
|
292
337
|
) : null
|
|
293
338
|
] })
|
|
@@ -732,12 +777,262 @@ function MedusaNextPayPalAdapter(props) {
|
|
|
732
777
|
)
|
|
733
778
|
] }, selectedProviderId);
|
|
734
779
|
}
|
|
780
|
+
var PAYPAL_WALLET_PROVIDER_ID = "pp_paypal_paypal";
|
|
781
|
+
var PAYPAL_CARD_PROVIDER_ID = "pp_paypal_card_paypal_card";
|
|
782
|
+
var PAYPAL_PROVIDER_IDS = [
|
|
783
|
+
PAYPAL_WALLET_PROVIDER_ID,
|
|
784
|
+
PAYPAL_CARD_PROVIDER_ID
|
|
785
|
+
];
|
|
786
|
+
function isPayPalProviderId(id) {
|
|
787
|
+
if (!id) return false;
|
|
788
|
+
return PAYPAL_PROVIDER_IDS.includes(id);
|
|
789
|
+
}
|
|
790
|
+
var SPIN_STYLE4 = `@keyframes _pp_section_spin { to { transform: rotate(360deg) } }`;
|
|
791
|
+
function SessionInitCard() {
|
|
792
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
793
|
+
"div",
|
|
794
|
+
{
|
|
795
|
+
style: {
|
|
796
|
+
display: "flex",
|
|
797
|
+
alignItems: "center",
|
|
798
|
+
gap: 12,
|
|
799
|
+
padding: "14px 16px",
|
|
800
|
+
marginTop: 8,
|
|
801
|
+
background: "#f9fafb",
|
|
802
|
+
border: "1px solid #e5e7eb",
|
|
803
|
+
borderRadius: 10
|
|
804
|
+
},
|
|
805
|
+
children: [
|
|
806
|
+
/* @__PURE__ */ jsxRuntime.jsx("style", { children: SPIN_STYLE4 }),
|
|
807
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
808
|
+
"div",
|
|
809
|
+
{
|
|
810
|
+
style: {
|
|
811
|
+
width: 20,
|
|
812
|
+
height: 20,
|
|
813
|
+
borderRadius: "50%",
|
|
814
|
+
border: "2.5px solid #e5e7eb",
|
|
815
|
+
borderTopColor: "#0070ba",
|
|
816
|
+
animation: "_pp_section_spin .7s linear infinite",
|
|
817
|
+
flexShrink: 0
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
),
|
|
821
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { style: { fontSize: 13, fontWeight: 500, color: "#111827" }, children: "Setting up payment\u2026" })
|
|
822
|
+
]
|
|
823
|
+
}
|
|
824
|
+
);
|
|
825
|
+
}
|
|
826
|
+
function ConfigLoadingCard() {
|
|
827
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
828
|
+
"div",
|
|
829
|
+
{
|
|
830
|
+
style: {
|
|
831
|
+
display: "flex",
|
|
832
|
+
alignItems: "center",
|
|
833
|
+
gap: 12,
|
|
834
|
+
padding: "14px 16px",
|
|
835
|
+
background: "#f9fafb",
|
|
836
|
+
border: "1px solid #e5e7eb",
|
|
837
|
+
borderRadius: 10
|
|
838
|
+
},
|
|
839
|
+
children: [
|
|
840
|
+
/* @__PURE__ */ jsxRuntime.jsx("style", { children: SPIN_STYLE4 }),
|
|
841
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
842
|
+
"div",
|
|
843
|
+
{
|
|
844
|
+
style: {
|
|
845
|
+
width: 22,
|
|
846
|
+
height: 22,
|
|
847
|
+
borderRadius: "50%",
|
|
848
|
+
border: "2.5px solid #e5e7eb",
|
|
849
|
+
borderTopColor: "#0070ba",
|
|
850
|
+
animation: "_pp_section_spin .7s linear infinite",
|
|
851
|
+
flexShrink: 0
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
),
|
|
855
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
|
|
856
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { style: { fontSize: 13, fontWeight: 500, color: "#111827" }, children: "Connecting to PayPal\u2026" }),
|
|
857
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { style: { fontSize: 12, color: "#6b7280", marginTop: 2 }, children: "Setting up secure payment" })
|
|
858
|
+
] })
|
|
859
|
+
]
|
|
860
|
+
}
|
|
861
|
+
);
|
|
862
|
+
}
|
|
863
|
+
function ErrorCard({ message }) {
|
|
864
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
865
|
+
"div",
|
|
866
|
+
{
|
|
867
|
+
style: {
|
|
868
|
+
padding: "12px 16px",
|
|
869
|
+
background: "#fef2f2",
|
|
870
|
+
border: "1px solid #fecaca",
|
|
871
|
+
borderRadius: 10,
|
|
872
|
+
fontSize: 13,
|
|
873
|
+
color: "#b91c1c"
|
|
874
|
+
},
|
|
875
|
+
children: message
|
|
876
|
+
}
|
|
877
|
+
);
|
|
878
|
+
}
|
|
879
|
+
function PayPalPaymentSection({
|
|
880
|
+
cartId,
|
|
881
|
+
selectedProviderId,
|
|
882
|
+
baseUrl,
|
|
883
|
+
publishableApiKey,
|
|
884
|
+
sessionLoading = false,
|
|
885
|
+
onSuccess,
|
|
886
|
+
onError,
|
|
887
|
+
onPaid
|
|
888
|
+
}) {
|
|
889
|
+
const shouldRender = isPayPalProviderId(selectedProviderId);
|
|
890
|
+
const { config, loading, error } = usePayPalConfig({
|
|
891
|
+
baseUrl,
|
|
892
|
+
publishableApiKey,
|
|
893
|
+
cartId,
|
|
894
|
+
enabled: shouldRender
|
|
895
|
+
});
|
|
896
|
+
const handlePaid = react.useCallback(
|
|
897
|
+
(captureResult) => {
|
|
898
|
+
onPaid?.(captureResult);
|
|
899
|
+
onSuccess?.(cartId);
|
|
900
|
+
},
|
|
901
|
+
[cartId, onPaid, onSuccess]
|
|
902
|
+
);
|
|
903
|
+
if (!shouldRender) return null;
|
|
904
|
+
if (sessionLoading) return /* @__PURE__ */ jsxRuntime.jsx(SessionInitCard, {});
|
|
905
|
+
if (loading) return /* @__PURE__ */ jsxRuntime.jsx(ConfigLoadingCard, {});
|
|
906
|
+
if (error) return /* @__PURE__ */ jsxRuntime.jsx(ErrorCard, { message: error });
|
|
907
|
+
if (!config) return null;
|
|
908
|
+
if (config.paypal_enabled === false && selectedProviderId === PAYPAL_WALLET_PROVIDER_ID) {
|
|
909
|
+
return null;
|
|
910
|
+
}
|
|
911
|
+
const isCardProvider = selectedProviderId === PAYPAL_CARD_PROVIDER_ID;
|
|
912
|
+
if (isCardProvider && config.card_enabled === false) return null;
|
|
913
|
+
const disableFunding = Array.isArray(config.disable_buttons) ? config.disable_buttons.join(",") : void 0;
|
|
914
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { style: { display: "grid", gap: 12 }, children: [
|
|
915
|
+
/* @__PURE__ */ jsxRuntime.jsx(PayPalCurrencyNotice, { config }),
|
|
916
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
917
|
+
PayPalProvider,
|
|
918
|
+
{
|
|
919
|
+
config,
|
|
920
|
+
intent: config.intent === "authorize" ? "authorize" : "capture",
|
|
921
|
+
disableFunding,
|
|
922
|
+
children: isCardProvider ? /* @__PURE__ */ jsxRuntime.jsx(
|
|
923
|
+
PayPalAdvancedCard,
|
|
924
|
+
{
|
|
925
|
+
baseUrl,
|
|
926
|
+
publishableApiKey,
|
|
927
|
+
cartId,
|
|
928
|
+
config,
|
|
929
|
+
onPaid: handlePaid,
|
|
930
|
+
onError
|
|
931
|
+
}
|
|
932
|
+
) : /* @__PURE__ */ jsxRuntime.jsx(
|
|
933
|
+
PayPalSmartButtons,
|
|
934
|
+
{
|
|
935
|
+
baseUrl,
|
|
936
|
+
publishableApiKey,
|
|
937
|
+
cartId,
|
|
938
|
+
config,
|
|
939
|
+
onPaid: handlePaid,
|
|
940
|
+
onError
|
|
941
|
+
}
|
|
942
|
+
)
|
|
943
|
+
}
|
|
944
|
+
)
|
|
945
|
+
] }, selectedProviderId);
|
|
946
|
+
}
|
|
947
|
+
var _cache2 = /* @__PURE__ */ new Map();
|
|
948
|
+
var CACHE_TTL2 = 5 * 60 * 1e3;
|
|
949
|
+
function cacheKey2(baseUrl, cartId) {
|
|
950
|
+
return `ppm::${baseUrl}::${cartId ?? ""}`;
|
|
951
|
+
}
|
|
952
|
+
var DEFAULT_RESULT = {
|
|
953
|
+
paypalEnabled: true,
|
|
954
|
+
paypalTitle: "PayPal",
|
|
955
|
+
cardEnabled: true,
|
|
956
|
+
cardTitle: "Credit or Debit Card",
|
|
957
|
+
loading: false
|
|
958
|
+
};
|
|
959
|
+
function usePayPalPaymentMethods({
|
|
960
|
+
baseUrl,
|
|
961
|
+
publishableApiKey,
|
|
962
|
+
cartId,
|
|
963
|
+
enabled = true
|
|
964
|
+
}) {
|
|
965
|
+
const api = react.useMemo(
|
|
966
|
+
() => createPayPalStoreApi({ baseUrl, publishableApiKey }),
|
|
967
|
+
[baseUrl, publishableApiKey]
|
|
968
|
+
);
|
|
969
|
+
const key = cacheKey2(baseUrl, cartId);
|
|
970
|
+
const hit = _cache2.get(key);
|
|
971
|
+
const seed = hit && Date.now() - hit.at < CACHE_TTL2 ? hit.result : null;
|
|
972
|
+
const [result, setResult] = react.useState(seed ?? { ...DEFAULT_RESULT, loading: enabled });
|
|
973
|
+
const fetchIdRef = react.useRef(0);
|
|
974
|
+
react.useEffect(() => {
|
|
975
|
+
if (!enabled) {
|
|
976
|
+
setResult((prev) => ({ ...prev, loading: false }));
|
|
977
|
+
return;
|
|
978
|
+
}
|
|
979
|
+
const k = cacheKey2(baseUrl, cartId);
|
|
980
|
+
const cached = _cache2.get(k);
|
|
981
|
+
if (cached && Date.now() - cached.at < CACHE_TTL2) {
|
|
982
|
+
setResult(cached.result);
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
985
|
+
const id = ++fetchIdRef.current;
|
|
986
|
+
let mounted = true;
|
|
987
|
+
const controller = new AbortController();
|
|
988
|
+
setResult((prev) => ({ ...prev, loading: true }));
|
|
989
|
+
(async () => {
|
|
990
|
+
try {
|
|
991
|
+
const cfg = await api.getConfig(cartId, controller.signal);
|
|
992
|
+
if (!mounted || id !== fetchIdRef.current) return;
|
|
993
|
+
const next = {
|
|
994
|
+
paypalEnabled: cfg.paypal_enabled !== false,
|
|
995
|
+
paypalTitle: typeof cfg.paypal_title === "string" && cfg.paypal_title ? cfg.paypal_title : "PayPal",
|
|
996
|
+
cardEnabled: cfg.card_enabled !== false,
|
|
997
|
+
cardTitle: typeof cfg.card_title === "string" && cfg.card_title ? cfg.card_title : "Credit or Debit Card",
|
|
998
|
+
loading: false
|
|
999
|
+
};
|
|
1000
|
+
_cache2.set(k, { result: next, at: Date.now() });
|
|
1001
|
+
setResult(next);
|
|
1002
|
+
} catch (e) {
|
|
1003
|
+
if (e?.name === "AbortError") return;
|
|
1004
|
+
if (!mounted || id !== fetchIdRef.current) return;
|
|
1005
|
+
if (e?.message?.includes("403") || e?.message?.includes("Forbidden")) {
|
|
1006
|
+
const disabled = {
|
|
1007
|
+
...DEFAULT_RESULT,
|
|
1008
|
+
paypalEnabled: false,
|
|
1009
|
+
cardEnabled: false,
|
|
1010
|
+
loading: false
|
|
1011
|
+
};
|
|
1012
|
+
setResult(disabled);
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
1015
|
+
setResult({ ...DEFAULT_RESULT, loading: false });
|
|
1016
|
+
}
|
|
1017
|
+
})();
|
|
1018
|
+
return () => {
|
|
1019
|
+
mounted = false;
|
|
1020
|
+
controller.abort();
|
|
1021
|
+
};
|
|
1022
|
+
}, [api, baseUrl, cartId, enabled]);
|
|
1023
|
+
return result;
|
|
1024
|
+
}
|
|
735
1025
|
|
|
736
1026
|
exports.MedusaNextPayPalAdapter = MedusaNextPayPalAdapter;
|
|
1027
|
+
exports.PAYPAL_CARD_PROVIDER_ID = PAYPAL_CARD_PROVIDER_ID;
|
|
1028
|
+
exports.PAYPAL_WALLET_PROVIDER_ID = PAYPAL_WALLET_PROVIDER_ID;
|
|
737
1029
|
exports.PayPalAdvancedCard = PayPalAdvancedCard;
|
|
738
1030
|
exports.PayPalCurrencyNotice = PayPalCurrencyNotice;
|
|
1031
|
+
exports.PayPalPaymentSection = PayPalPaymentSection;
|
|
739
1032
|
exports.PayPalProvider = PayPalProvider;
|
|
740
1033
|
exports.PayPalSmartButtons = PayPalSmartButtons;
|
|
741
1034
|
exports.createPayPalStoreApi = createPayPalStoreApi;
|
|
1035
|
+
exports.isPayPalProviderId = isPayPalProviderId;
|
|
742
1036
|
exports.usePayPalConfig = usePayPalConfig;
|
|
1037
|
+
exports.usePayPalPaymentMethods = usePayPalPaymentMethods;
|
|
743
1038
|
//# sourceMappingURL=index.cjs.map
|