First integration. BitPay invoice generation is ready. Fake responses from amazon

This commit is contained in:
Gustavo Maximiliano Cortez 2016-05-13 11:48:48 -03:00
parent 2c442d3642
commit 2c89ff7f66
No known key found for this signature in database
GPG Key ID: 15EDAD8D9F2EB1AF
17 changed files with 734 additions and 2 deletions

View File

@ -52,7 +52,7 @@ module.exports = function(grunt) {
},
},
css: {
files: ['src/css/*.css'],
files: ['src/sass/*.css', 'src/css/*.css'],
tasks: ['concat:css']
},
sass: {

BIN
public/img/amazon-card.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.5 KiB

BIN
public/img/amazon-item.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

75
public/views/amazon.html Normal file
View File

@ -0,0 +1,75 @@
<div class="topbar-container">
<nav ng-controller="topbarController as topbar"
class="tab-bar">
<section class="left-small">
<a class="p10"
ng-click="topbar.goHome()">
<span class="text-close">Close</span>
</a>
</section>
<section class="right-small">
<a class="p10" ng-click="$root.go('buyAmazon')">
<i class="fi-shopping-cart size-24"></i>
</a>
</section>
<section class="middle tab-bar-section">
<h1 class="title ellipsis">
Amazon Gift Card
</h1>
</section>
</nav>
</div>
<div class="content amazon p20b" ng-controller="amazonController as amazon">
<div class="onGoingProcess" ng-show="amazon.loading">
<div class="onGoingProcess-content">
<div class="spinner">
<div class="rect1"></div>
<div class="rect2"></div>
<div class="rect3"></div>
<div class="rect4"></div>
<div class="rect5"></div>
</div>
<span ng-show="amazon.loading">{{amazon.loading}}</span>
</div>
</div>
<div ng-init="amazon.init()">
<div class="p20t text-center">
<img src="img/amazon-gift-card.png" alt="Amazon Gift Card" width="200">
</div>
<div class="p20t text-center size-12" ng-if="!amazon.giftCards">
No gift card in your wallet. <a ng-click="$root.go('buyAmazon')">Buy one now!</a>
</div>
<div ng-if="amazon.giftCards">
<h4 class="title">Cards</h4>
<div ng-repeat="(id, item) in amazon.giftCards | orderObjectBy:'date':true track by $index"
ng-click="amazon.openCardModal(item)"
class="row collapse last-transactions-content size-12">
<div class="large-2 medium-2 small-2 columns">
<img src="img/amazon-item.png" alt="{{id}}" width="30">
</div>
<div class="large-4 medium-4 small-4 columns text-bold">
{{item.cardInfo.value.amount | currency : item.cardInfo.value.currencyCode + ' ' : 2}}
</div>
<div class="large-5 medium-5 small-5 columns text-right">
<span class="text-warning" ng-if="item.status == 'ERROR'">Error</span>
<span class="text-light" ng-if="item.status != 'ERROR'">{{item.date * 1000 | amCalendar}}</span>
</div>
<div class="large-1 medium-1 small-1 columns text-right">
<i class="icon-arrow-right3 size-18"></i>
</div>
</div>
</div>
</div>
<div class="extra-margin-bottom"></div>
</div>

119
public/views/buyAmazon.html Normal file
View File

@ -0,0 +1,119 @@
<div
class="topbar-container"
ng-include="'views/includes/topbar.html'"
ng-init="titleSection='Buy a gift card'; goBackToState = 'amazon'; noColor = true">
</div>
<div class="content amazon" ng-controller="buyAmazonController as buy">
<div class="onGoingProcess" ng-show="buy.loading">
<div class="onGoingProcess-content" ng-style="{'background-color': '#2b71b1'}">
<div class="spinner">
<div class="rect1"></div>
<div class="rect2"></div>
<div class="rect3"></div>
<div class="rect4"></div>
<div class="rect5"></div>
</div>
<span>{{buy.loading}}</span>
</div>
</div>
<div class="row m20t" ng-show="!buy.giftCard">
<div class="columns">
<div class="box-notification m20b" ng-show="buy.error" ng-click="buy.error = null">
<span class="text-warning">
{{buy.error}}
</span>
</div>
<div class="text-center">
<img src="img/amazon-card.png" alt="Amazon Gift Card" width="200">
<div class="tu size-12 text-gray">Redeem online</div>
</div>
<form
name="buyAmazonForm"
ng-submit="buy.createTx()"
novalidate>
<div class="m20v text-center">
<a class="amazon-select-amount" ng-click="buy.setAmount()">-</a>
<span class="amazon-amount">${{fiat}}</span>
<a class="amazon-select-amount" ng-click="buy.setAmount(1)">+</a>
</div>
<div
ng-init="buy.init()"
ng-click="openWalletsModal(buy.otherWallets)">
<label>Pay From Copay Wallet</label>
<div class="input">
<input type="text" id="address" name="address" ng-disabled="buy.selectedWalletId"
ng-attr-placeholder="{{'Choose your source wallet'}}"
ng-model="buy.selectedWalletName" required>
<a class="postfix size-12 m0 text-gray">
<i class="icon-wallet size-18"></i>
</a>
</div>
</div>
<div class="size-12 text-gray text-light m10v">
Use your Amazon.com Gift Card to shop from a huge selection of books, electronics, music, movies, software, apparel, toys, and more.
</div>
<div class="input m20t">
<input class="button black expand round"
ng-disabled="buy.loading || !fiat"
ng-style="{'background-color': '#2b71b1'}"
type="submit" value="Buy">
</div>
</form>
</div>
</div>
<div class="m20t row" ng-show="buy.giftCard">
<div class="columns">
<h1 class="text-center">Gift card ready to redeem!</h1>
<div class="size-12 text-center">
<span class="label gray radius">
{{buy.giftCard.cardInfo.value.amount | currency : buy.giftCard.cardInfo.value.currencyCode + ' ' : 2 }}
</span>
</div>
<div class="white p10 m10">
<div class="tu text-gray size-12">Claim code</div>
<div class="text-bold size-16 m5t enable_text_select">{{buy.giftCard.gcClaimCode}}</div>
</div>
<div class="text-center m20t">
<button class="button outline round dark-gray tiny"
ng-click="$root.openExternalLink('https://www.amazon.com/gc/redeem?claimCode=' + buy.giftCard.gcClaimCode)">
<span class="text-gray">Go to amazon.com</span>
</button>
</div>
<div class="m20 size-12 text-gray line-t p10t">
To redeem your gift card, follow these steps:
<ol class="size-12">
<li>Visit www.amazon.com/gc.</li>
<li>Click Apply to Account and enter the claim code when prompted.</li>
<li>Gift card funds will be applied automatically to eligible orders during the checkout process.</li>
<li>You must pay for any remaining balance on your order with another payment method.</li>
</ol>
</div>
<div class="text-center m20t line-t p10t">
<div class="tu text-gray size-12 text-left">BitPay invoice</div>
<div class="text-bold size-16 m5t text-left">{{buy.giftCard.bitpayInvoiceId}}</div>
<button class="m20t button outline round dark-gray tiny"
ng-click="$root.openExternalLink('https://bitpay.com/invoice/' + buy.giftCard.bitpayInvoiceId)">
<span class="text-gray">View invoice</span>
</button>
</div>
</div>
</div>
</div>
<div class="extra-margin-bottom"></div>

View File

@ -30,6 +30,13 @@
<span class="size-12" translate>Buy and Sell</span>
</div>
</li>
<li menu-toggle href ui-sref="amazon" ng-show="index.isComplete && index.amazonEnabled">
<i class="icon-arrow-right3 size-18 right m10t vm"></i>
<i class="fi-social-amazon size-24 icon vm"></i>
<div class="tu text-bold m5t">
<span class="size-12" translate>Gift Card</span>
</div>
</li>
<li ng-show="!index.noFocusedWallet" menu-toggle href ui-sref="preferencesGlobal">
<i class="icon-arrow-right3 size-18 right m10t vm"></i>
<i class="fi-widget size-24 icon vm"></i>

View File

@ -0,0 +1,45 @@
<nav class="tab-bar">
<section class="left-small">
<a ng-click="cancel()">
<i class="icon-arrow-left3 icon-back"></i>
<span class="text-back">Back</span>
</a>
</section>
<section class="middle tab-bar-section">
<h1 class="title ellipsis">
Details
</h1>
</section>
</nav>
<div class="modal-content fix-modals-touch"
ng-swipe-disable-mouse
ng-swipe-right="cancel()">
<div class="header-modal bg-gray text-center">
<img src="img/amazon-card.png" alt="Amazon" width="200">
<div class="size-24 text-bold m10t">
{{card.cardInfo.value.amount | currency : card.cardInfo.value.currencyCode + ' ' : 2}}
</div>
</div>
<ul class="no-bullet size-14">
<li class="line-b p10 oh">
<span class="text-gray">Claim code</span>
<span class="text-bold right enable_text_select">{{card.gcClaimCode}}</span>
</li>
<li class="line-b p10 oh">
<span class="text-gray">Status</span>
<span class="text-success right" ng-if="card.status == 'SUCCESS'">Completed</span>
<span class="text-info right" ng-if="card.status == 'PENDING'">Pending</span>
<span class="text-warning right" ng-if="card.status == 'ERROR'">Error</span>
</li>
<li class="line-b p10 oh">
<span class="text-gray">BitPay Invoice ID</span>
<span class="text-gray right enable_text_select">{{card.bitpayInvoiceId}}</span>
</li>
</ul>
<div class="extra-margin-bottom"></div>
</div>

View File

@ -0,0 +1,53 @@
'use strict';
angular.module('copayApp.controllers').controller('amazonController',
function($rootScope, $scope, $timeout, $modal, profileService, configService, storageService, amazonService, isChromeApp, animationService, lodash, nodeWebkit) {
window.ignoreMobilePause = true;
this.init = function() {
var self = this;
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
amazonService.setCredentials(network);
amazonService.getGiftCards(function(err, gcds) {
if (err) {
self.error = err;
return;
}
self.giftCards = gcds;
});
};
this.openCardModal = function(card) {
$rootScope.modalOpened = true;
var self = this;
var fc = profileService.focusedClient;
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.card = card;
$scope.cancel = lodash.debounce(function() {
$modalInstance.dismiss('cancel');
}, 0, 1000);
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/amazon-card-details.html',
windowClass: animationService.modalAnimated.slideRight,
controller: ModalInstanceCtrl,
});
var disableCloseModal = $rootScope.$on('closeModal', function() {
modalInstance.dismiss('cancel');
});
modalInstance.result.finally(function() {
$rootScope.modalOpened = false;
disableCloseModal();
var m = angular.element(document.getElementsByClassName('reveal-modal'));
m.addClass(animationService.modalAnimated.slideOutRight);
});
};
});

View File

@ -0,0 +1,239 @@
'use strict';
angular.module('copayApp.controllers').controller('buyAmazonController',
function($rootScope, $scope, $modal, $log, $timeout, lodash, profileService, animationService, bwsError, configService, walletService, fingerprintService, amazonService) {
window.ignoreMobilePause = true;
var self = this;
var fc;
var minimumAmount = 1;
var stepAmount = 1;
var multiplierAmount = 2;
var maximumAmount = 10;
var otherWallets = function(network) {
return lodash.filter(profileService.getWallets(network), function(w) {
return w.network == network && w.m == 1;
});
};
var handleEncryptedWallet = function(client, cb) {
if (!walletService.isEncrypted(client)) return cb();
$rootScope.$emit('Local/NeedsPassword', false, function(err, password) {
if (err) return cb(err);
return cb(walletService.unlock(client, password));
});
};
this.init = function() {
$scope.fiat = minimumAmount * multiplierAmount;
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
amazonService.setCredentials(network);
self.otherWallets = otherWallets(network);
// Choose focused wallet
try {
var currentWalletId = profileService.focusedClient.credentials.walletId;
lodash.find(self.otherWallets, function(w) {
if (w.id == currentWalletId) {
$timeout(function() {
self.selectedWalletId = w.id;
self.selectedWalletName = w.name;
fc = profileService.getClient(w.id);
$scope.$apply();
}, 100);
}
});
} catch (e) {
$log.debug(e);
};
};
$scope.openWalletsModal = function(wallets) {
self.error = null;
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.type = 'SELL';
$scope.wallets = wallets;
$scope.noColor = true;
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
$scope.selectWallet = function(walletId, walletName) {
if (!profileService.getClient(walletId).isComplete()) {
self.error = bwsError.msg('WALLET_NOT_COMPLETE');
$modalInstance.dismiss('cancel');
return;
}
$modalInstance.close({
'walletId': walletId,
'walletName': walletName,
});
};
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/wallets.html',
windowClass: animationService.modalAnimated.slideUp,
controller: ModalInstanceCtrl,
});
modalInstance.result.finally(function() {
var m = angular.element(document.getElementsByClassName('reveal-modal'));
m.addClass(animationService.modalAnimated.slideOutDown);
});
modalInstance.result.then(function(obj) {
$timeout(function() {
self.selectedWalletId = obj.walletId;
self.selectedWalletName = obj.walletName;
fc = profileService.getClient(obj.walletId);
$scope.$apply();
}, 100);
});
};
this.setAmount = function(plus) {
if (plus && $scope.fiat < maximumAmount ) {
stepAmount = stepAmount + 1;
$scope.fiat = stepAmount * multiplierAmount;
} else if (!plus && $scope.fiat > minimumAmount * multiplierAmount) {
stepAmount = stepAmount - 1;
$scope.fiat = stepAmount * multiplierAmount;
}
};
this.createTx = function() {
self.error = null;
var dataSrc = {
price: $scope.fiat,
currency: 'USD'
};
var outputs = [];
var config = configService.getSync();
var configWallet = config.wallet;
var walletSettings = configWallet.settings;
self.loading = 'Creating invoice...';
$timeout(function() {
amazonService.createBitPayInvoice(dataSrc, function(err, data) {
if (err) {
self.loading = null;
self.error = err;
return;
}
var address, comment, amount;
address = data.data.bitcoinAddress;
amount = parseInt((data.data.btcPrice * 100000000).toFixed(0));
comment = 'Buy Amazon Gift Card';
outputs.push({
'toAddress': address,
'amount': amount,
'message': comment
});
var txp = {
toAddress: address,
amount: amount,
outputs: outputs,
message: comment,
payProUrl: null,
excludeUnconfirmedUtxos: configWallet.spendUnconfirmed ? false : true,
feeLevel: walletSettings.feeLevel || 'normal'
};
self.loading = 'Creating transaction...';
walletService.createTx(fc, txp, function(err, createdTxp) {
self.loading = null;
if (err) {
self.loading = null;
$log.debug(err);
self.error = bwsError.msg(err);
$scope.$apply();
return;
}
$scope.$emit('Local/NeedsConfirmation', createdTxp, function(accept) {
if (accept) {
self.loading = 'Sending bitcoin...';
self.confirmTx(createdTxp, function(err, tx) {
if (err) {
self.loading = null;
self.error = err;
return;
}
var gift = {
amount: dataSrc.price,
currencyCode: dataSrc.currency,
bitpayInvoiceId: data.data.id
};
self.loading = 'Buying gift card...';
amazonService.buyGiftCard(gift, function(err, giftCard) {
self.loading = null;
if (err) {
self.error = err;
return;
}
self.giftCard = giftCard;
});
});
}
});
});
});
}, 100);
};
this.confirmTx = function(txp, cb) {
fingerprintService.check(fc, function(err) {
if (err) {
$log.debug(err);
return cb(err);
}
handleEncryptedWallet(fc, function(err) {
if (err) {
$log.debug(err);
return bwsError.cb(err, null, cb);
}
walletService.publishTx(fc, txp, function(err, publishedTxp) {
if (err) {
$log.debug(err);
return bwsError.cb(err, null, cb);
}
walletService.signTx(fc, publishedTxp, function(err, signedTxp) {
walletService.lock(fc);
if (err) {
$log.debug(err);
walletService.removeTx(fc, signedTxp, function(err) {
if (err) $log.debug(err);
});
return bwsError.cb(err, null, cb);
}
walletService.broadcastTx(fc, signedTxp, function(err, broadcastedTxp) {
if (err) {
$log.debug(err);
walletService.removeTx(fc, broadcastedTxp, function(err) {
if (err) $log.debug(err);
});
return bwsError.cb(err, null, cb);
}
$timeout(function() {
return cb(null, broadcastedTxp);
}, 5000);
});
});
});
});
});
};
});

View File

@ -160,6 +160,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.initGlidera();
self.initCoinbase();
self.initAmazon();
self.hideBalance();
@ -1040,6 +1041,10 @@ angular.module('copayApp.controllers').controller('indexController', function($r
});
};
self.initAmazon = function() {
self.amazonEnabled = configService.getSync().amazon.enabled;
};
self.initGlidera = function(accessToken) {
self.glideraEnabled = configService.getSync().glidera.enabled;
self.glideraTestnet = configService.getSync().glidera.testnet;

View File

@ -315,6 +315,26 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
},
}
})
.state('amazon', {
url: '/amazon',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/amazon.html'
},
}
})
.state('buyAmazon', {
url: '/buyamazon',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/buyAmazon.html'
},
}
})
.state('preferencesAdvanced', {
url: '/preferencesAdvanced',
templateUrl: 'views/preferencesAdvanced.html',

View File

@ -0,0 +1,123 @@
'use strict';
angular.module('copayApp.services').factory('amazonService', function($http, $log, isCordova, lodash, storageService, configService) {
var root = {};
var credentials = {};
var fakeData = {
"cardInfo": {
"cardNumber":null,
"cardStatus":"RefundedToPurchaser",
"expirationDate":null,
"value":{
"amount":1.0,
"currencyCode":"USD"
}
},
"creationRequestId":"AwssbTSpecTest001",
"gcClaimCode":"Z7NV-LBBG39-75MU",
"gcExpirationDate":null,
"gcId":"A2GCN9BRX5QS76",
"status":"SUCCESS",
"bitpayInvoiceId":"NJtevvEponHbQVmYoL7FYp"
};
root.setCredentials = function(network) {
if (network == 'testnet') {
credentials.BITPAY_API = 'https://test.bitpay.com';
credentials.BITPAY_API_TOKEN = 'GDtYwBqbMZvjz5JrYZ1d2ba96StV92U4Yg4AGhT3C4He';
credentials.AMAZON_HOST = 'https://agcod-v2-gamma.amazon.com';
}
else {
credentials.BITPAY_API = 'https://bitpay.com';
credentials.BITPAY_API_TOKEN = window.bitpay_token;
credentials.AMAZON_HOST = 'https://agcod-v2.amazon.com';
};
};
var _getBitPay = function(endpoint, token) {
return {
method: 'GET',
url: credentials.BITPAY_API + endpoint,
headers: {
'content-type': 'application/json'
}
};
};
var _postBitPay = function(endpoint, data) {
data.token = credentials.BITPAY_API_TOKEN;
return {
method: 'POST',
url: credentials.BITPAY_API + endpoint,
headers: {
'content-type': 'application/json'
},
data: data
};
};
root.createBitPayInvoice = function(data, cb) {
var data = {
price: data.price,
currency: data.currency
};
$http(_postBitPay('/invoices', data)).then(function(data) {
$log.info('BitPay Create Invoice: SUCCESS');
return cb(null, data.data);
}, function(data) {
$log.error('BitPay Create Invoice: ERROR ' + data.statusText);
return cb(data);
});
};
root.saveGiftCard = function(gc, opts, cb) {
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
storageService.getAmazonGiftCards(network, function(err, oldGiftCards) {
if (lodash.isString(oldGiftCards)) {
oldGiftCards = JSON.parse(oldGiftCards);
}
if (lodash.isString(gc)) {
gc = JSON.parse(gc);
}
var inv = oldGiftCards || {};
inv[gc.gcId] = gc;
if (opts && (opts.error || opts.status)) {
inv[gc.gcId] = lodash.assign(inv[gc.gcId], opts);
}
if (opts && opts.remove) {
delete(inv[gc.gcId]);
}
inv = JSON.stringify(inv);
storageService.setAmazonGiftCards(network, inv, function(err) {
return cb(err);
});
});
};
root.getGiftCards = function(cb) {
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
storageService.getAmazonGiftCards(network, function(err, giftCards) {
var _gcds = giftCards ? JSON.parse(giftCards) : null;
return cb(err, _gcds);
});
};
root.buyGiftCard = function(gift, cb) {
var newId = Math.floor(Date.now() / 1000);
var saveData = fakeData;
saveData.gcId = saveData.gcId + '' + newId;
saveData.cardInfo.value.amount = gift.amount;
saveData.cardInfo.value.currencyCode = gift.currencyCode;
saveData['bitpayInvoiceId'] = gift.bitpayInvoiceId;
saveData['date'] = newId;
root.saveGiftCard(saveData, null, function(err) {
return cb(null, fakeData);
});
};
return root;
});

View File

@ -43,6 +43,11 @@ angular.module('copayApp.services').factory('configService', function(storageSer
testnet: false
},
amazon: {
enabled: true,
testnet: false
},
rates: {
url: 'https://insight.bitpay.com:443/api/rates',
},
@ -101,6 +106,9 @@ angular.module('copayApp.services').factory('configService', function(storageSer
if (!configCache.coinbase) {
configCache.coinbase = defaultConfig.coinbase;
}
if (!configCache.amazon) {
configCache.amazon = defaultConfig.amazon;
}
if (!configCache.pushNotifications) {
configCache.pushNotifications = defaultConfig.pushNotifications;
}
@ -117,6 +125,10 @@ angular.module('copayApp.services').factory('configService', function(storageSer
// Disabled for testnet
configCache.coinbase.testnet = false;
// Amazon
// Disabled for testnet
configCache.amazon.testnet = true;
$log.debug('Preferences read:', configCache)
return cb(err, configCache);
});

View File

@ -317,6 +317,18 @@ angular.module('copayApp.services')
});
});
};
root.setAmazonGiftCards = function(network, gcs, cb) {
storage.set('amazonGiftCards-' + network, gcs, cb);
};
root.getAmazonGiftCards = function(network, cb) {
storage.get('amazonGiftCards-' + network, cb);
};
root.removeAmazonGiftCards = function(network, cb) {
storage.remove('amazonGiftCards-' + network, cb);
};
return root;
});

22
src/sass/amazon.scss Normal file
View File

@ -0,0 +1,22 @@
.amazon-select-amount {
display: inline-block;
padding: 3px 15px;
background-color: #4B6178;
color: #ffffff;
border-radius: 5px;
margin: 0 5px;
&:hover {
color: #ffffff;
}
}
.amazon-amount {
display: inline-block;
background-color: #E4E8EC;
padding: 3px 10px;
width: 60px;
text-align: center;
border: 1px solid #ccc;
border-radius: 5px;
}

View File

@ -99,7 +99,7 @@ h4.title a {
}
}
.modal-content h4, .glidera h4, .coinbase h4 {
.modal-content h4, .glidera h4, .coinbase h4, .amazon h4 {
background: #F6F7F9;
padding: 25px 0px 5px 10px;
text-transform: uppercase;