PaymentMethod.js 10.0 KB

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