2018-12-25 11:34:51 -05:00
|
|
|
import React, { Component } from 'react';
|
2019-11-06 18:01:09 +01:00
|
|
|
import { View, Text, Dimensions, ScrollView, BackHandler, InteractionManager, TouchableOpacity } from 'react-native';
|
2019-08-06 00:20:33 -04:00
|
|
|
import Share from 'react-native-share';
|
2019-01-22 16:17:45 +00:00
|
|
|
import {
|
|
|
|
BlueLoading,
|
|
|
|
BlueText,
|
|
|
|
SafeBlueArea,
|
|
|
|
BlueButton,
|
|
|
|
BlueCopyTextToClipboard,
|
|
|
|
BlueNavigationStyle,
|
|
|
|
BlueSpacing20,
|
|
|
|
} from '../../BlueComponents';
|
2018-12-25 11:34:51 -05:00
|
|
|
import PropTypes from 'prop-types';
|
2018-12-27 01:12:19 -05:00
|
|
|
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
|
2018-12-30 15:34:13 +00:00
|
|
|
import { Icon } from 'react-native-elements';
|
2019-02-14 00:15:56 -05:00
|
|
|
import QRCode from 'react-native-qrcode-svg';
|
2018-12-25 11:34:51 -05:00
|
|
|
/** @type {AppStorage} */
|
|
|
|
let BlueApp = require('../../BlueApp');
|
2018-12-27 01:12:19 -05:00
|
|
|
const loc = require('../../loc');
|
|
|
|
const EV = require('../../events');
|
2019-01-04 20:28:08 -05:00
|
|
|
const { width, height } = Dimensions.get('window');
|
2018-12-25 11:34:51 -05:00
|
|
|
|
|
|
|
export default class LNDViewInvoice extends Component {
|
2019-01-14 01:55:55 -05:00
|
|
|
static navigationOptions = ({ navigation }) =>
|
|
|
|
navigation.getParam('isModal') === true
|
|
|
|
? {
|
|
|
|
...BlueNavigationStyle(navigation, true, () => navigation.dismiss()),
|
|
|
|
title: 'Lightning Invoice',
|
|
|
|
headerLeft: null,
|
|
|
|
}
|
|
|
|
: { ...BlueNavigationStyle(), title: 'Lightning Invoice' };
|
2018-12-27 10:12:22 -05:00
|
|
|
|
2018-12-25 11:34:51 -05:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2018-12-27 01:12:19 -05:00
|
|
|
const invoice = props.navigation.getParam('invoice');
|
|
|
|
const fromWallet = props.navigation.getParam('fromWallet');
|
2018-12-25 11:34:51 -05:00
|
|
|
this.state = {
|
|
|
|
invoice,
|
2018-12-27 01:12:19 -05:00
|
|
|
fromWallet,
|
2018-12-27 19:26:46 -05:00
|
|
|
isLoading: typeof invoice === 'string',
|
2019-01-01 17:31:42 -05:00
|
|
|
addressText: typeof invoice === 'object' && invoice.hasOwnProperty('payment_request') ? invoice.payment_request : invoice,
|
2018-12-30 15:34:13 +00:00
|
|
|
isFetchingInvoices: true,
|
2019-01-11 14:52:03 +00:00
|
|
|
qrCodeHeight: height > width ? width - 20 : width / 2,
|
2018-12-25 11:34:51 -05:00
|
|
|
};
|
2018-12-27 01:12:19 -05:00
|
|
|
this.fetchInvoiceInterval = undefined;
|
2019-08-30 00:14:06 -04:00
|
|
|
BackHandler.addEventListener('hardwareBackPress', this.handleBackButton);
|
2018-12-27 01:12:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
2018-12-27 18:33:39 -05:00
|
|
|
this.fetchInvoiceInterval = setInterval(async () => {
|
2018-12-30 15:34:13 +00:00
|
|
|
if (this.state.isFetchingInvoices) {
|
2018-12-27 19:26:46 -05:00
|
|
|
try {
|
2019-01-10 17:50:33 +00:00
|
|
|
const userInvoices = await this.state.fromWallet.getUserInvoices(20);
|
|
|
|
// fetching only last 20 invoices
|
|
|
|
// for invoice that was created just now - that should be enough (it is basically the last one, so limit=1 would be sufficient)
|
|
|
|
// but that might not work as intended IF user creates 21 invoices, and then tries to check the status of invoice #0, it just wont be updated
|
|
|
|
const updatedUserInvoice = userInvoices.filter(invoice =>
|
2018-12-27 19:26:46 -05:00
|
|
|
typeof this.state.invoice === 'object'
|
|
|
|
? invoice.payment_request === this.state.invoice.payment_request
|
|
|
|
: invoice.payment_request === this.state.invoice,
|
|
|
|
)[0];
|
2019-01-02 20:10:16 -05:00
|
|
|
|
|
|
|
if (typeof updatedUserInvoice !== 'undefined') {
|
|
|
|
this.setState({ invoice: updatedUserInvoice, isLoading: false, addressText: updatedUserInvoice.payment_request });
|
|
|
|
if (updatedUserInvoice.ispaid) {
|
|
|
|
// we fetched the invoice, and it is paid :-)
|
2018-12-27 19:26:46 -05:00
|
|
|
this.setState({ isFetchingInvoices: false });
|
2019-05-03 13:36:11 +01:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationSuccess', { ignoreAndroidSystemSettings: false });
|
2018-12-27 19:26:46 -05:00
|
|
|
clearInterval(this.fetchInvoiceInterval);
|
|
|
|
this.fetchInvoiceInterval = undefined;
|
2019-01-25 21:55:17 +00:00
|
|
|
EV(EV.enum.REMOTE_TRANSACTIONS_COUNT_CHANGED); // remote because we want to refetch from server tx list and balance
|
2019-01-02 20:10:16 -05:00
|
|
|
} else {
|
|
|
|
const currentDate = new Date();
|
|
|
|
const now = (currentDate.getTime() / 1000) | 0;
|
|
|
|
const invoiceExpiration = updatedUserInvoice.timestamp + updatedUserInvoice.expire_time;
|
|
|
|
if (invoiceExpiration < now && !updatedUserInvoice.ispaid) {
|
|
|
|
// invoice expired :-(
|
|
|
|
this.setState({ isFetchingInvoices: false });
|
2019-05-03 13:36:11 +01:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
2019-01-02 20:10:16 -05:00
|
|
|
clearInterval(this.fetchInvoiceInterval);
|
|
|
|
this.fetchInvoiceInterval = undefined;
|
|
|
|
EV(EV.enum.TRANSACTIONS_COUNT_CHANGED);
|
|
|
|
}
|
2018-12-27 19:26:46 -05:00
|
|
|
}
|
2018-12-27 18:40:55 -05:00
|
|
|
}
|
2018-12-27 19:26:46 -05:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
2018-12-27 18:40:55 -05:00
|
|
|
}
|
2018-12-27 18:33:39 -05:00
|
|
|
}
|
2019-01-01 00:09:10 +00:00
|
|
|
}, 3000);
|
2018-12-27 01:12:19 -05:00
|
|
|
}
|
|
|
|
|
2019-08-25 02:13:41 -04:00
|
|
|
async componentWillUnmount() {
|
2018-12-27 10:12:22 -05:00
|
|
|
clearInterval(this.fetchInvoiceInterval);
|
2018-12-27 18:33:39 -05:00
|
|
|
this.fetchInvoiceInterval = undefined;
|
2019-08-30 00:14:06 -04:00
|
|
|
BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton);
|
2019-01-11 18:01:52 -05:00
|
|
|
}
|
|
|
|
|
2019-08-30 00:14:06 -04:00
|
|
|
handleBackButton = () => {
|
2019-02-16 01:12:14 -05:00
|
|
|
this.props.navigation.goBack(null);
|
2019-01-11 18:01:52 -05:00
|
|
|
return true;
|
2019-08-30 00:14:06 -04:00
|
|
|
};
|
2018-12-25 11:34:51 -05:00
|
|
|
|
2019-01-04 20:28:08 -05:00
|
|
|
onLayout = () => {
|
|
|
|
const { height } = Dimensions.get('window');
|
2019-01-11 14:52:03 +00:00
|
|
|
this.setState({ qrCodeHeight: height > width ? width - 20 : width / 2 });
|
2019-01-04 20:28:08 -05:00
|
|
|
};
|
|
|
|
|
2018-12-25 11:34:51 -05:00
|
|
|
render() {
|
|
|
|
if (this.state.isLoading) {
|
|
|
|
return <BlueLoading />;
|
|
|
|
}
|
|
|
|
|
2018-12-27 01:12:19 -05:00
|
|
|
const { invoice } = this.state;
|
|
|
|
if (typeof invoice === 'object') {
|
|
|
|
const currentDate = new Date();
|
|
|
|
const now = (currentDate.getTime() / 1000) | 0;
|
|
|
|
const invoiceExpiration = invoice.timestamp + invoice.expire_time;
|
|
|
|
|
2019-05-03 23:24:17 +01:00
|
|
|
if (this.state.showPreimageQr) {
|
|
|
|
return (
|
|
|
|
<SafeBlueArea style={{ flex: 1 }}>
|
|
|
|
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
|
|
|
|
<BlueText>Preimage:</BlueText>
|
|
|
|
<BlueSpacing20 />
|
|
|
|
<QRCode
|
|
|
|
value={(invoice.payment_preimage && typeof invoice.payment_preimage === 'string' && invoice.payment_preimage) || 'none'}
|
|
|
|
logo={require('../../img/qr-code.png')}
|
|
|
|
size={this.state.qrCodeHeight}
|
|
|
|
logoSize={90}
|
2019-08-06 00:20:33 -04:00
|
|
|
getRef={c => (this.qrCodeSVG = c)}
|
2019-05-03 23:24:17 +01:00
|
|
|
color={BlueApp.settings.foregroundColor}
|
|
|
|
logoBackgroundColor={BlueApp.settings.brandingColor}
|
|
|
|
/>
|
|
|
|
<BlueSpacing20 />
|
2019-08-10 22:37:52 -04:00
|
|
|
<BlueCopyTextToClipboard text={invoice.payment_preimage} />
|
2019-05-03 23:24:17 +01:00
|
|
|
</View>
|
|
|
|
</SafeBlueArea>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-01 17:31:42 -05:00
|
|
|
if (invoice.ispaid || invoice.type === 'paid_invoice') {
|
2018-12-27 01:12:19 -05:00
|
|
|
return (
|
|
|
|
<SafeBlueArea style={{ flex: 1 }}>
|
2019-11-06 18:01:09 +01:00
|
|
|
<View style={{ flex: 2, flexDirection: 'column', justifyContent: 'center' }}>
|
2019-11-19 14:49:09 +00:00
|
|
|
{invoice.type === 'paid_invoice' && invoice.value && (
|
2019-11-06 18:01:09 +01:00
|
|
|
<View style={{ flexDirection: 'row', justifyContent: 'center', paddingBottom: 8 }}>
|
2019-11-19 14:49:09 +00:00
|
|
|
<Text style={{ color: '#0f5cc0', fontSize: 32, fontWeight: '600' }}>{invoice.value}</Text>
|
|
|
|
<Text
|
|
|
|
style={{
|
|
|
|
color: '#0f5cc0',
|
|
|
|
fontSize: 16,
|
|
|
|
marginHorizontal: 4,
|
|
|
|
paddingBottom: 3,
|
|
|
|
fontWeight: '600',
|
|
|
|
alignSelf: 'flex-end',
|
|
|
|
}}
|
|
|
|
>
|
2019-11-06 18:01:09 +01:00
|
|
|
{loc.lndViewInvoice.sats}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
)}
|
2019-11-19 14:49:09 +00:00
|
|
|
{invoice.type === 'user_invoice' && invoice.amt && (
|
2019-11-06 18:01:09 +01:00
|
|
|
<View style={{ flexDirection: 'row', justifyContent: 'center', paddingBottom: 8 }}>
|
2019-11-19 14:49:09 +00:00
|
|
|
<Text style={{ color: '#0f5cc0', fontSize: 32, fontWeight: '600' }}>{invoice.amt}</Text>
|
|
|
|
<Text
|
|
|
|
style={{
|
|
|
|
color: '#0f5cc0',
|
|
|
|
fontSize: 16,
|
|
|
|
marginHorizontal: 4,
|
|
|
|
paddingBottom: 3,
|
|
|
|
fontWeight: '600',
|
|
|
|
alignSelf: 'flex-end',
|
|
|
|
}}
|
|
|
|
>
|
2019-11-06 18:01:09 +01:00
|
|
|
{loc.lndViewInvoice.sats}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
)}
|
2019-11-19 14:49:09 +00:00
|
|
|
{!invoice.ispaid && invoice.memo && invoice.memo.length > 0 && (
|
|
|
|
<Text
|
|
|
|
style={{ color: '#9aa0aa', fontSize: 14, marginHorizontal: 4, paddingBottom: 6, fontWeight: '400', alignSelf: 'center' }}
|
|
|
|
>
|
2019-11-06 18:01:09 +01:00
|
|
|
{invoice.memo}
|
|
|
|
</Text>
|
|
|
|
)}
|
2019-11-19 14:49:09 +00:00
|
|
|
</View>
|
2019-11-06 18:01:09 +01:00
|
|
|
|
|
|
|
<View style={{ flex: 3, alignItems: 'center', justifyContent: 'center' }}>
|
2018-12-30 15:34:13 +00:00
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
backgroundColor: '#ccddf9',
|
|
|
|
width: 120,
|
|
|
|
height: 120,
|
|
|
|
borderRadius: 60,
|
|
|
|
alignSelf: 'center',
|
|
|
|
justifyContent: 'center',
|
2019-09-18 00:27:24 +02:00
|
|
|
marginTop: -100,
|
2019-11-06 18:01:09 +01:00
|
|
|
marginBottom: 16,
|
2018-12-30 15:34:13 +00:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon name="check" size={50} type="font-awesome" color="#0f5cc0" />
|
|
|
|
</View>
|
2019-05-03 23:58:23 +01:00
|
|
|
<BlueText>{loc.lndViewInvoice.has_been_paid}</BlueText>
|
2019-11-06 18:01:09 +01:00
|
|
|
</View>
|
|
|
|
<View style={{ flex: 1, justifyContent: 'flex-end', marginBottom: 24, alignItems: 'center' }}>
|
2019-05-03 23:24:17 +01:00
|
|
|
{invoice.payment_preimage && typeof invoice.payment_preimage === 'string' && (
|
2019-11-06 18:01:09 +01:00
|
|
|
<TouchableOpacity
|
|
|
|
style={{ flexDirection: 'row', alignItems: 'center' }}
|
|
|
|
onPress={() => this.setState({ showPreimageQr: true })}
|
|
|
|
>
|
2019-11-19 14:49:09 +00:00
|
|
|
<Text style={{ color: '#9aa0aa', fontSize: 14, marginRight: 8 }}>{loc.send.create.details}</Text>
|
2019-11-06 18:01:09 +01:00
|
|
|
<Icon name="angle-right" size={18} type="font-awesome" color="#9aa0aa" />
|
|
|
|
</TouchableOpacity>
|
2019-05-03 23:24:17 +01:00
|
|
|
)}
|
2018-12-27 01:12:19 -05:00
|
|
|
</View>
|
|
|
|
</SafeBlueArea>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (invoiceExpiration < now && !invoice.ispaid) {
|
|
|
|
return (
|
|
|
|
<SafeBlueArea style={{ flex: 1 }}>
|
|
|
|
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
|
2019-01-01 17:31:42 -05:00
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
backgroundColor: '#ccddf9',
|
|
|
|
width: 120,
|
|
|
|
height: 120,
|
|
|
|
borderRadius: 60,
|
|
|
|
alignSelf: 'center',
|
|
|
|
justifyContent: 'center',
|
2019-09-18 00:27:24 +02:00
|
|
|
marginTop: -100,
|
|
|
|
marginBottom: 30,
|
2019-01-01 17:31:42 -05:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Icon name="times" size={50} type="font-awesome" color="#0f5cc0" />
|
|
|
|
</View>
|
2019-05-03 23:58:23 +01:00
|
|
|
<BlueText>{loc.lndViewInvoice.wasnt_paid_and_expired}</BlueText>
|
2018-12-27 01:12:19 -05:00
|
|
|
</View>
|
|
|
|
</SafeBlueArea>
|
|
|
|
);
|
|
|
|
} else if (invoiceExpiration > now && invoice.ispaid) {
|
|
|
|
if (invoice.ispaid) {
|
|
|
|
return (
|
|
|
|
<SafeBlueArea style={{ flex: 1 }}>
|
|
|
|
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
|
2019-05-03 23:58:23 +01:00
|
|
|
<BlueText>{loc.lndViewInvoice.has_been_paid}</BlueText>
|
2018-12-27 01:12:19 -05:00
|
|
|
</View>
|
|
|
|
</SafeBlueArea>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Invoice has not expired, nor has it been paid for.
|
2018-12-25 11:34:51 -05:00
|
|
|
return (
|
2019-01-02 20:05:53 -05:00
|
|
|
<SafeBlueArea>
|
2019-01-11 14:52:03 +00:00
|
|
|
<ScrollView>
|
|
|
|
<View
|
|
|
|
style={{
|
|
|
|
flex: 1,
|
|
|
|
alignItems: 'center',
|
|
|
|
marginTop: 8,
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
}}
|
|
|
|
onLayout={this.onLayout}
|
|
|
|
>
|
2019-01-22 16:17:45 +00:00
|
|
|
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center', paddingHorizontal: 16 }}>
|
2019-02-14 00:15:56 -05:00
|
|
|
<QRCode
|
2019-01-11 14:52:03 +00:00
|
|
|
value={typeof this.state.invoice === 'object' ? invoice.payment_request : invoice}
|
2019-02-14 00:15:56 -05:00
|
|
|
logo={require('../../img/qr-code.png')}
|
2019-01-11 14:52:03 +00:00
|
|
|
size={this.state.qrCodeHeight}
|
2019-02-14 00:15:56 -05:00
|
|
|
logoSize={90}
|
2019-08-06 00:20:33 -04:00
|
|
|
getRef={c => (this.qrCodeSVG = c)}
|
2019-02-14 00:15:56 -05:00
|
|
|
color={BlueApp.settings.foregroundColor}
|
|
|
|
logoBackgroundColor={BlueApp.settings.brandingColor}
|
2019-01-11 14:52:03 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
|
|
|
|
<BlueSpacing20 />
|
2019-05-03 23:58:23 +01:00
|
|
|
{invoice && invoice.amt && (
|
|
|
|
<BlueText>
|
|
|
|
{loc.lndViewInvoice.please_pay} {invoice.amt} {loc.lndViewInvoice.sats}
|
|
|
|
</BlueText>
|
|
|
|
)}
|
2019-01-25 18:32:35 -05:00
|
|
|
{invoice && invoice.hasOwnProperty('description') && invoice.description.length > 0 && (
|
2019-05-03 23:58:23 +01:00
|
|
|
<BlueText>
|
|
|
|
{loc.lndViewInvoice.for} {invoice.description}
|
|
|
|
</BlueText>
|
2019-01-25 18:32:35 -05:00
|
|
|
)}
|
2019-01-22 16:17:45 +00:00
|
|
|
<BlueCopyTextToClipboard text={this.state.invoice.payment_request} />
|
2019-01-11 14:52:03 +00:00
|
|
|
|
|
|
|
<BlueButton
|
|
|
|
icon={{
|
|
|
|
name: 'share-alternative',
|
|
|
|
type: 'entypo',
|
2019-05-20 22:09:06 +02:00
|
|
|
size: 10,
|
2019-01-11 14:52:03 +00:00
|
|
|
color: BlueApp.settings.buttonTextColor,
|
|
|
|
}}
|
|
|
|
onPress={async () => {
|
2019-08-06 00:20:33 -04:00
|
|
|
if (this.qrCodeSVG === undefined) {
|
|
|
|
Share.open({ message: `lightning:${invoice.payment_request}` }).catch(error => console.log(error));
|
|
|
|
} else {
|
|
|
|
InteractionManager.runAfterInteractions(async () => {
|
|
|
|
this.qrCodeSVG.toDataURL(data => {
|
|
|
|
let shareImageBase64 = {
|
|
|
|
message: `lightning:${invoice.payment_request}`,
|
|
|
|
url: `data:image/png;base64,${data}`,
|
|
|
|
};
|
|
|
|
Share.open(shareImageBase64).catch(error => console.log(error));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2019-01-11 14:52:03 +00:00
|
|
|
}}
|
|
|
|
title={loc.receive.details.share}
|
|
|
|
/>
|
2019-01-25 18:32:35 -05:00
|
|
|
<BlueSpacing20 />
|
2019-05-22 13:09:00 +01:00
|
|
|
<BlueButton
|
|
|
|
style={{
|
|
|
|
backgroundColor: BlueApp.settings.brandingColor,
|
|
|
|
}}
|
2019-01-11 14:52:03 +00:00
|
|
|
onPress={() => this.props.navigation.navigate('LNDViewAdditionalInvoiceInformation', { fromWallet: this.state.fromWallet })}
|
2019-05-03 23:58:23 +01:00
|
|
|
title={loc.lndViewInvoice.additional_info}
|
2019-01-01 17:31:42 -05:00
|
|
|
/>
|
2018-12-25 11:34:51 -05:00
|
|
|
</View>
|
2019-01-02 20:05:53 -05:00
|
|
|
<BlueSpacing20 />
|
2019-01-11 14:52:03 +00:00
|
|
|
</ScrollView>
|
2018-12-25 11:34:51 -05:00
|
|
|
</SafeBlueArea>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LNDViewInvoice.propTypes = {
|
|
|
|
navigation: PropTypes.shape({
|
2019-02-01 10:28:43 -05:00
|
|
|
goBack: PropTypes.func,
|
|
|
|
navigate: PropTypes.func,
|
|
|
|
getParam: PropTypes.func,
|
2019-02-14 23:41:02 +00:00
|
|
|
popToTop: PropTypes.func,
|
2018-12-25 11:34:51 -05:00
|
|
|
}),
|
|
|
|
};
|