diff options
author | Florian Dold <florian.dold@gmail.com> | 2019-08-28 02:49:27 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2019-08-28 02:49:27 +0200 |
commit | 1390175a9afc53948dd1d6f8a2f88e51c1bf53cc (patch) | |
tree | 1e65581f11354ec61532dbbf3174e9bd26b515c4 /src/headless | |
parent | 70c0a557f9c89a2a0006f74bd8b361b62660bde2 (diff) |
rudimentary taler://withdraw support
Diffstat (limited to 'src/headless')
-rw-r--r-- | src/headless/bank.ts | 4 | ||||
-rw-r--r-- | src/headless/helpers.ts | 74 | ||||
-rw-r--r-- | src/headless/taler-wallet-cli.ts | 57 |
3 files changed, 84 insertions, 51 deletions
diff --git a/src/headless/bank.ts b/src/headless/bank.ts index 7d8db9fe5..f35021003 100644 --- a/src/headless/bank.ts +++ b/src/headless/bank.ts @@ -51,7 +51,7 @@ export class Bank { reservePub: string, exchangePaytoUri: string, ) { - const reqUrl = new URI("taler/withdraw") + const reqUrl = new URI("api/withdraw-headless") .absoluteTo(this.bankBaseUrl) .href(); @@ -80,7 +80,7 @@ export class Bank { } async registerRandomUser(): Promise<BankUser> { - const reqUrl = new URI("register").absoluteTo(this.bankBaseUrl).href(); + const reqUrl = new URI("api/register").absoluteTo(this.bankBaseUrl).href(); const randId = makeId(8); const bankUser: BankUser = { username: `testuser-${randId}`, diff --git a/src/headless/helpers.ts b/src/headless/helpers.ts index 9652c630f..a86b26738 100644 --- a/src/headless/helpers.ts +++ b/src/headless/helpers.ts @@ -54,17 +54,21 @@ class ConsoleBadge implements Badge { export class NodeHttpLib implements HttpRequestLibrary { async get(url: string): Promise<import("../http").HttpResponse> { enableTracing && console.log("making GET request to", url); - const resp = await Axios({ - method: "get", - url: url, - responseType: "json", - }); - enableTracing && console.log("got response", resp.data); - enableTracing && console.log("resp type", typeof resp.data); - return { - responseJson: resp.data, - status: resp.status, - }; + try { + const resp = await Axios({ + method: "get", + url: url, + responseType: "json", + }); + enableTracing && console.log("got response", resp.data); + enableTracing && console.log("resp type", typeof resp.data); + return { + responseJson: resp.data, + status: resp.status, + }; + } catch (e) { + throw e; + } } async postJson( @@ -72,37 +76,22 @@ export class NodeHttpLib implements HttpRequestLibrary { body: any, ): Promise<import("../http").HttpResponse> { enableTracing && console.log("making POST request to", url); - const resp = await Axios({ - method: "post", - url: url, - responseType: "json", - data: body, - }); - enableTracing && console.log("got response", resp.data); - enableTracing && console.log("resp type", typeof resp.data); - return { - responseJson: resp.data, - status: resp.status, - }; - } - - async postForm( - url: string, - form: any, - ): Promise<import("../http").HttpResponse> { - enableTracing && console.log("making POST request to", url); - const resp = await Axios({ - method: "post", - url: url, - data: querystring.stringify(form), - responseType: "json", - }); - enableTracing && console.log("got response", resp.data); - enableTracing && console.log("resp type", typeof resp.data); - return { - responseJson: resp.data, - status: resp.status, - }; + try { + const resp = await Axios({ + method: "post", + url: url, + responseType: "json", + data: body, + }); + enableTracing && console.log("got response", resp.data); + enableTracing && console.log("resp type", typeof resp.data); + return { + responseJson: resp.data, + status: resp.status, + }; + } catch (e) { + throw e; + } } } @@ -221,6 +210,7 @@ export async function withdrawTestBalance( const reserveResponse = await myWallet.createReserve({ amount: amounts.parseOrThrow(amount), exchange: exchangeBaseUrl, + exchangeWire: "payto://unknown", }); const bank = new Bank(bankBaseUrl); diff --git a/src/headless/taler-wallet-cli.ts b/src/headless/taler-wallet-cli.ts index 4a1f5d91e..65b2a0297 100644 --- a/src/headless/taler-wallet-cli.ts +++ b/src/headless/taler-wallet-cli.ts @@ -103,15 +103,14 @@ program console.log("created new order with order ID", orderResp.orderId); const checkPayResp = await merchantBackend.checkPayment(orderResp.orderId); const qrcode = qrcodeGenerator(0, "M"); - const contractUrl = checkPayResp.contract_url; - if (typeof contractUrl !== "string") { - console.error("fata: no contract url received from backend"); + const talerPayUri = checkPayResp.taler_pay_uri; + if (!talerPayUri) { + console.error("fatal: no taler pay URI received from backend"); process.exit(1); return; } - const url = "talerpay:" + querystring.escape(contractUrl); - console.log("contract url:", url); - qrcode.addData(url); + console.log("taler pay URI:", talerPayUri); + qrcode.addData(talerPayUri); qrcode.make(); console.log(qrcode.createASCII()); console.log("waiting for payment ..."); @@ -128,6 +127,45 @@ program }); program + .command("withdraw-url <withdraw-url>") + .action(async (withdrawUrl, cmdObj) => { + applyVerbose(program.verbose); + console.log("withdrawing", withdrawUrl); + const wallet = await getDefaultNodeWallet({ + persistentStoragePath: walletDbPath, + }); + + const withdrawInfo = await wallet.downloadWithdrawInfo(withdrawUrl); + + console.log("withdraw info", withdrawInfo); + + const selectedExchange = withdrawInfo.suggestedExchange; + if (!selectedExchange) { + console.error("no suggested exchange!"); + process.exit(1); + return; + } + + const { + reservePub, + confirmTransferUrl, + } = await wallet.createReserveFromWithdrawUrl( + withdrawUrl, + selectedExchange, + ); + + if (confirmTransferUrl) { + console.log("please confirm the transfer at", confirmTransferUrl); + } + + await wallet.processReserve(reservePub); + + console.log("finished withdrawing"); + + wallet.stop(); + }); + +program .command("pay-url <pay-url>") .option("-y, --yes", "automatically answer yes to prompts") .action(async (payUrl, cmdObj) => { @@ -153,6 +191,11 @@ program process.exit(0); return; } + if (result.status === "session-replayed") { + console.log("already paid! (replayed in different session)"); + process.exit(0); + return; + } if (result.status === "payment-possible") { console.log("paying ..."); } else { @@ -179,7 +222,7 @@ program if (pay) { const payRes = await wallet.confirmPay(result.proposalId!, undefined); - console.log("paid!"); + console.log("paid!"); } else { console.log("not paying"); } |