Skip to content

Commit 4c2276f

Browse files
authored
Migrate from address change requested to address change start (#394)
1 parent 9aa83c5 commit 4c2276f

File tree

20 files changed

+1077
-229
lines changed

20 files changed

+1077
-229
lines changed

modules/@shopify/checkout-sheet-kit/android/src/main/java/com/shopify/reactnative/checkoutsheetkit/CustomCheckoutEventProcessor.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ of this software and associated documentation files (the "Software"), to deal
3636
import com.facebook.react.bridge.ReactApplicationContext;
3737
import com.shopify.checkoutsheetkit.lifecycleevents.CheckoutCompleteEvent;
3838
import com.shopify.checkoutsheetkit.lifecycleevents.CheckoutStartEvent;
39+
import com.shopify.checkoutsheetkit.rpc.events.CheckoutAddressChangeStart;
40+
import com.shopify.checkoutsheetkit.rpc.events.CheckoutAddressChangeStartEvent;
3941
import com.fasterxml.jackson.databind.ObjectMapper;
4042
import java.io.IOException;
4143
import java.util.HashMap;
@@ -142,6 +144,28 @@ public void onStart(@NonNull CheckoutStartEvent event) {
142144
}
143145
}
144146

147+
@Override
148+
public void onAddressChangeStart(@NonNull CheckoutAddressChangeStart event) {
149+
try {
150+
CheckoutAddressChangeStartEvent params = event.getParams();
151+
if (params == null) {
152+
Log.e("ShopifyCheckoutSheetKit", "Address change event has null params");
153+
return;
154+
}
155+
156+
Map<String, Object> eventData = new HashMap<>();
157+
eventData.put("id", event.getId());
158+
eventData.put("type", "addressChangeStart");
159+
eventData.put("addressType", params.getAddressType());
160+
eventData.put("cart", params.getCart());
161+
162+
String data = mapper.writeValueAsString(eventData);
163+
sendEventWithStringData("addressChangeStart", data);
164+
} catch (IOException e) {
165+
Log.e("ShopifyCheckoutSheetKit", "Error processing address change start event", e);
166+
}
167+
}
168+
145169
// Private
146170

147171
private Map<String, Object> populateErrorDetails(CheckoutException checkoutError) {

modules/@shopify/checkout-sheet-kit/android/src/main/java/com/shopify/reactnative/checkoutsheetkit/ShopifyCheckoutSheetKitModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public void initiateGeolocationRequest(Boolean allow) {
175175

176176
private CheckoutOptions parseCheckoutOptions(ReadableMap options) {
177177
if (options == null) {
178-
return null;
178+
return new CheckoutOptions();
179179
}
180180

181181
// Parse authentication

modules/@shopify/checkout-sheet-kit/ios/RCTCheckoutWebView.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class RCTCheckoutWebView: UIView {
8484
@objc var onComplete: RCTBubblingEventBlock?
8585
@objc var onCancel: RCTBubblingEventBlock?
8686
@objc var onLinkClick: RCTBubblingEventBlock?
87-
@objc var onAddressChangeIntent: RCTBubblingEventBlock?
87+
@objc var onAddressChangeStart: RCTBubblingEventBlock?
8888
@objc var onPaymentChangeIntent: RCTBubblingEventBlock?
8989

9090
override init(frame: CGRect) {
@@ -282,15 +282,27 @@ extension RCTCheckoutWebView: CheckoutDelegate {
282282
error.isRecoverable
283283
}
284284

285-
func checkoutDidRequestAddressChange(event: AddressChangeRequested) {
285+
/// Called when checkout starts an address change flow.
286+
///
287+
/// This event is only emitted when native address selection is enabled
288+
/// for the authenticated app.
289+
///
290+
/// - Parameter event: The address change start event containing:
291+
/// - id: Unique identifier for responding to the event
292+
/// - addressType: Type of address being changed ("shipping" or "billing")
293+
/// - cart: Current cart state
294+
func checkoutDidStartAddressChange(event: CheckoutAddressChangeStart) {
286295
guard let id = event.id else { return }
287296

288297
self.events.set(key: id, event: event)
289298

290-
onAddressChangeIntent?([
299+
let cartJSON = ShopifyEventSerialization.encodeToJSON(from: event.params.cart)
300+
301+
onAddressChangeStart?([
291302
"id": event.id,
292-
"type": "addressChangeIntent",
303+
"type": "addressChangeStart",
293304
"addressType": event.params.addressType,
305+
"cart": cartJSON,
294306
])
295307
}
296308

modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit+EventSerialization.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ internal enum ShopifyEventSerialization {
7373
return encodeToJSON(from: event)
7474
}
7575

76+
/**
77+
* Converts a CheckoutAddressChangeStart to a React Native compatible dictionary.
78+
*/
79+
static func serialize(checkoutAddressChangeStart event: CheckoutAddressChangeStart) -> [String: Any] {
80+
return [
81+
"id": event.id as Any,
82+
"type": "addressChangeStart",
83+
"addressType": event.params.addressType,
84+
"cart": encodeToJSON(from: event.params.cart)
85+
]
86+
}
87+
7688
static func serialize(clickEvent url: URL) -> [String: URL] {
7789
return ["url": url]
7890
}

modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ @interface RCT_EXTERN_MODULE (RCTCheckoutWebViewManager, RCTViewManager)
126126
/**
127127
* Emitted when checkout is moving to address selection screen
128128
*/
129-
RCT_EXPORT_VIEW_PROPERTY(onAddressChangeIntent, RCTBubblingEventBlock)
129+
RCT_EXPORT_VIEW_PROPERTY(onAddressChangeStart, RCTBubblingEventBlock)
130130

131131
/**
132132
* Emitted when checkout is moving to payment selection screen

modules/@shopify/checkout-sheet-kit/ios/ShopifyCheckoutSheetKit.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class RCTShopifyCheckoutSheetKit: RCTEventEmitter, CheckoutDelegate {
5353
}
5454

5555
override func supportedEvents() -> [String]! {
56-
return ["close", "complete", "start", "error", "addressChangeIntent"]
56+
return ["close", "complete", "start", "error", "addressChangeStart"]
5757
}
5858

5959
override func startObserving() {
@@ -76,6 +76,12 @@ class RCTShopifyCheckoutSheetKit: RCTEventEmitter, CheckoutDelegate {
7676
}
7777
}
7878

79+
func checkoutDidStartAddressChange(event: CheckoutAddressChangeStart) {
80+
if hasListeners {
81+
sendEvent(withName: "addressChangeStart", body: ShopifyEventSerialization.serialize(checkoutAddressChangeStart: event))
82+
}
83+
}
84+
7985
func shouldRecoverFromError(error: CheckoutError) -> Bool {
8086
return error.isRecoverable
8187
}

modules/@shopify/checkout-sheet-kit/src/components/Checkout.tsx

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import type {
3636
CheckoutException,
3737
} from '..';
3838
import {useCheckoutEvents} from '../CheckoutEventProvider';
39-
import type {CheckoutAddressChangeIntent, CheckoutPaymentChangeIntent, CheckoutStartEvent} from '../events';
39+
import type {CheckoutAddressChangeStart, CheckoutPaymentChangeIntent, CheckoutStartEvent} from '../events';
4040

4141
export interface CheckoutProps {
4242
/**
@@ -80,9 +80,12 @@ export interface CheckoutProps {
8080
onLinkClick?: (url: string) => void;
8181

8282
/**
83-
* Called when checkout requests an address change (e.g., for native address picker)
83+
* Called when checkout starts an address change flow (e.g., for native address picker).
84+
*
85+
* Note: This callback is only invoked when native address selection is enabled
86+
* for the authenticated app.
8487
*/
85-
onAddressChangeIntent?: (event: CheckoutAddressChangeIntent) => void;
88+
onAddressChangeStart?: (event: CheckoutAddressChangeStart) => void;
8689

8790
/**
8891
* Called when checkout requests a payment method change (e.g., for native payment selector)
@@ -93,6 +96,11 @@ export interface CheckoutProps {
9396
* Style for the webview container
9497
*/
9598
style?: ViewStyle;
99+
100+
/**
101+
* Test identifier for testing
102+
*/
103+
testID?: string;
96104
}
97105

98106
export interface CheckoutRef {
@@ -106,19 +114,14 @@ interface NativeCheckoutWebViewProps {
106114
checkoutUrl: string;
107115
auth?: string;
108116
style?: ViewStyle;
117+
testID?: string;
109118
onLoad?: (event: {nativeEvent: {url: string}}) => void;
110119
onStart?: (event: {nativeEvent: CheckoutStartEvent}) => void;
111120
onError?: (event: {nativeEvent: CheckoutException}) => void;
112121
onComplete?: (event: {nativeEvent: CheckoutCompleteEvent}) => void;
113122
onCancel?: () => void;
114123
onLinkClick?: (event: {nativeEvent: {url: string}}) => void;
115-
onAddressChangeIntent?: (event: {
116-
nativeEvent: {
117-
id: string;
118-
type: string;
119-
addressType: string;
120-
};
121-
}) => void;
124+
onAddressChangeStart?: (event: {nativeEvent: CheckoutAddressChangeStart}) => void;
122125
onPaymentChangeIntent?: (event: {
123126
nativeEvent: {
124127
id: string;
@@ -186,9 +189,10 @@ export const Checkout = forwardRef<CheckoutRef, CheckoutProps>(
186189
onComplete,
187190
onCancel,
188191
onLinkClick,
189-
onAddressChangeIntent,
192+
onAddressChangeStart,
190193
onPaymentChangeIntent,
191194
style,
195+
testID,
192196
},
193197
ref,
194198
) => {
@@ -257,16 +261,14 @@ export const Checkout = forwardRef<CheckoutRef, CheckoutProps>(
257261
[onLinkClick],
258262
);
259263

260-
const handleAddressChangeIntent = useCallback<
261-
Required<NativeCheckoutWebViewProps>['onAddressChangeIntent']
264+
const handleAddressChangeStart = useCallback<
265+
Required<NativeCheckoutWebViewProps>['onAddressChangeStart']
262266
>(
263-
(event: {
264-
nativeEvent: {id: string; type: string; addressType: string};
265-
}) => {
267+
(event: {nativeEvent: CheckoutAddressChangeStart}) => {
266268
if (!event.nativeEvent) return;
267-
onAddressChangeIntent?.(event.nativeEvent);
269+
onAddressChangeStart?.(event.nativeEvent);
268270
},
269-
[onAddressChangeIntent],
271+
[onAddressChangeStart],
270272
);
271273

272274
const handlePaymentChangeIntent = useCallback<
@@ -317,13 +319,14 @@ export const Checkout = forwardRef<CheckoutRef, CheckoutProps>(
317319
checkoutUrl={checkoutUrl}
318320
auth={auth}
319321
style={style}
322+
testID={testID}
320323
onLoad={handleLoad}
321324
onStart={handleStart}
322325
onError={handleError}
323326
onComplete={handleComplete}
324327
onCancel={handleCancel}
325328
onLinkClick={handleLinkClick}
326-
onAddressChangeIntent={handleAddressChangeIntent}
329+
onAddressChangeStart={handleAddressChangeStart}
327330
onPaymentChangeIntent={handlePaymentChangeIntent}
328331
/>
329332
);

0 commit comments

Comments
 (0)