TopupNew.js 11 KB

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