Skip to content

Commit

Permalink
Merge pull request #12018 from gabrielbazan7/ref/invoicerates
Browse files Browse the repository at this point in the history
[REF] use invoice exchange rates
  • Loading branch information
cmgustavo authored Mar 29, 2022
2 parents cfadbec + 20d6fe5 commit 95e27d5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/pages/send/confirm/confirm.html
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
{{requiredFee | satToUnit: getChain(tx.coin)}}
</div>
<div *ngIf="requiredFee" class="secondary-note fee-details">
{{requiredFee | satToFiat: getChain(tx.coin)}}
{{requiredFee | satToFiat: getChain(tx.coin):tx.paypro && tx.paypro.exchangeRates}}
</div>
</div>
<div *ngIf="tx.txp[wallet.id]" class="summary-item-detail">
Expand All @@ -172,7 +172,7 @@
{{tx.txp[wallet.id].fee | satToUnit: getChain(tx.coin)}}
</div>
<div *ngIf="tx.txp[wallet.id]" class="secondary-note fee-details" [ngStyle]="tx.txp[wallet.id].feeTooHigh === true ? { opacity: 1 } : null">
{{tx.txp[wallet.id].fee | satToFiat: getChain(tx.coin)}}
{{tx.txp[wallet.id].fee | satToFiat: getChain(tx.coin):tx.paypro && tx.paypro.exchangeRates}}
<span *ngIf="tx.txp[wallet.id].feeRatePerStr">
&middot;
<span>
Expand Down Expand Up @@ -409,7 +409,7 @@

<div class="amount-details" padding-bottom *ngIf="!coinbaseAccount && !currencyProvider.isCustomERCToken(tx.coin)">
<div class="total-amount-note" [ngClass]="{'subtotal-amount-note': !wallet?.credentials.token}">
<span *ngIf="!fromSelectInputs && !fromReplaceByFee">{{tx.amount | satToFiat: tx.coin}}</span>
<span *ngIf="!fromSelectInputs && !fromReplaceByFee">{{tx.amount | satToFiat: tx.coin:tx.paypro && tx.paypro.exchangeRates}}</span>
<span *ngIf="(fromSelectInputs || fromReplaceByFee) && wallet && tx.txp[wallet.id] && tx.txp[wallet.id].fee">{{tx.amount - tx.txp[wallet.id].fee | satToFiat: tx.coin}}</span>
</div>
<div class="total-amount-note" [ngClass]="{'subtotal-amount-note': !wallet?.credentials.token}">
Expand All @@ -436,7 +436,7 @@

<div class="amount-details" *ngIf="totalAmount" padding-bottom>
<div class="total-amount-note">
{{totalAmount | satToFiat: tx.coin}}
{{totalAmount | satToFiat: tx.coin:tx.paypro && tx.paypro.exchangeRates}}
</div>
</div>

Expand Down
12 changes: 10 additions & 2 deletions src/pipes/satToFiat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,19 @@ export class SatToFiatPipe implements PipeTransform {
) {
this.walletSettings = this.configProvider.get().wallet.settings;
}
transform(amount: number, coin: string) {
transform(amount: number, coin: string, exchangeRates?: any) {
const lowercaseKeys = obj => {
return Object.keys(obj).reduce((destination, key) => {
destination[key.toLowerCase()] = obj[key];
return destination;
}, {});
};

let amount_ = this.rateProvider.toFiat(
amount,
this.walletSettings.alternativeIsoCode,
coin.toLowerCase()
coin.toLowerCase(),
{ rates: exchangeRates && lowercaseKeys(exchangeRates) }
);
return (
this.decimalPipe.transform(amount_ || 0, '1.2-2') +
Expand Down
13 changes: 13 additions & 0 deletions src/providers/invoice/invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ export class InvoiceProvider {
return res.data;
}

public async getBitPayInvoiceWithNetwork(id: string, network: string) {
const host = network === 'testnet' ? 'test.bitpay.com' : 'bitpay.com';
const res: any = await this.http
.get(`https://${host}/invoices/${id}`)
.toPromise()
.catch(err => {
this.logger.error('BitPay Get Invoice: ERROR ' + err.error.message);
throw err.error.message;
});
this.logger.info('BitPay Get Invoice: SUCCESS');
return res.data;
}

public emailIsValid(email: string): boolean {
const validEmail = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(
email
Expand Down
16 changes: 15 additions & 1 deletion src/providers/paypro/paypro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Logger } from '../../providers/logger/logger';
// providers
import { BwcProvider } from '../bwc/bwc';
import { CurrencyProvider } from '../currency/currency';
import { InvoiceProvider } from '../invoice/invoice';
import { OnGoingProcessProvider } from '../on-going-process/on-going-process';

@Injectable()
Expand All @@ -12,7 +13,8 @@ export class PayproProvider {
private logger: Logger,
private bwcProvider: BwcProvider,
private currencyProvider: CurrencyProvider,
private onGoingProcessProvider: OnGoingProcessProvider
private onGoingProcessProvider: OnGoingProcessProvider,
private invoiceProvider: InvoiceProvider
) {
this.logger.debug('PayproProvider initialized');
}
Expand Down Expand Up @@ -85,6 +87,18 @@ export class PayproProvider {
}
});
if (!disableLoader) this.onGoingProcessProvider.clear();
// workaround for getting invoice exchange rates
if (payDetails && !payDetails.exchangeRates) {
const invoiceId = payDetails.payProUrl.split('i/')[1];
const network = payDetails.payProUrl.includes('test')
? 'testnet'
: 'livenet';
const invoice = await this.invoiceProvider.getBitPayInvoiceWithNetwork(
invoiceId,
network
);
payDetails.exchangeRates = invoice.exchangeRates;
}
this.logger.info('PayPro Details: SUCCESS', JSON.stringify(payDetails));
return payDetails;
}
Expand Down

0 comments on commit 95e27d5

Please sign in to comment.