TopupNew.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /**
  2. * 新版钱包充值页面
  3. * @邠心vbe on 2023/02/02
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, Switch, AppState, Linking } from 'react-native';
  7. import apiWallet from '../../api/apiWallet';
  8. import BadgeSelectItem from '../../components/BadgeSelectItem';
  9. import Button, { ElevationObject } from '../../components/Button';
  10. import Dialog from '../../components/Dialog';
  11. import TextView from '../../components/TextView';
  12. import { PaymentDefault } from '../payment/PaymentConfig';
  13. import { PageList } from '../Router';
  14. import { Balance } from './Payment';
  15. import TopupPaythod from './TopupPaythod';
  16. import app from '../../../app.json';
  17. import utils from '../../utils/utils';
  18. import CheckBox from '../../components/CheckBox';
  19. export default class TopupNew extends Component {
  20. constructor(props) {
  21. super(props);
  22. this.state = {
  23. isAuto: false,
  24. topupList: [],
  25. selectIndex: 0,
  26. payType: {},
  27. balance: 0,
  28. agree: false,
  29. isCallback: true
  30. };
  31. this.stateListener;
  32. }
  33. componentDidMount() {
  34. this.props.navigation.addListener('focus', () => {
  35. getUserInfo(info => {
  36. this.setState({
  37. balance: info?.creditStr
  38. })
  39. }, true);
  40. });
  41. this.getTopupList();
  42. this.stateListener = AppState.addEventListener("change", state => {
  43. if (state == 'active' && this.state.isCallback) {
  44. this.setState({
  45. agree: true,
  46. isCallback: false
  47. })
  48. }
  49. });
  50. }
  51. componentWillUnmount() {
  52. if (this.stateListener) {
  53. this.stateListener?.remove();
  54. }
  55. }
  56. getTopupList() {
  57. Dialog.showProgressDialog();
  58. // apiWallet.getTopUpAmountList()
  59. apiWallet.getTopUpAmountListV2().then(res => {
  60. Dialog.dismissLoading();
  61. if (res.data.length > 0) {
  62. this.setState({
  63. topupList: res.data
  64. });
  65. }
  66. }).catch(err => {
  67. toastShort(err)
  68. const data = apiWallet.getTempAmountListV2();
  69. this.setState({
  70. topupList: data
  71. });
  72. Dialog.dismissLoading();
  73. })
  74. }
  75. /**
  76. * 2C2P充值
  77. */
  78. topup2() {
  79. const topup = this.state.topupList[this.state.selectIndex]
  80. const params = {
  81. currency: topup?.currency,
  82. payAmount: topup?.amount
  83. }
  84. apiWallet.doPaymentV2(params).then(res => {
  85. Dialog.dismissLoading();
  86. if (res.data.webPaymentUrl) {
  87. startPage(PageList.paymentWeb, { amount: params.payAmount, url: res.data.webPaymentUrl, type: 'Topup' });
  88. } else {
  89. toastShort('Error 0')
  90. }
  91. }).catch(err => {
  92. Dialog.dismissLoading();
  93. toastShort(err);
  94. });
  95. }
  96. /**
  97. * FOMO充值
  98. */
  99. topup() {
  100. const topup = this.state.topupList[this.state.selectIndex]
  101. const params = {
  102. currency: topup?.currency,
  103. payAmount: topup?.amount,
  104. fomoPayType: this.state.payType.fomoPayType
  105. }
  106. //console.log('params',params);
  107. if (params.payAmount) {
  108. if (params.fomoPayType == 'PAYNOW' || params.fomoPayType == 'GRABPAY') {
  109. //PAYNOW支付
  110. Dialog.showProgressDialog();
  111. apiWallet.doPayment(params).then(res => {
  112. Dialog.dismissLoading();
  113. if (res.data.fomoId && res.data.qrCodeInBase64) {
  114. startPage(PageList.paynow, { amount: params.payAmount, base64: res.data.qrCodeInBase64 });
  115. } else if (res.data.url) {
  116. startPage(PageList.paymentWeb, { amount: params.payAmount, url: res.data.url, type: 'Topup' });
  117. } else {
  118. toastShort('Error 0')
  119. }
  120. }).catch(err => {
  121. Dialog.dismissLoading();
  122. toastShort(err);
  123. });
  124. } else {
  125. //信用卡支付
  126. startPage(PageList.formCard, { amount: params.payAmount, payType: params.fomoPayType });
  127. }
  128. } else {
  129. toastShort('Error 1')
  130. }
  131. }
  132. changeAgree(ag) {
  133. if (ag) {
  134. this.setState({
  135. isCallback: true
  136. })
  137. Linking.openURL(app.storeUrl.refundUrl);
  138. } else {
  139. this.setState({
  140. agree: ag
  141. });
  142. }
  143. }
  144. render() {
  145. return (
  146. <View style={styles.container}>
  147. <View style={styles.headerView}>
  148. <Balance balanceText={this.state.balance}/>
  149. </View>
  150. <View style={styles.contentView}>
  151. <View style={styles.topupView}>
  152. <WalletTitle>{$t('wallet.titleChooseCreditValue')}</WalletTitle>
  153. { app.modules.nationally
  154. ? <View style={styles.topupItems2}>
  155. { this.state.topupList.map((item, index) => {
  156. return (
  157. <BadgeSelectItem
  158. key={index}
  159. style={[styles.topupItem2]}
  160. checked={index == this.state.selectIndex}
  161. onPress={() => {
  162. this.setState({
  163. selectIndex: index
  164. })
  165. }}>
  166. <Text style={[styles.topupText, index == this.state.selectIndex && {color: colorAccent}]}>
  167. <Text style={{fontSize: 16}}>{item.currencySymbol} </Text>
  168. {item.amount}
  169. {/* <Text style={{fontSize: 16}}> {item.currency}</Text> */}
  170. </Text>
  171. </BadgeSelectItem>
  172. );
  173. })
  174. }
  175. </View>
  176. : <View style={styles.topupItems}>
  177. { this.state.topupList.map((item, index) => {
  178. return (
  179. <BadgeSelectItem
  180. key={index}
  181. style={[styles.topupItem, index > 0 && styles.right]}
  182. checked={index == this.state.selectIndex}
  183. onPress={() => {
  184. this.setState({
  185. selectIndex: index
  186. })
  187. }}>
  188. <Text style={[styles.topupText, index == this.state.selectIndex && {color: colorAccent}]}>
  189. <Text style={{fontSize: 16}}>{item.currencySymbol} </Text>
  190. {item.amount}
  191. {/* <Text style={{fontSize: 16}}> {item.currency}</Text> */}
  192. </Text>
  193. </BadgeSelectItem>
  194. );
  195. })
  196. }
  197. </View>
  198. }
  199. </View>
  200. { !PaymentDefault.is2c2p &&
  201. <View style={styles.topupView}>
  202. <WalletTitle>{$t('wallet.titleChoosePaymentType')}</WalletTitle>
  203. <TopupPaythod
  204. onChange={type => {
  205. this.setState({
  206. payType: type
  207. });
  208. }}/>
  209. </View>
  210. }
  211. </View>
  212. <View style={ui.flex1}></View>
  213. { utils.isNotEmpty(app.storeUrl.refundUrl) &&
  214. <View style={styles.agreeView}>
  215. <CheckBox
  216. value={this.state.agree}
  217. onTintColor={colorAccent}
  218. onCheckColor={colorAccent}
  219. onValueChange={v => this.changeAgree(v)}
  220. />
  221. <View style={styles.agreeTextRow}>
  222. <TextView style={styles.agreeText} onPress={() => this.changeAgree(!this.state.agree)}>
  223. {$t('sign.iHaveReadAndAgree')}
  224. </TextView>
  225. <TextView style={styles.agreeLink} onPress={() => this.changeAgree(true)}>Refund Policy</TextView>
  226. </View>
  227. </View>
  228. }
  229. <View style={styles.buttonView}>
  230. <Button
  231. text={$t('wallet.btnPurchase')}
  232. elevation={1.5}
  233. disabled={!this.state.agree}
  234. onClick={() => {
  235. PaymentDefault.is2c2p
  236. ? this.topup2()
  237. : this.topup()
  238. }}/>
  239. </View>
  240. </View>
  241. );
  242. }
  243. }
  244. export const WalletTitle = ({children}) => {
  245. return (
  246. <View style={styles.topupTitle}>
  247. {/* <Text style={styles.titleLeft}></Text> */}
  248. <TextView style={styles.titleText}>{children}</TextView>
  249. </View>
  250. );
  251. }
  252. const styles = StyleSheet.create({
  253. container: {
  254. flex: 1,
  255. backgroundColor: pageBackground
  256. },
  257. headerView: {
  258. paddingBottom: 76,
  259. //backgroundColor: colorAccent
  260. },
  261. contentView: {
  262. padding: 16,
  263. marginTop: -88
  264. },
  265. topupView: {
  266. marginBottom: 16
  267. },
  268. topupViewOld: {
  269. padding: 16,
  270. borderRadius: 10,
  271. marginBottom: 16,
  272. backgroundColor: colorLight
  273. },
  274. topupTitle: {
  275. marginBottom: 16,
  276. alignItems: 'center',
  277. flexDirection: 'row'
  278. },
  279. titleLeft: {
  280. width: 4,
  281. height: 15,
  282. borderRadius: 16,
  283. backgroundColor: colorPrimary
  284. },
  285. titleText: {
  286. color: textPrimary,
  287. fontSize: 16,
  288. paddingLeft: 0,
  289. fontWeight: 'bold',
  290. textShadowOffset: {
  291. width: 0,
  292. height: 1
  293. },
  294. textShadowRadius: 4,
  295. textShadowColor: "rgba(0, 0, 0, 0.25)",
  296. },
  297. subTitle: {
  298. color: textPrimary,
  299. fontSize: 14,
  300. marginTop: 16,
  301. marginBottom: 16
  302. },
  303. topupItems: {
  304. paddingBottom: 16,
  305. alignItems: 'center',
  306. flexDirection: 'row'
  307. },
  308. topupItems2: {
  309. paddingBottom: 16,
  310. alignItems: 'center'
  311. },
  312. topupItem: {
  313. flex: 1,
  314. alignItems: 'center',
  315. justifyContent: 'center',
  316. backgroundColor: colorLight,
  317. ...ElevationObject(5)
  318. },
  319. topupItem2: {
  320. width: "100%",
  321. marginBottom: 16,
  322. alignItems: 'center',
  323. justifyContent: 'center',
  324. backgroundColor: colorLight,
  325. ...ElevationObject(5)
  326. },
  327. topupText: {
  328. fontSize: 18,
  329. color: textPrimary,
  330. ...$padding(20, 0)
  331. },
  332. right: {
  333. marginLeft: 16
  334. },
  335. selected: {
  336. color: textLight,
  337. borderColor: colorDark,
  338. backgroundColor: colorPrimary
  339. },
  340. autoView: {
  341. flex: 1,
  342. color: textPrimary,
  343. fontSize: 14,
  344. paddingRight: 32
  345. },
  346. buttonView: {
  347. padding: 16,
  348. marginTop: 0,
  349. marginBottom: 16
  350. },
  351. agreeView: {
  352. margin: 16,
  353. flexDirection: 'row',
  354. alignItems: 'center',
  355. },
  356. agreeTextRow: {
  357. flex: 1,
  358. flexWrap: 'wrap',
  359. flexDirection: 'row'
  360. },
  361. agreeText: {
  362. color: textPrimary,
  363. fontSize: 14,
  364. paddingTop: 4,
  365. paddingBottom: 4
  366. },
  367. agreeLink: {
  368. ...ui.link,
  369. fontSize: 14,
  370. paddingTop: 4,
  371. paddingBottom: 4,
  372. textDecorationLine: 'underline'
  373. }
  374. })