|
| 1 | +import React, {useState} from 'react'; |
| 2 | +import { |
| 3 | + ActivityIndicator, |
| 4 | + Alert, |
| 5 | + ScrollView, |
| 6 | + StyleSheet, |
| 7 | + Text, |
| 8 | + TouchableOpacity, |
| 9 | + View, |
| 10 | +} from 'react-native'; |
| 11 | +import { |
| 12 | + PrimerCheckoutProvider, |
| 13 | + PrimerCheckoutSheet, |
| 14 | +} from '@primer-io/react-native'; |
| 15 | +import type {PrimerSettings} from '@primer-io/react-native'; |
| 16 | +import {createClientSession} from '../network/api'; |
| 17 | +import {appPaymentParameters} from '../models/IClientSessionRequestBody'; |
| 18 | +import {customAppearanceMode} from './SettingsScreen'; |
| 19 | +import {getPaymentHandlingStringVal} from '../network/Environment'; |
| 20 | +import {STRIPE_ACH_PUBLISHABLE_KEY} from '../Keys'; |
| 21 | + |
| 22 | +interface Example { |
| 23 | + id: string; |
| 24 | + title: string; |
| 25 | + description: string; |
| 26 | +} |
| 27 | + |
| 28 | +const EXAMPLES: Example[] = [ |
| 29 | + { |
| 30 | + id: 'default', |
| 31 | + title: 'Default', |
| 32 | + description: 'Basic checkout flow with payment method list', |
| 33 | + }, |
| 34 | +]; |
| 35 | + |
| 36 | +export function CheckoutComponentsListScreen() { |
| 37 | + const [loadingId, setLoadingId] = useState<string | null>(null); |
| 38 | + const [checkoutToken, setCheckoutToken] = useState<string | null>(null); |
| 39 | + const [sheetVisible, setSheetVisible] = useState(false); |
| 40 | + |
| 41 | + const handleOpen = async (example: Example) => { |
| 42 | + setLoadingId(example.id); |
| 43 | + try { |
| 44 | + const response = await createClientSession(); |
| 45 | + setCheckoutToken(response.clientToken); |
| 46 | + setSheetVisible(true); |
| 47 | + } catch (error) { |
| 48 | + Alert.alert('Error', String(error)); |
| 49 | + } finally { |
| 50 | + setLoadingId(null); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + let settings: PrimerSettings = { |
| 55 | + paymentHandling: getPaymentHandlingStringVal( |
| 56 | + appPaymentParameters.paymentHandling, |
| 57 | + ), |
| 58 | + paymentMethodOptions: { |
| 59 | + iOS: { |
| 60 | + urlScheme: 'merchant://primer.io', |
| 61 | + }, |
| 62 | + stripeOptions: { |
| 63 | + publishableKey: STRIPE_ACH_PUBLISHABLE_KEY, |
| 64 | + mandateData: { |
| 65 | + merchantName: 'My Merchant Name', |
| 66 | + }, |
| 67 | + }, |
| 68 | + googlePayOptions: { |
| 69 | + isCaptureBillingAddressEnabled: true, |
| 70 | + isExistingPaymentMethodRequired: false, |
| 71 | + shippingAddressParameters: {isPhoneNumberRequired: true}, |
| 72 | + requireShippingMethod: false, |
| 73 | + emailAddressRequired: true, |
| 74 | + }, |
| 75 | + }, |
| 76 | + uiOptions: { |
| 77 | + appearanceMode: customAppearanceMode, |
| 78 | + }, |
| 79 | + debugOptions: { |
| 80 | + is3DSSanityCheckEnabled: false, |
| 81 | + }, |
| 82 | + clientSessionCachingEnabled: true, |
| 83 | + apiVersion: '2.4', |
| 84 | + }; |
| 85 | + |
| 86 | + if (appPaymentParameters.merchantName) { |
| 87 | + settings.paymentMethodOptions!.applePayOptions = { |
| 88 | + merchantIdentifier: 'merchant.checkout.team', |
| 89 | + merchantName: appPaymentParameters.merchantName, |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + return ( |
| 94 | + <> |
| 95 | + <ScrollView style={componentStyles.container}> |
| 96 | + {EXAMPLES.map(example => { |
| 97 | + const isLoading = loadingId === example.id; |
| 98 | + return ( |
| 99 | + <TouchableOpacity |
| 100 | + key={example.id} |
| 101 | + style={componentStyles.item} |
| 102 | + onPress={() => handleOpen(example)} |
| 103 | + disabled={loadingId !== null}> |
| 104 | + <View style={componentStyles.itemContent}> |
| 105 | + <Text style={componentStyles.itemTitle}>{example.title}</Text> |
| 106 | + <Text style={componentStyles.itemDescription}> |
| 107 | + {example.description} |
| 108 | + </Text> |
| 109 | + </View> |
| 110 | + {isLoading && <ActivityIndicator />} |
| 111 | + </TouchableOpacity> |
| 112 | + ); |
| 113 | + })} |
| 114 | + </ScrollView> |
| 115 | + {checkoutToken !== null && ( |
| 116 | + <PrimerCheckoutProvider |
| 117 | + clientToken={checkoutToken} |
| 118 | + settings={settings} |
| 119 | + onCheckoutComplete={checkoutData => { |
| 120 | + console.log('Checkout complete:', checkoutData); |
| 121 | + setSheetVisible(false); |
| 122 | + }} |
| 123 | + onError={error => { |
| 124 | + console.error('Checkout error:', error); |
| 125 | + Alert.alert('Checkout Error', error.errorId ?? 'Unknown error'); |
| 126 | + }}> |
| 127 | + <PrimerCheckoutSheet |
| 128 | + visible={sheetVisible} |
| 129 | + onRequestDismiss={() => setSheetVisible(false)} |
| 130 | + onDismiss={() => setCheckoutToken(null)} |
| 131 | + /> |
| 132 | + </PrimerCheckoutProvider> |
| 133 | + )} |
| 134 | + </> |
| 135 | + ); |
| 136 | +} |
| 137 | + |
| 138 | +const componentStyles = StyleSheet.create({ |
| 139 | + container: { |
| 140 | + backgroundColor: '#f5f5f5', |
| 141 | + flex: 1, |
| 142 | + }, |
| 143 | + item: { |
| 144 | + alignItems: 'center', |
| 145 | + backgroundColor: 'white', |
| 146 | + borderBottomColor: '#e0e0e0', |
| 147 | + borderBottomWidth: 1, |
| 148 | + flexDirection: 'row', |
| 149 | + padding: 16, |
| 150 | + }, |
| 151 | + itemContent: { |
| 152 | + flex: 1, |
| 153 | + }, |
| 154 | + itemDescription: { |
| 155 | + color: '#666', |
| 156 | + fontSize: 14, |
| 157 | + marginTop: 4, |
| 158 | + }, |
| 159 | + itemTitle: { |
| 160 | + color: '#212121', |
| 161 | + fontSize: 16, |
| 162 | + fontWeight: '600', |
| 163 | + }, |
| 164 | +}); |
0 commit comments