PaymentMethod.js 9.1 KB

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