aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2015-12-24 03:20:18 +0100
committerFlorian Dold <florian.dold@gmail.com>2015-12-24 03:20:18 +0100
commit80f43b85c8e98031b26c927e58f04858f9683d2f (patch)
tree50e6af12e96b01a28b573cfcb307a2c57e967ddb
parent4101bc0f53df0c6ed32f6710d24a1b8b26777bb9 (diff)
downloadwallet-core-80f43b85c8e98031b26c927e58f04858f9683d2f.tar.xz
payment
-rw-r--r--extension/background/wallet.js56
-rw-r--r--extension/background/wallet.ts62
-rw-r--r--extension/content_scripts/notify.js27
-rw-r--r--extension/content_scripts/notify.ts26
-rw-r--r--extension/pages/confirm-contract.html3
5 files changed, 114 insertions, 60 deletions
diff --git a/extension/background/wallet.js b/extension/background/wallet.js
index c4216c4de..905febf09 100644
--- a/extension/background/wallet.js
+++ b/extension/background/wallet.js
@@ -83,7 +83,8 @@ function getPossibleMintCoins(db, paymentAmount, depositFeeLimit, allowedMints)
req_mints.onsuccess = (e) => {
let mint = req_mints.result;
if (!mint) {
- throw Error("no matching mint in index");
+ // We don't have that mint ...
+ return;
}
let req_coins = tx.objectStore("coins")
.index("mintBaseUrl")
@@ -99,7 +100,7 @@ function getPossibleMintCoins(db, paymentAmount, depositFeeLimit, allowedMints)
denom: mint.keys.denoms.find((e) => e.denom_pub === value.denomPub)
};
if (!cd.denom) {
- throw Error("denom not found");
+ throw Error("denom not found (database inconsistent)");
}
let x = m[mint.baseUrl];
if (!x) {
@@ -124,18 +125,25 @@ function getPossibleMintCoins(db, paymentAmount, depositFeeLimit, allowedMints)
let maxFee = new Amount(depositFeeLimit);
let minAmount = new Amount(paymentAmount);
let accFee = new Amount(coins[0].c.denom.fee_deposit);
- let accAmount = new Amount(coins[0].c.coin.currentAmount);
- for (let i = 0; i < coins.length; i++) {
+ let accAmount = Amount.getZero(coins[0].c.coin.currentAmount.currency);
+ nextCoin: for (let i = 0; i < coins.length; i++) {
+ let coinAmount = new Amount(coins[i].c.coin.currentAmount);
+ let coinFee = coins[i].a;
+ if (coinAmount.cmp(coinFee) <= 0) {
+ continue nextCoin;
+ }
+ accFee.add(coinFee);
+ accAmount.add(coinAmount);
if (accFee.cmp(maxFee) >= 0) {
+ console.log("too much fees");
continue nextMint;
}
if (accAmount.cmp(minAmount) >= 0) {
ret[key] = m[key];
continue nextMint;
}
- accFee.add(coins[i].a);
- accFee.add(new Amount(coins[i].c.coin.currentAmount));
}
+ console.log(format("mint {0}: acc {1} is not enough for {2}", key, JSON.stringify(accAmount.toJson()), JSON.stringify(minAmount.toJson())));
}
resolve(ret);
};
@@ -161,9 +169,13 @@ function executePay(db, offer, payCoinInfo, merchantBaseUrl, chosenMint) {
payUrl: payUrl.href(),
payReq: payReq
};
- let tx = db.transaction(['transactions'], 'readwrite');
+ let tx = db.transaction(["transactions", "coins"], "readwrite");
tx.objectStore('transactions').put(t);
+ for (let c of payCoinInfo) {
+ tx.objectStore("coins").put(c.updatedCoin);
+ }
tx.oncomplete = (e) => {
+ updateBadge(db);
resolve();
};
});
@@ -196,7 +208,9 @@ function confirmPay(db, detail, sendResponse) {
}
function doPayment(db, detail, sendResponse) {
let H_contract = detail.H_contract;
- let req = db.transaction(['transactions']).objectStore("transactions").get(H_contract);
+ let req = db.transaction(['transactions'])
+ .objectStore("transactions")
+ .get(H_contract);
console.log("executing contract", H_contract);
req.onsuccess = (e) => {
console.log("got db response for existing contract");
@@ -204,7 +218,11 @@ function doPayment(db, detail, sendResponse) {
sendResponse({ success: false, error: "contract not found" });
return;
}
- sendResponse({ success: true, payUrl: req.result.payUrl, payReq: req.result.payReq });
+ sendResponse({
+ success: true,
+ payUrl: req.result.payUrl,
+ payReq: req.result.payReq
+ });
};
return true;
}
@@ -386,11 +404,13 @@ function updateBadge(db) {
req.onsuccess = (e) => {
let cursor = req.result;
if (cursor) {
- n++;
+ let c = cursor.value;
+ if (c.currentAmount.fraction != 0 || c.currentAmount.value != 0) {
+ n++;
+ }
cursor.continue();
}
else {
- console.log("badge");
chrome.browserAction.setBadgeText({ text: "" + n });
chrome.browserAction.setBadgeBackgroundColor({ color: "#0F0" });
}
@@ -576,18 +596,16 @@ function balances(db, detail, sendResponse) {
req.onsuccess = (e) => {
let cursor = req.result;
if (cursor) {
- tx.objectStore('denoms').get(cursor.value.denomPub).onsuccess = (e2) => {
+ let c = cursor.value;
+ tx.objectStore('denoms').get(c.denomPub).onsuccess = (e2) => {
let d = e2.target.result;
let acc = byCurrency[d.value.currency];
if (!acc) {
- acc = new Amount(d.value);
- byCurrency[d.value.currency] = acc.toJson();
- }
- else {
- let am = new Amount(acc);
- am.add(new Amount(d.value));
- byCurrency[d.value.currency] = am.toJson();
+ acc = Amount.getZero(c.currentAmount.currency);
}
+ let am = new Amount(c.currentAmount);
+ am.add(new Amount(acc));
+ byCurrency[d.value.currency] = am.toJson();
console.log("counting", byCurrency[d.value.currency]);
};
cursor.continue();
diff --git a/extension/background/wallet.ts b/extension/background/wallet.ts
index e4c1d557c..d9b3080c1 100644
--- a/extension/background/wallet.ts
+++ b/extension/background/wallet.ts
@@ -161,7 +161,8 @@ function getPossibleMintCoins(db: IDBDatabase,
req_mints.onsuccess = (e) => {
let mint: Db.Mint = req_mints.result;
if (!mint) {
- throw Error("no matching mint in index");
+ // We don't have that mint ...
+ return;
}
let req_coins = tx.objectStore("coins")
.index("mintBaseUrl")
@@ -177,7 +178,7 @@ function getPossibleMintCoins(db: IDBDatabase,
denom: mint.keys.denoms.find((e) => e.denom_pub === value.denomPub)
};
if (!cd.denom) {
- throw Error("denom not found");
+ throw Error("denom not found (database inconsistent)");
}
let x = m[mint.baseUrl];
if (!x) {
@@ -204,18 +205,29 @@ function getPossibleMintCoins(db: IDBDatabase,
let maxFee = new Amount(depositFeeLimit);
let minAmount = new Amount(paymentAmount);
let accFee = new Amount(coins[0].c.denom.fee_deposit);
- let accAmount = new Amount(coins[0].c.coin.currentAmount);
+ let accAmount = Amount.getZero(coins[0].c.coin.currentAmount.currency);
+ nextCoin:
for (let i = 0; i < coins.length; i++) {
+ let coinAmount = new Amount(coins[i].c.coin.currentAmount);
+ let coinFee = coins[i].a;
+ if (coinAmount.cmp(coinFee) <= 0) {
+ continue nextCoin;
+ }
+ accFee.add(coinFee);
+ accAmount.add(coinAmount);
if (accFee.cmp(maxFee) >= 0) {
+ console.log("too much fees");
continue nextMint;
}
if (accAmount.cmp(minAmount) >= 0) {
ret[key] = m[key];
continue nextMint;
}
- accFee.add(coins[i].a);
- accFee.add(new Amount(coins[i].c.coin.currentAmount));
}
+ console.log(format("mint {0}: acc {1} is not enough for {2}",
+ key,
+ JSON.stringify(accAmount.toJson()),
+ JSON.stringify(minAmount.toJson())));
}
resolve(ret);
};
@@ -256,9 +268,13 @@ function executePay(db,
payUrl: payUrl.href(),
payReq: payReq
};
- let tx = db.transaction(['transactions'], 'readwrite');
+ let tx = db.transaction(["transactions", "coins"], "readwrite");
tx.objectStore('transactions').put(t);
+ for (let c of payCoinInfo) {
+ tx.objectStore("coins").put(c.updatedCoin);
+ }
tx.oncomplete = (e) => {
+ updateBadge(db);
resolve();
};
});
@@ -290,17 +306,18 @@ function confirmPay(db, detail: ConfirmPayRequest, sendResponse) {
})
.then(() => {
sendResponse({
- success: true,
- });
+ success: true,
+ });
});
return true;
}
-
function doPayment(db, detail, sendResponse) {
let H_contract = detail.H_contract;
- let req = db.transaction(['transactions']).objectStore("transactions").get(H_contract);
+ let req = db.transaction(['transactions'])
+ .objectStore("transactions")
+ .get(H_contract);
console.log("executing contract", H_contract);
req.onsuccess = (e) => {
console.log("got db response for existing contract");
@@ -308,7 +325,11 @@ function doPayment(db, detail, sendResponse) {
sendResponse({success: false, error: "contract not found"});
return;
}
- sendResponse({success: true, payUrl: req.result.payUrl, payReq: req.result.payReq});
+ sendResponse({
+ success: true,
+ payUrl: req.result.payUrl,
+ payReq: req.result.payReq
+ });
};
return true;
}
@@ -523,10 +544,12 @@ function updateBadge(db) {
req.onsuccess = (e) => {
let cursor = req.result;
if (cursor) {
- n++;
+ let c: Db.Coin = cursor.value;
+ if (c.currentAmount.fraction != 0 || c.currentAmount.value != 0) {
+ n++;
+ }
cursor.continue();
} else {
- console.log("badge");
chrome.browserAction.setBadgeText({text: "" + n});
chrome.browserAction.setBadgeBackgroundColor({color: "#0F0"});
}
@@ -729,17 +752,16 @@ function balances(db, detail, sendResponse) {
req.onsuccess = (e) => {
let cursor = req.result;
if (cursor) {
- tx.objectStore('denoms').get(cursor.value.denomPub).onsuccess = (e2) => {
+ let c: Db.Coin = cursor.value;
+ tx.objectStore('denoms').get(c.denomPub).onsuccess = (e2) => {
let d = e2.target.result;
let acc = byCurrency[d.value.currency];
if (!acc) {
- acc = new Amount(d.value);
- byCurrency[d.value.currency] = acc.toJson();
- } else {
- let am = new Amount(acc);
- am.add(new Amount(d.value));
- byCurrency[d.value.currency] = am.toJson();
+ acc = Amount.getZero(c.currentAmount.currency);
}
+ let am = new Amount(c.currentAmount);
+ am.add(new Amount(acc));
+ byCurrency[d.value.currency] = am.toJson();
console.log("counting", byCurrency[d.value.currency]);
};
cursor.continue();
diff --git a/extension/content_scripts/notify.js b/extension/content_scripts/notify.js
index 065aae193..676fd7653 100644
--- a/extension/content_scripts/notify.js
+++ b/extension/content_scripts/notify.js
@@ -55,25 +55,32 @@ document.addEventListener('taler-execute-payment', function (e) {
},
};
chrome.runtime.sendMessage(msg, (resp) => {
- console.log("got backend response to execute-payment:", JSON.stringify(resp));
if (!resp.success) {
console.log("failure!");
return;
}
+ console.log("Making request to ", resp.payUrl);
let r = new XMLHttpRequest();
r.open('post', resp.payUrl);
r.send(JSON.stringify(resp.payReq));
- let evt;
+ let detail = {};
r.onload = (e) => {
- if (r.status != 200) {
- console.log("non-200 error");
- console.log(r.responseText);
- alert("merchant returned HTTP status " + r.status);
+ switch (r.status) {
+ case 200:
+ detail.success = true;
+ break;
+ case 301:
+ detail.success = true;
+ console.log("Headers:", r.getAllResponseHeaders());
+ detail.fulfillmentUrl = r.getResponseHeader('Location');
+ break;
+ default:
+ detail.success = false;
+ break;
}
- else {
- evt = new CustomEvent("taler-payment-result", { detail: resp });
- }
- document.dispatchEvent(evt);
+ console.log("status was:", r.status);
+ console.log("detail:", JSON.stringify(detail));
+ document.dispatchEvent(new CustomEvent("taler-payment-result", { detail: detail }));
};
});
});
diff --git a/extension/content_scripts/notify.ts b/extension/content_scripts/notify.ts
index c45797db3..45e9145fb 100644
--- a/extension/content_scripts/notify.ts
+++ b/extension/content_scripts/notify.ts
@@ -63,24 +63,32 @@ document.addEventListener('taler-execute-payment', function(e: CustomEvent) {
},
};
chrome.runtime.sendMessage(msg, (resp) => {
- console.log("got backend response to execute-payment:", JSON.stringify(resp));
if (!resp.success) {
console.log("failure!");
return;
}
+ console.log("Making request to ", resp.payUrl);
let r = new XMLHttpRequest();
r.open('post', resp.payUrl);
r.send(JSON.stringify(resp.payReq));
- let evt;
+ let detail: any = {};
r.onload = (e) => {
- if (r.status != 200) {
- console.log("non-200 error");
- console.log(r.responseText);
- alert("merchant returned HTTP status " + r.status);
- } else {
- evt = new CustomEvent("taler-payment-result", {detail: resp});
+ switch (r.status) {
+ case 200:
+ detail.success = true;
+ break;
+ case 301:
+ detail.success = true;
+ console.log("Headers:", r.getAllResponseHeaders());
+ detail.fulfillmentUrl = r.getResponseHeader('Location');
+ break;
+ default:
+ detail.success = false;
+ break;
}
- document.dispatchEvent(evt);
+ console.log("status was:", r.status);
+ console.log("detail:", JSON.stringify(detail));
+ document.dispatchEvent(new CustomEvent("taler-payment-result", {detail: detail}));
};
});
}); \ No newline at end of file
diff --git a/extension/pages/confirm-contract.html b/extension/pages/confirm-contract.html
index 6b09ffeb1..242a489fd 100644
--- a/extension/pages/confirm-contract.html
+++ b/extension/pages/confirm-contract.html
@@ -7,7 +7,6 @@
<script src="../lib/handlebars-v4.0.5.js"></script>
<script src="../lib/commonHelpers.js"></script>
<script src="confirm-contract.js"></script>
- <link rel="stylesheet" type="text/css" href="../style/page.css">
<link rel="stylesheet" type="text/css" href="../style/wallet.css">
<script id="contract-template" type="text/x-handlebars-template">
@@ -45,8 +44,8 @@
<article id="contract">
<div id="render-contract"></div>
-
<button id="confirm-pay">Confirm Payment</button>
+ <div id="confirm-warning"></div>
</article>
<article id="status"></article>