@bloque/payments-react 0.0.6 → 0.0.7

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,6 +1,6 @@
1
1
  # @bloque/payments-react
2
2
 
3
- React wrapper for `@bloque/payments-elements` web components.
3
+ React component that embeds the Bloque hosted checkout.
4
4
 
5
5
  ## Installation
6
6
 
@@ -1,27 +1,123 @@
1
- import '@bloque/payments-elements';
2
- import type { AppearanceConfig, CheckoutConfig, PaymentMethodType, PaymentResponse, PaymentSubmitPayload } from '@bloque/payments-elements';
3
- declare global {
4
- namespace JSX {
5
- interface IntrinsicElements {
6
- 'bloque-checkout': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
7
- }
8
- }
1
+ export type CheckoutMode = 'sandbox' | 'production';
2
+ export type CheckoutLang = 'es' | 'en';
3
+ export interface SDKInitOptions {
4
+ /**
5
+ * Your Bloque public API key
6
+ */
7
+ publicApiKey: string;
8
+ /**
9
+ * Operation mode: 'sandbox' or 'production'. Default: 'production'
10
+ */
11
+ mode?: CheckoutMode;
12
+ /**
13
+ * Default language for all components
14
+ */
15
+ lang?: CheckoutLang;
16
+ }
17
+ /**
18
+ * Initialize the Bloque Payments SDK with global configuration.
19
+ * This allows you to set the publicApiKey and mode once, instead of
20
+ * passing them to every component.
21
+ *
22
+ * @example
23
+ * ```tsx
24
+ * import { init } from '@bloque/payments-react';
25
+ *
26
+ * init({
27
+ * publicApiKey: 'pk_test_xxx',
28
+ * mode: 'sandbox',
29
+ * });
30
+ * ```
31
+ */
32
+ export declare function init(options: SDKInitOptions): void;
33
+ /**
34
+ * Get the current global configuration.
35
+ * Returns null if init() has not been called.
36
+ */
37
+ export declare function getConfig(): SDKInitOptions | null;
38
+ export interface PaymentResult {
39
+ payment_id: string;
40
+ status: 'approved' | 'pending' | 'rejected';
41
+ message: string;
42
+ amount: number;
43
+ currency: string;
44
+ reference: string;
45
+ created_at: string;
46
+ }
47
+ export interface PaymentError {
48
+ message: string;
49
+ data?: unknown;
50
+ type: string;
51
+ }
52
+ export interface AppearanceConfig {
53
+ /**
54
+ * Primary color (hex). Example: '#4f46e5'
55
+ */
56
+ primaryColor?: string;
57
+ /**
58
+ * Border radius. Example: '8px'
59
+ */
60
+ borderRadius?: string;
61
+ /**
62
+ * Font family. Example: 'Inter, system-ui, sans-serif'
63
+ */
64
+ fontFamily?: string;
65
+ }
66
+ export interface CheckoutConfig {
67
+ /**
68
+ * The payment amount in USD (required)
69
+ */
70
+ amount: number;
71
+ webhookUrl?: string;
72
+ /**
73
+ * Require email in the card form. Default: true
74
+ */
75
+ requireEmail?: boolean;
9
76
  }
10
77
  export interface BloqueCheckoutProps {
11
- config?: CheckoutConfig;
78
+ /**
79
+ * Checkout configuration with amount and optional settings
80
+ */
81
+ config: CheckoutConfig;
82
+ /**
83
+ * Appearance customization (colors, fonts, etc.)
84
+ */
12
85
  appearance?: AppearanceConfig;
13
- amount?: number;
14
- availableMethods?: PaymentMethodType[];
15
- requireEmail?: boolean;
16
- showMethodSelector?: boolean;
17
- onSubmit?: (payload: PaymentSubmitPayload) => Promise<PaymentResponse | undefined>;
18
- onSuccess?: (response: PaymentResponse) => void;
19
- onError?: (error: {
20
- message: string;
21
- data: unknown;
22
- type: string;
23
- }) => void;
86
+ /**
87
+ * Your Bloque public API key. Optional if init() was called.
88
+ */
89
+ publicApiKey?: string;
90
+ /**
91
+ * Language: 'es' (Spanish) or 'en' (English). Default: 'es'
92
+ */
93
+ lang?: CheckoutLang;
94
+ /**
95
+ * Operation mode: 'sandbox' or 'production'. Default: 'production'
96
+ */
97
+ mode?: CheckoutMode;
98
+ /**
99
+ * Callback when payment is successful
100
+ */
101
+ onSuccess?: (result: PaymentResult) => void;
102
+ /**
103
+ * Callback when payment fails
104
+ */
105
+ onError?: (error: PaymentError) => void;
106
+ /**
107
+ * Additional CSS class name
108
+ */
24
109
  className?: string;
110
+ /**
111
+ * Additional inline styles
112
+ */
25
113
  style?: React.CSSProperties;
114
+ /**
115
+ * iframe width. Default: '100%'
116
+ */
117
+ width?: string | number;
118
+ /**
119
+ * iframe height. Default: '600px'
120
+ */
121
+ height?: string | number;
26
122
  }
27
- export declare function BloqueCheckout({ config, appearance, amount, availableMethods, requireEmail, showMethodSelector, onSubmit, onSuccess, onError, className, style, }: BloqueCheckoutProps): import("react/jsx-runtime").JSX.Element;
123
+ export declare function BloqueCheckout({ config, appearance, publicApiKey, lang, mode, onSuccess, onError, className, style, width, height, }: BloqueCheckoutProps): import("react/jsx-runtime").JSX.Element;
@@ -1,35 +1,84 @@
1
1
  import { jsx } from "react/jsx-runtime";
2
- import "@bloque/payments-elements";
3
- import { useEffect, useRef } from "react";
4
- function BloqueCheckout({ config, appearance, amount, availableMethods, requireEmail = true, showMethodSelector = true, onSubmit, onSuccess, onError, className, style }) {
5
- const checkoutRef = useRef(null);
2
+ import { useCallback, useEffect, useMemo, useRef } from "react";
3
+ const CHECKOUT_BASE_URL = 'https://payments.bloque.app';
4
+ let globalConfig = null;
5
+ function init(options) {
6
+ globalConfig = options;
7
+ }
8
+ function getConfig() {
9
+ return globalConfig;
10
+ }
11
+ function buildCheckoutUrl(props) {
12
+ const params = new URLSearchParams();
13
+ params.set('amount', props.amount.toString());
14
+ params.set('publicApiKey', props.publicApiKey);
15
+ params.set('lang', props.lang);
16
+ params.set('mode', props.mode);
17
+ params.set('requireEmail', props.requireEmail.toString());
18
+ if (props.webhookUrl) params.set('webhookUrl', props.webhookUrl);
19
+ if (props.appearance?.primaryColor) params.set('primaryColor', props.appearance.primaryColor);
20
+ if (props.appearance?.borderRadius) params.set('borderRadius', props.appearance.borderRadius);
21
+ if (props.appearance?.fontFamily) params.set('fontFamily', props.appearance.fontFamily);
22
+ return `${CHECKOUT_BASE_URL}/?${params.toString()}`;
23
+ }
24
+ function BloqueCheckout({ config, appearance, publicApiKey, lang, mode, onSuccess, onError, className, style, width = '100%', height = '600px' }) {
25
+ const iframeRef = useRef(null);
26
+ const onSuccessRef = useRef(onSuccess);
27
+ const onErrorRef = useRef(onError);
28
+ const resolvedPublicApiKey = publicApiKey ?? globalConfig?.publicApiKey;
29
+ const resolvedMode = mode ?? globalConfig?.mode ?? 'production';
30
+ const resolvedLang = lang ?? globalConfig?.lang ?? 'es';
31
+ if (!resolvedPublicApiKey) throw new Error('BloqueCheckout: publicApiKey is required. Either pass it as a prop or call init() first.');
6
32
  useEffect(()=>{
7
- const element = checkoutRef.current;
8
- if (!element) return;
9
- if (config) element.config = config;
10
- if (appearance) element.appearance = appearance;
11
- if (void 0 !== amount) element.amount = amount;
12
- if (availableMethods) element.availableMethods = availableMethods;
13
- element.requireEmail = requireEmail;
14
- element.showMethodSelector = showMethodSelector;
15
- if (onSubmit) element.onSubmit = onSubmit;
16
- if (onSuccess) element.onSuccess = onSuccess;
17
- if (onError) element.onError = onError;
33
+ onSuccessRef.current = onSuccess;
34
+ onErrorRef.current = onError;
18
35
  }, [
19
- config,
20
- appearance,
21
- amount,
22
- availableMethods,
23
- requireEmail,
24
- showMethodSelector,
25
- onSubmit,
26
36
  onSuccess,
27
37
  onError
28
38
  ]);
29
- return /*#__PURE__*/ jsx("bloque-checkout", {
30
- ref: checkoutRef,
39
+ const checkoutUrl = useMemo(()=>buildCheckoutUrl({
40
+ amount: config.amount,
41
+ publicApiKey: resolvedPublicApiKey,
42
+ lang: resolvedLang,
43
+ mode: resolvedMode,
44
+ requireEmail: config.requireEmail ?? true,
45
+ webhookUrl: config.webhookUrl,
46
+ appearance
47
+ }), [
48
+ config.amount,
49
+ config.requireEmail,
50
+ config.webhookUrl,
51
+ resolvedPublicApiKey,
52
+ resolvedLang,
53
+ resolvedMode,
54
+ appearance
55
+ ]);
56
+ const handleMessage = useCallback((event)=>{
57
+ if (event.origin !== CHECKOUT_BASE_URL) return;
58
+ const { type, data, error } = event.data || {};
59
+ if ('payment-result' === type && onSuccessRef.current) onSuccessRef.current(data);
60
+ if ('payment-error' === type && onErrorRef.current) onErrorRef.current(error);
61
+ }, []);
62
+ useEffect(()=>{
63
+ window.addEventListener('message', handleMessage);
64
+ return ()=>{
65
+ window.removeEventListener('message', handleMessage);
66
+ };
67
+ }, [
68
+ handleMessage
69
+ ]);
70
+ return /*#__PURE__*/ jsx("iframe", {
71
+ ref: iframeRef,
72
+ src: checkoutUrl,
73
+ width: width,
74
+ height: height,
31
75
  className: className,
32
- style: style
76
+ style: {
77
+ border: 'none',
78
+ ...style
79
+ },
80
+ title: "Bloque Checkout",
81
+ allow: "payment"
33
82
  });
34
83
  }
35
- export { BloqueCheckout };
84
+ export { BloqueCheckout, getConfig, init };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,2 @@
1
- export type { AppearanceConfig, CheckoutConfig, PaymentResponse, PaymentSubmitPayload, SDKInitOptions, } from '@bloque/payments-elements';
2
- export { init } from '@bloque/payments-elements';
3
- export type { BloqueCheckoutProps } from './bloque-checkout';
4
- export { BloqueCheckout } from './bloque-checkout';
1
+ export type { AppearanceConfig, BloqueCheckoutProps, CheckoutConfig, CheckoutLang, CheckoutMode, PaymentError, PaymentResult, SDKInitOptions, } from './bloque-checkout';
2
+ export { BloqueCheckout, getConfig, init } from './bloque-checkout';
package/dist/index.js CHANGED
@@ -1,3 +1,2 @@
1
- import { init } from "@bloque/payments-elements";
2
- import { BloqueCheckout } from "./bloque-checkout.js";
3
- export { BloqueCheckout, init };
1
+ import { BloqueCheckout, getConfig, init } from "./bloque-checkout.js";
2
+ export { BloqueCheckout, getConfig, init };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bloque/payments-react",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
4
4
  "description": "React wrapper for Bloque payments web components",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -33,9 +33,6 @@
33
33
  "access": "public",
34
34
  "provenance": true
35
35
  },
36
- "dependencies": {
37
- "@bloque/payments-elements": "0.0.6"
38
- },
39
36
  "peerDependencies": {
40
37
  "react": ">=16.9.0",
41
38
  "react-dom": ">=16.9.0"