PaymentMethod.js 9.5 KB

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