From f3c622997d930824cb6f62906dee93527d73529f Mon Sep 17 00:00:00 2001 From: Gabriel Masclef Date: Thu, 1 Mar 2018 11:21:30 -0300 Subject: [PATCH] Feat: support required fee in paypro --- app-template/package-template.json | 4 +- package.json | 2 +- src/pages/send/confirm/confirm.ts | 53 ++++++++++++++++++-- src/providers/incoming-data/incoming-data.ts | 6 ++- src/providers/logger/logger.ts | 3 +- src/providers/paypro/paypro.ts | 2 +- 6 files changed, 59 insertions(+), 11 deletions(-) diff --git a/app-template/package-template.json b/app-template/package-template.json index 7c77c63c4..954f776da 100644 --- a/app-template/package-template.json +++ b/app-template/package-template.json @@ -115,7 +115,7 @@ "@types/papaparse": "^4.1.33", "angular2-moment": "^1.7.1", "bitauth": "git+https://github.com/bitpay/bitauth.git#copay", - "bitcore-wallet-client": "^6.6.4", + "bitcore-wallet-client": "6.7.1", "buffer-compare": "^1.1.1", "cordova-android": "7.0.0", "cordova-clipboard": "^1.1.1", @@ -235,4 +235,4 @@ "prettier": { "singleQuote": true } -} +} \ No newline at end of file diff --git a/package.json b/package.json index 4231f6c70..9d1a417c6 100644 --- a/package.json +++ b/package.json @@ -17,4 +17,4 @@ "url": "git://github.com/bitpay/copay.git", "type": "git" } -} +} \ No newline at end of file diff --git a/src/pages/send/confirm/confirm.ts b/src/pages/send/confirm/confirm.ts index 5d85af2e1..9150f355a 100644 --- a/src/pages/send/confirm/confirm.ts +++ b/src/pages/send/confirm/confirm.ts @@ -58,6 +58,7 @@ export class ConfirmPage { // custom fee flag public usingCustomFee: boolean = false; + public usingMerchantFee: boolean = false; constructor( private bwcProvider: BwcProvider, @@ -127,6 +128,13 @@ export class ConfirmPage { }; this.tx.origToAddress = this.tx.toAddress; + if (this.navParams.data.requiredFeeRate) { + this.usingMerchantFee = true; + this.tx.feeRate = parseInt(this.navParams.data.requiredFeeRate, 10); + } else { + this.tx.feeLevel = (this.tx.coin && this.tx.coin == 'bch') ? 'normal ' : this.configFeeLevel; + } + if (this.tx.coin && this.tx.coin == 'bch') { this.tx.feeLevel = 'normal'; @@ -237,7 +245,10 @@ export class ConfirmPage { // If select another wallet this.tx.coin = this.wallet.coin; - if (this.tx.coin == 'bch') this.tx.feeLevel = 'normal'; + + if (!this.usingCustomFee && !this.usingMerchantFee) { + this.tx.feeLevel = wallet.coin == 'bch' ? 'normal' : this.configFeeLevel; + } this.setButtonText(this.wallet.credentials.m > 1, !!this.tx.paypro); @@ -304,9 +315,34 @@ export class ConfirmPage { return resolve(); } + let maxAllowedMerchantFee = { + btc: 'urgent', + bch: 'normal', + } + this.onGoingProcessProvider.set('calculatingFee'); - this.feeProvider.getFeeRate(wallet.coin, tx.network, tx.feeLevel).then((feeRate: any) => { - if (!this.usingCustomFee) tx.feeRate = feeRate; + this.feeProvider.getFeeRate(wallet.coin, tx.network, this.usingMerchantFee ? maxAllowedMerchantFee[wallet.coin] : this.tx.feeLevel).then((feeRate: any) => { + + let msg; + if (this.usingCustomFee) { + msg = this.translate.instant('Custom'); + tx.feeLevelName = msg; + } else if (this.usingMerchantFee) { + + let maxAllowedfee = feeRate * 2; + this.logger.info('Using Merchant Fee:' + tx.feeRate + ' vs. referent level:' + maxAllowedfee); + if (tx.feeRate > maxAllowedfee) { + this.onGoingProcessProvider.set('calculatingFee'); + this.setNoWallet(this.translate.instant('Merchant fee to high. Payment rejected'), true); + return reject('fee_too_high'); + } + + msg = this.translate.instant('Suggested by Merchant'); + tx.feeLevelName = msg; + } else { + tx.feeLevelName = this.feeProvider.feeOpts[tx.feeLevel]; + tx.feeRate = feeRate; + } // call getSendMaxInfo if was selected from amount view if (tx.sendMax) { @@ -394,7 +430,13 @@ export class ConfirmPage { this.logger.debug('Confirm. TX Fully Updated for wallet:' + wallet.id, JSON.stringify(tx)); return resolve(); }).catch((err: any) => { - return reject(err); + if (err.message == 'Insufficient funds') { + this.setNoWallet(this.translate.instant('Insufficient funds')); + this.popupProvider.ionicAlert(this.translate.instant('Error'), this.translate.instant('Not enough funds for fee')); + return reject('no_funds'); + } else { + return reject(err); + } }); }); } @@ -477,7 +519,7 @@ export class ConfirmPage { txp.inputs = tx.sendMaxInfo.inputs; txp.fee = tx.sendMaxInfo.fee; } else { - if (this.usingCustomFee) { + if (this.usingCustomFee || this.usingMerchantFee) { txp.feePerKb = tx.feeRate; } else txp.feeLevel = tx.feeLevel; } @@ -628,6 +670,7 @@ export class ConfirmPage { public chooseFeeLevel(): void { if (this.tx.coin == 'bch') return; + if (this.usingMerchantFee) return; // ToDo: should we allow overwride? let txObject: any = {}; txObject.network = this.tx.network; diff --git a/src/providers/incoming-data/incoming-data.ts b/src/providers/incoming-data/incoming-data.ts index 9372b64a4..28eceae89 100644 --- a/src/providers/incoming-data/incoming-data.ts +++ b/src/providers/incoming-data/incoming-data.ts @@ -322,13 +322,17 @@ export class IncomingDataProvider { } private handlePayPro(payProDetails: any, coin?: string): void { - let stateParams = { + let stateParams: any = { amount: payProDetails.amount, toAddress: payProDetails.toAddress, description: payProDetails.memo, paypro: payProDetails, coin }; + // fee + if (payProDetails.requiredFeeRate) { + stateParams.requiredFeeRate = payProDetails.requiredFeeRate * 1024; + } this.scanProvider.pausePreview(); this.navCtrl.push(ConfirmPage, stateParams); } diff --git a/src/providers/logger/logger.ts b/src/providers/logger/logger.ts index 0c214fd24..17a7e6147 100644 --- a/src/providers/logger/logger.ts +++ b/src/providers/logger/logger.ts @@ -67,7 +67,8 @@ export class Logger { } public add(level, msg): any { - msg = msg.replace('/xpriv.*/', 'xpriv[Hidden]'); + msg = msg.replace('/xpriv.*/', '[...]'); + msg = msg.replace('/walletPrivKey.*/', 'walletPrivKey:[...]'); this.logs.push({ timestamp: new Date().toISOString(), level, diff --git a/src/providers/paypro/paypro.ts b/src/providers/paypro/paypro.ts index f25e56d1e..f7c0e24a1 100644 --- a/src/providers/paypro/paypro.ts +++ b/src/providers/paypro/paypro.ts @@ -35,7 +35,7 @@ export class PayproProvider { payProUrl: uri, }, (err, paypro) => { if (!disableLoader) this.onGoingProcessProvider.clear(); - if (err) return reject(err); + if (err) return reject(this.translate.instant('Could Not Fetch Payment: Check if it is still valid')); else if (!paypro.verified) { this.logger.warn('Failed to verify payment protocol signatures'); return reject(this.translate.instant('Payment Protocol Invalid'));