PaymentMethod.js 10 KB

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