PayNow.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. <ViewShot
  135. ref={this.viewShot}
  136. style={styles.contentView2}
  137. options={{
  138. format: "png",
  139. quality: 1,
  140. fileName: "PayNow"
  141. }}>
  142. <View style={ui.center}>
  143. <EndView/>
  144. <Image
  145. source={require('../../images/tool-logo.png')}
  146. style={{width: $vw(50), height: $vw(11)}}
  147. resizeMode="contain"
  148. />
  149. </View>
  150. { this.state.qrInfo.imageBase64Code
  151. ? <Image
  152. style={styles.qrCode2}
  153. source={{uri: this.state.qrInfo.imageBase64Code}}/>
  154. : <View style={styles.qrCode2}></View>
  155. }
  156. <View style={$padding(0, 16, 16)}>
  157. <View style={ui.flexc}>
  158. <TextView style={styles.qrTextLabel}>PAYMENT AMOUNT:</TextView>
  159. <TextView style={styles.qrTextMsg}>{this.state.qrInfo.paymentAmount}</TextView>
  160. </View>
  161. <View style={ui.flexc}>
  162. <TextView style={styles.qrTextLabel}>EXPIRATION TIME:</TextView>
  163. <TextView style={styles.qrTextMsg}>{this.state.qrInfo.expirationTime}</TextView>
  164. </View>
  165. </View>
  166. </ViewShot>
  167. <View style={styles.button3}>
  168. <Button
  169. style={ui.flex1}
  170. text={$t('payment.btnSave2gallery')}
  171. elevation={1.5}
  172. onClick={() => {
  173. this.checkPermission();
  174. }}/>
  175. </View>
  176. </ScrollView>
  177. )
  178. } else {
  179. return (
  180. <ScrollView
  181. style={styles.container}>
  182. <View style={styles.headerView}>
  183. <Balance/>
  184. </View>
  185. <ViewShot
  186. ref={this.viewShot}
  187. style={styles.contentView}
  188. options={{
  189. format: "jpg",
  190. quality: 1
  191. }}>
  192. { this.state.base64
  193. ? <Image
  194. style={styles.qrCode}
  195. source={{uri: this.state.base64}}/>
  196. : <View style={styles.qrCode}></View>
  197. }
  198. </ViewShot>
  199. <Text style={styles.title}>{$t('payment.labelTopUpAmount')} {currency + ' ' + this.state.amount}</Text>
  200. <Text style={styles.tips}>{$t('payment.tipsQrPaymentSave')}</Text>
  201. <Text style={styles.tips}>{$t('payment.tipsQrPayment')}</Text>
  202. <View style={styles.button2}>
  203. <Button
  204. text={$t('payment.btnSave2gallery')}
  205. elevation={1.5}
  206. onClick={() => {
  207. this.checkPermission();
  208. }}/>
  209. </View>
  210. <View style={styles.button}>
  211. <Button
  212. text={$t('payment.paymentCompleted')}
  213. elevation={1.5}
  214. onClick={() => {
  215. goBack();
  216. }}/>
  217. </View>
  218. </ScrollView>
  219. );
  220. }
  221. }
  222. }
  223. const styles = StyleSheet.create({
  224. container: {
  225. flex: 1,
  226. backgroundColor: '#F5F5F5'
  227. },
  228. headerView: {
  229. paddingBottom: 76,
  230. //backgroundColor: colorAccent
  231. },
  232. headerInfo: {
  233. ...$padding(32, 16),
  234. alignItems: 'center'
  235. },
  236. contentView: {
  237. marginTop: -88
  238. },
  239. contentView2: {
  240. padding: 8,
  241. marginTop: 8,
  242. borderRadius: 4,
  243. backgroundColor: colorLight
  244. },
  245. qrCode: {
  246. margin: 16,
  247. width: $vw(100) - 32,
  248. height: $vw(100) - 32,
  249. borderRadius: 4,
  250. backgroundColor: '#efefef'
  251. },
  252. qrCode2: {
  253. width: $vw(85) - 32,
  254. height: $vw(85) - 32,
  255. borderRadius: 4,
  256. backgroundColor: '#efefef'
  257. },
  258. title: {
  259. color: textPrimary,
  260. fontSize: 18,
  261. paddingBottom: 16,
  262. textAlign: 'center'
  263. },
  264. tips: {
  265. color: textPrimary,
  266. padding: 16,
  267. },
  268. button: {
  269. margin: 16,
  270. paddingBottom: 16
  271. },
  272. button2: {
  273. marginLeft: 16,
  274. marginRight: 16
  275. },
  276. button3: {
  277. ...$margin(56, 16, 40),
  278. flexDirection: 'row'
  279. },
  280. paymentTitle: {
  281. color: textPrimary,
  282. fontSize: 16,
  283. fontWeight: 'bold'
  284. },
  285. paymentText: {
  286. color: textPrimary,
  287. fontSize: 15,
  288. paddingTop: 2
  289. },
  290. qrTextLabel: {
  291. color: textPrimary,
  292. padding: 2,
  293. fontSize: 11,
  294. fontWeight: 'bold'
  295. },
  296. qrTextMsg: {
  297. color: textPrimary,
  298. fontSize: 11,
  299. paddingLeft: 2
  300. }
  301. })