Payment.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * 支付组件
  3. * @邠心vbe on 2021/04/23
  4. */
  5. import React, { useEffect, useState } from 'react';
  6. import { View, Text, StyleSheet, Image } from 'react-native';
  7. import Button from '../../components/Button';
  8. import utils from '../../utils/utils';
  9. import { LowCreditDialog } from '../charge/InfoDialog';
  10. import { PageList } from '../Router';
  11. /**
  12. * DEFAULT
  13. * 控制是否启用2C2P
  14. */
  15. export const PaymentDefault = {
  16. DEFAULT: {
  17. payType: "CreditWallet",
  18. payName: "Credit Wallet",
  19. isWallet: true,
  20. isPayPerUse: false
  21. },
  22. DEFAULT2: {
  23. payType: "PayPerUse",
  24. payName: "Pay Per Use (SGQR)",
  25. isWallet: false,
  26. isPayPerUse: true
  27. }
  28. }
  29. export const PAYTYPE = {
  30. CREDIT_WALLET: "CreditWallet", // 钱包余额支付
  31. PAY_PER_USE: "PayPerUse" // 按次支付
  32. }
  33. const Payment = ({
  34. topup, balance,
  35. payType = PaymentDefault.DEFAULT.payName,
  36. isWallet = PaymentDefault.DEFAULT.isWallet,
  37. isPayPerUse = PaymentDefault.DEFAULT.isPayPerUse,
  38. canShowLowCreditDialog = false,
  39. onMethodChange,
  40. refreshId = 0
  41. }) => {
  42. const [visible, showDialog] = useState(false)
  43. useEffect(() => {
  44. if (balance == undefined && canShowLowCreditDialog) {
  45. if (userInfo.credit <= 5 && refreshId > 0) {
  46. showDialog(true)
  47. }
  48. }
  49. }, [refreshId]);
  50. return (
  51. <View style={styles.paymentView}>
  52. <Image
  53. style={styles.walletIcon}
  54. source={require('../../images/charge/ic-payment.png')}
  55. />
  56. { isWallet || isPayPerUse
  57. ? <View style={{paddingLeft: 16}}>
  58. <Image
  59. style={{
  60. width: 51,
  61. height: 16
  62. }}
  63. source={require('../../images/app-logo.png')}/>
  64. <Text
  65. style={styles.rateText}
  66. numberOfLines={1}
  67. ellipsizeMode={'clip'}>{payType}</Text>
  68. </View>
  69. : <View style={{paddingLeft: 16}}>
  70. <Text style={styles.creditText}>Visa/Mastercard</Text>
  71. <Text style={styles.creditText}>Credit/Debit Card</Text>
  72. </View>
  73. }
  74. <Text
  75. style={styles.balance}
  76. numberOfLines={1}
  77. suppressHighlighting={true}>
  78. {isWallet ? currency + (balance ?? userInfo.credit) : ''}
  79. </Text>
  80. { topup
  81. ? <Button
  82. style={styles.topupView}
  83. text="+ Top Up"
  84. textStyle={styles.topupText}
  85. viewStyle={styles.infoStatus}
  86. onClick={topup}/>
  87. : <Button
  88. style={styles.topupView}
  89. text="Selected"
  90. textStyle={styles.topupText}
  91. viewStyle={styles.infoStatus}
  92. onClick={() => {if (onMethodChange) onMethodChange()}}/>
  93. }
  94. <LowCreditDialog
  95. visible={visible}
  96. onClose={topup => {
  97. showDialog(false)
  98. if (topup) {
  99. startPage(PageList.topup)
  100. } else {
  101. //goBack();
  102. }
  103. }
  104. }/>
  105. </View>
  106. );
  107. }
  108. export default Payment;
  109. export const Balance = ({balance}) => {
  110. return (
  111. <View style={styles.balanceView}>
  112. <Image
  113. style={styles.balanceIcon}
  114. source={require('../../images/icon/draw-wallet.png')}/>
  115. <Text style={styles.balanceTitle}>Credit Wallet</Text>
  116. <Text style={styles.balanceValue}>{currency}{getBalance(utils.isNotEmpty(balance) ? balance : userInfo.credit)}</Text>
  117. </View>
  118. );
  119. }
  120. const getBalance = (balance) => {
  121. console.log('getBalance', balance, userInfo);
  122. return balance ? balance.toFixed(2) : '0.0'
  123. }
  124. export const toTopupPage = () => {
  125. if (PaymentDefault.DEFAULT.payType == PAYTYPE.CREDIT_WALLET) {
  126. startPage(PageList.topup);
  127. } else {
  128. startPage(PageList.topupV2);
  129. }
  130. }
  131. const styles = StyleSheet.create({
  132. paymentView: {
  133. borderRadius: 8,
  134. paddingTop: 12,
  135. paddingLeft: 16,
  136. paddingRight: 16,
  137. paddingBottom: 12,
  138. alignItems: 'center',
  139. flexDirection: 'row',
  140. backgroundColor: '#F5F5F5',
  141. justifyContent: 'space-between'
  142. },
  143. walletIcon: {
  144. width: 36,
  145. height: 36
  146. },
  147. creditText: {
  148. color: '#333',
  149. fontSize: 14,
  150. },
  151. rateText: {
  152. color: '#333',
  153. fontSize: 14,
  154. },
  155. balance: {
  156. flex: 1,
  157. color: '#333',
  158. fontSize: 16,
  159. textAlign: 'right',
  160. fontWeight: 'bold',
  161. paddingRight: 24,
  162. },
  163. balanceIcon: {
  164. width: 30,
  165. height: 30
  166. },
  167. balanceTitle: {
  168. flex: 1,
  169. color: '#000',
  170. fontSize: 16,
  171. paddingLeft: 12
  172. },
  173. balanceValue: {
  174. color: '#000',
  175. fontSize: 18,
  176. paddingRight: 6
  177. },
  178. infoStatus: {
  179. paddingTop: 4,
  180. paddingLeft: 10,
  181. paddingRight: 10,
  182. paddingBottom: 4,
  183. borderRadius: 4
  184. },
  185. selected: {
  186. color: colorAccent,
  187. backgroundColor: '#333'
  188. },
  189. topupView: {
  190. borderRadius: 4,
  191. backgroundColor: '#333'
  192. },
  193. topupText: {
  194. color: colorAccent,
  195. fontSize: 12,
  196. fontWeight: 'normal'
  197. },
  198. balanceView: {
  199. padding: 16,
  200. alignItems: 'center',
  201. flexDirection: 'row'
  202. }
  203. });