@@ -96,6 +96,30 @@ export const Bitrefill = ({ accounts, code }: TProps) => {
96
96
} ;
97
97
} , [ onResize ] ) ;
98
98
99
+ const handleConfiguration = useCallback ( async ( event : MessageEvent ) => {
100
+ if (
101
+ ! account
102
+ || ! bitrefillInfo ?. success
103
+ ) {
104
+ return ;
105
+ }
106
+ event . source ?. postMessage ( {
107
+ event : 'configuration' ,
108
+ ref : bitrefillInfo . ref ,
109
+ utm_source : 'BITBOX' ,
110
+ theme : isDarkMode ? 'dark' : 'light' ,
111
+ hl : i18n . resolvedLanguage ? localeMapping [ i18n . resolvedLanguage ] : 'en' ,
112
+ paymentMethods : account . coinCode ? coinMapping [ account . coinCode ] : 'bitcoin' ,
113
+ refundAddress : bitrefillInfo . address ,
114
+ // Option to keep pending payment information longer in session, defaults to 'false'
115
+ paymentPending : 'true' ,
116
+ // Option to show payment information in the widget, defaults to 'true'
117
+ showPaymentInfo : 'true'
118
+ } , {
119
+ targetOrigin : event . origin
120
+ } ) ;
121
+ } , [ account , bitrefillInfo , isDarkMode ] ) ;
122
+
99
123
const handlePaymentRequest = useCallback ( async ( event : MessageEvent ) => {
100
124
if ( ! account || pendingPayment ) {
101
125
return ;
@@ -164,8 +188,7 @@ export const Bitrefill = ({ accounts, code }: TProps) => {
164
188
165
189
const handleMessage = useCallback ( async ( event : MessageEvent ) => {
166
190
if (
167
- ! account
168
- || ! bitrefillInfo ?. success
191
+ ! bitrefillInfo ?. success
169
192
|| ! [ getURLOrigin ( bitrefillInfo . url ) , 'https://embed.bitrefill.com' ] . includes ( event . origin )
170
193
) {
171
194
return ;
@@ -175,21 +198,7 @@ export const Bitrefill = ({ accounts, code }: TProps) => {
175
198
176
199
switch ( data . event ) {
177
200
case 'request-configuration' : {
178
- event . source ?. postMessage ( {
179
- event : 'configuration' ,
180
- ref : bitrefillInfo . ref ,
181
- utm_source : 'BITBOX' ,
182
- theme : isDarkMode ? 'dark' : 'light' ,
183
- hl : i18n . resolvedLanguage ? localeMapping [ i18n . resolvedLanguage ] : 'en' ,
184
- paymentMethods : account . coinCode ? coinMapping [ account . coinCode ] : 'bitcoin' ,
185
- refundAddress : bitrefillInfo . address ,
186
- // Option to keep pending payment information longer in session, defaults to 'false'
187
- paymentPending : 'true' ,
188
- // Option to show payment information in the widget, defaults to 'true'
189
- showPaymentInfo : 'true'
190
- } , {
191
- targetOrigin : event . origin
192
- } ) ;
201
+ handleConfiguration ( event ) ;
193
202
break ;
194
203
}
195
204
case 'payment_intent' : {
@@ -200,7 +209,7 @@ export const Bitrefill = ({ accounts, code }: TProps) => {
200
209
break ;
201
210
}
202
211
}
203
- } , [ account , bitrefillInfo , handlePaymentRequest , isDarkMode ] ) ;
212
+ } , [ bitrefillInfo , handleConfiguration , handlePaymentRequest ] ) ;
204
213
205
214
useEffect ( ( ) => {
206
215
window . addEventListener ( 'message' , handleMessage ) ;
0 commit comments