@bemony/card-embed-react 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 +62 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +84 -0
- package/package.json +55 -0
package/README.md
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# @bemony/card-embed-react — adaptador React
|
|
2
|
+
|
|
3
|
+
## Papel
|
|
4
|
+
|
|
5
|
+
Fornecer uma experiência React natural sobre a mesma instância do SDK Web. O componente recebe `sessionClientSecret`, renderiza um container e expõe `tokenize()` e `destroy()` por `ref`.
|
|
6
|
+
|
|
7
|
+
## Regras
|
|
8
|
+
|
|
9
|
+
- importar e montar somente `@bemony/card-embed`;
|
|
10
|
+
- criar a instância em `useEffect` e destruí-la no cleanup;
|
|
11
|
+
- usar `useImperativeHandle` para o `ref`;
|
|
12
|
+
- refletir callbacks sem manter uma máquina de estado própria;
|
|
13
|
+
- remontar de forma segura quando `sessionClientSecret` mudar.
|
|
14
|
+
|
|
15
|
+
## Proibido
|
|
16
|
+
|
|
17
|
+
- `postMessage`, iframe ou tokenização direta;
|
|
18
|
+
- Provider/Context no V1;
|
|
19
|
+
- chamadas HTTP de CheckoutSession ou Payment;
|
|
20
|
+
- duplicar tipos de estado, erros ou resultado do SDK Web.
|
|
21
|
+
|
|
22
|
+
## Uso
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
pnpm add @bemony/card-embed-react
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
import { BemonyCardEmbed, type BemonyCardEmbedHandle } from '@bemony/card-embed-react';
|
|
30
|
+
import { useRef } from 'react';
|
|
31
|
+
|
|
32
|
+
const ref = useRef<BemonyCardEmbedHandle>(null);
|
|
33
|
+
|
|
34
|
+
<BemonyCardEmbed
|
|
35
|
+
ref={ref}
|
|
36
|
+
sessionClientSecret={sessionClientSecret}
|
|
37
|
+
layout="combined"
|
|
38
|
+
onStateChange={(state) => setCanPay(state.complete && state.valid)}
|
|
39
|
+
/>;
|
|
40
|
+
|
|
41
|
+
const { token } = await ref.current!.tokenize();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`layout` é opcional e usa `combined` por padrão. A troca de `sessionClientSecret` ou
|
|
45
|
+
`layout` desmonta a instância anterior e cria outra. Os callbacks mais recentes são
|
|
46
|
+
encaminhados ao SDK Web sem reinicializar o iframe.
|
|
47
|
+
|
|
48
|
+
Os callbacks públicos incluem `onReady`, `onStateChange`, `onError`, `onSessionExpiring`,
|
|
49
|
+
`onSessionExpired` e `onDestroyed`.
|
|
50
|
+
|
|
51
|
+
O adapter usa um `AbortController` privado para cancelar montagens ainda pendentes no cleanup.
|
|
52
|
+
Isso é necessário no Strict Mode do React, que executa um ciclo extra de efeito em
|
|
53
|
+
desenvolvimento, e impede timeouts de uma instância que já foi descartada.
|
|
54
|
+
|
|
55
|
+
`sessionClientSecret` deve vir do backend do merchant. Nunca exponha uma API key Bemony em código
|
|
56
|
+
React, variáveis `NEXT_PUBLIC_*`, bundles ou respostas enviadas ao navegador.
|
|
57
|
+
|
|
58
|
+
## Distribuição
|
|
59
|
+
|
|
60
|
+
`@bemony/card-embed-react` é publicado como ESM tipado e declara React/React DOM 19 como peer
|
|
61
|
+
dependencies. O pacote reutiliza `@bemony/card-embed`; não inclui uma segunda bridge, iframe ou
|
|
62
|
+
implementação de tokenização.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type BemonyCardEmbedInstance, type CardEmbedError, type CardEmbedMountOptions, type CardState, type TokenizeResult } from '@bemony/card-embed';
|
|
2
|
+
export type BemonyCardEmbedHandle = Pick<BemonyCardEmbedInstance, 'tokenize' | 'destroy'>;
|
|
3
|
+
export type BemonyCardEmbedProps = Omit<CardEmbedMountOptions, 'target' | 'sessionClientSecret' | 'signal'> & {
|
|
4
|
+
sessionClientSecret: string;
|
|
5
|
+
};
|
|
6
|
+
export declare const BemonyCardEmbed: import("react").ForwardRefExoticComponent<Omit<CardEmbedMountOptions, "target" | "sessionClientSecret" | "signal"> & {
|
|
7
|
+
sessionClientSecret: string;
|
|
8
|
+
} & import("react").RefAttributes<BemonyCardEmbedHandle>>;
|
|
9
|
+
export type { CardEmbedError, CardState, TokenizeResult };
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,uBAAuB,EAC5B,KAAK,cAAc,EACnB,KAAK,qBAAqB,EAC1B,KAAK,SAAS,EACd,KAAK,cAAc,EACpB,MAAM,oBAAoB,CAAC;AAG5B,MAAM,MAAM,qBAAqB,GAAG,IAAI,CAAC,uBAAuB,EAAE,UAAU,GAAG,SAAS,CAAC,CAAC;AAE1F,MAAM,MAAM,oBAAoB,GAAG,IAAI,CACrC,qBAAqB,EACrB,QAAQ,GAAG,qBAAqB,GAAG,QAAQ,CAC5C,GAAG;IACF,mBAAmB,EAAE,MAAM,CAAC;CAC7B,CAAC;AAOF,eAAO,MAAM,eAAe;yBARL,MAAM;yDAgG5B,CAAC;AAmBF,YAAY,EAAE,cAAc,EAAE,SAAS,EAAE,cAAc,EAAE,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { BemonyCardEmbed as WebCardEmbed } from '@bemony/card-embed';
|
|
3
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef } from 'react';
|
|
4
|
+
export const BemonyCardEmbed = forwardRef(function BemonyCardEmbed({ sessionClientSecret, layout, onDestroyed, onError, onReady, onSessionExpired, onSessionExpiring, onStateChange }, ref) {
|
|
5
|
+
const targetRef = useRef(null);
|
|
6
|
+
const instanceRef = useRef(null);
|
|
7
|
+
const callbacksRef = useRef({});
|
|
8
|
+
callbacksRef.current = {
|
|
9
|
+
onDestroyed,
|
|
10
|
+
onError,
|
|
11
|
+
onReady,
|
|
12
|
+
onSessionExpired,
|
|
13
|
+
onSessionExpiring,
|
|
14
|
+
onStateChange
|
|
15
|
+
};
|
|
16
|
+
useImperativeHandle(ref, () => ({
|
|
17
|
+
tokenize() {
|
|
18
|
+
return instanceRef.current?.tokenize() ?? Promise.reject(notReadyError());
|
|
19
|
+
},
|
|
20
|
+
destroy() {
|
|
21
|
+
instanceRef.current?.destroy();
|
|
22
|
+
instanceRef.current = null;
|
|
23
|
+
}
|
|
24
|
+
}), []);
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const target = targetRef.current;
|
|
27
|
+
if (target === null)
|
|
28
|
+
return;
|
|
29
|
+
let disposed = false;
|
|
30
|
+
let reportedMountError = false;
|
|
31
|
+
let instance;
|
|
32
|
+
const mountController = new AbortController();
|
|
33
|
+
void WebCardEmbed.mount({
|
|
34
|
+
target,
|
|
35
|
+
sessionClientSecret,
|
|
36
|
+
signal: mountController.signal,
|
|
37
|
+
...(layout === undefined ? {} : { layout }),
|
|
38
|
+
onReady: () => callbacksRef.current.onReady?.(),
|
|
39
|
+
onStateChange: (state) => callbacksRef.current.onStateChange?.(state),
|
|
40
|
+
onError: (error) => {
|
|
41
|
+
if (disposed)
|
|
42
|
+
return;
|
|
43
|
+
reportedMountError = true;
|
|
44
|
+
callbacksRef.current.onError?.(error);
|
|
45
|
+
},
|
|
46
|
+
onSessionExpiring: () => callbacksRef.current.onSessionExpiring?.(),
|
|
47
|
+
onSessionExpired: () => callbacksRef.current.onSessionExpired?.(),
|
|
48
|
+
onDestroyed: () => callbacksRef.current.onDestroyed?.()
|
|
49
|
+
})
|
|
50
|
+
.then((mounted) => {
|
|
51
|
+
instance = mounted;
|
|
52
|
+
if (disposed) {
|
|
53
|
+
mounted.destroy();
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
instanceRef.current = mounted;
|
|
57
|
+
})
|
|
58
|
+
.catch((error) => {
|
|
59
|
+
if (!disposed && !reportedMountError)
|
|
60
|
+
callbacksRef.current.onError?.(toPublicError(error));
|
|
61
|
+
});
|
|
62
|
+
return () => {
|
|
63
|
+
disposed = true;
|
|
64
|
+
mountController.abort();
|
|
65
|
+
instance?.destroy();
|
|
66
|
+
if (instanceRef.current === instance)
|
|
67
|
+
instanceRef.current = null;
|
|
68
|
+
};
|
|
69
|
+
}, [layout, sessionClientSecret]);
|
|
70
|
+
return _jsx("div", { ref: targetRef });
|
|
71
|
+
});
|
|
72
|
+
function notReadyError() {
|
|
73
|
+
return { code: 'INVALID_STATE', message: 'Card Embed is not ready.' };
|
|
74
|
+
}
|
|
75
|
+
function toPublicError(error) {
|
|
76
|
+
if (typeof error === 'object' &&
|
|
77
|
+
error !== null &&
|
|
78
|
+
'code' in error &&
|
|
79
|
+
'message' in error &&
|
|
80
|
+
typeof error.code === 'string' &&
|
|
81
|
+
typeof error.message === 'string')
|
|
82
|
+
return { code: 'NETWORK_ERROR', message: error.message };
|
|
83
|
+
return { code: 'NETWORK_ERROR', message: 'Card Embed could not be initialized.' };
|
|
84
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bemony/card-embed-react",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Official React adapter for the secure Bemony Card Embed SDK.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"bemony",
|
|
7
|
+
"payments",
|
|
8
|
+
"checkout",
|
|
9
|
+
"react",
|
|
10
|
+
"card-fields"
|
|
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/card-embed-react"
|
|
20
|
+
},
|
|
21
|
+
"type": "module",
|
|
22
|
+
"sideEffects": false,
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"module": "./dist/index.js",
|
|
25
|
+
"main": "./dist/index.js",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.js",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist"
|
|
36
|
+
],
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@bemony/card-embed": "^0.1.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"react": ">=19.0.0 <20.0.0",
|
|
42
|
+
"react-dom": ">=19.0.0 <20.0.0"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"registry": "https://registry.npmjs.org/",
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"build": "node ../../scripts/clean-package-dist.mjs && tsc -p tsconfig.build.json && pnpm run verify:dist",
|
|
50
|
+
"verify:dist": "node ../../scripts/verify-react-package.mjs",
|
|
51
|
+
"lint": "eslint src",
|
|
52
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
53
|
+
"test": "vitest run --passWithNoTests"
|
|
54
|
+
}
|
|
55
|
+
}
|