@@ -23,8 +23,8 @@ of this software and associated documentation files (the "Software"), to deal
2323
2424package com .shopify .reactnative .checkoutsheetkit ;
2525
26+ import android .app .Activity ;
2627import android .content .Context ;
27- import android .net .Uri ;
2828import android .os .Handler ;
2929import android .os .Looper ;
3030import android .util .AttributeSet ;
@@ -34,32 +34,28 @@ of this software and associated documentation files (the "Software"), to deal
3434import androidx .annotation .NonNull ;
3535
3636import com .facebook .react .bridge .Arguments ;
37+ import com .facebook .react .bridge .ReactApplicationContext ;
3738import com .facebook .react .bridge .ReactContext ;
3839import com .facebook .react .bridge .WritableMap ;
40+ import com .facebook .react .uimanager .ThemedReactContext ;
3941import com .facebook .react .uimanager .events .RCTEventEmitter ;
4042
41- import com .fasterxml .jackson .core .type .TypeReference ;
42- import com .shopify .checkoutsheetkit .CheckoutAddressChangeRequestedEvent ;
43- import com .shopify .checkoutsheetkit .CheckoutException ;
43+ import com .shopify .checkoutsheetkit .Authentication ;
4444import com .shopify .checkoutsheetkit .CheckoutOptions ;
4545import com .shopify .checkoutsheetkit .CheckoutWebView ;
4646import com .shopify .checkoutsheetkit .CheckoutWebViewEventProcessor ;
47- import com .shopify .checkoutsheetkit .DefaultCheckoutEventProcessor ;
48- import com .shopify .checkoutsheetkit .lifecycleevents .CheckoutCompleteEvent ;
49- import com .shopify .checkoutsheetkit .pixelevents .PixelEvent ;
5047
51- import com .fasterxml .jackson .core .JsonProcessingException ;
5248import com .fasterxml .jackson .databind .ObjectMapper ;
5349
54- import java .util .Map ;
5550import java .util .Objects ;
5651
5752import kotlin .Unit ;
5853
5954public class RCTCheckoutWebView extends FrameLayout {
6055 private static final String TAG = "RCTCheckoutWebView" ;
56+ private final ThemedReactContext context ;
6157
62- private CheckoutWebView checkoutWebView ;
58+ private CheckoutWebView checkoutWebView ;
6359 private String checkoutUrl ;
6460 private String auth ;
6561 private boolean pendingSetup = false ;
@@ -80,14 +76,16 @@ public boolean equals(Object obj) {
8076 }
8177 }
8278
83- public RCTCheckoutWebView (Context context ) {
79+ public RCTCheckoutWebView (ThemedReactContext context ) {
8480 super (context );
81+ this .context = context ;
8582 Log .d (TAG , "RCTCheckoutWebView constructor called" );
8683 init ();
8784 }
8885
89- public RCTCheckoutWebView (Context context , AttributeSet attrs ) {
86+ public RCTCheckoutWebView (ThemedReactContext context , AttributeSet attrs ) {
9087 super (context , attrs );
88+ this .context = context ;
9189 Log .d (TAG , "RCTCheckoutWebView constructor with attrs called" );
9290 init ();
9391 }
@@ -166,7 +164,7 @@ private void setupCheckoutWebView(String url, CheckoutConfiguration configuratio
166164 // Configure authentication if provided
167165 CheckoutOptions options = null ;
168166 if (auth != null && !auth .isEmpty ()) {
169- options = new CheckoutOptions (auth );
167+ options = new CheckoutOptions (new Authentication . Token ( auth ) );
170168 }
171169
172170 // Add to view hierarchy
@@ -177,7 +175,7 @@ private void setupCheckoutWebView(String url, CheckoutConfiguration configuratio
177175 addView (checkoutWebView , params );
178176
179177 // Load the URL with options
180- checkoutWebView .loadCheckout (url , false , options );
178+ checkoutWebView .loadCheckout (url , options );
181179 checkoutWebView .notifyPresented ();
182180
183181 // Send onLoad event
@@ -190,8 +188,11 @@ private void setupCheckoutWebView(String url, CheckoutConfiguration configuratio
190188
191189 @ NonNull
192190 private CheckoutWebViewEventProcessor getCheckoutWebViewEventProcessor () {
191+ Activity currentActivity = this .context .getCurrentActivity ();
192+ ReactApplicationContext reactAppContext = this .context .getReactApplicationContext ();
193+
193194 return new CheckoutWebViewEventProcessor (
194- new CheckoutWebEventProcessor ( ),
195+ new CustomCheckoutEventProcessor ( currentActivity , reactAppContext ),
195196 (visible ) -> Unit .INSTANCE , // toggleHeader
196197 (error ) -> Unit .INSTANCE , // closeCheckoutDialogWithError
197198 (visibility ) -> Unit .INSTANCE , // setProgressBarVisibility
@@ -250,108 +251,4 @@ private void sendEvent(String eventName, WritableMap params) {
250251 reactContext .getJSModule (RCTEventEmitter .class )
251252 .receiveEvent (getId (), eventName , params );
252253 }
253-
254- private class CheckoutWebEventProcessor extends DefaultCheckoutEventProcessor {
255-
256- public CheckoutWebEventProcessor () {
257- super (getContext ());
258- }
259-
260- @ Override
261- public void onCheckoutCompleted (CheckoutCompleteEvent event ) {
262- try {
263- String eventJson = mapper .writeValueAsString (event );
264- WritableMap params = Arguments .createMap ();
265- // Parse the JSON to extract fields
266- Map <String , Object > eventMap = mapper .readValue (eventJson , new TypeReference <>(){ });
267- for (Map .Entry <String , Object > entry : eventMap .entrySet ()) {
268- if (entry .getValue () instanceof String ) {
269- params .putString (entry .getKey (), (String ) entry .getValue ());
270- } else if (entry .getValue () instanceof Number ) {
271- params .putDouble (entry .getKey (), ((Number ) entry .getValue ()).doubleValue ());
272- } else if (entry .getValue () instanceof Boolean ) {
273- params .putBoolean (entry .getKey (), (Boolean ) entry .getValue ());
274- } else if (entry .getValue () != null ) {
275- params .putString (entry .getKey (), mapper .writeValueAsString (entry .getValue ()));
276- }
277- }
278-
279- sendEvent ("onComplete" , params );
280- } catch (JsonProcessingException e ) {
281- Log .e (TAG , "Failed to serialize completed event" , e );
282- }
283- }
284-
285- @ Override
286- public void onCheckoutFailed (CheckoutException error ) {
287- WritableMap params = Arguments .createMap ();
288- params .putString ("message" , error .getMessage ());
289- params .putString ("code" , error .getErrorCode ());
290- params .putBoolean ("recoverable" , error .isRecoverable ());
291- sendEvent ("onError" , params );
292- }
293-
294- @ Override
295- public void onCheckoutCanceled () {
296- sendEvent ("onCancel" , Arguments .createMap ());
297- }
298-
299- @ Override
300- public void onWebPixelEvent (PixelEvent event ) {
301- try {
302- String eventJson = mapper .writeValueAsString (event );
303- WritableMap params = Arguments .createMap ();
304-
305- // Parse the JSON to extract fields
306- Map <String , Object > eventMap = mapper .readValue (eventJson , new TypeReference <>(){ });
307- for (Map .Entry <String , Object > entry : eventMap .entrySet ()) {
308- if (entry .getValue () instanceof String ) {
309- params .putString (entry .getKey (), (String ) entry .getValue ());
310- } else if (entry .getValue () != null ) {
311- params .putString (entry .getKey (), mapper .writeValueAsString (entry .getValue ()));
312- }
313- }
314-
315- sendEvent ("onPixelEvent" , params );
316- } catch (JsonProcessingException e ) {
317- Log .e (TAG , "Failed to serialize pixel event" , e );
318- }
319- }
320-
321- @ Override
322- public void onCheckoutLinkClicked (Uri url ) {
323- WritableMap params = Arguments .createMap ();
324- params .putString ("url" , url .toString ());
325- sendEvent ("onClickLink" , params );
326- }
327-
328- @ Override
329- public void onAddressChangeRequested (CheckoutAddressChangeRequestedEvent event ) {
330- String eventId = event .getId ();
331- if (eventId == null ) {
332- Log .e (TAG , "Event ID is null for address change event" );
333- return ;
334- }
335-
336- WritableMap params = Arguments .createMap ();
337- params .putString ("id" , eventId );
338- params .putString ("type" , "addressChangeIntent" );
339- params .putString ("addressType" , event .getAddressType ());
340-
341- // Include selected address if available
342- if (event .getSelectedAddress () != null ) {
343- try {
344- String selectedAddressJson = mapper .writeValueAsString (event .getSelectedAddress ());
345- params .putString ("selectedAddress" , selectedAddressJson );
346- } catch (JsonProcessingException e ) {
347- Log .e (TAG , "Failed to serialize selected address" , e );
348- }
349- }
350-
351- sendEvent ("onAddressChangeIntent" , params );
352- }
353-
354- // Note: Payment change events are not yet supported in Android SDK
355- // This will be added when the SDK supports it
356- }
357254}
0 commit comments