PaymentMethod.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /**
  2. * 选择支付方式页面
  3. * @邠心vbe on 2022/01/10
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, Pressable, Image } from 'react-native';
  7. import apiWallet from '../../api/apiWallet';
  8. import Button from '../../components/Button';
  9. import Dialog from '../../components/Dialog';
  10. import { PageList } from '../Router';
  11. import { PAYTYPE } from '../wallet/Payment';
  12. import { WalletTitle } from '../wallet/Topup';
  13. export default class PaymentMethod extends Component {
  14. constructor(props) {
  15. super(props);
  16. this.state = {
  17. cIndex: 0,
  18. selectIndex: 0,
  19. paymentOptions: [{
  20. title: "Credit Wallet",
  21. desc: '',
  22. value: PAYTYPE.CREDIT_WALLET,
  23. icon: require('../../images/icon/draw-wallet.png')
  24. }, {
  25. title: "Pay Per Use",
  26. desc: '(SGQR)',
  27. value: PAYTYPE.PAY_PER_USE,
  28. icon: require('../../images/wallet/ic_payperuse.png')
  29. }],
  30. amountList: [{
  31. amount: 10,
  32. power: 0
  33. }, {
  34. amount: 20,
  35. power: 0
  36. }, {
  37. amount: 40,
  38. power: 0
  39. }],
  40. connectorInfo: {}
  41. };
  42. this.rate = 0
  43. }
  44. componentDidMount() {
  45. const params = this.props.route.params;
  46. const info = params.info ?? {};
  47. console.log('----params----', params);
  48. if (info.rate && params.type) {
  49. this.rate = info.rate;
  50. this.setState({
  51. connectorInfo: info,
  52. cIndex: (params.type == PAYTYPE.CREDIT_WALLET) ? 0 : 1
  53. });
  54. this.calculatePower();
  55. }
  56. }
  57. calculatePower() {
  58. if (this.rate > 0) {
  59. this.state.amountList.forEach(item => {
  60. item.power = (item.amount / this.rate).toFixed(0)
  61. });
  62. }
  63. }
  64. changePayType(index) {
  65. this.setState({
  66. cIndex: index
  67. })
  68. }
  69. onConfirm() {
  70. const info = this.state.paymentOptions[this.state.cIndex];
  71. global.paymentOption = {
  72. title: info.title,
  73. value: info.value
  74. };
  75. if (this.state.cIndex == 0) {
  76. goBack();
  77. } else {
  78. const amount = this.state.amountList[this.state.selectIndex].amount;
  79. global.paymentOption.amount = amount;
  80. const params = {
  81. payAmount: amount,
  82. fomoPayType: "PAYNOW",
  83. paymentOption: info.value,
  84. chargeBoxId: this.state.connectorInfo.chargeBoxId,
  85. connectorId: this.state.connectorInfo.connectorId
  86. }
  87. //console.log('params',params);
  88. if (params.payAmount) {
  89. //PAYNOW支付
  90. Dialog.showProgressDialog();
  91. apiWallet.doPayment(params).then(res => {
  92. Dialog.dismissLoading();
  93. if (res.data.fomoId && res.data.qrCodeInBase64) {
  94. startPage(PageList.payPeruse, { amount: params.payAmount, base64: res.data.qrCodeInBase64 });
  95. } else {
  96. toastShort('Error 0')
  97. }
  98. }).catch(err => {
  99. Dialog.dismissLoading();
  100. toastShort(err);
  101. });
  102. } else {
  103. toastShort('Error 1')
  104. }
  105. }
  106. }
  107. onConfirmV2() {
  108. const info = this.state.paymentOptions[this.state.cIndex];
  109. global.paymentOption = {
  110. title: info.title,
  111. value: info.value
  112. };
  113. if (this.state.cIndex == 0) {
  114. goBack();
  115. } else {
  116. const amount = this.state.amountList[this.state.selectIndex].amount;
  117. global.paymentOption.amount = amount;
  118. const params = {
  119. payAmount: amount,
  120. fomoPayType: "PAYNOW",
  121. paymentOption: info.value,
  122. paymentChannel: "SGQR",
  123. chargeBoxId: this.state.connectorInfo.chargeBoxId,
  124. connectorId: this.state.connectorInfo.connectorId
  125. }
  126. //console.log('params',params);
  127. if (params.payAmount) {
  128. //PAYNOW支付
  129. Dialog.showProgressDialog();
  130. apiWallet.doPaymentV2(params).then(res => {
  131. Dialog.dismissLoading();
  132. if (res.data.webPaymentUrl) {
  133. startPage(PageList.paymentWeb, { amount: params.payAmount, url: res.data.webPaymentUrl, type: 'PayPerUse' });
  134. } else {
  135. toastShort('Error 0')
  136. }
  137. }).catch(err => {
  138. Dialog.dismissLoading();
  139. toastShort(err);
  140. });
  141. /*apiWallet.doPayment(params).then(res => {
  142. Dialog.dismissLoading();
  143. if (res.data.fomoId && res.data.qrCodeInBase64) {
  144. startPage(PageList.payPeruse, { amount: params.payAmount, base64: res.data.qrCodeInBase64 });
  145. } else {
  146. toastShort('Error 0')
  147. }
  148. }).catch(err => {
  149. Dialog.dismissLoading();
  150. toastShort(err);
  151. });*/
  152. } else {
  153. toastShort('Error 2')
  154. }
  155. }
  156. }
  157. render() {
  158. return (
  159. <View style={styles.container}>
  160. <View style={styles.headerView}>
  161. <Text></Text>
  162. </View>
  163. <View style={styles.contentView}>
  164. <View style={styles.optionView}>
  165. <WalletTitle>Payment Option</WalletTitle>
  166. <Text style={styles.subTitle}>Pay with: </Text>
  167. { this.state.paymentOptions.map((item, index) => {
  168. return (
  169. <Pressable
  170. key={index}
  171. style={[
  172. styles.paytypeView,
  173. index > 0 && styles.next,
  174. index == this.state.cIndex && styles.selected
  175. ]}
  176. onPress={() => {
  177. this.changePayType(index);
  178. }}>
  179. <Image
  180. style={styles.accountLogo}
  181. source={item.icon}/>
  182. <Text style={styles.paytypeText}>
  183. {item.title} {' '}
  184. {item.desc ? item.desc : '(Balance '+currency+userInfo.credit+')'}
  185. </Text>
  186. { index == this.state.cIndex ?
  187. <Image
  188. style={styles.selectIcon}
  189. source={require('../../images/charge/charge-item-select.png')}/>
  190. : <AntDesign
  191. name='checkcircle'
  192. size={18}
  193. color='#ccc'/>
  194. }
  195. </Pressable>
  196. );
  197. })
  198. }
  199. </View>
  200. { this.state.cIndex > 0 &&
  201. <View style={[styles.optionView, ui.flex1]}>
  202. <WalletTitle>Pay Per Use</WalletTitle>
  203. <Text style={styles.subTitle}>Choose Amount:</Text>
  204. <View style={styles.amountItems}>
  205. { this.state.amountList.map((item, index) => {
  206. return (
  207. <Pressable
  208. key={index}
  209. style={[
  210. styles.amountItem,
  211. index > 0 && styles.right,
  212. index == this.state.selectIndex && styles.amountSelect]}
  213. onPress={() => {
  214. this.setState({
  215. selectIndex: index
  216. })
  217. }}>
  218. <Text
  219. style={index == this.state.selectIndex ? styles.amountActive : styles.amountText}>
  220. {currency}{item.amount}
  221. </Text>
  222. <Text style={styles.powerText}>~{item.power}kWh</Text>
  223. </Pressable>
  224. );
  225. })
  226. }
  227. </View>
  228. <Text style={ui.flex1}></Text>
  229. <Text style={styles.warningText}>Un-utilized amount will be refunded.</Text>
  230. </View>
  231. }
  232. </View>
  233. <Button
  234. text="Confirm"
  235. style={$margin(16)}
  236. onClick={() => this.onConfirm()}
  237. />
  238. </View>
  239. );
  240. }
  241. }
  242. const styles = StyleSheet.create({
  243. container: {
  244. flex: 1,
  245. backgroundColor: '#F5F5F5'
  246. },
  247. headerView: {
  248. paddingBottom: 76,
  249. backgroundColor: colorAccent
  250. },
  251. contentView: {
  252. flex: 1,
  253. padding: 16,
  254. marginTop: -80,
  255. marginBottom: -20
  256. },
  257. optionView: {
  258. padding: 16,
  259. borderRadius: 10,
  260. marginBottom: 16,
  261. backgroundColor: 'white'
  262. },
  263. subTitle: {
  264. color: '#333',
  265. fontSize: 14,
  266. marginTop: 16,
  267. marginBottom: 16
  268. },
  269. paytypeView: {
  270. padding: 16,
  271. borderWidth: 1,
  272. borderColor: '#eee',
  273. borderRadius: 10,
  274. alignItems: 'center',
  275. flexDirection: 'row',
  276. backgroundColor: '#F5F5F5'
  277. },
  278. selected: {
  279. borderColor: colorAccent
  280. },
  281. next: {
  282. marginTop: 16,
  283. },
  284. paytypeText: {
  285. flex: 1,
  286. color: '#333',
  287. fontSize: 14,
  288. },
  289. selectIcon: {
  290. width: 18,
  291. height: 18,
  292. },
  293. amountItems: {
  294. paddingBottom: 16,
  295. alignItems: 'center',
  296. flexDirection: 'row'
  297. },
  298. amountItem: {
  299. flex: 1,
  300. borderWidth: 1,
  301. borderRadius: 4,
  302. borderColor: '#999',
  303. },
  304. right: {
  305. marginLeft: 20
  306. },
  307. amountSelect: {
  308. color: '#333',
  309. borderColor: '#333',
  310. backgroundColor: colorAccent
  311. },
  312. amountText: {
  313. color: '#666',
  314. fontSize: 24,
  315. height: 50,
  316. lineHeight: 60,
  317. textAlign: 'center'
  318. },
  319. amountActive: {
  320. color: '#333',
  321. fontSize: 24,
  322. height: 50,
  323. lineHeight: 60,
  324. textAlign: 'center'
  325. },
  326. powerText: {
  327. color: '#333',
  328. padding: 6,
  329. fontSize: 12,
  330. textAlign: 'right',
  331. },
  332. warningText: {
  333. color: '#ED4A4A',
  334. fontSize: 14,
  335. textAlign: 'center',
  336. paddingBottom: 16
  337. },
  338. accountLogo: {
  339. width: 26,
  340. height: 26,
  341. marginRight: 12
  342. },
  343. });