TopupNew.js 10 KB

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