/** * 选择支付方式页面 * @邠心vbe on 2022/01/10 */ import React, { Component } from 'react'; import { View, Text, StyleSheet, Pressable, Image } from 'react-native'; import apiWallet from '../../api/apiWallet'; import Button from '../../components/Button'; import Dialog from '../../components/Dialog'; import TextView from '../../components/TextView'; import ChargeItemSelect from '../../icons/ChargeItemSelect'; import { PageList } from '../Router'; import { WalletTitle } from '../wallet/Topup'; import { getPaymenOptions, PaymentDefault, PAYTYPE } from './PaymentConfig'; export default class PaymentMethod extends Component { constructor(props) { super(props); this.state = { cIndex: 0, selectIndex: 0, paymentOptions: [], amountList: [{ amount: 10, power: 0 }, { amount: 20, power: 0 }, { amount: 40, power: 0 }], connectorInfo: {} }; this.rate = 0 this.isPayPerUseFirst = PaymentDefault.DEFAULT.payType == PAYTYPE.PAY_PER_USE; this.ENABLE_2C2P = PaymentDefault.is2c2p; } componentDidMount() { getPaymenOptions(options => { this.setState({ paymentOptions: options }); }) const params = this.props.route.params; const info = params.info ?? {}; console.log('----params----', params); if (info.rate && params.type) { this.rate = info.rate; this.setState({ connectorInfo: info, cIndex: (params.type == PAYTYPE.CREDIT_WALLET) ? 0 : 1 }); this.calculatePower(); } } calculatePower() { if (this.rate > 0) { this.state.amountList.forEach(item => { item.power = (item.amount / this.rate).toFixed(0) }); } } changePayType(index) { this.setState({ cIndex: index }) } onConfirm() { const info = this.state.paymentOptions[this.state.cIndex]; global.paymentOption = { title: info.title, value: info.value }; if (info.value != PAYTYPE.PAY_PER_USE) { goBack(); } else { const amount = this.state.amountList[this.state.selectIndex].amount; global.paymentOption.amount = amount; const params = { payAmount: amount, fomoPayType: "PAYNOW", paymentOption: info.value, chargeBoxId: this.state.connectorInfo.chargeBoxId, connectorId: this.state.connectorInfo.connectorId } //console.log('params',params); if (params.payAmount) { //PAYNOW支付 Dialog.showProgressDialog(); apiWallet.doPayment(params).then(res => { Dialog.dismissLoading(); if (res.data.fomoId && res.data.qrCodeInBase64) { startPage(PageList.payPeruse, { amount: params.payAmount, base64: res.data.qrCodeInBase64 }); } else { toastShort('Error 0') } }).catch(err => { Dialog.dismissLoading(); toastShort(err); }); } else { toastShort('Error 1') } } } onConfirmV2() { const info = this.state.paymentOptions[this.state.cIndex]; global.paymentOption = { title: info.title, value: info.value }; if (this.state.cIndex == 1) { goBack(); } else { const amount = this.state.amountList[this.state.selectIndex].amount; global.paymentOption.amount = amount; const params = { payAmount: amount, fomoPayType: "PAYNOW", paymentOption: info.value, paymentChannel: "SGQR", chargeBoxId: this.state.connectorInfo.chargeBoxId, connectorId: this.state.connectorInfo.connectorId } //console.log('params',params); if (params.payAmount) { //PAYNOW支付 Dialog.showProgressDialog(); apiWallet.doPaymentV2(params).then(res => { Dialog.dismissLoading(); if (res.data.webPaymentUrl) { startPage(PageList.paymentWeb, { amount: params.payAmount, url: res.data.webPaymentUrl, type: 'PayPerUse' }); } else { toastShort('Error 0') } }).catch(err => { Dialog.dismissLoading(); toastShort(err); }); /*apiWallet.doPayment(params).then(res => { Dialog.dismissLoading(); if (res.data.fomoId && res.data.qrCodeInBase64) { startPage(PageList.payPeruse, { amount: params.payAmount, base64: res.data.qrCodeInBase64 }); } else { toastShort('Error 0') } }).catch(err => { Dialog.dismissLoading(); toastShort(err); });*/ } else { toastShort('Error 2') } } } render() { return ( {$t('payment.paymentOption')} {$t('payment.labelPayWith')} { this.state.paymentOptions.map((item, index) => { return ( 0 && styles.next, index == this.state.cIndex && styles.selected ]} onPress={() => { this.changePayType(index); }}> {item.title} {' '} {item.desc ? item.desc : '(' + $t('wallet.balance') + ' ' +currency+userInfo.creditStr+')'} ); }) } { (this.state.cIndex == (this.isPayPerUseFirst ? 0 : 1)) && {$t('payment.payPerUse')} {$t('payment.labelChooseAmount')} { this.state.amountList.map((item, index) => { return ( 0 && styles.right, index == this.state.selectIndex && styles.amountSelect]} onPress={() => { this.setState({ selectIndex: index }) }}> {currency}{item.amount} ~{item.power}kWh ); }) } {$t('payment.tipsPayPerUse')} }