Skip to content

Commit e7b17ff

Browse files
feat: expose checkout options on present / preload (#400)
* feat: expose checkout options on present / preload * fix: make auth optional for present * fix: compilation errors on native level * fix: swift tests
1 parent cf0ce3d commit e7b17ff

File tree

6 files changed

+41
-43
lines changed

6 files changed

+41
-43
lines changed

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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -305,27 +305,6 @@ extension RCTCheckoutWebView: CheckoutDelegate {
305305
])
306306
}
307307

308-
func checkoutDidRequestCardChange(event: CheckoutCardChangeRequested) {
309-
guard let id = event.id else { return }
310-
311-
self.events.set(key: id, event: event)
312-
313-
var eventData: [String: Any] = [
314-
"id": event.id,
315-
"type": "paymentChangeIntent",
316-
]
317-
318-
// Include current card info if available
319-
if let currentCard = event.params.currentCard {
320-
eventData["currentCard"] = [
321-
"last4": currentCard.last4,
322-
"brand": currentCard.brand,
323-
]
324-
}
325-
326-
onPaymentChangeIntent?(eventData)
327-
}
328-
329308
/// Called when the buyer attempts to submit the checkout.
330309
///
331310
/// This event is only emitted when native payment delegation is configured

modules/@shopify/checkout-sheet-kit/src/context.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import type {
3131
RemoveEventListeners,
3232
CheckoutEvent,
3333
Configuration,
34+
CheckoutOptions,
3435
} from './index.d';
3536

3637
type Maybe<T> = T | undefined;
@@ -41,8 +42,8 @@ interface Context {
4142
getConfig: () => Promise<Configuration | undefined>;
4243
setConfig: (config: Configuration) => Promise<void>;
4344
removeEventListeners: RemoveEventListeners;
44-
preload: (checkoutUrl: string) => void;
45-
present: (checkoutUrl: string) => void;
45+
preload: (checkoutUrl: string, options?: CheckoutOptions) => void;
46+
present: (checkoutUrl: string, options?: CheckoutOptions) => void;
4647
dismiss: () => void;
4748
invalidate: () => void;
4849
version: Maybe<string>;
@@ -95,15 +96,16 @@ export function ShopifyCheckoutSheetProvider({
9596
instance.current?.removeEventListeners(eventName);
9697
}, []);
9798

98-
const present = useCallback((checkoutUrl: string) => {
99+
const present = useCallback((checkoutUrl: string, options?: CheckoutOptions) => {
99100
if (checkoutUrl) {
100-
instance.current?.present(checkoutUrl);
101+
instance.current?.present(checkoutUrl, options
102+
);
101103
}
102104
}, []);
103105

104-
const preload = useCallback((checkoutUrl: string) => {
106+
const preload = useCallback((checkoutUrl: string, options?: CheckoutOptions) => {
105107
if (checkoutUrl) {
106-
instance.current?.preload(checkoutUrl);
108+
instance.current?.preload(checkoutUrl, options);
107109
}
108110
}, []);
109111

sample/android/app/src/test/java/com/shopify/checkoutkitreactnative/ShopifyCheckoutSheetKitModuleTest.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import static com.shopify.checkoutkitreactnative.TestFixtures.createTestCart;
44

55
import androidx.activity.ComponentActivity;
6+
import androidx.annotation.NonNull;
7+
import androidx.annotation.Nullable;
68

79
import com.facebook.react.bridge.JavaOnlyMap;
810
import com.facebook.react.bridge.ReactApplicationContext;
@@ -14,6 +16,7 @@
1416
import com.shopify.checkoutsheetkit.Preloading;
1517
import com.shopify.checkoutsheetkit.ColorScheme;
1618
import com.shopify.checkoutsheetkit.Authentication;
19+
import com.shopify.checkoutsheetkit.lifecycleevents.CartPaymentInstrument;
1720
import com.shopify.checkoutsheetkit.lifecycleevents.CheckoutCompleteEvent;
1821
import com.shopify.checkoutsheetkit.lifecycleevents.CheckoutStartEvent;
1922
import com.shopify.checkoutsheetkit.lifecycleevents.Cart;
@@ -44,6 +47,11 @@
4447
import android.content.Context;
4548

4649
import java.util.ArrayList;
50+
import java.util.Collection;
51+
import java.util.Collections;
52+
import java.util.Iterator;
53+
import java.util.List;
54+
import java.util.ListIterator;
4755

4856
@RunWith(MockitoJUnitRunner.class)
4957
public class ShopifyCheckoutSheetKitModuleTest {
@@ -543,18 +551,19 @@ public void testCompleteConfigurationAndEventFlow() {
543551

544552
private Cart buildMinimalCart(String cartId, String amount, String currencyCode) {
545553
return new Cart(
546-
cartId,
547-
new ArrayList<>(), // lines
548-
new CartCost(
549-
new Money(amount, currencyCode),
550-
new Money(amount, currencyCode)
551-
),
552-
new CartBuyerIdentity(null, null, null, null),
553-
new ArrayList<>(), // deliveryGroups
554-
new ArrayList<>(), // discountCodes
555-
new ArrayList<>(), // appliedGiftCards
556-
new ArrayList<>(), // discountAllocations
557-
new CartDelivery(new ArrayList<>())
554+
cartId,
555+
new ArrayList<>(), // lines
556+
new CartCost(
557+
new Money(amount, currencyCode),
558+
new Money(amount, currencyCode)
559+
),
560+
new CartBuyerIdentity(null, null, null, null),
561+
new ArrayList<>(), // deliveryGroups
562+
new ArrayList<>(), // discountCodes
563+
new ArrayList<>(), // appliedGiftCards
564+
new ArrayList<>(), // discountAllocations
565+
new CartDelivery(new ArrayList<>()),
566+
new ArrayList<>() { } // paymentInstruments
558567
);
559568
}
560569

sample/android/app/src/test/java/com/shopify/checkoutkitreactnative/TestFixtures.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ public static Cart createTestCart(
7676
Collections.emptyList(), // discountCodes
7777
Collections.emptyList(), // appliedGiftCards
7878
Collections.emptyList(), // discountAllocations
79-
delivery
79+
delivery,
80+
Collections.emptyList() // paymentInstruments
8081
);
8182
}
8283

sample/ios/ReactNativeTests/RCTCheckoutWebViewTests.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ class RCTCheckoutWebViewTests: XCTestCase {
313313
discountCodes: [],
314314
appliedGiftCards: [],
315315
discountAllocations: [],
316-
delivery: CartDelivery(addresses: [])
316+
delivery: CartDelivery(addresses: []),
317+
payment: .init(instruments: [])
317318
)
318319
let event = CheckoutStartEvent(cart: cart)
319320

@@ -347,7 +348,8 @@ private func createTestCart(
347348
discountCodes: [],
348349
appliedGiftCards: [],
349350
discountAllocations: [],
350-
delivery: .init(addresses: [])
351+
delivery: .init(addresses: []),
352+
payment: .init(instruments: [])
351353
)
352354
}
353355

sample/src/screens/CartScreen.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {useTheme} from '../context/Theme';
4848
import {useCart} from '../context/Cart';
4949
import {currency} from '../utils';
5050
import {useShopifyEventHandlers} from '../hooks/useCheckoutEventHandlers';
51+
import { fetchToken } from '../services/TokenClient';
5152

5253
function CartScreen(): React.JSX.Element {
5354
const ShopifyCheckout = useShopifyCheckoutSheet();
@@ -85,7 +86,11 @@ function CartScreen(): React.JSX.Element {
8586

8687
const presentCheckout = async () => {
8788
if (checkoutURL) {
88-
ShopifyCheckout.present(checkoutURL);
89+
const token = await fetchToken();
90+
91+
ShopifyCheckout.present(checkoutURL, {
92+
authentication: token ? { token } : undefined,
93+
});
8994
}
9095
};
9196

0 commit comments

Comments
 (0)