TopupNew.js 12 KB

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