/** * 添加信用卡页面 * @邠心vbe on 2021/05/08 */ import React, { Component } from 'react'; import { View, Text, StyleSheet, TextInput, Switch } from 'react-native'; import apiUser from '../../api/apiUser'; import Button from '../../components/Button'; import apiWallet from '../../api/apiWallet'; import Dialog from '../../components/Dialog'; import Dropdown from '../../components/Dropdown'; export default class AddCard extends Component { constructor(props) { super(props); this.state = { primary: false, validTill: '', countryList: [], countryCode: '', }; this.form = {} } componentDidMount() { this.getCountryList(); } getCountryList() { apiUser.getCountryCode().then(res => { if (res.data) { const list = [] for (let item in res.data) { if (item) { list.push({ code: item, name: res.data[item] }); } } this.setState({ countryList: list }); } }).catch(err => { }); } validate() { if (!this.form.nameOnCard) { toastShort('Please type name on card'); return; } if (!this.form.cardNumber) { toastShort('Please type card number'); return; } if (!/^\d{9,}$/.test(this.form.cardNumber)) { toastShort('Please type a correct card number'); return; } if (!this.state.validTill) { toastShort('Please type valid till'); return; } if (!/^(0[1-9]|1[0-2])\/(2[1-9]|[3-5][0-9])/.test(this.state.validTill)) { toastShort('Please type a correct valid till'); return; } if (!this.form.cardCvv) { toastShort('Please type CVV'); return; } if (!/^\d{3}$/.test(this.form.cardCvv)) { toastShort('Please type a correct CVV'); return; } const params = this.form; params.country = this.state.countryCode; params.primary = this.state.primary ? 'Y' : 'N'; params.validTill = this.state.validTill; this.addCard(params); } addCard(params) { Dialog.showProgressDialog(); apiWallet.addCreditCard(params).then(res => { Dialog.dismissLoading(); toastShort('Add card successfully'); goBack(); }).catch(err => { Dialog.dismissLoading(); toastShort(err); }); } render() { return ( Name on Card this.form.nameOnCard = text}/> Card Number { this.form.cardNumber = text; }}/> Valid Till { if (this.state.validTill.length == 2 && text.length == 3) { if (text.indexOf('/') == -1) { let s = this.state.validTill + '/' + text.substring(2) this.setState({ validTill: s }); } else { this.setState({ validTill: text }); } } else { this.setState({ validTill: text }); } }}/> CVV { this.form.cardCvv = text; }}/> Country where card was issued { this.setState({ countryCode: value, countryIndex: index }); }} /> Change { this.setState({ primary: !this.state.primary }); }}>Set as Primary { this.setState({ primary: v }); }}/> Your card may be charged to make sure it’s vailid.That amount will be automatically refunded. By adding a card,you have read and agree to our terms and conditions.