Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.33 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import { ScrollView, StyleSheet, View, Image, TouchableOpacity, Text, Linking,AsyncStorage} from 'react-native';
  3. import { Header } from 'react-native-elements';
  4. import ShoppingCart from '../../controllers/ShoppingCart';
  5. import { Content, Item, Picker, Toast } from 'native-base';
  6.  
  7. import { PAY_URL } from '../../controllers/constants';
  8. import { BASE_URL } from '../../controllers/constants';
  9.  
  10. import Spinner from 'react-native-loading-spinner-overlay';
  11.  
  12. import { connect } from 'react-redux';
  13.  
  14.  
  15. class Payment extends Component {
  16.  
  17. static navigationOptions = { header: null }
  18. static navigationOptions = {
  19. drawerLabel: () => null
  20. }
  21.  
  22. constructor(props){
  23. super(props)
  24. this.state = {
  25. selected2: undefined,
  26. selected3: undefined,
  27. userinfo:'',
  28. cartitems:[],
  29. qqty:[],
  30. cash:'',
  31. spinner: false,
  32.  
  33.  
  34.  
  35. };
  36. }
  37.  
  38. onValueChange2(value: string) {
  39. this.setState({
  40. selected2: value
  41. });
  42. }
  43.  
  44. onValueChange3(value: string) {
  45. this.setState({
  46. selected3: value
  47. });
  48. }
  49.  
  50.  
  51.  
  52. getcartItems = async() =>{
  53. var fetchitem = await AsyncStorage.getItem('shopping');
  54. var convertcartItems = JSON.parse(fetchitem);
  55. this.setState({cartitems: convertcartItems})
  56.  
  57. }
  58. getQty = async() =>{
  59. var fetchqty= await AsyncStorage.getItem('qtymoney');
  60. var convertcartItemsqty = JSON.parse(fetchqty);
  61. this.setState({qqty: convertcartItemsqty})
  62.  
  63. }
  64.  
  65. getCash = async() =>{
  66. var fetchtotal= await AsyncStorage.getItem('total');
  67. var convertttola = JSON.parse(fetchtotal);
  68. this.setState({cash: convertttola})
  69.  
  70. }
  71.  
  72.  
  73.  
  74.  
  75. componentDidMount() {
  76. this.props.navigation.addListener('willFocus',this.getcartItems);
  77. this.props.navigation.addListener('willFocus',this.getQty);
  78.  
  79. this.props.navigation.addListener('willFocus',this.getCash);
  80.  
  81.  
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88. render() {
  89.  
  90. var {navigate} = this.props.navigation;
  91.  
  92. const {params} = this.props.navigation.state;
  93. return(
  94. <View>
  95. <Header
  96. statusBarProps={{ barStyle: 'light-content' }}
  97. outerContainerStyles={{ backgroundColor: '#D60B2B' }}
  98. leftComponent={{ icon: 'arrow-back', color: '#fff',onPress: () => navigate('Cart') }}
  99. centerComponent={{ text: 'Payment Method', style: { color: '#fff' } }}
  100. rightComponent={ <ShoppingCart navigation={ this.props.navigation }/>}
  101. />
  102.  
  103. <ScrollView contentContainerStyle={{ flexGrow: 1, backgroundColor: 'rgba(255,255,255,0)'}}>
  104. <Spinner
  105. visible={this.state.spinner}
  106. textContent={'Loading...'}
  107. textStyle={styles.spinnerTextStyle}
  108. />
  109. <View style={styles.Container}>
  110. <Text style={{fontWeight:'700', fontSize:15, color: '#787878'}}>DELIVERY ADDRESS:</Text>
  111.  
  112. <View style={{borderColor:'#D60B2B',padding:10, borderWidth:2, borderRadius:5, marginTop:10}}>
  113. <View style={{flexDirection:'row', marginBottom:15, borderBottomColor: '#D60B2B', borderBottomWidth:1}}>
  114. <Text style={{color:'#D60B2B', fontSize:15, padding:5}}>DELIVERY LOCATIONS:</Text>
  115. <TouchableOpacity style={{marginLeft: 'auto', backgroundColor:'#D60B2B', paddingLeft:10, paddingRight:10, paddingTop:5, borderTopLeftRadius:3, borderTopRightRadius:3}} onPress={() => {navigate('LocationEdit')}}>
  116. <Text style={{fontSize:10, color:'#fff', fontWeight:'600'}}>EDIT</Text>
  117. </TouchableOpacity>
  118.  
  119. </View>
  120.  
  121. <Content>
  122. {/* <Item picker style={styles.SelectDrops}>
  123. <Picker
  124. mode="dropdown"
  125. style={{ width: 290}}
  126. placeholder="Select"
  127. placeholderStyle={{ color: "#bfc6ea" }}
  128. placeholderIconColor="#007aff"
  129. selectedValue={this.state.selected2}
  130. onValueChange={this.onValueChange2.bind(this)}>
  131.  
  132. <Picker.Item label="Address #1" value="key0" />
  133. <Picker.Item label="Address #2" value="key1" />
  134. </Picker>
  135. </Item> */}
  136. <Text>{params.address}</Text>
  137. </Content>
  138.  
  139. </View>
  140.  
  141. <View style={{marginTop: 30}}>
  142. <Text style={{fontWeight:'700', fontSize:15, color: '#787878'}}>CHOOSE A PAYMENT METHOD</Text>
  143.  
  144. <Content>
  145. <Item picker style={styles.SelectDrops}>
  146. <Picker
  147. mode="dropdown"
  148. style={{ width: 290}}
  149. placeholder="Select"
  150. placeholderStyle={{ color: "#bfc6ea" }}
  151. placeholderIconColor="#007aff"
  152. selectedValue={this.state.selected3}
  153. onValueChange={this.onValueChange3.bind(this)}>
  154.  
  155. <Picker.Item label="PayStack" value="PayStack" />
  156. <Picker.Item label="Payment on Delivery" value="Payment on Delivery" />
  157. </Picker>
  158. </Item>
  159. </Content>
  160.  
  161. </View>
  162.  
  163. <View style={{flexDirection:'row', alignContent:'center',width:'100%', justifyContent:'center', marginTop:20}}>
  164. <TouchableOpacity style={styles.btn} onPress={this.pay}>
  165. <Text style={{color:'#fff', fontSize:13, alignSelf:'center',marginLeft: 20, marginRight: 20, fontWeight:'700'}}>PROCEED TO PAYMENT</Text>
  166. </TouchableOpacity>
  167. </View>
  168. </View>
  169. </ScrollView>
  170. </View>
  171. );
  172. }
  173.  
  174. pay = async () => {
  175.  
  176.  
  177. this.setState({spinner: !this.state.spinner});
  178. var payparam = this.state.selected3;
  179. var cartItems = this.state.cartitems;
  180. var QuantAmount = this.state.qqty;
  181. var Cash = this.state.cash;
  182.  
  183.  
  184.  
  185. if(payparam == undefined){
  186. Toast.show({
  187. text: 'Choose a payment method',
  188. position: 'bottom',
  189. buttonText: 'Okay'
  190. });
  191.  
  192. this.setState({spinner: false});
  193. }else{
  194.  
  195.  
  196.  
  197.  
  198. if(payparam == 'PayStack'){
  199.  
  200. var fetchuser = await AsyncStorage.getItem('userBilling');
  201. var realdata = JSON.parse(fetchuser);
  202.  
  203. var id = realdata.id;
  204. var fname = realdata.first_name;
  205. var lname = realdata.first_name;
  206. var addr = realdata.billing.address_1;
  207. var state = realdata.billing.state;
  208. var phone = realdata.billing.phone;
  209. var email = realdata.email;
  210.  
  211.  
  212. fetch(BASE_URL+'/orders',{
  213. method: 'POST',
  214. headers:{
  215. Accept: 'application/json',
  216. 'Content-Type': 'application/json',
  217. 'Authorization': 'Basic Y2tfZTQwOGQzOTdhNTAzYjAzYmI0ZGMzMGNhYmZjZTgyM2ZjYjNjYzZhMzpjc18xNTFhZWJiNWMwMzBkZTk4M2FlNWM1ODkzZGQ2NmViYzEwNmYxOTc3'
  218. },
  219.  
  220. body: JSON.stringify({
  221. payment_method: 'bacs',
  222. payment_method_title: 'Paystack',
  223. set_paid: true,
  224. customer_id:id,
  225. status:'processing',
  226.  
  227.  
  228. billing: {
  229. first_name: fname,
  230. last_name: lname,
  231. address_1: addr,
  232. state: state,
  233. email: email,
  234. phone: phone,
  235.  
  236. },
  237.  
  238. })
  239. })
  240.  
  241. .then((response)=> response.json())
  242. .then((res)=>{
  243. if(res.id){
  244.  
  245.  
  246. for (let index = 0; index < cartItems.length; index++) {
  247.  
  248. fetch(BASE_URL+'/orders/'+res.id,{
  249. method: 'POST',
  250. headers:{
  251. Accept: 'application/json',
  252. 'Content-Type': 'application/json',
  253. 'Authorization': 'Basic Y2tfZTQwOGQzOTdhNTAzYjAzYmI0ZGMzMGNhYmZjZTgyM2ZjYjNjYzZhMzpjc18xNTFhZWJiNWMwMzBkZTk4M2FlNWM1ODkzZGQ2NmViYzEwNmYxOTc3'
  254. },
  255.  
  256. body: JSON.stringify({
  257.  
  258. line_items: [
  259. {
  260. product_id: cartItems[index]['id'],
  261. quantity: QuantAmount['prosize'][index]
  262. }
  263. ],
  264.  
  265. })
  266. })
  267.  
  268. .then((response)=> response.json())
  269. .then((res)=>{
  270. if(res.id){
  271.  
  272. this.setState({amount: res.id});
  273. }
  274.  
  275. });
  276.  
  277.  
  278. }
  279.  
  280. // generate hash
  281.  
  282. fetch('http://paymentapi.genesisrestaurantng.com/payhash',{
  283. method: 'POST',
  284. headers:{
  285. Accept: 'application/json',
  286. 'Content-Type': 'application/json',
  287. },
  288.  
  289. body: JSON.stringify({
  290. 'total': Cash,
  291. 'email':email,
  292. 'oid': res.id
  293.  
  294. })
  295. })
  296.  
  297. .then((response)=> response.json())
  298. .then((res)=>{
  299.  
  300. if(res.status == 200){
  301.  
  302. Linking.openURL(PAY_URL+'/'+res.hash);
  303. }
  304.  
  305.  
  306. });
  307.  
  308. // end generate hash
  309.  
  310. this.setState({spinner: false});
  311. this.props.removeItem();
  312.  
  313.  
  314.  
  315. }
  316.  
  317. });
  318.  
  319.  
  320. }else{
  321.  
  322. var fetchuser = await AsyncStorage.getItem('userBilling');
  323. var realdata = JSON.parse(fetchuser);
  324.  
  325. var id = realdata.id;
  326. var fname = realdata.first_name;
  327. var lname = realdata.first_name;
  328. var addr = realdata.billing.address_1;
  329. var state = realdata.billing.state;
  330. var phone = realdata.billing.phone;
  331. var email = realdata.email;
  332.  
  333. fetch(BASE_URL+'/orders',{
  334. method: 'POST',
  335. headers:{
  336. Accept: 'application/json',
  337. 'Content-Type': 'application/json',
  338. 'Authorization': 'Basic Y2tfZTQwOGQzOTdhNTAzYjAzYmI0ZGMzMGNhYmZjZTgyM2ZjYjNjYzZhMzpjc18xNTFhZWJiNWMwMzBkZTk4M2FlNWM1ODkzZGQ2NmViYzEwNmYxOTc3'
  339. },
  340.  
  341. body: JSON.stringify({
  342. payment_method: 'bacs',
  343. payment_method_title: 'Pay On delivery',
  344. set_paid: true,
  345. customer_id:id,
  346. status:'processing',
  347.  
  348. billing: {
  349. first_name: fname,
  350. last_name: lname,
  351. address_1: addr,
  352. state: state,
  353. email: email,
  354. phone: phone,
  355.  
  356. },
  357.  
  358.  
  359. })
  360. })
  361.  
  362. .then((response)=> response.json())
  363. .then((res)=>{
  364. if(res.id){
  365.  
  366.  
  367. for (let index = 0; index < cartItems.length; index++) {
  368. fetch(BASE_URL+'/orders/'+res.id,{
  369. method: 'post',
  370. headers:{
  371. Accept: 'application/json',
  372. 'Content-Type': 'application/json',
  373. 'Authorization': 'Basic Y2tfZTQwOGQzOTdhNTAzYjAzYmI0ZGMzMGNhYmZjZTgyM2ZjYjNjYzZhMzpjc18xNTFhZWJiNWMwMzBkZTk4M2FlNWM1ODkzZGQ2NmViYzEwNmYxOTc3'
  374. },
  375.  
  376. body: JSON.stringify({
  377.  
  378. line_items: [
  379. {
  380. product_id: cartItems[index]['id'],
  381. quantity: QuantAmount['prosize'][index]
  382. }
  383. ],
  384.  
  385. })
  386. })
  387.  
  388. .then((response)=> response.json())
  389. .then((res)=>{
  390. if(res.id){
  391.  
  392. this.setState({amount: res.id});
  393. }
  394.  
  395. });
  396.  
  397.  
  398. }
  399.  
  400. this.props.removeItem();
  401. this.setState({spinner: false});
  402. Toast.show({
  403. text: 'Successful !!! You will contacted upon delivery',
  404. position: 'bottom',
  405. buttonText: 'Okay'
  406. });
  407. }
  408.  
  409. });
  410.  
  411.  
  412. }
  413.  
  414. }
  415. }
  416. }
  417.  
  418. const mapDispatchToProps = (dispatch)=>{
  419. return{
  420. removeItem:(products) =>dispatch({type: 'CLEAR_CART', payload: products })
  421. }
  422. }
  423.  
  424.  
  425.  
  426. export default connect(null, mapDispatchToProps)(Payment);
  427.  
  428. const styles = StyleSheet.create({
  429. Container:{
  430. backgroundColor:'#fff',
  431. padding:10,
  432. borderRadius:10,
  433. flex:1,
  434. marginTop:30,
  435. marginBottom: 50,
  436. marginLeft: 10,
  437. marginRight:10,
  438. paddingBottom:40
  439. },
  440. btn:{
  441. textAlign:'center',
  442. padding:15,
  443. backgroundColor:'#C00A27',
  444. borderRadius:50,
  445. },
  446.  
  447. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement