@bemony/payment-form-react 0.0.0-stage → 0.1.0

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 CHANGED
@@ -1,3 +1,55 @@
1
- # Temporary Holding Version
1
+ # @bemony/payment-form-react
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ React adapter for `@bemony/payment-form`. It uses the same hosted iframe and public `pay(input)`
4
+ contract; it does not make a payment request from the merchant application.
5
+
6
+ ## Install
7
+
8
+ ```bash
9
+ pnpm add @bemony/payment-form-react
10
+ ```
11
+
12
+ Use a ref when the checkout button is close to the form:
13
+
14
+ ```tsx
15
+ import { BemonyPaymentForm, type BemonyPaymentFormRef } from '@bemony/payment-form-react';
16
+ import { useRef } from 'react';
17
+
18
+ const ref = useRef<BemonyPaymentFormRef>(null);
19
+
20
+ <BemonyPaymentForm ref={ref} clientSecret={clientSecret} />;
21
+
22
+ await ref.current?.pay({
23
+ merchantOrderId: 'order_123',
24
+ currency: 'USD',
25
+ operation: 'SALE',
26
+ items: [{ description: 'Order', unitAmount: 1000, quantity: 1, type: 'unique' }]
27
+ });
28
+ ```
29
+
30
+ For a button or summary in another component, use the optional provider and hook:
31
+
32
+ ```tsx
33
+ import {
34
+ BemonyPaymentForm,
35
+ BemonyPaymentFormProvider,
36
+ useBemonyPaymentForm
37
+ } from '@bemony/payment-form-react';
38
+
39
+ function PayButton() {
40
+ const { ready, pay } = useBemonyPaymentForm();
41
+ return (
42
+ <button disabled={!ready} onClick={() => void pay(input)}>
43
+ Pay
44
+ </button>
45
+ );
46
+ }
47
+
48
+ <BemonyPaymentFormProvider clientSecret={clientSecret}>
49
+ <BemonyPaymentForm />
50
+ <PayButton />
51
+ </BemonyPaymentFormProvider>;
52
+ ```
53
+
54
+ Create `clientSecret` on the merchant server. Never expose a Bemony secret API key in React code
55
+ or a public environment variable.
@@ -0,0 +1,42 @@
1
+ import { type CardState, type PayInput, type PayOutcome, type PaymentFormAppearance, type PaymentFormCallbacks, type PaymentFormError, type PaymentFormState, type ThreeDSPresentation } from '@bemony/payment-form';
2
+ import { type ReactNode } from 'react';
3
+ export type BemonyPaymentFormRef = Readonly<{
4
+ pay(input: PayInput): Promise<PayOutcome>;
5
+ getState(): PaymentFormState;
6
+ destroy(): void;
7
+ }>;
8
+ type FormConfig = PaymentFormCallbacks & Readonly<{
9
+ clientSecret: string;
10
+ appearance?: PaymentFormAppearance;
11
+ threeDS?: Readonly<{
12
+ presentation?: ThreeDSPresentation;
13
+ }>;
14
+ onCardStateChange?: (state: CardState) => void;
15
+ }>;
16
+ export type BemonyPaymentFormProps = Partial<FormConfig> & Readonly<{
17
+ className?: string;
18
+ }>;
19
+ export type BemonyPaymentFormProviderProps = FormConfig & Readonly<{
20
+ children: ReactNode;
21
+ }>;
22
+ /** Optional React-only convenience layer over the single Web SDK instance. */
23
+ export declare function BemonyPaymentFormProvider({ children, ...config }: BemonyPaymentFormProviderProps): import("react").JSX.Element;
24
+ export declare function useBemonyPaymentForm(): Readonly<{
25
+ ready: boolean;
26
+ state: PaymentFormState;
27
+ error: PaymentFormError | null;
28
+ pay(input: PayInput): Promise<PayOutcome>;
29
+ }>;
30
+ /**
31
+ * Works standalone with a ref or as the mount point inside
32
+ * BemonyPaymentFormProvider. Both paths mount the same Web SDK instance.
33
+ */
34
+ export declare const BemonyPaymentForm: import("react").ForwardRefExoticComponent<Partial<FormConfig> & Readonly<{
35
+ className?: string;
36
+ }> & import("react").RefAttributes<Readonly<{
37
+ pay(input: PayInput): Promise<PayOutcome>;
38
+ getState(): PaymentFormState;
39
+ destroy(): void;
40
+ }>>>;
41
+ export {};
42
+ //# sourceMappingURL=payment-form.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"payment-form.d.ts","sourceRoot":"","sources":["../../src/components/payment-form.tsx"],"names":[],"mappings":"AAEA,OAAO,EAEL,KAAK,SAAS,EACd,KAAK,QAAQ,EACb,KAAK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AAO,CAAC,UAAU,CAAC,CAAC;CAC3C,CAAC,CAaD;AAED;;;GAGG;AACH,eAAO,MAAM,iBAAiB;gBAlDoD,MAAM;;eAb3E,QAAQ,GAAG,OAAO,CAAC,UAAU,CAAC;gBAC7B,gBAAgB;eACjB,IAAI;IAuLhB,CAAC"}
@@ -0,0 +1,175 @@
1
+ 'use client';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { BemonyPaymentForm as WebPaymentForm } from '@bemony/payment-form';
4
+ import { createContext, forwardRef, useContext, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react';
5
+ const PaymentFormContext = createContext(undefined);
6
+ const mountingState = { status: 'MOUNTING', ready: false, isSubmitting: false };
7
+ /** Optional React-only convenience layer over the single Web SDK instance. */
8
+ export function BemonyPaymentFormProvider({ children, ...config }) {
9
+ const [instance, register] = useState(undefined);
10
+ const [state, setState] = useState(mountingState);
11
+ const value = useMemo(() => ({ config, instance, state, register, setState }), [config, instance, state]);
12
+ return _jsx(PaymentFormContext.Provider, { value: value, children: children });
13
+ }
14
+ export function useBemonyPaymentForm() {
15
+ const context = useContext(PaymentFormContext);
16
+ if (context === undefined) {
17
+ throw new Error('useBemonyPaymentForm deve ser usado dentro de BemonyPaymentFormProvider.');
18
+ }
19
+ return {
20
+ ready: context.state.ready,
21
+ state: context.state,
22
+ error: context.state.error ?? null,
23
+ pay(input) {
24
+ return context.instance?.pay(input) ?? Promise.reject(notReady());
25
+ }
26
+ };
27
+ }
28
+ /**
29
+ * Works standalone with a ref or as the mount point inside
30
+ * BemonyPaymentFormProvider. Both paths mount the same Web SDK instance.
31
+ */
32
+ export const BemonyPaymentForm = forwardRef(function BemonyPaymentForm(props, ref) {
33
+ const context = useContext(PaymentFormContext);
34
+ const config = context?.config ?? completeConfig(props);
35
+ const register = context?.register;
36
+ const setProviderState = context?.setState;
37
+ const targetRef = useRef(null);
38
+ const instanceRef = useRef(undefined);
39
+ const stateRef = useRef(mountingState);
40
+ const latestConfig = useRef(config);
41
+ latestConfig.current = config;
42
+ useImperativeHandle(ref, () => ({
43
+ pay(input) {
44
+ return instanceRef.current?.pay(input) ?? Promise.reject(notReady());
45
+ },
46
+ getState() {
47
+ return instanceRef.current?.getState() ?? stateRef.current;
48
+ },
49
+ destroy() {
50
+ instanceRef.current?.destroy();
51
+ instanceRef.current = undefined;
52
+ register?.(undefined);
53
+ }
54
+ }), [register]);
55
+ useEffect(() => {
56
+ const target = targetRef.current;
57
+ if (target === null)
58
+ return;
59
+ const abort = new AbortController();
60
+ let disposed = false;
61
+ let instance;
62
+ const publish = (next) => {
63
+ stateRef.current = next;
64
+ setProviderState?.(next);
65
+ };
66
+ const mount = async () => {
67
+ try {
68
+ const mounted = await WebPaymentForm.mount({
69
+ target,
70
+ clientSecret: config.clientSecret,
71
+ ...(config.appearance === undefined ? {} : { appearance: config.appearance }),
72
+ ...(config.threeDS === undefined ? {} : { threeDS: config.threeDS }),
73
+ signal: abort.signal,
74
+ onReady() {
75
+ if (disposed)
76
+ return;
77
+ latestConfig.current.onReady?.();
78
+ publish({ ...stateRef.current, status: 'READY', ready: true, isSubmitting: false });
79
+ },
80
+ onCardStateChange(state) {
81
+ if (!disposed)
82
+ latestConfig.current.onCardStateChange?.(state);
83
+ },
84
+ onStatusChange(status) {
85
+ if (disposed)
86
+ return;
87
+ latestConfig.current.onStatusChange?.(status);
88
+ publish({
89
+ ...stateRef.current,
90
+ status,
91
+ ready: status !== 'MOUNTING' && status !== 'DESTROYED',
92
+ isSubmitting: [
93
+ 'TOKENIZING',
94
+ 'SUBMITTING',
95
+ 'AUTHENTICATING_3DS',
96
+ 'PROCESSING'
97
+ ].includes(status)
98
+ });
99
+ },
100
+ onSuccess(payment) {
101
+ if (disposed)
102
+ return;
103
+ latestConfig.current.onSuccess?.(payment);
104
+ publish({ status: payment.status, ready: true, isSubmitting: false, payment });
105
+ },
106
+ onError(error) {
107
+ if (disposed)
108
+ return;
109
+ latestConfig.current.onError?.(error);
110
+ publish({ ...stateRef.current, isSubmitting: false, error });
111
+ }
112
+ });
113
+ if (disposed) {
114
+ mounted.destroy();
115
+ return;
116
+ }
117
+ instance = mounted;
118
+ instanceRef.current = mounted;
119
+ register?.(mounted);
120
+ publish(mounted.getState());
121
+ }
122
+ catch (error) {
123
+ if (disposed)
124
+ return;
125
+ const publicError = mountError(error);
126
+ latestConfig.current.onError?.(publicError);
127
+ publish({ ...stateRef.current, isSubmitting: false, error: publicError });
128
+ }
129
+ };
130
+ // React Strict Mode runs a speculative effect setup/cleanup pair. Deferring
131
+ // the imperative mount to a cancelable task lets that discarded pass clean
132
+ // up before an iframe is inserted, so only the committed effect owns a Frame.
133
+ const scheduledMount = window.setTimeout(() => {
134
+ if (!disposed)
135
+ void mount();
136
+ }, 0);
137
+ return () => {
138
+ disposed = true;
139
+ window.clearTimeout(scheduledMount);
140
+ abort.abort();
141
+ instance?.destroy();
142
+ if (instanceRef.current === instance)
143
+ instanceRef.current = undefined;
144
+ register?.(undefined);
145
+ target.replaceChildren();
146
+ };
147
+ }, [
148
+ config.clientSecret,
149
+ config.threeDS?.presentation,
150
+ config.appearance,
151
+ register,
152
+ setProviderState
153
+ ]);
154
+ return _jsx("div", { ref: targetRef, className: props.className, "data-bemony-payment-form": "" });
155
+ });
156
+ function completeConfig(value) {
157
+ if (typeof value.clientSecret !== 'string' || value.clientSecret.length === 0) {
158
+ throw new Error('BemonyPaymentForm exige clientSecret fora de BemonyPaymentFormProvider.');
159
+ }
160
+ return { ...value, clientSecret: value.clientSecret };
161
+ }
162
+ function notReady() {
163
+ return { code: 'FORM_NOT_READY', message: 'O formulário de pagamento ainda não está pronto.' };
164
+ }
165
+ function mountError(error) {
166
+ if (typeof error === 'object' &&
167
+ error !== null &&
168
+ 'code' in error &&
169
+ 'message' in error &&
170
+ typeof error.code === 'string' &&
171
+ typeof error.message === 'string') {
172
+ return { code: error.code, message: error.message };
173
+ }
174
+ return { code: 'MOUNT_FAILED', message: 'Não foi possível montar o formulário de pagamento.' };
175
+ }
@@ -0,0 +1,4 @@
1
+ export { BemonyPaymentForm, BemonyPaymentFormProvider, useBemonyPaymentForm } from './components/payment-form';
2
+ export type { BemonyPaymentFormProps, BemonyPaymentFormProviderProps, BemonyPaymentFormRef } from './components/payment-form';
3
+ export type { PayInput, PayOutcome, PaymentFormAppearance, PaymentFormError, PaymentFormState, PaymentFormStatus, ThreeDSPresentation } from '@bemony/payment-form';
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,iBAAiB,EACjB,yBAAyB,EACzB,oBAAoB,EACrB,MAAM,2BAA2B,CAAC;AACnC,YAAY,EACV,sBAAsB,EACtB,8BAA8B,EAC9B,oBAAoB,EACrB,MAAM,2BAA2B,CAAC;AACnC,YAAY,EACV,QAAQ,EACR,UAAU,EACV,qBAAqB,EACrB,gBAAgB,EAChB,gBAAgB,EAChB,iBAAiB,EACjB,mBAAmB,EACpB,MAAM,sBAAsB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,178 @@
1
+ // src/components/payment-form.tsx
2
+ import {
3
+ BemonyPaymentForm as WebPaymentForm
4
+ } from "@bemony/payment-form";
5
+ import {
6
+ createContext,
7
+ forwardRef,
8
+ useContext,
9
+ useEffect,
10
+ useImperativeHandle,
11
+ useMemo,
12
+ useRef,
13
+ useState
14
+ } from "react";
15
+ import { jsx } from "react/jsx-runtime";
16
+ var PaymentFormContext = createContext(void 0);
17
+ var mountingState = { status: "MOUNTING", ready: false, isSubmitting: false };
18
+ function BemonyPaymentFormProvider({ children, ...config }) {
19
+ const [instance, register] = useState(void 0);
20
+ const [state, setState] = useState(mountingState);
21
+ const value = useMemo(
22
+ () => ({ config, instance, state, register, setState }),
23
+ [config, instance, state]
24
+ );
25
+ return /* @__PURE__ */ jsx(PaymentFormContext.Provider, { value, children });
26
+ }
27
+ function useBemonyPaymentForm() {
28
+ const context = useContext(PaymentFormContext);
29
+ if (context === void 0) {
30
+ throw new Error("useBemonyPaymentForm deve ser usado dentro de BemonyPaymentFormProvider.");
31
+ }
32
+ return {
33
+ ready: context.state.ready,
34
+ state: context.state,
35
+ error: context.state.error ?? null,
36
+ pay(input) {
37
+ return context.instance?.pay(input) ?? Promise.reject(notReady());
38
+ }
39
+ };
40
+ }
41
+ var BemonyPaymentForm = forwardRef(
42
+ function BemonyPaymentForm2(props, ref) {
43
+ const context = useContext(PaymentFormContext);
44
+ const config = context?.config ?? completeConfig(props);
45
+ const register = context?.register;
46
+ const setProviderState = context?.setState;
47
+ const targetRef = useRef(null);
48
+ const instanceRef = useRef(void 0);
49
+ const stateRef = useRef(mountingState);
50
+ const latestConfig = useRef(config);
51
+ latestConfig.current = config;
52
+ useImperativeHandle(
53
+ ref,
54
+ () => ({
55
+ pay(input) {
56
+ return instanceRef.current?.pay(input) ?? Promise.reject(notReady());
57
+ },
58
+ getState() {
59
+ return instanceRef.current?.getState() ?? stateRef.current;
60
+ },
61
+ destroy() {
62
+ instanceRef.current?.destroy();
63
+ instanceRef.current = void 0;
64
+ register?.(void 0);
65
+ }
66
+ }),
67
+ [register]
68
+ );
69
+ useEffect(() => {
70
+ const target = targetRef.current;
71
+ if (target === null) return;
72
+ const abort = new AbortController();
73
+ let disposed = false;
74
+ let instance;
75
+ const publish = (next) => {
76
+ stateRef.current = next;
77
+ setProviderState?.(next);
78
+ };
79
+ const mount = async () => {
80
+ try {
81
+ const mounted = await WebPaymentForm.mount({
82
+ target,
83
+ clientSecret: config.clientSecret,
84
+ ...config.appearance === void 0 ? {} : { appearance: config.appearance },
85
+ ...config.threeDS === void 0 ? {} : { threeDS: config.threeDS },
86
+ signal: abort.signal,
87
+ onReady() {
88
+ if (disposed) return;
89
+ latestConfig.current.onReady?.();
90
+ publish({ ...stateRef.current, status: "READY", ready: true, isSubmitting: false });
91
+ },
92
+ onCardStateChange(state) {
93
+ if (!disposed) latestConfig.current.onCardStateChange?.(state);
94
+ },
95
+ onStatusChange(status) {
96
+ if (disposed) return;
97
+ latestConfig.current.onStatusChange?.(status);
98
+ publish({
99
+ ...stateRef.current,
100
+ status,
101
+ ready: status !== "MOUNTING" && status !== "DESTROYED",
102
+ isSubmitting: [
103
+ "TOKENIZING",
104
+ "SUBMITTING",
105
+ "AUTHENTICATING_3DS",
106
+ "PROCESSING"
107
+ ].includes(status)
108
+ });
109
+ },
110
+ onSuccess(payment) {
111
+ if (disposed) return;
112
+ latestConfig.current.onSuccess?.(payment);
113
+ publish({ status: payment.status, ready: true, isSubmitting: false, payment });
114
+ },
115
+ onError(error) {
116
+ if (disposed) return;
117
+ latestConfig.current.onError?.(error);
118
+ publish({ ...stateRef.current, isSubmitting: false, error });
119
+ }
120
+ });
121
+ if (disposed) {
122
+ mounted.destroy();
123
+ return;
124
+ }
125
+ instance = mounted;
126
+ instanceRef.current = mounted;
127
+ register?.(mounted);
128
+ publish(mounted.getState());
129
+ } catch (error) {
130
+ if (disposed) return;
131
+ const publicError = mountError(error);
132
+ latestConfig.current.onError?.(publicError);
133
+ publish({ ...stateRef.current, isSubmitting: false, error: publicError });
134
+ }
135
+ };
136
+ const scheduledMount = window.setTimeout(() => {
137
+ if (!disposed) void mount();
138
+ }, 0);
139
+ return () => {
140
+ disposed = true;
141
+ window.clearTimeout(scheduledMount);
142
+ abort.abort();
143
+ instance?.destroy();
144
+ if (instanceRef.current === instance) instanceRef.current = void 0;
145
+ register?.(void 0);
146
+ target.replaceChildren();
147
+ };
148
+ }, [
149
+ config.clientSecret,
150
+ config.threeDS?.presentation,
151
+ config.appearance,
152
+ register,
153
+ setProviderState
154
+ ]);
155
+ return /* @__PURE__ */ jsx("div", { ref: targetRef, className: props.className, "data-bemony-payment-form": "" });
156
+ }
157
+ );
158
+ function completeConfig(value) {
159
+ if (typeof value.clientSecret !== "string" || value.clientSecret.length === 0) {
160
+ throw new Error("BemonyPaymentForm exige clientSecret fora de BemonyPaymentFormProvider.");
161
+ }
162
+ return { ...value, clientSecret: value.clientSecret };
163
+ }
164
+ function notReady() {
165
+ return { code: "FORM_NOT_READY", message: "O formul\xE1rio de pagamento ainda n\xE3o est\xE1 pronto." };
166
+ }
167
+ function mountError(error) {
168
+ if (typeof error === "object" && error !== null && "code" in error && "message" in error && typeof error.code === "string" && typeof error.message === "string") {
169
+ return { code: error.code, message: error.message };
170
+ }
171
+ return { code: "MOUNT_FAILED", message: "N\xE3o foi poss\xEDvel montar o formul\xE1rio de pagamento." };
172
+ }
173
+ export {
174
+ BemonyPaymentForm,
175
+ BemonyPaymentFormProvider,
176
+ useBemonyPaymentForm
177
+ };
178
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/components/payment-form.tsx"],
4
+ "sourcesContent": ["'use client';\n\nimport {\n BemonyPaymentForm as WebPaymentForm,\n type CardState,\n type PayInput,\n type PayOutcome,\n type PaymentFormAppearance,\n type PaymentFormCallbacks,\n type PaymentFormError,\n type PaymentFormInstance,\n type PaymentFormState,\n type ThreeDSPresentation\n} from '@bemony/payment-form';\nimport {\n createContext,\n forwardRef,\n useContext,\n useEffect,\n useImperativeHandle,\n useMemo,\n useRef,\n useState,\n type ReactNode\n} from 'react';\n\nexport type BemonyPaymentFormRef = Readonly<{\n pay(input: PayInput): Promise<PayOutcome>;\n getState(): PaymentFormState;\n destroy(): void;\n}>;\n\ntype FormConfig = PaymentFormCallbacks &\n Readonly<{\n clientSecret: string;\n appearance?: PaymentFormAppearance;\n threeDS?: Readonly<{ presentation?: ThreeDSPresentation }>;\n onCardStateChange?: (state: CardState) => void;\n }>;\n\nexport type BemonyPaymentFormProps = Partial<FormConfig> & Readonly<{ className?: string }>;\n\ntype ContextValue = Readonly<{\n config: FormConfig;\n instance?: PaymentFormInstance;\n state: PaymentFormState;\n register(instance: PaymentFormInstance | undefined): void;\n setState(state: PaymentFormState): void;\n}>;\n\nconst PaymentFormContext = createContext<ContextValue | undefined>(undefined);\nconst mountingState: PaymentFormState = { status: 'MOUNTING', ready: false, isSubmitting: false };\n\nexport type BemonyPaymentFormProviderProps = FormConfig & Readonly<{ children: ReactNode }>;\n\n/** Optional React-only convenience layer over the single Web SDK instance. */\nexport function BemonyPaymentFormProvider({ children, ...config }: BemonyPaymentFormProviderProps) {\n const [instance, register] = useState<PaymentFormInstance | undefined>(undefined);\n const [state, setState] = useState<PaymentFormState>(mountingState);\n const value = useMemo<ContextValue>(\n () => ({ config, instance, state, register, setState }),\n [config, instance, state]\n );\n return <PaymentFormContext.Provider value={value}>{children}</PaymentFormContext.Provider>;\n}\n\nexport function useBemonyPaymentForm(): Readonly<{\n ready: boolean;\n state: PaymentFormState;\n error: PaymentFormError | null;\n pay(input: PayInput): Promise<PayOutcome>;\n}> {\n const context = useContext(PaymentFormContext);\n if (context === undefined) {\n throw new Error('useBemonyPaymentForm deve ser usado dentro de BemonyPaymentFormProvider.');\n }\n return {\n ready: context.state.ready,\n state: context.state,\n error: context.state.error ?? null,\n pay(input) {\n return context.instance?.pay(input) ?? Promise.reject(notReady());\n }\n };\n}\n\n/**\n * Works standalone with a ref or as the mount point inside\n * BemonyPaymentFormProvider. Both paths mount the same Web SDK instance.\n */\nexport const BemonyPaymentForm = forwardRef<BemonyPaymentFormRef, BemonyPaymentFormProps>(\n function BemonyPaymentForm(props, ref) {\n const context = useContext(PaymentFormContext);\n const config = context?.config ?? completeConfig(props);\n const register = context?.register;\n const setProviderState = context?.setState;\n const targetRef = useRef<HTMLDivElement>(null);\n const instanceRef = useRef<PaymentFormInstance | undefined>(undefined);\n const stateRef = useRef<PaymentFormState>(mountingState);\n const latestConfig = useRef(config);\n latestConfig.current = config;\n\n useImperativeHandle(\n ref,\n () => ({\n pay(input) {\n return instanceRef.current?.pay(input) ?? Promise.reject(notReady());\n },\n getState() {\n return instanceRef.current?.getState() ?? stateRef.current;\n },\n destroy() {\n instanceRef.current?.destroy();\n instanceRef.current = undefined;\n register?.(undefined);\n }\n }),\n [register]\n );\n\n useEffect(() => {\n const target = targetRef.current;\n if (target === null) return;\n const abort = new AbortController();\n let disposed = false;\n let instance: PaymentFormInstance | undefined;\n const publish = (next: PaymentFormState): void => {\n stateRef.current = next;\n setProviderState?.(next);\n };\n const mount = async (): Promise<void> => {\n try {\n const mounted = await WebPaymentForm.mount({\n target,\n clientSecret: config.clientSecret,\n ...(config.appearance === undefined ? {} : { appearance: config.appearance }),\n ...(config.threeDS === undefined ? {} : { threeDS: config.threeDS }),\n signal: abort.signal,\n onReady() {\n if (disposed) return;\n latestConfig.current.onReady?.();\n publish({ ...stateRef.current, status: 'READY', ready: true, isSubmitting: false });\n },\n onCardStateChange(state) {\n if (!disposed) latestConfig.current.onCardStateChange?.(state);\n },\n onStatusChange(status) {\n if (disposed) return;\n latestConfig.current.onStatusChange?.(status);\n publish({\n ...stateRef.current,\n status,\n ready: status !== 'MOUNTING' && status !== 'DESTROYED',\n isSubmitting: [\n 'TOKENIZING',\n 'SUBMITTING',\n 'AUTHENTICATING_3DS',\n 'PROCESSING'\n ].includes(status)\n });\n },\n onSuccess(payment) {\n if (disposed) return;\n latestConfig.current.onSuccess?.(payment);\n publish({ status: payment.status, ready: true, isSubmitting: false, payment });\n },\n onError(error) {\n if (disposed) return;\n latestConfig.current.onError?.(error);\n publish({ ...stateRef.current, isSubmitting: false, error });\n }\n });\n if (disposed) {\n mounted.destroy();\n return;\n }\n instance = mounted;\n instanceRef.current = mounted;\n register?.(mounted);\n publish(mounted.getState());\n } catch (error: unknown) {\n if (disposed) return;\n const publicError = mountError(error);\n latestConfig.current.onError?.(publicError);\n publish({ ...stateRef.current, isSubmitting: false, error: publicError });\n }\n };\n // React Strict Mode runs a speculative effect setup/cleanup pair. Deferring\n // the imperative mount to a cancelable task lets that discarded pass clean\n // up before an iframe is inserted, so only the committed effect owns a Frame.\n const scheduledMount = window.setTimeout(() => {\n if (!disposed) void mount();\n }, 0);\n return () => {\n disposed = true;\n window.clearTimeout(scheduledMount);\n abort.abort();\n instance?.destroy();\n if (instanceRef.current === instance) instanceRef.current = undefined;\n register?.(undefined);\n target.replaceChildren();\n };\n }, [\n config.clientSecret,\n config.threeDS?.presentation,\n config.appearance,\n register,\n setProviderState\n ]);\n\n return <div ref={targetRef} className={props.className} data-bemony-payment-form=\"\" />;\n }\n);\n\nfunction completeConfig(value: BemonyPaymentFormProps): FormConfig {\n if (typeof value.clientSecret !== 'string' || value.clientSecret.length === 0) {\n throw new Error('BemonyPaymentForm exige clientSecret fora de BemonyPaymentFormProvider.');\n }\n return { ...value, clientSecret: value.clientSecret };\n}\n\nfunction notReady(): PaymentFormError {\n return { code: 'FORM_NOT_READY', message: 'O formul\u00E1rio de pagamento ainda n\u00E3o est\u00E1 pronto.' };\n}\n\nfunction mountError(error: unknown): PaymentFormError {\n if (\n typeof error === 'object' &&\n error !== null &&\n 'code' in error &&\n 'message' in error &&\n typeof error.code === 'string' &&\n typeof error.message === 'string'\n ) {\n return { code: error.code, message: error.message };\n }\n return { code: 'MOUNT_FAILED', message: 'N\u00E3o foi poss\u00EDvel montar o formul\u00E1rio de pagamento.' };\n}\n"],
5
+ "mappings": ";AAEA;AAAA,EACE,qBAAqB;AAAA,OAUhB;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAEK;AAuCE;AAbT,IAAM,qBAAqB,cAAwC,MAAS;AAC5E,IAAM,gBAAkC,EAAE,QAAQ,YAAY,OAAO,OAAO,cAAc,MAAM;AAKzF,SAAS,0BAA0B,EAAE,UAAU,GAAG,OAAO,GAAmC;AACjG,QAAM,CAAC,UAAU,QAAQ,IAAI,SAA0C,MAAS;AAChF,QAAM,CAAC,OAAO,QAAQ,IAAI,SAA2B,aAAa;AAClE,QAAM,QAAQ;AAAA,IACZ,OAAO,EAAE,QAAQ,UAAU,OAAO,UAAU,SAAS;AAAA,IACrD,CAAC,QAAQ,UAAU,KAAK;AAAA,EAC1B;AACA,SAAO,oBAAC,mB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QAAQ;AAAA,EACpD;AACA,SAAO,EAAE,MAAM,gBAAgB,SAAS,8DAAqD;AAC/F;",
6
+ "names": ["BemonyPaymentForm"]
7
+ }
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "@bemony/payment-form-react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Official React adapter for the hosted Bemony Payment Form.",
5
+ "keywords": [
6
+ "bemony",
7
+ "payments",
8
+ "checkout",
9
+ "3ds",
10
+ "react"
11
+ ],
12
+ "homepage": "https://github.com/AgenusPayments/orchestration-embed#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/AgenusPayments/orchestration-embed/issues"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/AgenusPayments/orchestration-embed.git",
19
+ "directory": "packages/payment-form-react"
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "type": "module",
25
+ "sideEffects": false,
26
+ "types": "./dist/index.d.ts",
27
+ "module": "./dist/index.js",
28
+ "main": "./dist/index.js",
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "import": "./dist/index.js",
33
+ "default": "./dist/index.js"
34
+ }
35
+ },
36
+ "dependencies": {
37
+ "@bemony/payment-form": "^0.1.0"
38
+ },
39
+ "peerDependencies": {
40
+ "react": ">=19.0.0 <20.0.0",
41
+ "react-dom": ">=19.0.0 <20.0.0"
42
+ },
43
+ "devDependencies": {
44
+ "esbuild": "0.28.2"
45
+ },
46
+ "publishConfig": {
47
+ "registry": "https://registry.npmjs.org/",
48
+ "access": "public"
49
+ },
50
+ "scripts": {
51
+ "build": "node ../../scripts/clean-package-dist.mjs && tsc -p tsconfig.build.json && esbuild ./src/index.ts --bundle --format=esm --platform=browser --target=es2022 --packages=external --sourcemap --outfile=dist/index.js && pnpm run verify:dist",
52
+ "verify:dist": "node ../../scripts/verify-payment-form-react-package.mjs",
53
+ "lint": "eslint src",
54
+ "typecheck": "tsc --noEmit -p tsconfig.json",
55
+ "test": "vitest run --passWithNoTests"
56
+ }
6
57
  }