forked from just1and0/React-Native-Paystack-WebView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
207 lines (191 loc) · 6.65 KB
/
index.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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/
import React, {
useState,
useEffect,
forwardRef,
useImperativeHandle,
} from "react";
import {
Modal,
Text,
View,
TouchableOpacity,
ActivityIndicator,
SafeAreaView,
} from "react-native";
import { WebView } from "react-native-webview";
function Paystack(props, ref) {
const [isLoading, setisLoading] = useState(true);
const [showModal, setshowModal] = useState(false);
useEffect(() => {
autoStartCheck();
}, []);
const autoStartCheck = () => {
if (props.autoStart) {
setshowModal(true);
}
};
useImperativeHandle(ref, () => ({
StartTransaction() {
setshowModal(true);
},
endTransaction() {
setshowModal(false);
},
}));
const Paystackcontent = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Fonts -->
<link rel="dns-prefetch" href="//fonts.gstatic.com">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet" type="text/css">
<title>SUBSCRIPTION</title>
</head>
<body onload="payWithPaystack()" style="background-color:#fff;height:100vh ">
<script src="https://js.paystack.co/v1/inline.js"></script>
<script type="text/javascript">
window.onload = payWithPaystack;
function payWithPaystack(){
var handler = PaystackPop.setup({
key: '${props.paystackKey}',
email: '${props.billingEmail}',
amount: ${props.amount}00,
channels: ${props.channels},
currency: ${props.currency},
ref: ${props.refNumber}, // generates a pseudo-unique reference. Please replace with a reference you generated. Or remove the line entirely so our API will generate one for you
metadata: {
custom_fields: [
{
display_name: '${props.billingName}',
variable_name: '${props.billingName}',
value:''
}
]
},
callback: function(response){
var resp = {event:'successful', transactionRef:response};
window.ReactNativeWebView.postMessage(JSON.stringify(resp))
},
onClose: function(){
var resp = {event:'cancelled'};
window.ReactNativeWebView.postMessage(JSON.stringify(resp))
}
});
handler.openIframe();
}
</script>
</body>
</html>
`;
const messageReceived = (data) => {
var webResponse = JSON.parse(data);
if (props.handleWebViewMessage) {
props.handleWebViewMessage(data);
}
//console.log(webResponse.event);
switch (webResponse.event) {
case "cancelled":
setshowModal(false);
props.onCancel({status:'cancelled'});
break;
case "successful":
setshowModal(false);
const reference = webResponse.transactionRef.reference;
fetch(`https://api.paystack.co/transaction/verify/${reference}`, {
method: "GET",
headers: new Headers({
Authorization: "Bearer " + props.paystackSecretKey,
}),
})
.then((response) => response.json())
.then((data) => {
props.onSuccess({
status: "success",
data: webResponse.transactionRef,
cardDetails: data.data.authorization,
});
})
.catch((error) => {
props.onCancel({
status: "unverified",
data: webResponse.transactionRef,
message: "payment successful but could not verify payment"
});
});
break;
default:
if (props.handleWebViewMessage) {
props.handleWebViewMessage(data);
}
console.warn("Unhandled event", webResponse);
break;
}
};
const showPaymentModal = () => {
setshowModal(true);
};
const button = props.renderButton ? (
props.renderButton(showPaymentModal)
) : (
<TouchableOpacity
style={props.btnStyles}
onPress={() => showPaymentModal()}
>
<Text style={props.textStyles}>{props.buttonText}</Text>
</TouchableOpacity>
);
return (
<SafeAreaView style={[{ flex: 1 }, props.SafeAreaViewContainer]}>
<Modal
style={[{ flex: 1 }]}
visible={showModal}
animationType="slide"
transparent={false}
>
<SafeAreaView style={[{ flex: 1 }, props.SafeAreaViewContainerModal]}>
<WebView
style={[{ flex: 1 }]}
source={{ html: Paystackcontent }}
onMessage={(e) => {
messageReceived(e.nativeEvent.data);
}}
onLoadStart={() => setisLoading(true)}
onLoadEnd={() => setisLoading(false)}
/>
{isLoading && (
<View>
<ActivityIndicator
size="large"
color={props.ActivityIndicatorColor}
/>
</View>
)}
</SafeAreaView>
</Modal>
{props.showPayButton && button}
</SafeAreaView>
);
}
export default forwardRef(Paystack);
Paystack.defaultProps = {
buttonText: "Pay Now",
amount: 10,
ActivityIndicatorColor: "green",
autoStart: false,
showPayButton: true,
currency: "NGN",
refNumber: "" + Math.floor(Math.random() * 1000000000 + 1),
channels: ["card"],
};