-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example.js
399 lines (340 loc) · 8.8 KB
/
Example.js
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React from 'react';
import {
StyleSheet,
Text,
TextInput,
View,
KeyboardAvoidingView,
Button,
Switch,
Platform,
Dimensions,
Alert,
} from 'react-native';
import { P24Payment } from './P24';
function getTestTransactionParams(amount = 3) {
return {
sessionId: generateSessionId(),
amount: amount,
currency: 'PLN',
description: 'Test payment description',
email: 'test@test.com',
country: 'PL',
client: 'John Smith',
address: 'Test street',
zip: '20-001',
city: 'Lublin',
phone: '1246423234',
language: 'pl',
}
}
function getTestPassageItems(itemsCount = 10) {
let amount = 0;
let items = [];
for (let i = 0; i < itemsCount; i++) {
const price = 2 * (100 + i);
const item = {
name: 'Product name ' + i,
description: 'Product description ' + i,
number: i,
quantity: 2,
price: price / 2,
targetAmount: price,
// targetPosId: i / 2 == 1 ? 51986 : 51987,
}
amount += item['price'];
items.push(item);
}
return { amount, items };
}
function getTestApplePayParams(appleMerchantId, amount = 3) {
if (typeof appleMerchantId === 'number') {
amount = appleMerchantId;
appleMerchantId = void 0;
}
return {
appleMerchantId: appleMerchantId || 'merchant.Przelewy24.sandbox',
amount: amount,
currency: 'PLN',
description: 'Test payment with Apple Pay',
}
}
function getTestApplePayParamsWithItems(appleMerchantId, amount) {
const params = getTestApplePayParams(appleMerchantId, amount);
params.items = [
{ amount: params.amount + 1, itemDescription: 'Item 1st' },
{ amount: params.amount + 3, itemDescription: 'Item 2nd' },
];
return params;
}
function generateSessionId() {
return 'xxxxxxxxxxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
let r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
})
}
export class SandboxSwitch extends React.PureComponent {
render() {
return (
<View style={styles.buttonContainer}>
<Text style={styles.instructions}>
{'Sandbox'}
</Text>
<Switch
title={'Sandbox'}
value={this.props.value}
onValueChange={this.props.onChange}
/>
<Text style={styles.instructions}>
{'\n'}
</Text>
</View>
)
}
}
export class P24TestButton extends React.PureComponent
{
render() {
return (
<View style={styles.buttonContainer}>
<Button {...this.props} />
</View>
)
}
}
export class TrnRequestButton extends React.PureComponent {
render() {
return (
<P24TestButton
title={'Transfer trnRequest'}
{...this.props}
/>
)
}
}
export class TrnDirectButton extends React.PureComponent {
render() {
return (
<P24TestButton
title={'Transfer trnDirect'}
{...this.props}
/>
)
}
}
export class ExpressButton extends React.PureComponent {
render() {
return (
<P24TestButton
title={'Transfer express'}
{...this.props}
/>
)
}
}
export class PassageButton extends React.PureComponent {
render() {
return (
<P24TestButton
title={'Transfer passage'}
{...this.props}
/>
)
}
}
export class ApplePayButton extends React.PureComponent {
render() {
return (
<P24TestButton
{...this.props}
title={this.props.title || 'Apple Pay'}
disabled={Platform.OS !== 'ios'}
/>
)
}
}
export class GooglePayButton extends React.PureComponent {
render() {
return (
<P24TestButton
{...this.props}
title={this.props.title || 'Google Pay'}
disabled={Platform.OS !== 'android'}
/>
)
}
}
GooglePayButton
export class TokenInput extends React.PureComponent {
render() {
return (
<View>
<Text style={styles.instructions}>
{'\n'}
{'Transaction token/url:'}
</Text>
<TextInput
style={styles.input}
value={this.props.value}
onChangeText={this.props.onChange}
/>
</View>
)
}
}
export default class P24Example extends React.PureComponent {
constructor(...args) {
super(...args);
this.state = {
is_sandbox: this.props.isSandbox,
url_or_token: '',
}
const checkProps = () => {
const { merchantId, crc, isSandbox, sandboxCrc } = this.props;
const info = 'Missing required props: '+ ['merchantId', 'crc', isSandbox && 'sandboxCrc (when isSandbox={true})'].filter(e => e).join(', ');
if (!merchantId || !crc || (isSandbox && !sandboxCrc)) {
Alert.alert('Warning', info);
}
}
if (process?.env?.NODE_ENV && process?.env?.NODE_ENV !== 'production' && Platform.OS == 'ios') {
const info = 'The library contains anti-debug traps, so when using the library methods make sure the „Debug Executable” option is off.';
Alert.alert('Warning', info, [{ onPress:checkProps, title:'checkProps' }]);
} else {
checkProps();
}
}
constructP24() {
const p24 = new P24Payment({
merchant_id: this.props.merchantId,
crc: this.props.crc,
sandbox_crc: this.props.sandboxCrc,
is_sandbox: this.state.is_sandbox,
ssl_pinning: this.props.sslPinning,
})
console.log({ p24 }, this.props);
return p24;
}
getCallbacks() {
return {
success: this.props.onSuccess || (msg => console.log(`Success: ${msg}`)),
cancel: this.props.onCancel || (msg => console.log(`Cancel: ${msg}`)),
error: this.props.onError || (msg => console.log(`Error: ${msg}`)),
}
}
startTrnRequest = () => {
this.constructP24().startTrnRequest(this.state.url_or_token, this.getCallbacks());
}
startTrnDirect = () => {
this.constructP24().startTrnDirect(getTestTransactionParams(), this.getCallbacks());
}
startTrnExpress = () => {
this.constructP24().startTrnExpress(this.state.url_or_token, this.getCallbacks());
}
startTrnPassage = () => {
const { amount, items } = getTestPassageItems(4);
const params = getTestTransactionParams(amount);
this.constructP24().startTrnPassage(params, items, this.getCallbacks());
}
startApplePay = async () => {
const p24 = this.constructP24();
const { appleMerchantId } = this.props;
try {
P24Payment.canMakeApplePayPayments().then((can) => console.log({ can })).catch(e => console.warn(e));
console.log('Can make payments?', await P24Payment.canMakeApplePayPayments());
} catch (e) {
console.error(e);
}
if (!appleMerchantId) {
return Alert.alert('Error', 'You should provide `appleMerchantId` prop for testing Apple Pay');
}
p24.startApplePay(getTestApplePayParams(appleMerchantId), this.getCallbacks(), this.onApplePayToken);
}
startApplePayWithItems = () => {
const { appleMerchantId } = this.props;
if (!appleMerchantId) {
return Alert.alert('Error', 'You should provide `appleMerchantId` prop for testing Apple Pay');
}
this.constructP24().startApplePay(getTestApplePayParamsWithItems(appleMerchantId), this.getCallbacks());
}
onApplePayToken = async (token, ...args) => {
const { onApplePayToken } = this.props;
console.log(typeof onApplePayToken, { token, args });
if (typeof onApplePayToken !== 'function') {
const info = 'You should provide `onApplePayToken` prop (async func, which should return valid P24_TRANSACTION_TOKEN) for finish Apple Pay';
Alert.alert('Error', info);
P24Payment.clear();
return;
}
const p24_token = await onApplePayToken(token);
await P24Payment.finishApplePay(p24_token);
}
startGoolePay = async () => {
console.error('Google Pay is not implemented yet');
}
render() {
return (
<KeyboardAvoidingView style={styles.container} behavior={'position'} enabled>
<Text style={styles.welcome}>
{'Przelewy24 (PayPro S.A.) payments implementation for React Native'}
</Text>
<Text style={styles.instructions}>
{'To get started, press any button below\n'}
</Text>
<SandboxSwitch
value={this.state.is_sandbox}
onChange={(is_sandbox) => this.setState({ is_sandbox })}
/>
<TrnRequestButton onPress={this.startTrnRequest} />
<TrnDirectButton onPress={this.startTrnDirect} />
<ExpressButton onPress={this.startTrnExpress} />
<PassageButton onPress={this.startTrnPassage} />
<ApplePayButton onPress={this.startApplePay} />
<ApplePayButton
onPress={this.startApplePayWithItems}
title={'Apple Pay with items'}
/>
<GooglePayButton onPress={this.startGoolePay} />
<TokenInput
value={this.state.url_or_token}
onChange={(url_or_token) => this.setState({ url_or_token })}
/>
</KeyboardAvoidingView>
)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
input: {
width: Dimensions.get('window').width * 0.8,
height: 40,
borderBottomWidth: 0.5,
color: '#333333',
},
buttonContainer: {
justifyContent: 'center',
alignItems: 'center',
marginVertical: 4,
},
})