PayNow.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**
  2. * PayNow支付页面
  3. * @邠心vbe on 2021/05/12
  4. */
  5. import React, { Component } from 'react';
  6. import { View, Text, StyleSheet, Image, ScrollView } from 'react-native';
  7. import {CameraRoll} from "@react-native-camera-roll/camera-roll";
  8. import ViewShot from "react-native-view-shot";
  9. import Button from '../../components/Button';
  10. import Dialog from '../../components/Dialog';
  11. import { Balance } from '../wallet/Payment';
  12. import {check, request, openSettings, PERMISSIONS, RESULTS} from 'react-native-permissions';
  13. import TextView from '../../components/TextView';
  14. import utils from '../../utils/utils';
  15. export default class PayNow extends Component {
  16. constructor(props) {
  17. super(props);
  18. this.state = {
  19. amount: 0,
  20. base64: '',
  21. qrInfo: {
  22. merchantUEN: "",
  23. merchantName: "",
  24. paymentAmount: "",
  25. expirationTime: "",
  26. imageBase64Code: ""
  27. },
  28. isNewVersion: false
  29. };
  30. this.denied = true;
  31. this.viewShot = React.createRef();
  32. }
  33. componentDidMount() {
  34. if (this.props.route.params.info) {
  35. this.setState({
  36. qrInfo: this.props.route.params.info,
  37. isNewVersion: true
  38. })
  39. console.log("新版ANZ支付");
  40. } else if (this.props.route.params.amount) {
  41. this.setState({
  42. isNewVersion: false,
  43. amount: this.props.route.params?.amount,
  44. base64: 'data:image/png;base64,' + this.props.route.params.base64
  45. });
  46. }
  47. }
  48. savePhoto() {
  49. Dialog.showProgressDialog();
  50. this.viewShot.current.capture().then(uri => {
  51. Dialog.dismissLoading();
  52. CameraRoll.save(uri, { type: 'photo' });
  53. console.log("Save Photo with ", uri);
  54. toastShort($t('payment.successSave2gallery'));
  55. }).catch(errs => {
  56. toastShort(errs)
  57. Dialog.dismissLoading();
  58. });
  59. }
  60. getPermission() {
  61. request(
  62. isIOS
  63. ? PERMISSIONS.IOS.PHOTO_LIBRARY_ADD_ONLY
  64. : utils.getFilePermissionString())
  65. .then(res => {
  66. console.log('getPermission', res)
  67. this.checkPermission();
  68. }).catch(errs => {
  69. console.info('getPermission', errs)
  70. });
  71. }
  72. checkPermission() {
  73. check(
  74. isIOS
  75. ? PERMISSIONS.IOS.PHOTO_LIBRARY_ADD_ONLY
  76. : utils.getFilePermissionString())
  77. .then(res => {
  78. switch (res) {
  79. case RESULTS.UNAVAILABLE:
  80. console.log('此功能不可用(在此设备上/在此上下文中)');
  81. if (isIOS) {
  82. this.savePhoto();
  83. } else {
  84. toastShort($t('payment.errSave2gallery'));
  85. }
  86. break;
  87. case RESULTS.DENIED:
  88. console.log('权限未被请求/被拒绝,但可以请求');
  89. if (this.denied) {
  90. this.denied = false;
  91. this.getPermission();
  92. } else {
  93. this.denied = true;
  94. toastShort($t('payment.errSave2gallery'));
  95. }
  96. break;
  97. case RESULTS.LIMITED:
  98. console.log('权限是有限的:有些操作是可能的');
  99. this.savePhoto();
  100. break;
  101. case RESULTS.GRANTED:
  102. console.log('许可被授予');
  103. this.savePhoto();
  104. break;
  105. case RESULTS.BLOCKED:
  106. console.log('权限被拒绝,不再可请求');
  107. Dialog.showDialog({
  108. title: $t('common.error'),
  109. message: $t('payment.errSave2galleryPermission'),
  110. ok: $t('payment.btnSetting'),
  111. callback: btn => {
  112. if (btn == Dialog.BUTTON_OK) {
  113. console.log('ok');
  114. openSettings().catch(() => console.warn('cannot open settings'));
  115. }
  116. }
  117. });
  118. //toastShort('Save to gallery failed');
  119. break;
  120. }
  121. }).catch(err1 => {
  122. })
  123. }
  124. render() {
  125. if (this.state.isNewVersion) {
  126. return (
  127. <ScrollView
  128. style={styles.container}
  129. contentContainerStyle={ui.center}>
  130. {/* <View style={styles.headerInfo}>
  131. <TextView style={styles.paymentTitle}>PAYMENT TO:</TextView>
  132. <TextView style={styles.paymentText}>{this.state.qrInfo.merchantName}</TextView>
  133. <TextView style={styles.paymentText}>({this.state.qrInfo.merchantUEN})</TextView>
  134. </View> */}
  135. <EndView/>
  136. <ViewShot
  137. ref={this.viewShot}
  138. style={styles.contentView2}
  139. options={{
  140. format: "png",
  141. quality: 1,
  142. fileName: "PayNow"
  143. }}>
  144. <View style={ui.center}>
  145. <EndView/>
  146. <Image
  147. source={require('../../images/tool-logo.png')}
  148. style={{width: $vw(50), height: $vw(11)}}
  149. resizeMode="contain"
  150. />
  151. </View>
  152. { this.state.qrInfo.imageBase64Code
  153. ? <Image
  154. style={styles.qrCode2}
  155. source={{uri: this.state.qrInfo.imageBase64Code}}/>
  156. : <View style={styles.qrCode2}></View>
  157. }
  158. <View style={$padding(0, 16, 16)}>
  159. <View style={ui.flexc}>
  160. <TextView style={styles.qrTextLabel}>PAYMENT AMOUNT:</TextView>
  161. <TextView style={styles.qrTextMsg}>{this.state.qrInfo.paymentAmount}</TextView>
  162. </View>
  163. <View style={ui.flexc}>
  164. <TextView style={styles.qrTextLabel}>EXPIRATION TIME:</TextView>
  165. <TextView style={styles.qrTextMsg}>{this.state.qrInfo.expirationTime}</TextView>
  166. </View>
  167. </View>
  168. </ViewShot>
  169. <View style={styles.button3}>
  170. <Button
  171. style={ui.flex1}
  172. text={$t('payment.btnSave2gallery')}
  173. elevation={1.5}
  174. onClick={() => {
  175. this.checkPermission();
  176. }}/>
  177. </View>
  178. </ScrollView>
  179. )
  180. } else {
  181. return (
  182. <ScrollView
  183. style={styles.container}>
  184. <View style={styles.headerView}>
  185. <Balance/>
  186. </View>
  187. <ViewShot
  188. ref={this.viewShot}
  189. style={styles.contentView}
  190. options={{
  191. format: "jpg",
  192. quality: 1
  193. }}>
  194. { this.state.base64
  195. ? <Image
  196. style={styles.qrCode}
  197. source={{uri: this.state.base64}}/>
  198. : <View style={styles.qrCode}></View>
  199. }
  200. </ViewShot>
  201. <Text style={styles.title}>{$t('payment.labelTopUpAmount')} {currency + ' ' + this.state.amount}</Text>
  202. <Text style={styles.tips}>{$t('payment.tipsQrPaymentSave')}</Text>
  203. <Text style={styles.tips}>{$t('payment.tipsQrPayment')}</Text>
  204. <View style={styles.button2}>
  205. <Button
  206. text={$t('payment.btnSave2gallery')}
  207. elevation={1.5}
  208. onClick={() => {
  209. this.checkPermission();
  210. }}/>
  211. </View>
  212. <View style={styles.button}>
  213. <Button
  214. text={$t('payment.paymentCompleted')}
  215. elevation={1.5}
  216. onClick={() => {
  217. goBack();
  218. }}/>
  219. </View>
  220. </ScrollView>
  221. );
  222. }
  223. }
  224. }
  225. const styles = StyleSheet.create({
  226. container: {
  227. flex: 1,
  228. backgroundColor: '#F5F5F5'
  229. },
  230. headerView: {
  231. paddingBottom: 76,
  232. //backgroundColor: colorAccent
  233. },
  234. headerInfo: {
  235. ...$padding(32, 16),
  236. alignItems: 'center'
  237. },
  238. contentView: {
  239. marginTop: -88
  240. },
  241. contentView2: {
  242. padding: 8,
  243. marginTop: 8,
  244. borderRadius: 4,
  245. backgroundColor: colorLight
  246. },
  247. qrCode: {
  248. margin: 16,
  249. width: $vw(100) - 32,
  250. height: $vw(100) - 32,
  251. borderRadius: 4,
  252. backgroundColor: '#efefef'
  253. },
  254. qrCode2: {
  255. width: $vw(85) - 32,
  256. height: $vw(85) - 32,
  257. borderRadius: 4,
  258. backgroundColor: '#efefef'
  259. },
  260. title: {
  261. color: textPrimary,
  262. fontSize: 18,
  263. paddingBottom: 16,
  264. textAlign: 'center'
  265. },
  266. tips: {
  267. color: textPrimary,
  268. padding: 16,
  269. },
  270. button: {
  271. margin: 16,
  272. paddingBottom: 16
  273. },
  274. button2: {
  275. marginLeft: 16,
  276. marginRight: 16
  277. },
  278. button3: {
  279. ...$margin(56, 16, 40),
  280. flexDirection: 'row'
  281. },
  282. paymentTitle: {
  283. color: textPrimary,
  284. fontSize: 16,
  285. fontWeight: 'bold'
  286. },
  287. paymentText: {
  288. color: textPrimary,
  289. fontSize: 15,
  290. paddingTop: 2
  291. },
  292. qrTextLabel: {
  293. color: textPrimary,
  294. padding: 2,
  295. fontSize: 11,
  296. fontWeight: 'bold'
  297. },
  298. qrTextMsg: {
  299. color: textPrimary,
  300. fontSize: 11,
  301. paddingLeft: 2
  302. }
  303. })