Skip to content
Merged
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
1 change: 1 addition & 0 deletions platforms/android/lib/api/lib.api
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public final class com/shopify/checkoutkit/CheckoutProtocol {
public static final field SPEC_VERSION Ljava/lang/String;
public final fun getComplete ()Lcom/shopify/ucp/embedded/checkout/NotificationDescriptor;
public final fun getError ()Lcom/shopify/ucp/embedded/checkout/NotificationDescriptor;
public final fun getFulfillmentChange ()Lcom/shopify/ucp/embedded/checkout/NotificationDescriptor;
public final fun getLineItemsChange ()Lcom/shopify/ucp/embedded/checkout/NotificationDescriptor;
public final fun getMessagesChange ()Lcom/shopify/ucp/embedded/checkout/NotificationDescriptor;
public final fun getStart ()Lcom/shopify/ucp/embedded/checkout/NotificationDescriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public object CheckoutProtocol {
public val messagesChange: NotificationDescriptor<Checkout> = EmbeddedCheckoutProtocol.messagesChange
public val lineItemsChange: NotificationDescriptor<Checkout> = EmbeddedCheckoutProtocol.lineItemsChange
public val totalsChange: NotificationDescriptor<Checkout> = EmbeddedCheckoutProtocol.totalsChange
public val fulfillmentChange: NotificationDescriptor<Checkout> = EmbeddedCheckoutProtocol.fulfillmentChange
public val error: NotificationDescriptor<ErrorResponse> = EmbeddedCheckoutProtocol.error

public val windowOpen: DelegationDescriptor<WindowOpenRequest, WindowOpenResult> = EmbeddedCheckoutProtocol.windowOpen.map(
Expand All @@ -53,6 +54,7 @@ public object CheckoutProtocol {
lineItemsChange.method,
messagesChange.method,
totalsChange.method,
fulfillmentChange.method,
windowOpen.method,
)

Expand All @@ -75,6 +77,7 @@ public object CheckoutProtocol {
messagesChange,
lineItemsChange,
totalsChange,
fulfillmentChange,
error,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class CheckoutProtocolTest {
assertThat(CheckoutProtocol.lineItemsChange.method).isEqualTo("ec.line_items.change")
}

@Test
fun `fulfillmentChange descriptor has correct method`() {
assertThat(CheckoutProtocol.fulfillmentChange.method).isEqualTo("ec.fulfillment.change")
}

@Test
fun `error descriptor has correct method`() {
assertThat(CheckoutProtocol.error.method).isEqualTo("ec.error")
Expand All @@ -75,6 +80,7 @@ class CheckoutProtocolTest {
CheckoutProtocol.lineItemsChange.method,
CheckoutProtocol.messagesChange.method,
CheckoutProtocol.totalsChange.method,
CheckoutProtocol.fulfillmentChange.method,
CheckoutProtocol.windowOpen.method,
)
}
Expand Down Expand Up @@ -150,6 +156,18 @@ class CheckoutProtocolTest {
assertThat(received).hasSize(1)
}

@Test
fun `process dispatches ec fulfillment change to registered handler`() {
val received = mutableListOf<Checkout>()
val client = CheckoutProtocol.Client()
.on(CheckoutProtocol.fulfillmentChange) { checkout -> received.add(checkout) }

client.process(checkoutMessage(method = CheckoutProtocol.fulfillmentChange.method))
shadowOf(Looper.getMainLooper()).runToEndOfTasks()

assertThat(received).hasSize(1)
}

@Test
fun `process does not dispatch to unregistered method`() {
val received = mutableListOf<Checkout>()
Expand Down Expand Up @@ -506,10 +524,22 @@ class CheckoutProtocolTest {
// region helpers

private fun ecStartMessage(currency: String = "USD"): String =
"""{"jsonrpc":"2.0","method":"ec.start","params":{"checkout":${checkoutJson(currency = currency)}}}"""
checkoutMessage(method = "ec.start", currency = currency)

private fun ecCompleteMessage(): String =
"""{"jsonrpc":"2.0","method":"ec.complete","params":{"checkout":${checkoutJson(status = "completed")}}}"""
checkoutMessage(method = "ec.complete", status = "completed")

private fun checkoutMessage(
method: String,
currency: String = "USD",
status: String = "incomplete",
): String {
val checkout = checkoutJson(
currency = currency,
status = status,
)
return """{"jsonrpc":"2.0","method":"$method","params":{"checkout":$checkout}}"""
}

private fun windowOpenMessage(id: String, url: String = "https://example.com"): String =
"""{"jsonrpc":"2.0","method":"ec.window.open_request","id":$id,"params":{"url":"$url"}}"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class CartViewModel(
.on(CheckoutProtocol.totalsChange) { Timber.i("ECP ec.totals.change: $it") }
.on(CheckoutProtocol.lineItemsChange) { Timber.i("ECP ec.line_items.change: $it") }
.on(CheckoutProtocol.messagesChange) { Timber.i("ECP ec.messages.change: $it") }
.on(CheckoutProtocol.fulfillmentChange) { Timber.i("ECP ec.fulfillment.change: $it") }

return when (windowOpenHandler) {
WindowOpenHandler.Default -> base
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ public final class com/shopify/ucp/embedded/checkout/EmbeddedCheckoutProtocol {
public static final field SPEC_VERSION Ljava/lang/String;
public final fun getComplete ()Lcom/shopify/ucp/embedded/checkout/NotificationDescriptor;
public final fun getError ()Lcom/shopify/ucp/embedded/checkout/NotificationDescriptor;
public final fun getFulfillmentChange ()Lcom/shopify/ucp/embedded/checkout/NotificationDescriptor;
public final fun getLineItemsChange ()Lcom/shopify/ucp/embedded/checkout/NotificationDescriptor;
public final fun getMessagesChange ()Lcom/shopify/ucp/embedded/checkout/NotificationDescriptor;
public final fun getStart ()Lcom/shopify/ucp/embedded/checkout/NotificationDescriptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public object EmbeddedCheckoutProtocol {
public val totalsChange: NotificationDescriptor<Checkout>
get() = embeddedCheckoutTotalsChangeDescriptor

public val fulfillmentChange: NotificationDescriptor<Checkout>
get() = embeddedCheckoutFulfillmentChangeDescriptor

public val error: NotificationDescriptor<ErrorResponse>
get() = embeddedCheckoutErrorDescriptor

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ internal val embeddedCheckoutLineItemsChangeDescriptor: NotificationDescriptor<C
checkoutDescriptor(EmbeddedCheckoutProtocol.Event.lineItemsChange)
internal val embeddedCheckoutTotalsChangeDescriptor: NotificationDescriptor<Checkout> =
checkoutDescriptor(EmbeddedCheckoutProtocol.Event.totalsChange)
internal val embeddedCheckoutFulfillmentChangeDescriptor: NotificationDescriptor<Checkout> =
checkoutDescriptor(EmbeddedCheckoutProtocol.Event.fulfillmentChange)
internal val embeddedCheckoutErrorDescriptor: NotificationDescriptor<ErrorResponse> = notificationDescriptor(
method = EmbeddedCheckoutProtocol.Event.error,
paramsSerializer = ErrorParams.serializer(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ class EmbeddedCheckoutProtocolTest {
assertThat(payload?.currency).isEqualTo("USD")
}

@Test
fun `embedded checkout fulfillment change descriptor decodes checkout notifications`() {
val payload = EmbeddedCheckoutProtocol.fulfillmentChange.decode(Json.parseToJsonElement(checkoutParamsFixture))

assertThat(payload?.id).isEqualTo("checkout-123")
assertThat(payload?.currency).isEqualTo("USD")
}

@Test
fun `embedded checkout descriptors decode error notifications`() {
val payload = EmbeddedCheckoutProtocol.error.decode(Json.parseToJsonElement(errorParamsFixture))
Expand Down
3 changes: 3 additions & 0 deletions protocol/scripts/generate_kotlin_catalog.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ ${delegations.map(delegation => ` ${delegation.identifier},`).joi
public val totalsChange: NotificationDescriptor<Checkout>
get() = embeddedCheckoutTotalsChangeDescriptor

public val fulfillmentChange: NotificationDescriptor<Checkout>
get() = embeddedCheckoutFulfillmentChangeDescriptor

public val error: NotificationDescriptor<ErrorResponse>
get() = embeddedCheckoutErrorDescriptor

Expand Down
Loading