TopupNew.js 11 KB

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