forked from cloudipsp/react-native-cloudipsp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudipsp-webview.js
66 lines (60 loc) · 2.38 KB
/
cloudipsp-webview.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
import React from 'react';
import {
View,
WebView
} from 'react-native';
import {Receipt} from './cloudipsp'
export class CloudipspWebView extends React.Component {
constructor(props) {
super(props);
this.state = {};
this.urlStartPattern = 'http://secure-redirect.cloudipsp.com/submit/#';
}
__confirm__ = (baseUrl, html) => {
if (this.onSuccess !== undefined) {
throw new Error('CloudipspWebView already waiting for confirmation');
}
let state = {baseUrl: baseUrl, html: html};
this.setState(state);
return new Promise((onOk, onNotOk) => {
this.onSuccess = onOk;
});
}
render() {
if (this.state.baseUrl === undefined) {
return (<View />);
} else {
return (
<WebView
style={{flex:1}}
ref='webView'
javaScriptEnabled={true}
domStorageEnabled={true}
scalesPageToFit={true}
source={{ baseUrl : this.state.baseUrl, html : this.state.html}}
onLoadStart={(event) => {
if (this.onSuccess !== undefined) {
let url = event.nativeEvent.url;
if (url.startsWith(this.urlStartPattern)) {
let jsonOfConfirmation = url.split(this.urlStartPattern)[1];
console.log('json of confirmation: '+jsonOfConfirmation);
var response;
try {
response = JSON.parse(jsonOfConfirmation);
} catch (e) {
response = JSON.parse(decodeURIComponent(jsonOfConfirmation));
}
let receipt = Receipt.__fromOrderData__(response.params);
this.setState({baseUrl : undefined, html : undefined}, () => {
this.onSuccess(receipt);
this.onSuccess = undefined;
});
this.refs.webView.goBack();
}
}
}}
/>
);
}
}
}