-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHomeScreen.tsx
162 lines (152 loc) · 4.16 KB
/
HomeScreen.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { useNavigation } from '@react-navigation/native';
import {
AdditionalPaymentMethodType,
CurrencyCode,
hideMockReaderUI,
PromptMode,
showMockReaderUI,
showSettings,
startPayment,
mapUserInfoToFailure,
type PaymentParameters,
type PromptParameters,
} from 'mobile-payments-sdk-react-native';
import React, { useState } from 'react';
import {
View,
Text,
Image,
StyleSheet,
SafeAreaView,
TouchableOpacity,
} from 'react-native';
import uuid from 'react-native-uuid';
import LoadingButton from '../components/LoadingButton';
import HeaderButton from '../components/HeaderButton';
const HomeView = () => {
const navigation = useNavigation();
const [isMockReaderPresented, setMockReaderPresented] = useState(false);
const dismissMockReader = () => {
hideMockReaderUI();
setMockReaderPresented(false);
};
const presentMockReader = async () => {
try {
const result = await showMockReaderUI();
console.log(result);
setMockReaderPresented(true);
} catch (error) {
console.log('Mock Reader UI error:', error);
}
};
const handleStartPayment = async () => {
const paymentParameters: PaymentParameters = {
amountMoney: { amount: 100, currencyCode: CurrencyCode.USD },
appFeeMoney: { amount: 0, currencyCode: CurrencyCode.USD },
idempotencyKey: uuid.v4(),
note: 'Payment for services',
// Other parameters you could add:
// autocomplete: true,
// delayAction: DelayAction.CANCEL,
// tipMoney: { amount: 0, currencyCode: CurrencyCode.USD },
// etc
// For more information, visit
// iOS: https://developer.squareup.com/docs/mobile-payments-sdk/ios/take-payments#create-payment-parameters
// Android: https://developer.squareup.com/docs/mobile-payments-sdk/android/take-payments#create-payment-parameters
};
const promptParameters: PromptParameters = {
additionalMethods: [AdditionalPaymentMethodType.ALL],
mode: PromptMode.DEFAULT,
};
try {
const payment = await startPayment(paymentParameters, promptParameters);
console.log('Payment successful:', payment);
} catch (error) {
// convert the error.userInfo into a Failure object
const failure: Failure = mapUserInfoToFailure(error.userInfo);
console.log('Payment error:', JSON.stringify(failure));
}
};
return (
<SafeAreaView style={styles.container}>
<View style={styles.header}>
<HeaderButton title="Settings" onPress={showSettings} />
<View style={styles.headerSpacer} />
<HeaderButton
title="Permissions"
onPress={() => navigation.navigate('Permissions')}
/>
</View>
<View style={styles.content}>
<Image
source={require('../assets/donut.png')}
style={styles.donutImage}
/>
<Text style={styles.title}>Donut Counter</Text>
<LoadingButton
isLoading={false}
isActive={true}
handleOnPress={() => handleStartPayment()}
activeLabel="Buy for $1"
/>
</View>
<TouchableOpacity
style={styles.mockButton}
onPress={() => {
if (isMockReaderPresented) {
dismissMockReader();
} else {
presentMockReader();
}
}}
>
<Text style={styles.mockReaderText}>
{isMockReaderPresented ? 'Hide Mock Reader' : 'Show Mock Reader'}
</Text>
</TouchableOpacity>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 16,
backgroundColor: 'white',
},
header: {
flexDirection: 'row',
marginBottom: 50,
paddingLeft: 16,
paddingRight: 16,
},
headerSpacer: {
flex: 4,
},
content: {
alignItems: 'center',
flex: 9,
paddingLeft: 16,
paddingRight: 16,
},
donutImage: {
width: 248,
height: 248,
marginBottom: 50,
},
title: {
fontSize: 24,
fontWeight: 'bold',
color: 'black',
marginBottom: 32,
},
mockButton: {
flex: 1,
alignItems: 'center',
},
mockReaderText: {
color: '#007BFF',
fontSize: 16,
fontWeight: 'bold',
},
});
export default HomeView;