PaymentMethod.js 9.4 KB

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