Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -579,8 +579,8 @@ methods - available on both the context provider as well as the class instance.
| Name | Callback | Description |
| ----------- | ----------------------------------------- | ------------------------------------------------------------ |
| `close` | `() => void` | Fired when the checkout has been closed. |
| `completed` | `(event: CheckoutCompletedEvent) => void` | Fired when the checkout has been successfully completed. |
| `started` | `(event: CheckoutStartedEvent) => void` | Fired when the checkout has been started. |
| `complete` | `(event: CheckoutCompleteEvent) => void` | Fired when the checkout has been successfully completed. |
| `start` | `(event: CheckoutStartEvent) => void` | Fired when the checkout has been started. |
| `error` | `(error: {message: string}) => void` | Fired when a checkout exception has been raised. |

### `addEventListener(eventName, callback)`
Expand All @@ -599,10 +599,10 @@ useEffect(() => {
});

const completed = shopifyCheckout.addEventListener(
'completed',
(event: CheckoutCompletedEvent) => {
'complete',
(event: CheckoutCompleteEvent) => {
// Lookup order on checkout completion
const orderId = event.orderDetails.id;
const orderId = event.orderConfirmation.order.id;
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ of this software and associated documentation files (the "Software"), to deal
import com.facebook.react.bridge.ReactApplicationContext;
import com.shopify.checkoutsheetkit.lifecycleevents.CheckoutCompleteEvent;
import com.shopify.checkoutsheetkit.lifecycleevents.CheckoutStartEvent;
import com.shopify.checkoutsheetkit.rpc.events.CheckoutAddressChangeStart;
import com.shopify.checkoutsheetkit.rpc.events.CheckoutAddressChangeStartEvent;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.HashMap;
Expand Down Expand Up @@ -108,7 +110,7 @@ public void onGeolocationPermissionsHidePrompt() {
}

@Override
public void onCheckoutFailed(CheckoutException checkoutError) {
public void onFail(CheckoutException checkoutError) {
try {
String data = mapper.writeValueAsString(populateErrorDetails(checkoutError));
sendEventWithStringData("error", data);
Expand All @@ -118,27 +120,49 @@ public void onCheckoutFailed(CheckoutException checkoutError) {
}

@Override
public void onCheckoutCanceled() {
public void onCancel() {
sendEvent("close", null);
}

@Override
public void onCheckoutCompleted(@NonNull CheckoutCompleteEvent event) {
public void onComplete(@NonNull CheckoutCompleteEvent event) {
try {
String data = mapper.writeValueAsString(event);
sendEventWithStringData("completed", data);
sendEventWithStringData("complete", data);
} catch (IOException e) {
Log.e("ShopifyCheckoutSheetKit", "Error processing completed event", e);
Log.e("ShopifyCheckoutSheetKit", "Error processing complete event", e);
}
}

@Override
public void onCheckoutStarted(@NonNull CheckoutStartEvent event) {
public void onStart(@NonNull CheckoutStartEvent event) {
try {
String data = mapper.writeValueAsString(event);
sendEventWithStringData("started", data);
sendEventWithStringData("start", data);
} catch (IOException e) {
Log.e("ShopifyCheckoutSheetKit", "Error processing started event", e);
Log.e("ShopifyCheckoutSheetKit", "Error processing start event", e);
}
}

@Override
public void onAddressChangeStart(@NonNull CheckoutAddressChangeStart event) {
try {
CheckoutAddressChangeStartEvent params = event.getParams();
if (params == null) {
Log.e("ShopifyCheckoutSheetKit", "Address change event has null params");
return;
}

Map<String, Object> eventData = new HashMap<>();
eventData.put("id", event.getId());
eventData.put("type", "addressChangeStart");
eventData.put("addressType", params.getAddressType());
eventData.put("cart", params.getCart());

String data = mapper.writeValueAsString(eventData);
sendEventWithStringData("addressChangeStart", data);
} catch (IOException e) {
Log.e("ShopifyCheckoutSheetKit", "Error processing address change start event", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void initiateGeolocationRequest(Boolean allow) {

private CheckoutOptions parseCheckoutOptions(ReadableMap options) {
if (options == null) {
return null;
return new CheckoutOptions();
}

// Parse authentication
Expand All @@ -184,12 +184,12 @@ private CheckoutOptions parseCheckoutOptions(ReadableMap options) {
if (authMap != null && authMap.hasKey("token")) {
String token = authMap.getString("token");
if (token != null) {
return new CheckoutOptions(token);
return new CheckoutOptions(new Authentication.Token(token));
}
}
}

return null;
return new CheckoutOptions();
}

private ColorScheme getColorScheme(String colorScheme) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ class RCTCheckoutWebView: UIView {
}
}
@objc var onLoad: RCTDirectEventBlock?
@objc var onStart: RCTBubblingEventBlock?
@objc var onError: RCTBubblingEventBlock?
@objc var onComplete: RCTBubblingEventBlock?
@objc var onCancel: RCTBubblingEventBlock?
@objc var onClickLink: RCTBubblingEventBlock?
@objc var onAddressChangeIntent: RCTBubblingEventBlock?
@objc var onAddressChangeStart: RCTBubblingEventBlock?
@objc var onPaymentChangeIntent: RCTBubblingEventBlock?

override init(frame: CGRect) {
Expand Down Expand Up @@ -209,7 +210,7 @@ class RCTCheckoutWebView: UIView {
try event.respondWith(json: responseData)
print("[CheckoutWebView] Successfully responded to event: \(id)")
self.events.remove(key: id)
} catch let error as EventResponseError {
} catch let error as CheckoutEventResponseError {
print("[CheckoutWebView] Event response error: \(error)")
handleEventError(eventId: id, error: error)
} catch {
Expand All @@ -222,7 +223,7 @@ class RCTCheckoutWebView: UIView {
let errorMessage: String
let errorCode: String

if let eventError = error as? EventResponseError {
if let eventError = error as? CheckoutEventResponseError {
switch eventError {
case .invalidEncoding:
errorMessage = "Invalid response data encoding"
Expand Down Expand Up @@ -257,6 +258,10 @@ class RCTCheckoutWebView: UIView {
}

extension RCTCheckoutWebView: CheckoutDelegate {
func checkoutDidStart(event: CheckoutStartEvent) {
onStart?(ShopifyEventSerialization.serialize(checkoutStartEvent: event))
}

func checkoutDidComplete(event: CheckoutCompletedEvent) {
onComplete?(ShopifyEventSerialization.serialize(checkoutCompletedEvent: event))
}
Expand All @@ -277,15 +282,26 @@ extension RCTCheckoutWebView: CheckoutDelegate {
error.isRecoverable
}

func checkoutDidRequestAddressChange(event: AddressChangeRequested) {
/// Called when checkout starts an address change flow.
/// This method stores the event for later response and emits it to the React Native layer.
///
/// - Parameter event: The address change start event containing:
/// - id: Unique identifier for responding to the event
/// - addressType: Type of address being changed ("shipping" or "billing")
/// - cart: Current cart state
func checkoutDidStartAddressChange(event: CheckoutAddressChangeStart) {
guard let id = event.id else { return }

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

onAddressChangeIntent?([
// Serialize the cart to JSON
let cartJSON = ShopifyEventSerialization.encodeToJSON(from: event.params.cart)

onAddressChangeStart?([
"id": event.id,
"type": "addressChangeIntent",
"type": "addressChangeStart",
"addressType": event.params.addressType,
"cart": cartJSON,
])
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ @interface RCT_EXTERN_MODULE (RCTCheckoutWebViewManager, RCTViewManager)
RCT_EXPORT_VIEW_PROPERTY(onCancel, RCTBubblingEventBlock)

/**
* Emitted when checkout is moving to address selection screen
* Emitted when checkout starts an address change flow
*/
RCT_EXPORT_VIEW_PROPERTY(onAddressChangeIntent, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onAddressChangeStart, RCTBubblingEventBlock)

/**
* Emitted when checkout is moving to payment selection screen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class RCTShopifyCheckoutSheetKit: RCTEventEmitter, CheckoutDelegate {
}

override func supportedEvents() -> [String]! {
return ["close", "completed", "started", "error", "addressChangeIntent"]
return ["close", "complete", "start", "error", "addressChangeStart"]
}

override func startObserving() {
Expand All @@ -66,13 +66,13 @@ class RCTShopifyCheckoutSheetKit: RCTEventEmitter, CheckoutDelegate {

func checkoutDidComplete(event: CheckoutCompletedEvent) {
if hasListeners {
sendEvent(withName: "completed", body: ShopifyEventSerialization.serialize(checkoutCompletedEvent: event))
sendEvent(withName: "complete", body: ShopifyEventSerialization.serialize(checkoutCompletedEvent: event))
}
}

func checkoutDidStart(event: CheckoutStartEvent) {
if hasListeners {
sendEvent(withName: "started", body: ShopifyEventSerialization.serialize(checkoutStartEvent: event))
sendEvent(withName: "start", body: ShopifyEventSerialization.serialize(checkoutStartEvent: event))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {requireNativeComponent, Platform} from 'react-native';
import type {ViewStyle} from 'react-native';
import type {
AcceleratedCheckoutWallet,
CheckoutCompletedEvent,
CheckoutCompleteEvent,
CheckoutException,
} from '..';

Expand Down Expand Up @@ -94,7 +94,7 @@ interface CommonAcceleratedCheckoutButtonsProps {
/**
* Called when checkout is completed successfully
*/
onComplete?: (event: CheckoutCompletedEvent) => void;
onComplete?: (event: CheckoutCompleteEvent) => void;

/**
* Called when checkout is cancelled
Expand Down Expand Up @@ -147,7 +147,7 @@ interface NativeAcceleratedCheckoutButtonsProps {
cornerRadius?: number;
wallets?: AcceleratedCheckoutWallet[];
onFail?: (event: {nativeEvent: CheckoutException}) => void;
onComplete?: (event: {nativeEvent: CheckoutCompletedEvent}) => void;
onComplete?: (event: {nativeEvent: CheckoutCompleteEvent}) => void;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add start here?

onCancel?: () => void;
onRenderStateChange?: (event: {
nativeEvent: {state: string; reason?: string | undefined};
Expand Down Expand Up @@ -209,7 +209,7 @@ export const AcceleratedCheckoutButtons: React.FC<
);

const handleComplete = useCallback(
(event: {nativeEvent: CheckoutCompletedEvent}) => {
(event: {nativeEvent: CheckoutCompleteEvent}) => {
onComplete?.(event.nativeEvent);
},
[onComplete],
Expand Down
Loading
Loading