TopupNew.js 11 KB

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