TopupNew.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. 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. if (this.state.payType?.fomoPayType) {
  85. params.payTypeSelection = this.state.payType.fomoPayType
  86. }
  87. Dialog.showProgressDialog();
  88. //console.log('充值请求参数', params);
  89. apiWallet.doPaymentV2(params).then(res => {
  90. Dialog.dismissLoading();
  91. if (app.v3.anzPayment) {
  92. if (res.data.qr) {
  93. startPage(PageList.paynow, { info: res.data.qr, type: 'Topup' });
  94. } else if (res.data.webPaymentUrl) {
  95. startPage(PageList.paymentWeb, { amount: params.payAmount, url: res.data.webPaymentUrl, type: 'Topup' });
  96. } else {
  97. toastShort('Error 01')
  98. }
  99. } else {
  100. if (res.data.webPaymentUrl) {
  101. startPage(PageList.paymentWeb, { amount: params.payAmount, url: res.data.webPaymentUrl, type: 'Topup' });
  102. } else {
  103. toastShort('Error 0')
  104. }
  105. }
  106. }).catch(err => {
  107. Dialog.dismissLoading();
  108. toastShort(err);
  109. });
  110. }
  111. /**
  112. * FOMO充值
  113. */
  114. topup() {
  115. const topup = this.state.topupList[this.state.selectIndex]
  116. const params = {
  117. currency: topup?.currency,
  118. payAmount: topup?.amount,
  119. fomoPayType: this.state.payType?.fomoPayType
  120. }
  121. //console.log('params',params);
  122. if (params.payAmount) {
  123. if (params.fomoPayType == 'PAYNOW' || params.fomoPayType == 'GRABPAY') {
  124. //PAYNOW支付
  125. Dialog.showProgressDialog();
  126. apiWallet.doPayment(params).then(res => {
  127. Dialog.dismissLoading();
  128. if (res.data.fomoId && res.data.qrCodeInBase64) {
  129. startPage(PageList.paynow, { amount: params.payAmount, base64: res.data.qrCodeInBase64 });
  130. } else if (res.data.url) {
  131. startPage(PageList.paymentWeb, { amount: params.payAmount, url: res.data.url, type: 'Topup' });
  132. } else {
  133. toastShort('Error 0')
  134. }
  135. }).catch(err => {
  136. Dialog.dismissLoading();
  137. toastShort(err);
  138. });
  139. } else {
  140. //信用卡支付
  141. startPage(PageList.formCard, { amount: params.payAmount, payType: params.fomoPayType });
  142. }
  143. } else {
  144. toastShort('Error 1')
  145. }
  146. }
  147. changeAgree(ag) {
  148. this.setState({
  149. agree: ag
  150. });
  151. }
  152. toPolicy() {
  153. Linking.openURL(app.storeUrl.refundUrl);
  154. }
  155. render() {
  156. return (
  157. <View style={styles.container}>
  158. <ScrollView style={ui.flex1}>
  159. <View style={styles.headerView}>
  160. <Balance balanceText={this.state.balance}/>
  161. </View>
  162. <View style={styles.contentView}>
  163. <View style={styles.topupView}>
  164. <WalletTitle>{$t('wallet.titleChooseCreditValue')}</WalletTitle>
  165. { app.modules.nationally
  166. ? <View style={styles.topupItems2}>
  167. { this.state.topupList.map((item, index) => {
  168. return (
  169. <BadgeSelectItem
  170. key={index}
  171. style={[styles.topupItem2]}
  172. checked={index == this.state.selectIndex}
  173. onPress={() => {
  174. this.setState({
  175. selectIndex: index
  176. })
  177. }}>
  178. <Text style={[styles.topupText, index == this.state.selectIndex && {color: colorAccent}]}>
  179. <Text style={{fontSize: 16}}>{item.currencySymbol} </Text>
  180. {item.amount}
  181. {/* <Text style={{fontSize: 16}}> {item.currency}</Text> */}
  182. </Text>
  183. </BadgeSelectItem>
  184. );
  185. })
  186. }
  187. </View>
  188. : <View style={styles.topupItems}>
  189. { this.state.topupList.map((item, index) => {
  190. return (
  191. <BadgeSelectItem
  192. key={index}
  193. style={[styles.topupItem, index > 0 && styles.right]}
  194. checked={index == this.state.selectIndex}
  195. onPress={() => {
  196. this.setState({
  197. selectIndex: index
  198. })
  199. }}>
  200. <Text style={[styles.topupText, index == this.state.selectIndex && {color: colorAccent}]}>
  201. <Text style={{fontSize: 16}}>{item.currencySymbol} </Text>
  202. {item.amount}
  203. {/* <Text style={{fontSize: 16}}> {item.currency}</Text> */}
  204. </Text>
  205. </BadgeSelectItem>
  206. );
  207. })
  208. }
  209. </View>
  210. }
  211. </View>
  212. { (!PaymentDefault.is2c2p || app.modules.topup_payment_type) &&
  213. <View style={styles.topupView}>
  214. <WalletTitle>{$t('wallet.titleChoosePaymentType')}</WalletTitle>
  215. <TopupPaythod
  216. onChange={type => {
  217. this.setState({
  218. payType: type
  219. });
  220. }}/>
  221. </View>
  222. }
  223. </View>
  224. </ScrollView>
  225. <View style={styles.buttonView}>
  226. { utils.isNotEmpty(app.storeUrl.refundUrl) &&
  227. <View style={styles.agreeView}>
  228. <CheckBox
  229. value={this.state.agree}
  230. onTintColor={colorAccent}
  231. onCheckColor={colorAccent}
  232. onValueChange={v => this.changeAgree(v)}
  233. />
  234. <View style={styles.agreeTextRow}>
  235. <TextView style={styles.agreeText} onPress={() => this.changeAgree(!this.state.agree)}>
  236. {$t('sign.iHaveReadAndAgree')}
  237. </TextView>
  238. <TextView style={styles.agreeLink} onPress={() => this.toPolicy()}>Refund Policy</TextView>
  239. </View>
  240. </View>
  241. }
  242. <Button
  243. text={$t('wallet.btnPurchase')}
  244. elevation={1.5}
  245. disabled={!this.state.agree}
  246. onClick={() => {
  247. PaymentDefault.is2c2p
  248. ? this.topup2()
  249. : this.topup()
  250. }}/>
  251. </View>
  252. </View>
  253. );
  254. }
  255. }
  256. export const WalletTitle = ({children}) => {
  257. return (
  258. <View style={styles.topupTitle}>
  259. {/* <Text style={styles.titleLeft}></Text> */}
  260. <TextView style={styles.titleText}>{children}</TextView>
  261. </View>
  262. );
  263. }
  264. const styles = StyleSheet.create({
  265. container: {
  266. flex: 1,
  267. backgroundColor: pageBackground
  268. },
  269. headerView: {
  270. paddingBottom: 76,
  271. //backgroundColor: colorAccent
  272. },
  273. contentView: {
  274. padding: 16,
  275. marginTop: -88
  276. },
  277. topupView: {
  278. marginBottom: 16
  279. },
  280. topupViewOld: {
  281. padding: 16,
  282. borderRadius: 10,
  283. marginBottom: 16,
  284. backgroundColor: colorLight
  285. },
  286. topupTitle: {
  287. marginBottom: 16,
  288. alignItems: 'center',
  289. flexDirection: 'row'
  290. },
  291. titleLeft: {
  292. width: 4,
  293. height: 15,
  294. borderRadius: 16,
  295. backgroundColor: colorPrimary
  296. },
  297. titleText: {
  298. color: textPrimary,
  299. fontSize: 16,
  300. paddingLeft: 0,
  301. fontWeight: 'bold',
  302. textShadowOffset: {
  303. width: 0,
  304. height: 1
  305. },
  306. textShadowRadius: 4,
  307. textShadowColor: "rgba(0, 0, 0, 0.25)",
  308. },
  309. subTitle: {
  310. color: textPrimary,
  311. fontSize: 14,
  312. marginTop: 16,
  313. marginBottom: 16
  314. },
  315. topupItems: {
  316. paddingBottom: 16,
  317. alignItems: 'center',
  318. flexDirection: 'row'
  319. },
  320. topupItems2: {
  321. paddingBottom: 16,
  322. alignItems: 'center'
  323. },
  324. topupItem: {
  325. flex: 1,
  326. alignItems: 'center',
  327. justifyContent: 'center',
  328. backgroundColor: colorLight,
  329. ...ElevationObject(5)
  330. },
  331. topupItem2: {
  332. width: "100%",
  333. marginBottom: 16,
  334. alignItems: 'center',
  335. justifyContent: 'center',
  336. backgroundColor: colorLight,
  337. ...ElevationObject(5)
  338. },
  339. topupText: {
  340. fontSize: 18,
  341. color: textPrimary,
  342. fontWeight: 'bold',
  343. ...$padding(20, 0)
  344. },
  345. right: {
  346. marginLeft: 16
  347. },
  348. selected: {
  349. color: textLight,
  350. borderColor: colorDark,
  351. backgroundColor: colorPrimary
  352. },
  353. autoView: {
  354. flex: 1,
  355. color: textPrimary,
  356. fontSize: 14,
  357. paddingRight: 32
  358. },
  359. buttonView: {
  360. padding: 16,
  361. marginTop: 2,
  362. marginBottom: 16
  363. },
  364. agreeView: {
  365. marginBottom: 16,
  366. alignItems: 'center',
  367. flexDirection: 'row'
  368. },
  369. agreeTextRow: {
  370. flex: 1,
  371. flexWrap: 'wrap',
  372. flexDirection: 'row'
  373. },
  374. agreeText: {
  375. color: textPrimary,
  376. fontSize: 14,
  377. paddingTop: 4,
  378. paddingBottom: 4
  379. },
  380. agreeLink: {
  381. ...ui.link,
  382. fontSize: 14,
  383. paddingTop: 4,
  384. paddingBottom: 4,
  385. textDecorationLine: 'underline'
  386. }
  387. })