crain-react-stripe-elements-shim 1.0.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.
Files changed (2) hide show
  1. package/index.js +166 -0
  2. package/package.json +8 -0
package/index.js ADDED
@@ -0,0 +1,166 @@
1
+ /**
2
+ * react-stripe-elements compatibility shim for React 19
3
+ *
4
+ * Replaces the deprecated react-stripe-elements package (which uses legacy
5
+ * React context API removed in React 19) with a thin wrapper around
6
+ * @stripe/react-stripe-js (which uses React.createContext).
7
+ *
8
+ * Provides the same public API: StripeProvider, Elements, injectStripe,
9
+ * CardElement, CardNumberElement, CardExpiryElement, CardCvcElement, etc.
10
+ */
11
+ 'use strict';
12
+
13
+ var React = require('react');
14
+ var stripeReact = require('@stripe/react-stripe-js');
15
+ var stripeJs = require('@stripe/stripe-js');
16
+
17
+ // Internal context to pass stripe instance from StripeProvider to Elements
18
+ var StripeContext = React.createContext(null);
19
+
20
+ /**
21
+ * StripeProvider - compatibility replacement
22
+ * Original used getChildContext (legacy API). This uses React.createContext.
23
+ */
24
+ function StripeProvider(props) {
25
+ var stripeRef = React.useRef(null);
26
+ var _s = React.useState(null), stripeInstance = _s[0], setStripeInstance = _s[1];
27
+
28
+ React.useEffect(function() {
29
+ if (props.stripe) {
30
+ setStripeInstance(props.stripe);
31
+ } else if (props.apiKey && typeof window !== 'undefined') {
32
+ stripeJs.loadStripe(props.apiKey).then(function(s) {
33
+ stripeRef.current = s;
34
+ setStripeInstance(s);
35
+ });
36
+ }
37
+ }, [props.apiKey, props.stripe]);
38
+
39
+ return React.createElement(
40
+ StripeContext.Provider,
41
+ { value: stripeInstance },
42
+ props.children
43
+ );
44
+ }
45
+
46
+ /**
47
+ * Elements - wraps @stripe/react-stripe-js Elements
48
+ */
49
+ function Elements(props) {
50
+ var stripe = React.useContext(StripeContext);
51
+ if (!stripe) {
52
+ return props.children || null;
53
+ }
54
+ var elementProps = {};
55
+ if (props.fonts) elementProps.fonts = props.fonts;
56
+ if (props.locale) elementProps.locale = props.locale;
57
+
58
+ return React.createElement(
59
+ stripeReact.Elements,
60
+ Object.assign({ stripe: stripe }, elementProps),
61
+ props.children
62
+ );
63
+ }
64
+
65
+ /**
66
+ * injectStripe HOC - compatibility replacement
67
+ * Original used contextTypes (legacy API). This uses useStripe/useElements hooks.
68
+ */
69
+ function injectStripe(WrappedComponent, options) {
70
+ var opts = options || {};
71
+
72
+ function InjectedComponent(props) {
73
+ var stripe = null;
74
+ var elements = null;
75
+
76
+ try {
77
+ stripe = stripeReact.useStripe();
78
+ elements = stripeReact.useElements();
79
+ } catch (e) {
80
+ // Not inside Elements context — try our StripeContext
81
+ var ctxStripe = React.useContext(StripeContext);
82
+ if (ctxStripe) {
83
+ stripe = ctxStripe;
84
+ }
85
+ }
86
+
87
+ // Build stripe prop with wrapped methods matching react-stripe-elements API
88
+ var stripeProxy = null;
89
+ if (stripe) {
90
+ stripeProxy = Object.assign({}, stripe, {
91
+ createToken: function(tokenTypeOrElement, data) {
92
+ if (typeof tokenTypeOrElement === 'string' && elements) {
93
+ var el = elements.getElement(tokenTypeOrElement === 'card' ? 'card' : tokenTypeOrElement);
94
+ if (el) return stripe.createToken(el, data);
95
+ }
96
+ if (tokenTypeOrElement && typeof tokenTypeOrElement === 'object') {
97
+ return stripe.createToken(tokenTypeOrElement, data);
98
+ }
99
+ if (elements) {
100
+ var cardEl = elements.getElement('card');
101
+ if (cardEl) return stripe.createToken(cardEl, data || tokenTypeOrElement);
102
+ }
103
+ return stripe.createToken(tokenTypeOrElement, data);
104
+ },
105
+ createSource: function(sourceData) {
106
+ if (sourceData && typeof sourceData === 'object') {
107
+ if (elements) {
108
+ var el = elements.getElement(sourceData.type || 'card');
109
+ if (el) return stripe.createSource(el, sourceData);
110
+ }
111
+ return stripe.createSource(sourceData);
112
+ }
113
+ return stripe.createSource(sourceData);
114
+ },
115
+ createPaymentMethod: function(type, elementOrData, data) {
116
+ return stripe.createPaymentMethod.apply(stripe, arguments);
117
+ },
118
+ handleCardPayment: function(clientSecret, elementOrData, data) {
119
+ if (typeof stripe.confirmCardPayment === 'function') {
120
+ return stripe.confirmCardPayment(clientSecret, elementOrData);
121
+ }
122
+ return stripe.handleCardPayment.apply(stripe, arguments);
123
+ },
124
+ handleCardSetup: function(clientSecret, elementOrData, data) {
125
+ if (typeof stripe.confirmCardSetup === 'function') {
126
+ return stripe.confirmCardSetup(clientSecret, elementOrData);
127
+ }
128
+ return stripe.handleCardSetup.apply(stripe, arguments);
129
+ }
130
+ });
131
+ }
132
+
133
+ var ref = opts.withRef ? React.createRef() : undefined;
134
+ return React.createElement(
135
+ WrappedComponent,
136
+ Object.assign({}, props, { stripe: stripeProxy, elements: elements, ref: ref })
137
+ );
138
+ }
139
+
140
+ InjectedComponent.displayName = 'InjectStripe(' + (WrappedComponent.displayName || WrappedComponent.name || 'Component') + ')';
141
+ return InjectedComponent;
142
+ }
143
+
144
+ // Re-export element components from @stripe/react-stripe-js
145
+ var CardElement = stripeReact.CardElement;
146
+ var CardNumberElement = stripeReact.CardNumberElement;
147
+ var CardExpiryElement = stripeReact.CardExpiryElement;
148
+ var CardCvcElement = stripeReact.CardCvcElement;
149
+ var PaymentRequestButtonElement = stripeReact.PaymentRequestButtonElement;
150
+ var IbanElement = stripeReact.IbanElement;
151
+ var IdealBankElement = stripeReact.IdealBankElement;
152
+
153
+ // Legacy aliases
154
+ var CardCVCElement = CardCvcElement;
155
+
156
+ exports.StripeProvider = StripeProvider;
157
+ exports.Elements = Elements;
158
+ exports.injectStripe = injectStripe;
159
+ exports.CardElement = CardElement;
160
+ exports.CardNumberElement = CardNumberElement;
161
+ exports.CardExpiryElement = CardExpiryElement;
162
+ exports.CardCvcElement = CardCvcElement;
163
+ exports.CardCVCElement = CardCVCElement;
164
+ exports.PaymentRequestButtonElement = PaymentRequestButtonElement;
165
+ exports.IbanElement = IbanElement;
166
+ exports.IdealBankElement = IdealBankElement;
package/package.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "name": "crain-react-stripe-elements-shim",
3
+ "version": "1.0.0",
4
+ "description": "React 19 compat shim for react-stripe-elements using @stripe/react-stripe-js",
5
+ "main": "index.js",
6
+ "license": "MIT",
7
+ "dependencies": {}
8
+ }