PayNow.js 8.4 KB

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