-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathApp.js
More file actions
171 lines (161 loc) · 3.84 KB
/
Copy pathApp.js
File metadata and controls
171 lines (161 loc) · 3.84 KB
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
163
164
165
166
167
168
169
170
171
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import { SafeAreaView, ScrollView, Text, TouchableOpacity, View } from 'react-native';
import { ApplePayButton, PaymentRequest } from 'react-native-payments';
export default class App extends Component {
state = {
debug: ''
}
debug = text => {
this.setState({
debug: text
})
}
showPaymentSheet = (succeed = true) => {
const paymentRequest = new PaymentRequest(METHOD_DATA, DETAILS);
paymentRequest.show().then(paymentResponse => {
const card_token = paymentResponse.details.paymentToken;
if(succeed) {
paymentResponse.complete('success')
this.debug(`Payment request completed with card token ${card_token}`);
} else {
paymentResponse.complete('failure')
this.debug('Payment request failed');
}
}).catch(error => {
if(error.message === 'AbortError') {
this.debug('Payment request was dismissed');
}
});
};
render() {
return (
<SafeAreaView style={styles.container}>
<ScrollView>
<Text style={styles.title}>
Native Apple Pay Button
</Text>
<ApplePayButton
type="plain"
style="black"
onPress={() => this.showPaymentSheet(true)}
/>
<Text style={styles.title}>
Any tappable component
</Text>
<TouchableOpacity
style={styles.button}
onPress={() => this.showPaymentSheet(true)}
>
<Text style={styles.buttonText}>
Tap me
</Text>
</TouchableOpacity>
<Text style={styles.title}>
Try an error...
</Text>
<TouchableOpacity
style={styles.button}
onPress={() => this.showPaymentSheet(false)}
>
<Text style={styles.buttonText}>
This will fail
</Text>
</TouchableOpacity>
<Text style={styles.title}>
What's next?
</Text>
<Text style={styles.details}>
Thanks for trying out react-native-payments! There are so many options you can pass to PaymentRequest, so check out the main documentation.
</Text>
<Text style={styles.details}>
You can also pass in paymentMethodTokenizationParameters to automatically convert the Apple Pay token to either Stripe or Braintree format.
</Text>
{
this.state.debug.length > 0
&& <View style={styles.debug}>
<Text style={styles.debugText}>
{this.state.debug}
</Text>
</View>
}
</ScrollView>
</SafeAreaView>
);
}
}
const METHOD_DATA = [
{
supportedMethods: ['apple-pay'],
data: {
merchantIdentifier: 'merchant.com.your-app.namespace',
supportedNetworks: ['visa', 'mastercard', 'amex'],
countryCode: 'US',
currencyCode: 'USD',
// // uncomment this block to activate automatic Stripe tokenization.
// // try putting your key pk_test... in here and see how the token format changes.
// paymentMethodTokenizationParameters: {
// parameters: {
// gateway: 'stripe',
// 'stripe:publishableKey': Config.STRIPE_KEY,
// },
// },
},
},
];
const DETAILS = {
id: 'basic-example',
displayItems: [
{
label: 'Movie Ticket',
amount: { currency: 'USD', value: '15.00' },
},
],
total: {
label: 'Freeman Industries',
amount: { currency: 'USD', value: '15.00' },
},
};
const MARGIN = 20;
const styles = {
container: {
margin: MARGIN,
flex: 1,
alignItems: 'stretch'
},
title: {
margin: MARGIN,
marginTop: MARGIN * 3 / 2,
color: '#4000FF',
fontSize: 24,
textAlign: 'center'
},
button: {
backgroundColor: '#4000FF',
padding: MARGIN,
alignItems: 'center'
},
buttonText: {
fontSize: 18,
color: '#FFFFFF',
},
debug: {
marginTop: 'auto',
backgroundColor: '#301139',
padding: MARGIN,
borderRadius: 3
},
debugText: {
textAlign: 'center',
color: '#FFFFFF',
fontFamily: 'Menlo',
},
details: {
marginBottom: MARGIN,
fontSize: 16
},
}