@mavunta/react 1.1.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Chainwaka Technologies
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # @mavunta/react
2
+
3
+ React components and hooks for **Mavunta Pay** checkout. Wraps [`@mavunta/checkout-js`](https://www.npmjs.com/package/@mavunta/checkout-js) (publishable key, browser only); your backend creates the payment intent with a secret key via [`@mavunta/sdk`](https://www.npmjs.com/package/@mavunta/sdk).
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install @mavunta/react react
9
+ ```
10
+
11
+ ## Checkout button
12
+
13
+ ```tsx
14
+ import { MavuntaCheckoutButton } from '@mavunta/react'
15
+
16
+ export function PayButton({ paymentIntentId }: { paymentIntentId: string }) {
17
+ return (
18
+ <MavuntaCheckoutButton
19
+ publicKey="cwk_test_pk_…"
20
+ paymentIntentId={paymentIntentId}
21
+ onError={(e) => console.error(e)}
22
+ >
23
+ Pay with Mavunta
24
+ </MavuntaCheckoutButton>
25
+ )
26
+ }
27
+ ```
28
+
29
+ Success is confirmed server-side via webhooks after the redirect, so there is no `onSuccess` on the button.
30
+
31
+ ## Hooks
32
+
33
+ ```tsx
34
+ import { useMavuntaCheckout, useMavuntaPaymentStatus } from '@mavunta/react'
35
+
36
+ // imperative checkout
37
+ const { redirectToCheckout, loading, error } = useMavuntaCheckout('cwk_test_pk_…')
38
+
39
+ // poll a payment's status inline (e.g. a "waiting for payment" screen)
40
+ const { status, intent } = useMavuntaPaymentStatus('cwk_test_pk_…', paymentIntentId)
41
+ ```
42
+
43
+ ## Security
44
+
45
+ Use your **publishable** key only. A publishable key can read a single payment intent and nothing else; keep secret keys on your server.
46
+
47
+ ## License
48
+
49
+ MIT © Chainwaka Technologies. Not affiliated with CoinW or any similarly named exchange.
package/dist/index.cjs ADDED
@@ -0,0 +1,98 @@
1
+ 'use strict';
2
+
3
+ var react = require('react');
4
+ var checkoutJs = require('@mavunta/checkout-js');
5
+ var jsxRuntime = require('react/jsx-runtime');
6
+
7
+ // src/useMavuntaCheckout.ts
8
+ function useMavuntaCheckout(publicKey, baseUrl) {
9
+ const [loading, setLoading] = react.useState(false);
10
+ const [error, setError] = react.useState(null);
11
+ const clientRef = react.useRef(null);
12
+ const getClient = react.useCallback(() => {
13
+ const cached = clientRef.current;
14
+ if (cached && cached.key === publicKey && cached.base === baseUrl) return cached.promise;
15
+ const promise = checkoutJs.loadMavunta(publicKey, { baseUrl });
16
+ clientRef.current = { key: publicKey, base: baseUrl, promise };
17
+ return promise;
18
+ }, [publicKey, baseUrl]);
19
+ const retrievePaymentIntent = react.useCallback(
20
+ async (id) => {
21
+ const client = await getClient();
22
+ return client.retrievePaymentIntent(id);
23
+ },
24
+ [getClient]
25
+ );
26
+ const redirectToCheckout = react.useCallback(
27
+ async (paymentIntentId) => {
28
+ setLoading(true);
29
+ setError(null);
30
+ try {
31
+ const client = await getClient();
32
+ await client.redirectToCheckout({ paymentIntentId });
33
+ } catch (err) {
34
+ const e = err instanceof Error ? err : new Error(String(err));
35
+ setError(e);
36
+ setLoading(false);
37
+ throw e;
38
+ }
39
+ },
40
+ [getClient]
41
+ );
42
+ return { retrievePaymentIntent, redirectToCheckout, loading, error };
43
+ }
44
+ function MavuntaCheckoutButton({
45
+ publicKey,
46
+ paymentIntentId,
47
+ baseUrl,
48
+ onError,
49
+ className,
50
+ disabled,
51
+ children
52
+ }) {
53
+ const { redirectToCheckout, loading } = useMavuntaCheckout(publicKey, baseUrl);
54
+ return /* @__PURE__ */ jsxRuntime.jsx(
55
+ "button",
56
+ {
57
+ type: "button",
58
+ className,
59
+ disabled: disabled || loading,
60
+ onClick: () => {
61
+ void redirectToCheckout(paymentIntentId).catch((err) => {
62
+ onError?.(err instanceof Error ? err : new Error(String(err)));
63
+ });
64
+ },
65
+ children: children ?? "Pay with Mavunta"
66
+ }
67
+ );
68
+ }
69
+ function useMavuntaPaymentStatus(publicKey, paymentIntentId, options = {}) {
70
+ const [intent, setIntent] = react.useState(null);
71
+ const [error, setError] = react.useState(null);
72
+ const { baseUrl, intervalMs } = options;
73
+ react.useEffect(() => {
74
+ if (!paymentIntentId) return;
75
+ let active = true;
76
+ let stop = () => {
77
+ };
78
+ checkoutJs.loadMavunta(publicKey, { baseUrl }).then((client) => {
79
+ if (!active) return;
80
+ stop = client.onStatus(paymentIntentId, (next) => setIntent(next), { intervalMs });
81
+ }).catch((err) => setError(err instanceof Error ? err : new Error(String(err))));
82
+ return () => {
83
+ active = false;
84
+ stop();
85
+ };
86
+ }, [publicKey, paymentIntentId, baseUrl, intervalMs]);
87
+ return { intent, status: intent?.status ?? null, error };
88
+ }
89
+
90
+ Object.defineProperty(exports, "MavuntaCheckoutError", {
91
+ enumerable: true,
92
+ get: function () { return checkoutJs.MavuntaCheckoutError; }
93
+ });
94
+ exports.MavuntaCheckoutButton = MavuntaCheckoutButton;
95
+ exports.useMavuntaCheckout = useMavuntaCheckout;
96
+ exports.useMavuntaPaymentStatus = useMavuntaPaymentStatus;
97
+ //# sourceMappingURL=index.cjs.map
98
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/useMavuntaCheckout.ts","../src/MavuntaCheckoutButton.tsx","../src/useMavuntaPaymentStatus.ts"],"names":["useState","useRef","useCallback","loadMavunta","jsx","useEffect"],"mappings":";;;;;;;AAkBO,SAAS,kBAAA,CAAmB,WAAmB,OAAA,EAAsC;AAC1F,EAAA,MAAM,CAAC,OAAA,EAAS,UAAU,CAAA,GAAIA,eAAS,KAAK,CAAA;AAC5C,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAIA,eAAuB,IAAI,CAAA;AACrD,EAAA,MAAM,SAAA,GAAYC,aAAiF,IAAI,CAAA;AAEvG,EAAA,MAAM,SAAA,GAAYC,kBAAY,MAAgC;AAC5D,IAAA,MAAM,SAAS,SAAA,CAAU,OAAA;AACzB,IAAA,IAAI,MAAA,IAAU,OAAO,GAAA,KAAQ,SAAA,IAAa,OAAO,IAAA,KAAS,OAAA,SAAgB,MAAA,CAAO,OAAA;AACjF,IAAA,MAAM,OAAA,GAAUC,sBAAA,CAAY,SAAA,EAAW,EAAE,SAAS,CAAA;AAClD,IAAA,SAAA,CAAU,UAAU,EAAE,GAAA,EAAK,SAAA,EAAW,IAAA,EAAM,SAAS,OAAA,EAAQ;AAC7D,IAAA,OAAO,OAAA;AAAA,EACT,CAAA,EAAG,CAAC,SAAA,EAAW,OAAO,CAAC,CAAA;AAEvB,EAAA,MAAM,qBAAA,GAAwBD,iBAAA;AAAA,IAC5B,OAAO,EAAA,KAA2C;AAChD,MAAA,MAAM,MAAA,GAAS,MAAM,SAAA,EAAU;AAC/B,MAAA,OAAO,MAAA,CAAO,sBAAsB,EAAE,CAA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EAAE,MAAA,EAAQ,MAAA,EAAQ,MAAA,EAAQ,MAAA,IAAU,MAAM,KAAA,EAAM;AACzD","file":"index.cjs","sourcesContent":["import { useCallback, useRef, useState } from 'react'\nimport { loadMavunta, type MavuntaCheckout, type PaymentIntentView } from '@mavunta/checkout-js'\n\nexport interface UseMavuntaCheckout {\n /** Read the public view of a payment intent. */\n retrievePaymentIntent: (id: string) => Promise<PaymentIntentView>\n /** Send the customer to hosted checkout for an existing intent. */\n redirectToCheckout: (paymentIntentId: string) => Promise<void>\n /** True while a redirect is in flight. */\n loading: boolean\n /** The last error, if any. */\n error: Error | null\n}\n\n/**\n * Browser checkout for React. Loads a publishable-key client once and exposes\n * redirect + retrieve helpers with loading/error state.\n */\nexport function useMavuntaCheckout(publicKey: string, baseUrl?: string): UseMavuntaCheckout {\n const [loading, setLoading] = useState(false)\n const [error, setError] = useState<Error | null>(null)\n const clientRef = useRef<{ key: string; base?: string; promise: Promise<MavuntaCheckout> } | null>(null)\n\n const getClient = useCallback((): Promise<MavuntaCheckout> => {\n const cached = clientRef.current\n if (cached && cached.key === publicKey && cached.base === baseUrl) return cached.promise\n const promise = loadMavunta(publicKey, { baseUrl })\n clientRef.current = { key: publicKey, base: baseUrl, promise }\n return promise\n }, [publicKey, baseUrl])\n\n const retrievePaymentIntent = useCallback(\n async (id: string): Promise<PaymentIntentView> => {\n const client = await getClient()\n return client.retrievePaymentIntent(id)\n },\n [getClient],\n )\n\n const redirectToCheckout = useCallback(\n async (paymentIntentId: string): Promise<void> => {\n setLoading(true)\n setError(null)\n try {\n const client = await getClient()\n await client.redirectToCheckout({ paymentIntentId })\n } catch (err) {\n const e = err instanceof Error ? err : new Error(String(err))\n setError(e)\n setLoading(false)\n throw e\n }\n },\n [getClient],\n )\n\n return { retrievePaymentIntent, redirectToCheckout, loading, error }\n}\n","import type { ReactNode } from 'react'\nimport { useMavuntaCheckout } from './useMavuntaCheckout.js'\n\nexport interface MavuntaCheckoutButtonProps {\n /** Your publishable key (`cwk_…_pk_…`). */\n publicKey: string\n /** The payment intent your backend created (with a secret key). */\n paymentIntentId: string\n /** Override the API base URL. */\n baseUrl?: string\n /** Called if starting checkout fails (e.g. the intent is gone or expired). */\n onError?: (error: Error) => void\n className?: string\n disabled?: boolean\n /** Button label (default: \"Pay with Mavunta\"). */\n children?: ReactNode\n}\n\n/**\n * A button that sends the customer to Mavunta hosted checkout. Success is\n * confirmed server-side via webhooks after the redirect, so there is no\n * onSuccess here; use {@link useMavuntaPaymentStatus} for an inline status.\n */\nexport function MavuntaCheckoutButton({\n publicKey,\n paymentIntentId,\n baseUrl,\n onError,\n className,\n disabled,\n children,\n}: MavuntaCheckoutButtonProps) {\n const { redirectToCheckout, loading } = useMavuntaCheckout(publicKey, baseUrl)\n\n return (\n <button\n type=\"button\"\n className={className}\n disabled={disabled || loading}\n onClick={() => {\n void redirectToCheckout(paymentIntentId).catch((err) => {\n onError?.(err instanceof Error ? err : new Error(String(err)))\n })\n }}\n >\n {children ?? 'Pay with Mavunta'}\n </button>\n )\n}\n","import { useEffect, useState } from 'react'\nimport { loadMavunta, type PaymentIntentView } from '@mavunta/checkout-js'\n\nexport interface UseMavuntaPaymentStatus {\n intent: PaymentIntentView | null\n status: string | null\n error: Error | null\n}\n\n/**\n * Poll a payment intent's status while `paymentIntentId` is set, updating on\n * each change and stopping at a terminal status. Useful for an inline \"waiting\n * for payment\" view after redirecting away and back, or for a polled receipt.\n */\nexport function useMavuntaPaymentStatus(\n publicKey: string,\n paymentIntentId: string | null,\n options: { baseUrl?: string; intervalMs?: number } = {},\n): UseMavuntaPaymentStatus {\n const [intent, setIntent] = useState<PaymentIntentView | null>(null)\n const [error, setError] = useState<Error | null>(null)\n const { baseUrl, intervalMs } = options\n\n useEffect(() => {\n if (!paymentIntentId) return\n let active = true\n let stop: () => void = () => {}\n loadMavunta(publicKey, { baseUrl })\n .then((client) => {\n if (!active) return\n stop = client.onStatus(paymentIntentId, (next) => setIntent(next), { intervalMs })\n })\n .catch((err) => setError(err instanceof Error ? err : new Error(String(err))))\n return () => {\n active = false\n stop()\n }\n }, [publicKey, paymentIntentId, baseUrl, intervalMs])\n\n return { intent, status: intent?.status ?? null, error }\n}\n"]}
@@ -0,0 +1,58 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+ import { PaymentIntentView } from '@mavunta/checkout-js';
4
+ export { CheckoutOptions, MavuntaCheckoutError, PaymentIntentView } from '@mavunta/checkout-js';
5
+
6
+ interface MavuntaCheckoutButtonProps {
7
+ /** Your publishable key (`cwk_…_pk_…`). */
8
+ publicKey: string;
9
+ /** The payment intent your backend created (with a secret key). */
10
+ paymentIntentId: string;
11
+ /** Override the API base URL. */
12
+ baseUrl?: string;
13
+ /** Called if starting checkout fails (e.g. the intent is gone or expired). */
14
+ onError?: (error: Error) => void;
15
+ className?: string;
16
+ disabled?: boolean;
17
+ /** Button label (default: "Pay with Mavunta"). */
18
+ children?: ReactNode;
19
+ }
20
+ /**
21
+ * A button that sends the customer to Mavunta hosted checkout. Success is
22
+ * confirmed server-side via webhooks after the redirect, so there is no
23
+ * onSuccess here; use {@link useMavuntaPaymentStatus} for an inline status.
24
+ */
25
+ declare function MavuntaCheckoutButton({ publicKey, paymentIntentId, baseUrl, onError, className, disabled, children, }: MavuntaCheckoutButtonProps): react.JSX.Element;
26
+
27
+ interface UseMavuntaCheckout {
28
+ /** Read the public view of a payment intent. */
29
+ retrievePaymentIntent: (id: string) => Promise<PaymentIntentView>;
30
+ /** Send the customer to hosted checkout for an existing intent. */
31
+ redirectToCheckout: (paymentIntentId: string) => Promise<void>;
32
+ /** True while a redirect is in flight. */
33
+ loading: boolean;
34
+ /** The last error, if any. */
35
+ error: Error | null;
36
+ }
37
+ /**
38
+ * Browser checkout for React. Loads a publishable-key client once and exposes
39
+ * redirect + retrieve helpers with loading/error state.
40
+ */
41
+ declare function useMavuntaCheckout(publicKey: string, baseUrl?: string): UseMavuntaCheckout;
42
+
43
+ interface UseMavuntaPaymentStatus {
44
+ intent: PaymentIntentView | null;
45
+ status: string | null;
46
+ error: Error | null;
47
+ }
48
+ /**
49
+ * Poll a payment intent's status while `paymentIntentId` is set, updating on
50
+ * each change and stopping at a terminal status. Useful for an inline "waiting
51
+ * for payment" view after redirecting away and back, or for a polled receipt.
52
+ */
53
+ declare function useMavuntaPaymentStatus(publicKey: string, paymentIntentId: string | null, options?: {
54
+ baseUrl?: string;
55
+ intervalMs?: number;
56
+ }): UseMavuntaPaymentStatus;
57
+
58
+ export { MavuntaCheckoutButton, type MavuntaCheckoutButtonProps, type UseMavuntaCheckout, type UseMavuntaPaymentStatus, useMavuntaCheckout, useMavuntaPaymentStatus };
@@ -0,0 +1,58 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+ import { PaymentIntentView } from '@mavunta/checkout-js';
4
+ export { CheckoutOptions, MavuntaCheckoutError, PaymentIntentView } from '@mavunta/checkout-js';
5
+
6
+ interface MavuntaCheckoutButtonProps {
7
+ /** Your publishable key (`cwk_…_pk_…`). */
8
+ publicKey: string;
9
+ /** The payment intent your backend created (with a secret key). */
10
+ paymentIntentId: string;
11
+ /** Override the API base URL. */
12
+ baseUrl?: string;
13
+ /** Called if starting checkout fails (e.g. the intent is gone or expired). */
14
+ onError?: (error: Error) => void;
15
+ className?: string;
16
+ disabled?: boolean;
17
+ /** Button label (default: "Pay with Mavunta"). */
18
+ children?: ReactNode;
19
+ }
20
+ /**
21
+ * A button that sends the customer to Mavunta hosted checkout. Success is
22
+ * confirmed server-side via webhooks after the redirect, so there is no
23
+ * onSuccess here; use {@link useMavuntaPaymentStatus} for an inline status.
24
+ */
25
+ declare function MavuntaCheckoutButton({ publicKey, paymentIntentId, baseUrl, onError, className, disabled, children, }: MavuntaCheckoutButtonProps): react.JSX.Element;
26
+
27
+ interface UseMavuntaCheckout {
28
+ /** Read the public view of a payment intent. */
29
+ retrievePaymentIntent: (id: string) => Promise<PaymentIntentView>;
30
+ /** Send the customer to hosted checkout for an existing intent. */
31
+ redirectToCheckout: (paymentIntentId: string) => Promise<void>;
32
+ /** True while a redirect is in flight. */
33
+ loading: boolean;
34
+ /** The last error, if any. */
35
+ error: Error | null;
36
+ }
37
+ /**
38
+ * Browser checkout for React. Loads a publishable-key client once and exposes
39
+ * redirect + retrieve helpers with loading/error state.
40
+ */
41
+ declare function useMavuntaCheckout(publicKey: string, baseUrl?: string): UseMavuntaCheckout;
42
+
43
+ interface UseMavuntaPaymentStatus {
44
+ intent: PaymentIntentView | null;
45
+ status: string | null;
46
+ error: Error | null;
47
+ }
48
+ /**
49
+ * Poll a payment intent's status while `paymentIntentId` is set, updating on
50
+ * each change and stopping at a terminal status. Useful for an inline "waiting
51
+ * for payment" view after redirecting away and back, or for a polled receipt.
52
+ */
53
+ declare function useMavuntaPaymentStatus(publicKey: string, paymentIntentId: string | null, options?: {
54
+ baseUrl?: string;
55
+ intervalMs?: number;
56
+ }): UseMavuntaPaymentStatus;
57
+
58
+ export { MavuntaCheckoutButton, type MavuntaCheckoutButtonProps, type UseMavuntaCheckout, type UseMavuntaPaymentStatus, useMavuntaCheckout, useMavuntaPaymentStatus };
package/dist/index.js ADDED
@@ -0,0 +1,91 @@
1
+ import { useState, useRef, useCallback, useEffect } from 'react';
2
+ import { loadMavunta } from '@mavunta/checkout-js';
3
+ export { MavuntaCheckoutError } from '@mavunta/checkout-js';
4
+ import { jsx } from 'react/jsx-runtime';
5
+
6
+ // src/useMavuntaCheckout.ts
7
+ function useMavuntaCheckout(publicKey, baseUrl) {
8
+ const [loading, setLoading] = useState(false);
9
+ const [error, setError] = useState(null);
10
+ const clientRef = useRef(null);
11
+ const getClient = useCallback(() => {
12
+ const cached = clientRef.current;
13
+ if (cached && cached.key === publicKey && cached.base === baseUrl) return cached.promise;
14
+ const promise = loadMavunta(publicKey, { baseUrl });
15
+ clientRef.current = { key: publicKey, base: baseUrl, promise };
16
+ return promise;
17
+ }, [publicKey, baseUrl]);
18
+ const retrievePaymentIntent = useCallback(
19
+ async (id) => {
20
+ const client = await getClient();
21
+ return client.retrievePaymentIntent(id);
22
+ },
23
+ [getClient]
24
+ );
25
+ const redirectToCheckout = useCallback(
26
+ async (paymentIntentId) => {
27
+ setLoading(true);
28
+ setError(null);
29
+ try {
30
+ const client = await getClient();
31
+ await client.redirectToCheckout({ paymentIntentId });
32
+ } catch (err) {
33
+ const e = err instanceof Error ? err : new Error(String(err));
34
+ setError(e);
35
+ setLoading(false);
36
+ throw e;
37
+ }
38
+ },
39
+ [getClient]
40
+ );
41
+ return { retrievePaymentIntent, redirectToCheckout, loading, error };
42
+ }
43
+ function MavuntaCheckoutButton({
44
+ publicKey,
45
+ paymentIntentId,
46
+ baseUrl,
47
+ onError,
48
+ className,
49
+ disabled,
50
+ children
51
+ }) {
52
+ const { redirectToCheckout, loading } = useMavuntaCheckout(publicKey, baseUrl);
53
+ return /* @__PURE__ */ jsx(
54
+ "button",
55
+ {
56
+ type: "button",
57
+ className,
58
+ disabled: disabled || loading,
59
+ onClick: () => {
60
+ void redirectToCheckout(paymentIntentId).catch((err) => {
61
+ onError?.(err instanceof Error ? err : new Error(String(err)));
62
+ });
63
+ },
64
+ children: children ?? "Pay with Mavunta"
65
+ }
66
+ );
67
+ }
68
+ function useMavuntaPaymentStatus(publicKey, paymentIntentId, options = {}) {
69
+ const [intent, setIntent] = useState(null);
70
+ const [error, setError] = useState(null);
71
+ const { baseUrl, intervalMs } = options;
72
+ useEffect(() => {
73
+ if (!paymentIntentId) return;
74
+ let active = true;
75
+ let stop = () => {
76
+ };
77
+ loadMavunta(publicKey, { baseUrl }).then((client) => {
78
+ if (!active) return;
79
+ stop = client.onStatus(paymentIntentId, (next) => setIntent(next), { intervalMs });
80
+ }).catch((err) => setError(err instanceof Error ? err : new Error(String(err))));
81
+ return () => {
82
+ active = false;
83
+ stop();
84
+ };
85
+ }, [publicKey, paymentIntentId, baseUrl, intervalMs]);
86
+ return { intent, status: intent?.status ?? null, error };
87
+ }
88
+
89
+ export { MavuntaCheckoutButton, useMavuntaCheckout, useMavuntaPaymentStatus };
90
+ //# sourceMappingURL=index.js.map
91
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/useMavuntaCheckout.ts","../src/MavuntaCheckoutButton.tsx","../src/useMavuntaPaymentStatus.ts"],"names":["useState","loadMavunta"],"mappings":";;;;;;AAkBO,SAAS,kBAAA,CAAmB,WAAmB,OAAA,EAAsC;AAC1F,EAAA,MAAM,CAAC,OAAA,EAAS,UAAU,CAAA,GAAI,SAAS,KAAK,CAAA;AAC5C,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAI,SAAuB,IAAI,CAAA;AACrD,EAAA,MAAM,SAAA,GAAY,OAAiF,IAAI,CAAA;AAEvG,EAAA,MAAM,SAAA,GAAY,YAAY,MAAgC;AAC5D,IAAA,MAAM,SAAS,SAAA,CAAU,OAAA;AACzB,IAAA,IAAI,MAAA,IAAU,OAAO,GAAA,KAAQ,SAAA,IAAa,OAAO,IAAA,KAAS,OAAA,SAAgB,MAAA,CAAO,OAAA;AACjF,IAAA,MAAM,OAAA,GAAU,WAAA,CAAY,SAAA,EAAW,EAAE,SAAS,CAAA;AAClD,IAAA,SAAA,CAAU,UAAU,EAAE,GAAA,EAAK,SAAA,EAAW,IAAA,EAAM,SAAS,OAAA,EAAQ;AAC7D,IAAA,OAAO,OAAA;AAAA,EACT,CAAA,EAAG,CAAC,SAAA,EAAW,OAAO,CAAC,CAAA;AAEvB,EAAA,MAAM,qBAAA,GAAwB,WAAA;AAAA,IAC5B,OAAO,EAAA,KAA2C;AAChD,MAAA,MAAM,MAAA,GAAS,MAAM,SAAA,EAAU;AAC/B,MAAA,OAAO,MAAA,CAAO,sBAAsB,EAAE,CAAA;AAAA,IACxC,CAAA;AAAA,IACA,CAAC,SAAS;AAAA,GACZ;AAEA,EAAA,MAAM,kBAAA,GAAqB,WAAA;AAAA,IACzB,OAAO,eAAA,KAA2C;AAChD,MAAA,UAAA,CAAW,IAAI,CAAA;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A,GACf;AAEJ;AClCO,SAAS,uBAAA,CACd,SAAA,EACA,eAAA,EACA,OAAA,GAAqD,EAAC,EAC7B;AACzB,EAAA,MAAM,CAAC,MAAA,EAAQ,SAAS,CAAA,GAAIA,SAAmC,IAAI,CAAA;AACnE,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAIA,SAAuB,IAAI,CAAA;AACrD,EAAA,MAAM,EAAE,OAAA,EAAS,UAAA,EAAW,GAAI,OAAA;AAEhC,EAAA,SAAA,CAAU,MAAM;AACd,IAAA,IAAI,CAAC,eAAA,EAAiB;AACtB,IAAA,IAAI,MAAA,GAAS,IAAA;AACb,IAAA,IAAI,OAAmB,MAAM;AAAA,IAAC,CAAA;AAC9B,IAAAC,WAAAA,CAAY,WAAW,EAAE,OAAA,EAAS,CAAA,CAC/B,IAAA,CAAK,CAAC,MAAA,KAAW;AAChB,MAAA,IAAI,CAAC,MAAA,EAAQ;AACb,MAAA,IAAA,GAAO,MAAA,CAAO,QAAA,CAAS,eAAA,EAAiB,CAAC,IAAA,KAAS,UAAU,IAAI,CAAA,EAAG,EAAE,UAAA,EAAY,CAAA;AAAA,IACnF,CAAC,CAAA,CACA,KAAA,CAAM,CAAC,QAAQ,QAAA,CAAS,GAAA,YAAe,KAAA,GAAQ,GAAA,GAAM,IAAI,KAAA,CAAM,MAAA,CAAO,GAAG,CAAC,CAAC,CAAC,CAAA;AAC/E,IAAA,OAAO,MAAM;AACX,MAAA,MAAA,GAAS,KAAA;AACT,MAAA,IAAA,EAAK;AAAA,IACP,CAAA;AAAA,EACF,GAAG,CAAC,SAAA,EAAW,eAAA,EAAiB,OAAA,EAAS,UAAU,CAAC,CAAA;AAEpD,EAAA,OAAO,EAAE,MAAA,EAAQ,MAAA,EAAQ,MAAA,EAAQ,MAAA,IAAU,MAAM,KAAA,EAAM;AACzD","file":"index.js","sourcesContent":["import { useCallback, useRef, useState } from 'react'\nimport { loadMavunta, type MavuntaCheckout, type PaymentIntentView } from '@mavunta/checkout-js'\n\nexport interface UseMavuntaCheckout {\n /** Read the public view of a payment intent. */\n retrievePaymentIntent: (id: string) => Promise<PaymentIntentView>\n /** Send the customer to hosted checkout for an existing intent. */\n redirectToCheckout: (paymentIntentId: string) => Promise<void>\n /** True while a redirect is in flight. */\n loading: boolean\n /** The last error, if any. */\n error: Error | null\n}\n\n/**\n * Browser checkout for React. Loads a publishable-key client once and exposes\n * redirect + retrieve helpers with loading/error state.\n */\nexport function useMavuntaCheckout(publicKey: string, baseUrl?: string): UseMavuntaCheckout {\n const [loading, setLoading] = useState(false)\n const [error, setError] = useState<Error | null>(null)\n const clientRef = useRef<{ key: string; base?: string; promise: Promise<MavuntaCheckout> } | null>(null)\n\n const getClient = useCallback((): Promise<MavuntaCheckout> => {\n const cached = clientRef.current\n if (cached && cached.key === publicKey && cached.base === baseUrl) return cached.promise\n const promise = loadMavunta(publicKey, { baseUrl })\n clientRef.current = { key: publicKey, base: baseUrl, promise }\n return promise\n }, [publicKey, baseUrl])\n\n const retrievePaymentIntent = useCallback(\n async (id: string): Promise<PaymentIntentView> => {\n const client = await getClient()\n return client.retrievePaymentIntent(id)\n },\n [getClient],\n )\n\n const redirectToCheckout = useCallback(\n async (paymentIntentId: string): Promise<void> => {\n setLoading(true)\n setError(null)\n try {\n const client = await getClient()\n await client.redirectToCheckout({ paymentIntentId })\n } catch (err) {\n const e = err instanceof Error ? err : new Error(String(err))\n setError(e)\n setLoading(false)\n throw e\n }\n },\n [getClient],\n )\n\n return { retrievePaymentIntent, redirectToCheckout, loading, error }\n}\n","import type { ReactNode } from 'react'\nimport { useMavuntaCheckout } from './useMavuntaCheckout.js'\n\nexport interface MavuntaCheckoutButtonProps {\n /** Your publishable key (`cwk_…_pk_…`). */\n publicKey: string\n /** The payment intent your backend created (with a secret key). */\n paymentIntentId: string\n /** Override the API base URL. */\n baseUrl?: string\n /** Called if starting checkout fails (e.g. the intent is gone or expired). */\n onError?: (error: Error) => void\n className?: string\n disabled?: boolean\n /** Button label (default: \"Pay with Mavunta\"). */\n children?: ReactNode\n}\n\n/**\n * A button that sends the customer to Mavunta hosted checkout. Success is\n * confirmed server-side via webhooks after the redirect, so there is no\n * onSuccess here; use {@link useMavuntaPaymentStatus} for an inline status.\n */\nexport function MavuntaCheckoutButton({\n publicKey,\n paymentIntentId,\n baseUrl,\n onError,\n className,\n disabled,\n children,\n}: MavuntaCheckoutButtonProps) {\n const { redirectToCheckout, loading } = useMavuntaCheckout(publicKey, baseUrl)\n\n return (\n <button\n type=\"button\"\n className={className}\n disabled={disabled || loading}\n onClick={() => {\n void redirectToCheckout(paymentIntentId).catch((err) => {\n onError?.(err instanceof Error ? err : new Error(String(err)))\n })\n }}\n >\n {children ?? 'Pay with Mavunta'}\n </button>\n )\n}\n","import { useEffect, useState } from 'react'\nimport { loadMavunta, type PaymentIntentView } from '@mavunta/checkout-js'\n\nexport interface UseMavuntaPaymentStatus {\n intent: PaymentIntentView | null\n status: string | null\n error: Error | null\n}\n\n/**\n * Poll a payment intent's status while `paymentIntentId` is set, updating on\n * each change and stopping at a terminal status. Useful for an inline \"waiting\n * for payment\" view after redirecting away and back, or for a polled receipt.\n */\nexport function useMavuntaPaymentStatus(\n publicKey: string,\n paymentIntentId: string | null,\n options: { baseUrl?: string; intervalMs?: number } = {},\n): UseMavuntaPaymentStatus {\n const [intent, setIntent] = useState<PaymentIntentView | null>(null)\n const [error, setError] = useState<Error | null>(null)\n const { baseUrl, intervalMs } = options\n\n useEffect(() => {\n if (!paymentIntentId) return\n let active = true\n let stop: () => void = () => {}\n loadMavunta(publicKey, { baseUrl })\n .then((client) => {\n if (!active) return\n stop = client.onStatus(paymentIntentId, (next) => setIntent(next), { intervalMs })\n })\n .catch((err) => setError(err instanceof Error ? err : new Error(String(err))))\n return () => {\n active = false\n stop()\n }\n }, [publicKey, paymentIntentId, baseUrl, intervalMs])\n\n return { intent, status: intent?.status ?? null, error }\n}\n"]}
package/package.json ADDED
@@ -0,0 +1,70 @@
1
+ {
2
+ "name": "@mavunta/react",
3
+ "version": "1.1.1",
4
+ "description": "React components and hooks for Mavunta Pay checkout (M-Pesa, card, PayPal, crypto).",
5
+ "license": "MIT",
6
+ "author": "Chainwaka Technologies",
7
+ "homepage": "https://developers.mavunta.com",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/mavunta/mavunta-js.git",
11
+ "directory": "packages/react"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/mavunta/mavunta-js/issues"
15
+ },
16
+ "keywords": [
17
+ "mavunta",
18
+ "mavunta-pay",
19
+ "react",
20
+ "checkout",
21
+ "mpesa",
22
+ "kenya",
23
+ "payments",
24
+ "crypto"
25
+ ],
26
+ "type": "module",
27
+ "main": "./dist/index.cjs",
28
+ "module": "./dist/index.js",
29
+ "types": "./dist/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "types": "./dist/index.d.ts",
33
+ "import": "./dist/index.js",
34
+ "require": "./dist/index.cjs"
35
+ }
36
+ },
37
+ "files": [
38
+ "dist",
39
+ "README.md",
40
+ "LICENSE"
41
+ ],
42
+ "sideEffects": false,
43
+ "publishConfig": {
44
+ "access": "public"
45
+ },
46
+ "peerDependencies": {
47
+ "react": "^18.0.0 || ^19.0.0"
48
+ },
49
+ "dependencies": {
50
+ "@mavunta/checkout-js": "^1.1.1"
51
+ },
52
+ "devDependencies": {
53
+ "@testing-library/react": "^16.1.0",
54
+ "@types/react": "^18.3.0",
55
+ "@vitejs/plugin-react": "^4.3.0",
56
+ "jsdom": "^25.0.0",
57
+ "react": "^18.3.1",
58
+ "react-dom": "^18.3.1",
59
+ "rimraf": "^6.0.1",
60
+ "tsup": "^8.5.0",
61
+ "typescript": "^5.9.0",
62
+ "vitest": "^3.2.0"
63
+ },
64
+ "scripts": {
65
+ "clean": "rimraf dist",
66
+ "build": "tsup",
67
+ "typecheck": "tsc --noEmit",
68
+ "test": "vitest run"
69
+ }
70
+ }