Payment.js 4.9 KB

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