Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 8 additions & 6 deletions modules/@shopify/checkout-sheet-kit/src/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import type {
RemoveEventListeners,
CheckoutEvent,
Configuration,
CheckoutOptions,
} from './index.d';

type Maybe<T> = T | undefined;
Expand All @@ -41,8 +42,8 @@ interface Context {
getConfig: () => Promise<Configuration | undefined>;
setConfig: (config: Configuration) => Promise<void>;
removeEventListeners: RemoveEventListeners;
preload: (checkoutUrl: string) => void;
present: (checkoutUrl: string) => void;
preload: (checkoutUrl: string, options?: CheckoutOptions) => void;
present: (checkoutUrl: string, options?: CheckoutOptions) => void;
dismiss: () => void;
invalidate: () => void;
version: Maybe<string>;
Expand Down Expand Up @@ -95,15 +96,16 @@ export function ShopifyCheckoutSheetProvider({
instance.current?.removeEventListeners(eventName);
}, []);

const present = useCallback((checkoutUrl: string) => {
const present = useCallback((checkoutUrl: string, options?: CheckoutOptions) => {
if (checkoutUrl) {
instance.current?.present(checkoutUrl);
instance.current?.present(checkoutUrl, options
);
}
}, []);

const preload = useCallback((checkoutUrl: string) => {
const preload = useCallback((checkoutUrl: string, options?: CheckoutOptions) => {
if (checkoutUrl) {
instance.current?.preload(checkoutUrl);
instance.current?.preload(checkoutUrl, options);
}
}, []);

Expand Down
12 changes: 11 additions & 1 deletion sample/src/screens/CartScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
ActivityIndicator,
Pressable,
RefreshControl,
Alert,
} from 'react-native';
import Icon from 'react-native-vector-icons/Entypo';

Expand All @@ -48,6 +49,7 @@ import {useTheme} from '../context/Theme';
import {useCart} from '../context/Cart';
import {currency} from '../utils';
import {useShopifyEventHandlers} from '../hooks/useCheckoutEventHandlers';
import { fetchToken } from '../services/TokenClient';

function CartScreen(): React.JSX.Element {
const ShopifyCheckout = useShopifyCheckoutSheet();
Expand Down Expand Up @@ -85,7 +87,15 @@ function CartScreen(): React.JSX.Element {

const presentCheckout = async () => {
if (checkoutURL) {
ShopifyCheckout.present(checkoutURL);
const auth = await fetchToken();
if (!auth) {
Alert.alert('Error', 'Failed to authenticate');
return;
}

ShopifyCheckout.present(checkoutURL, {
authentication: { token: auth },
});
}
};

Expand Down
Loading