diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-11-30 04:07:36 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-12-01 03:00:09 +0100 |
commit | b8ccc7c990a1542cf80578b41972f9a5b0870af9 (patch) | |
tree | 6f16319f9ce3133c4c4617129a516e692cfc3ac1 /src/webex/notify.ts | |
parent | bc2c4aff8e657c7d5709433f137299491b98d257 (diff) | |
download | wallet-core-b8ccc7c990a1542cf80578b41972f9a5b0870af9.tar.xz |
partial implementation of tipping
Diffstat (limited to 'src/webex/notify.ts')
-rw-r--r-- | src/webex/notify.ts | 85 |
1 files changed, 84 insertions, 1 deletions
diff --git a/src/webex/notify.ts b/src/webex/notify.ts index cc8086ceb..ecc04e8a2 100644 --- a/src/webex/notify.ts +++ b/src/webex/notify.ts @@ -28,7 +28,9 @@ import URI = require("urijs"); import wxApi = require("./wxApi"); -import { QueryPaymentResult } from "../types"; +import { getTalerStampSec } from "../helpers"; +import { TipToken, QueryPaymentResult } from "../types"; + import axios from "axios"; @@ -260,6 +262,87 @@ function talerPay(msg: any): Promise<any> { // Use a promise directly instead of of an async // function since some paths never resolve the promise. return new Promise(async(resolve, reject) => { + if (msg.tip) { + const tipToken = TipToken.checked(JSON.parse(msg.tip)); + + console.log("got tip token", tipToken); + + const deadlineSec = getTalerStampSec(tipToken.expiration); + if (!deadlineSec) { + wxApi.logAndDisplayError({ + message: "invalid expiration", + name: "tipping-failed", + sameTab: true, + }); + return; + } + + const merchantDomain = new URI(document.location.href).origin(); + let walletResp; + try { + walletResp = await wxApi.getTipPlanchets(merchantDomain, tipToken.tip_id, tipToken.amount, deadlineSec, tipToken.exchange_url); + } catch (e) { + wxApi.logAndDisplayError({ + message: e.message, + name: "tipping-failed", + response: e.response, + sameTab: true, + }); + throw e; + } + + let planchets = walletResp; + + if (!planchets) { + wxApi.logAndDisplayError({ + message: "processing tip failed", + detail: walletResp, + name: "tipping-failed", + sameTab: true, + }); + return; + } + + let merchantResp; + + try { + const config = { + validateStatus: (s: number) => s === 200, + }; + const req = { planchets, tip_id: tipToken.tip_id }; + merchantResp = await axios.post(tipToken.pickup_url, req, config); + } catch (e) { + wxApi.logAndDisplayError({ + message: e.message, + name: "tipping-failed", + response: e.response, + sameTab: true, + }); + throw e; + } + + try { + wxApi.processTipResponse(merchantDomain, tipToken.tip_id, merchantResp.data); + } catch (e) { + wxApi.logAndDisplayError({ + message: e.message, + name: "tipping-failed", + response: e.response, + sameTab: true, + }); + throw e; + } + + // Go to tip dialog page, where the user can confirm the tip or + // decline if they are not happy with the exchange. + const uri = new URI(chrome.extension.getURL("/src/webex/pages/tip.html")); + const params = { tip_id: tipToken.tip_id, merchant_domain: merchantDomain }; + const redirectUrl = uri.query(params).href(); + window.location.href = redirectUrl; + + return; + } + if (msg.refund_url) { console.log("processing refund"); let resp; |