refactor tipping, adjust to new redirect-based API

This commit is contained in:
Florian Dold 2018-01-19 01:27:27 +01:00
parent 2f68e9e50e
commit 1671d9a508
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
13 changed files with 192 additions and 391 deletions

View File

@ -574,6 +574,11 @@ export interface TipRecord {
*/ */
accepted: boolean; accepted: boolean;
/**
* Have we picked up the tip record from the merchant already?
*/
pickedUp: boolean;
/** /**
* The tipped amount. * The tipped amount.
*/ */

View File

@ -42,13 +42,13 @@ msgstr ""
msgid "Exchanges in the wallet:" msgid "Exchanges in the wallet:"
msgstr "" msgstr ""
#: src/webex/pages/confirm-contract.tsx:188 #: src/webex/pages/confirm-contract.tsx:200
#, c-format #, c-format
msgid "You have insufficient funds of the requested currency in your wallet." msgid "You have insufficient funds of the requested currency in your wallet."
msgstr "" msgstr ""
#. tslint:disable-next-line:max-line-length #. tslint:disable-next-line:max-line-length
#: src/webex/pages/confirm-contract.tsx:190 #: src/webex/pages/confirm-contract.tsx:202
#, c-format #, c-format
msgid "" msgid ""
"You do not have any funds from an exchange that is accepted by this " "You do not have any funds from an exchange that is accepted by this "
@ -56,12 +56,12 @@ msgid ""
"wallet." "wallet."
msgstr "" msgstr ""
#: src/webex/pages/confirm-contract.tsx:251 #: src/webex/pages/confirm-contract.tsx:280
#, c-format #, c-format
msgid "The merchant%1$s offers you to purchase:\n" msgid "The merchant%1$s offers you to purchase:\n"
msgstr "" msgstr ""
#: src/webex/pages/confirm-contract.tsx:272 #: src/webex/pages/confirm-contract.tsx:301
#, fuzzy, c-format #, fuzzy, c-format
msgid "Confirm payment" msgid "Confirm payment"
msgstr "Bezahlung bestätigen" msgstr "Bezahlung bestätigen"

View File

@ -42,13 +42,13 @@ msgstr ""
msgid "Exchanges in the wallet:" msgid "Exchanges in the wallet:"
msgstr "" msgstr ""
#: src/webex/pages/confirm-contract.tsx:188 #: src/webex/pages/confirm-contract.tsx:200
#, c-format #, c-format
msgid "You have insufficient funds of the requested currency in your wallet." msgid "You have insufficient funds of the requested currency in your wallet."
msgstr "" msgstr ""
#. tslint:disable-next-line:max-line-length #. tslint:disable-next-line:max-line-length
#: src/webex/pages/confirm-contract.tsx:190 #: src/webex/pages/confirm-contract.tsx:202
#, c-format #, c-format
msgid "" msgid ""
"You do not have any funds from an exchange that is accepted by this " "You do not have any funds from an exchange that is accepted by this "
@ -56,12 +56,12 @@ msgid ""
"wallet." "wallet."
msgstr "" msgstr ""
#: src/webex/pages/confirm-contract.tsx:251 #: src/webex/pages/confirm-contract.tsx:280
#, c-format #, c-format
msgid "The merchant%1$s offers you to purchase:\n" msgid "The merchant%1$s offers you to purchase:\n"
msgstr "" msgstr ""
#: src/webex/pages/confirm-contract.tsx:272 #: src/webex/pages/confirm-contract.tsx:301
#, c-format #, c-format
msgid "Confirm payment" msgid "Confirm payment"
msgstr "" msgstr ""

View File

@ -42,13 +42,13 @@ msgstr ""
msgid "Exchanges in the wallet:" msgid "Exchanges in the wallet:"
msgstr "" msgstr ""
#: src/webex/pages/confirm-contract.tsx:188 #: src/webex/pages/confirm-contract.tsx:200
#, c-format #, c-format
msgid "You have insufficient funds of the requested currency in your wallet." msgid "You have insufficient funds of the requested currency in your wallet."
msgstr "" msgstr ""
#. tslint:disable-next-line:max-line-length #. tslint:disable-next-line:max-line-length
#: src/webex/pages/confirm-contract.tsx:190 #: src/webex/pages/confirm-contract.tsx:202
#, c-format #, c-format
msgid "" msgid ""
"You do not have any funds from an exchange that is accepted by this " "You do not have any funds from an exchange that is accepted by this "
@ -56,12 +56,12 @@ msgid ""
"wallet." "wallet."
msgstr "" msgstr ""
#: src/webex/pages/confirm-contract.tsx:251 #: src/webex/pages/confirm-contract.tsx:280
#, c-format #, c-format
msgid "The merchant%1$s offers you to purchase:\n" msgid "The merchant%1$s offers you to purchase:\n"
msgstr "" msgstr ""
#: src/webex/pages/confirm-contract.tsx:272 #: src/webex/pages/confirm-contract.tsx:301
#, c-format #, c-format
msgid "Confirm payment" msgid "Confirm payment"
msgstr "" msgstr ""

View File

@ -42,13 +42,13 @@ msgstr ""
msgid "Exchanges in the wallet:" msgid "Exchanges in the wallet:"
msgstr "" msgstr ""
#: src/webex/pages/confirm-contract.tsx:188 #: src/webex/pages/confirm-contract.tsx:200
#, c-format #, c-format
msgid "You have insufficient funds of the requested currency in your wallet." msgid "You have insufficient funds of the requested currency in your wallet."
msgstr "" msgstr ""
#. tslint:disable-next-line:max-line-length #. tslint:disable-next-line:max-line-length
#: src/webex/pages/confirm-contract.tsx:190 #: src/webex/pages/confirm-contract.tsx:202
#, c-format #, c-format
msgid "" msgid ""
"You do not have any funds from an exchange that is accepted by this " "You do not have any funds from an exchange that is accepted by this "
@ -56,12 +56,12 @@ msgid ""
"wallet." "wallet."
msgstr "" msgstr ""
#: src/webex/pages/confirm-contract.tsx:251 #: src/webex/pages/confirm-contract.tsx:280
#, c-format #, c-format
msgid "The merchant%1$s offers you to purchase:\n" msgid "The merchant%1$s offers you to purchase:\n"
msgstr "" msgstr ""
#: src/webex/pages/confirm-contract.tsx:272 #: src/webex/pages/confirm-contract.tsx:301
#, c-format #, c-format
msgid "Confirm payment" msgid "Confirm payment"
msgstr "" msgstr ""

View File

@ -42,13 +42,13 @@ msgstr ""
msgid "Exchanges in the wallet:" msgid "Exchanges in the wallet:"
msgstr "" msgstr ""
#: src/webex/pages/confirm-contract.tsx:188 #: src/webex/pages/confirm-contract.tsx:200
#, c-format #, c-format
msgid "You have insufficient funds of the requested currency in your wallet." msgid "You have insufficient funds of the requested currency in your wallet."
msgstr "" msgstr ""
#. tslint:disable-next-line:max-line-length #. tslint:disable-next-line:max-line-length
#: src/webex/pages/confirm-contract.tsx:190 #: src/webex/pages/confirm-contract.tsx:202
#, c-format #, c-format
msgid "" msgid ""
"You do not have any funds from an exchange that is accepted by this " "You do not have any funds from an exchange that is accepted by this "
@ -56,12 +56,12 @@ msgid ""
"wallet." "wallet."
msgstr "" msgstr ""
#: src/webex/pages/confirm-contract.tsx:251 #: src/webex/pages/confirm-contract.tsx:280
#, c-format #, c-format
msgid "The merchant%1$s offers you to purchase:\n" msgid "The merchant%1$s offers you to purchase:\n"
msgstr "" msgstr ""
#: src/webex/pages/confirm-contract.tsx:272 #: src/webex/pages/confirm-contract.tsx:301
#, c-format #, c-format
msgid "Confirm payment" msgid "Confirm payment"
msgstr "" msgstr ""

View File

@ -99,7 +99,6 @@ import {
NextUrlResult, NextUrlResult,
Notifier, Notifier,
PayCoinInfo, PayCoinInfo,
QueryPaymentResult,
ReserveCreationInfo, ReserveCreationInfo,
ReturnCoinsRequest, ReturnCoinsRequest,
SenderWireInfos, SenderWireInfos,
@ -652,8 +651,8 @@ export class Wallet {
contractTerms: proposal.contractTerms, contractTerms: proposal.contractTerms,
contractTermsHash: proposal.contractTermsHash, contractTermsHash: proposal.contractTermsHash,
finished: false, finished: false,
lastSessionSig: undefined,
lastSessionId: undefined, lastSessionId: undefined,
lastSessionSig: undefined,
merchantSig: proposal.merchantSig, merchantSig: proposal.merchantSig,
payReq, payReq,
refundsDone: {}, refundsDone: {},
@ -717,7 +716,11 @@ export class Wallet {
return id; return id;
} }
private async submitPay(purchase: PurchaseRecord, sessionId: string | undefined): Promise<ConfirmPayResult> { async submitPay(contractTermsHash: string, sessionId: string | undefined): Promise<ConfirmPayResult> {
const purchase = await this.q().get(Stores.purchases, contractTermsHash);
if (!purchase) {
throw Error("Purchase not found: " + contractTermsHash);
}
let resp; let resp;
const payReq = { ...purchase.payReq, session_id: sessionId }; const payReq = { ...purchase.payReq, session_id: sessionId };
try { try {
@ -764,7 +767,7 @@ export class Wallet {
let purchase = await this.q().get(Stores.purchases, proposal.contractTermsHash); let purchase = await this.q().get(Stores.purchases, proposal.contractTermsHash);
if (purchase) { if (purchase) {
return this.submitPay(purchase, sessionId); return this.submitPay(purchase.contractTermsHash, sessionId);
} }
const res = await this.getCoinsForPayment({ const res = await this.getCoinsForPayment({
@ -796,7 +799,7 @@ export class Wallet {
purchase = await this.recordConfirmPay(sd.proposal, sd.payCoinInfo, sd.exchangeUrl); purchase = await this.recordConfirmPay(sd.proposal, sd.payCoinInfo, sd.exchangeUrl);
} }
return this.submitPay(purchase, sessionId); return this.submitPay(purchase.contractTermsHash, sessionId);
} }
@ -885,52 +888,17 @@ export class Wallet {
* Retrieve information required to pay for a contract, where the * Retrieve information required to pay for a contract, where the
* contract is identified via the fulfillment url. * contract is identified via the fulfillment url.
*/ */
async queryPaymentByFulfillmentUrl(url: string): Promise<QueryPaymentResult> { async queryPaymentByFulfillmentUrl(url: string): Promise<PurchaseRecord | undefined> {
console.log("query for payment", url); console.log("query for payment", url);
const t = await this.q().getIndexed(Stores.purchases.fulfillmentUrlIndex, url); const t = await this.q().getIndexed(Stores.purchases.fulfillmentUrlIndex, url);
if (!t) { if (!t) {
console.log("query for payment failed"); console.log("query for payment failed");
return { return undefined;
found: false,
};
} }
console.log("query for payment succeeded:", t); console.log("query for payment succeeded:", t);
return { return t;
contractTerms: t.contractTerms,
contractTermsHash: t.contractTermsHash,
found: true,
lastSessionId: t.lastSessionId,
lastSessionSig: t.lastSessionSig,
payReq: t.payReq,
};
}
/**
* Retrieve information required to pay for a contract, where the
* contract is identified via the contract terms hash.
*/
async queryPaymentByContractTermsHash(contractTermsHash: string): Promise<QueryPaymentResult> {
console.log("query for payment", contractTermsHash);
const t = await this.q().get(Stores.purchases, contractTermsHash);
if (!t) {
console.log("query for payment failed");
return {
found: false,
};
}
console.log("query for payment succeeded:", t);
return {
contractTerms: t.contractTerms,
contractTermsHash: t.contractTermsHash,
found: true,
lastSessionSig: t.lastSessionSig,
lastSessionId: t.lastSessionId,
payReq: t.payReq,
};
} }
@ -2723,46 +2691,11 @@ export class Wallet {
} }
/** /**
* Get planchets for a tip. Creates new planchets if they don't exist already * Workaround for merchant bug (#5258)
* for this tip. The tip is uniquely identified by the merchant's domain and the tip id.
*/ */
async getTipPlanchets(merchantDomain: string, private tipPickupWorkaround: { [tipId: string]: boolean } = {};
tipId: string,
amount: AmountJson,
deadline: number,
exchangeUrl: string,
nextUrl: string): Promise<TipPlanchetDetail[]> {
let tipRecord = await this.q().get(Stores.tips, [tipId, merchantDomain]);
if (!tipRecord) {
await this.updateExchangeFromUrl(exchangeUrl);
const denomsForWithdraw = await this.getVerifiedWithdrawDenomList(exchangeUrl, amount);
const planchets = await Promise.all(denomsForWithdraw.map(d => this.cryptoApi.createTipPlanchet(d)));
const coinPubs: string[] = planchets.map(x => x.coinPub);
const now = (new Date()).getTime();
tipRecord = {
accepted: false,
amount,
coinPubs,
deadline,
exchangeUrl,
merchantDomain,
nextUrl,
planchets,
timestamp: now,
tipId,
};
await this.q().put(Stores.tips, tipRecord).finish();
}
// Planchets in the form that the merchant expects
const planchetDetail: TipPlanchetDetail[] = tipRecord.planchets.map((p) => ({
coin_ev: p.coinEv,
denom_pub_hash: p.denomPubHash,
}));
return planchetDetail;
}
async processTip(tipToken: TipToken): Promise<TipRecord> {
async processTip(tipToken: TipToken): Promise<void> {
console.log("got tip token", tipToken); console.log("got tip token", tipToken);
const deadlineSec = getTalerStampSec(tipToken.expiration); const deadlineSec = getTalerStampSec(tipToken.expiration);
@ -2770,55 +2703,61 @@ export class Wallet {
throw Error("tipping failed (invalid expiration)"); throw Error("tipping failed (invalid expiration)");
} }
const merchantDomain = new URI(document.location.href).origin(); const merchantDomain = new URI(tipToken.pickup_url).origin();
let walletResp; let tipRecord = await this.q().get(Stores.tips, [tipToken.tip_id, merchantDomain]);
walletResp = await this.getTipPlanchets(merchantDomain,
tipToken.tip_id,
tipToken.amount,
deadlineSec,
tipToken.exchange_url,
tipToken.next_url);
const planchets = walletResp; if (tipRecord && tipRecord.pickedUp) {
return tipRecord;
if (!planchets) {
console.log("failed tip", walletResp);
throw Error("processing tip failed");
} }
await this.updateExchangeFromUrl(tipToken.exchange_url);
const denomsForWithdraw = await this.getVerifiedWithdrawDenomList(tipToken.exchange_url, tipToken.amount);
const planchets = await Promise.all(denomsForWithdraw.map(d => this.cryptoApi.createTipPlanchet(d)));
const coinPubs: string[] = planchets.map(x => x.coinPub);
const now = (new Date()).getTime();
tipRecord = {
accepted: false,
amount: tipToken.amount,
coinPubs,
deadline: deadlineSec,
exchangeUrl: tipToken.exchange_url,
merchantDomain,
nextUrl: tipToken.next_url,
pickedUp: false,
planchets,
timestamp: now,
tipId: tipToken.tip_id,
};
// Planchets in the form that the merchant expects
const planchetsDetail: TipPlanchetDetail[] = tipRecord.planchets.map((p) => ({
coin_ev: p.coinEv,
denom_pub_hash: p.denomPubHash,
}));
let merchantResp; let merchantResp;
await this.q().put(Stores.tips, tipRecord).finish();
if (this.tipPickupWorkaround[tipRecord.tipId]) {
// Be careful to not accidentally download twice (#5258)
return tipRecord;
}
try { try {
const config = { const config = {
validateStatus: (s: number) => s === 200, validateStatus: (s: number) => s === 200,
}; };
const req = { planchets, tip_id: tipToken.tip_id }; const req = { planchets: planchetsDetail, tip_id: tipToken.tip_id };
merchantResp = await axios.post(tipToken.pickup_url, req, config); merchantResp = await axios.post(tipToken.pickup_url, req, config);
} catch (e) { } catch (e) {
console.log("tipping failed", e); console.log("tipping failed", e);
throw e; throw e;
} }
try { this.tipPickupWorkaround[tipToken.tip_id] = true;
this.processTipResponse(merchantDomain, tipToken.tip_id, merchantResp.data);
} catch (e) {
console.log("processTipResponse failed", e);
throw e;
}
return; const response = TipResponse.checked(merchantResp.data);
}
/**
* Accept a merchant's response to a tip pickup and start withdrawing the coins.
* These coins will not appear in the wallet yet.
*/
async processTipResponse(merchantDomain: string, tipId: string, response: TipResponse): Promise<void> {
const tipRecord = await this.q().get(Stores.tips, [tipId, merchantDomain]);
if (!tipRecord) {
throw Error("tip not found");
}
console.log("processing tip response", response);
if (response.reserve_sigs.length !== tipRecord.planchets.length) { if (response.reserve_sigs.length !== tipRecord.planchets.length) {
throw Error("number of tip responses does not match requested planchets"); throw Error("number of tip responses does not match requested planchets");
} }
@ -2840,12 +2779,21 @@ export class Wallet {
await this.q().put(Stores.precoins, preCoin); await this.q().put(Stores.precoins, preCoin);
this.processPreCoin(preCoin); this.processPreCoin(preCoin);
} }
tipRecord.pickedUp = true;
await this.q().put(Stores.tips, tipRecord).finish();
return tipRecord;
} }
/** /**
* Start using the coins from a tip. * Start using the coins from a tip.
*/ */
async acceptTip(merchantDomain: string, tipId: string): Promise<void> { async acceptTip(tipToken: TipToken): Promise<void> {
const tipId = tipToken.tip_id;
const merchantDomain = new URI(tipToken.pickup_url).origin();
const tipRecord = await this.q().get(Stores.tips, [tipId, merchantDomain]); const tipRecord = await this.q().get(Stores.tips, [tipId, merchantDomain]);
if (!tipRecord) { if (!tipRecord) {
throw Error("tip not found"); throw Error("tip not found");
@ -2875,11 +2823,9 @@ export class Wallet {
this.notifier.notify(); this.notifier.notify();
} }
async getTipStatus(merchantDomain: string, tipId: string): Promise<TipStatus> {
const tipRecord = await this.q().get(Stores.tips, [tipId, merchantDomain]); async getTipStatus(tipToken: TipToken): Promise<TipStatus> {
if (!tipRecord) { const tipRecord = await this.processTip(tipToken);
throw Error("tip not found");
}
const rci = await this.getReserveCreationInfo(tipRecord.exchangeUrl, tipRecord.amount); const rci = await this.getReserveCreationInfo(tipRecord.exchangeUrl, tipRecord.amount);
const tipStatus: TipStatus = { const tipStatus: TipStatus = {
rci, rci,

View File

@ -41,7 +41,6 @@ import {
CoinPaySig, CoinPaySig,
ContractTerms, ContractTerms,
PayReq, PayReq,
TipResponse,
} from "./talerTypes"; } from "./talerTypes";
@ -280,12 +279,6 @@ export interface HistoryRecord {
} }
/**
* Response to a query payment request. Tagged union over the 'found' field.
*/
export type QueryPaymentResult = QueryPaymentNotFound | QueryPaymentFound;
/** /**
* Query payment response when the payment was found. * Query payment response when the payment was found.
*/ */
@ -304,6 +297,7 @@ export interface QueryPaymentFound {
lastSessionSig?: string; lastSessionSig?: string;
lastSessionId?: string; lastSessionId?: string;
payReq: PayReq; payReq: PayReq;
proposalId: number;
} }
@ -438,7 +432,6 @@ export interface CoinWithDenom {
denom: DenominationRecord; denom: DenominationRecord;
} }
/** /**
* Status of processing a tip. * Status of processing a tip.
*/ */
@ -448,138 +441,6 @@ export interface TipStatus {
} }
/**
* Request to the wallet for the status of processing a tip.
*/
@Checkable.Class()
export class TipStatusRequest {
/**
* Identifier of the tip.
*/
@Checkable.String
tipId: string;
/**
* Merchant domain. Within each merchant domain, the tip identifier
* uniquely identifies a tip.
*/
@Checkable.String
merchantDomain: string;
/**
* Create a TipStatusRequest from untyped JSON.
*/
static checked: (obj: any) => TipStatusRequest;
}
/**
* Request to the wallet to accept a tip.
*/
@Checkable.Class()
export class AcceptTipRequest {
/**
* Identifier of the tip.
*/
@Checkable.String
tipId: string;
/**
* Merchant domain. Within each merchant domain, the tip identifier
* uniquely identifies a tip.
*/
@Checkable.String
merchantDomain: string;
/**
* Create an AcceptTipRequest from untyped JSON.
* Validates the schema and throws on error.
*/
static checked: (obj: any) => AcceptTipRequest;
}
/**
* Request for the wallet to process a tip response from a merchant.
*/
@Checkable.Class()
export class ProcessTipResponseRequest {
/**
* Identifier of the tip.
*/
@Checkable.String
tipId: string;
/**
* Merchant domain. Within each merchant domain, the tip identifier
* uniquely identifies a tip.
*/
@Checkable.String
merchantDomain: string;
/**
* Tip response from the merchant.
*/
@Checkable.Value(() => TipResponse)
tipResponse: TipResponse;
/**
* Create an AcceptTipRequest from untyped JSON.
* Validates the schema and throws on error.
*/
static checked: (obj: any) => ProcessTipResponseRequest;
}
/**
* Request for the wallet to generate tip planchets.
*/
@Checkable.Class()
export class GetTipPlanchetsRequest {
/**
* Identifier of the tip.
*/
@Checkable.String
tipId: string;
/**
* Merchant domain. Within each merchant domain, the tip identifier
* uniquely identifies a tip.
*/
@Checkable.String
merchantDomain: string;
/**
* Amount of the tip.
*/
@Checkable.Optional(Checkable.Value(() => AmountJson))
amount: AmountJson;
/**
* Deadline for picking up the tip.
*/
@Checkable.Number
deadline: number;
/**
* Exchange URL that must be used to pick up the tip.
*/
@Checkable.String
exchangeUrl: string;
/**
* URL to nagivate to after processing the tip.
*/
@Checkable.String
nextUrl: string;
/**
* Create an AcceptTipRequest from untyped JSON.
* Validates the schema and throws on error.
*/
static checked: (obj: any) => GetTipPlanchetsRequest;
}
/** /**
* Badge that shows activity for the wallet. * Badge that shows activity for the wallet.
*/ */

View File

@ -171,20 +171,12 @@ export interface MessageMap {
request: { refundPermissions: talerTypes.RefundPermission[] }; request: { refundPermissions: talerTypes.RefundPermission[] };
response: void; response: void;
}; };
"get-tip-planchets": {
request: walletTypes.GetTipPlanchetsRequest;
response: void;
};
"process-tip-response": {
request: walletTypes.ProcessTipResponseRequest;
response: void;
};
"accept-tip": { "accept-tip": {
request: walletTypes.AcceptTipRequest; request: { tipToken: talerTypes.TipToken };
response: void; response: void;
}; };
"get-tip-status": { "get-tip-status": {
request: walletTypes.TipStatusRequest; request: { tipToken: talerTypes.TipToken };
response: void; response: void;
}; };
"clear-notification": { "clear-notification": {
@ -199,6 +191,10 @@ export interface MessageMap {
request: any; request: any;
response: void; response: void;
}; };
"submit-pay": {
request: { contractTermsHash: string, sessionId: string | undefined };
response: void;
};
} }
/** /**

View File

@ -122,6 +122,7 @@ interface ContractPromptState {
*/ */
holdCheck: boolean; holdCheck: boolean;
payStatus?: CheckPayResult; payStatus?: CheckPayResult;
replaying: boolean;
} }
class ContractPrompt extends React.Component<ContractPromptProps, ContractPromptState> { class ContractPrompt extends React.Component<ContractPromptProps, ContractPromptState> {
@ -135,6 +136,7 @@ class ContractPrompt extends React.Component<ContractPromptProps, ContractPrompt
payDisabled: true, payDisabled: true,
proposal: null, proposal: null,
proposalId: props.proposalId, proposalId: props.proposalId,
replaying: false,
}; };
} }
@ -150,13 +152,23 @@ class ContractPrompt extends React.Component<ContractPromptProps, ContractPrompt
if (this.props.resourceUrl) { if (this.props.resourceUrl) {
const p = await wxApi.queryPaymentByFulfillmentUrl(this.props.resourceUrl); const p = await wxApi.queryPaymentByFulfillmentUrl(this.props.resourceUrl);
console.log("query for resource url", this.props.resourceUrl, "result", p); console.log("query for resource url", this.props.resourceUrl, "result", p);
if (p.found && (p.lastSessionSig === undefined || p.lastSessionSig === this.props.sessionId)) { if (p) {
if (p.lastSessionSig === undefined || p.lastSessionSig === this.props.sessionId) {
const nextUrl = new URI(p.contractTerms.fulfillment_url); const nextUrl = new URI(p.contractTerms.fulfillment_url);
nextUrl.addSearch("order_id", p.contractTerms.order_id); nextUrl.addSearch("order_id", p.contractTerms.order_id);
if (p.lastSessionSig) { if (p.lastSessionSig) {
nextUrl.addSearch("session_sig", p.lastSessionSig); nextUrl.addSearch("session_sig", p.lastSessionSig);
} }
location.href = nextUrl.href(); location.replace(nextUrl.href());
return;
} else {
// We're in a new session
this.setState({ replaying: true });
const payResult = await wxApi.submitPay(p.contractTermsHash, this.props.sessionId);
console.log("payResult", payResult);
location.replace(payResult.nextUrl);
return;
}
} }
} }
let proposalId = this.props.proposalId; let proposalId = this.props.proposalId;
@ -230,6 +242,9 @@ class ContractPrompt extends React.Component<ContractPromptProps, ContractPrompt
if (this.props.contractUrl === undefined && this.props.proposalId === undefined) { if (this.props.contractUrl === undefined && this.props.proposalId === undefined) {
return <span>Error: either contractUrl or proposalId must be given</span>; return <span>Error: either contractUrl or proposalId must be given</span>;
} }
if (this.state.replaying) {
return <span>Re-submitting existing payment</span>;
}
if (this.state.proposalId === undefined) { if (this.state.proposalId === undefined) {
return <span>Downloading contract terms</span>; return <span>Downloading contract terms</span>;
} }
@ -245,18 +260,32 @@ class ContractPrompt extends React.Component<ContractPromptProps, ContractPrompt
} }
const amount = <strong>{renderAmount(c.amount)}</strong>; const amount = <strong>{renderAmount(c.amount)}</strong>;
console.log("payStatus", this.state.payStatus); console.log("payStatus", this.state.payStatus);
return (
<div> let products = null;
<div> if (c.products.length) {
<i18n.Translate wrap="p"> products = (
The merchant <span>{merchantName}</span> {" "} <>
offers you to purchase: <span>The following items are included:</span>
</i18n.Translate>
<ul> <ul>
{c.products.map( {c.products.map(
(p: any, i: number) => (<li key={i}>{p.description}: {renderAmount(p.price)}</li>)) (p: any, i: number) => (<li key={i}>{p.description}: {renderAmount(p.price)}</li>))
} }
</ul> </ul>
</>
);
}
return (
<>
<div>
<i18n.Translate wrap="p">
The merchant <span>{merchantName}</span> {" "}
offers you to purchase:
</i18n.Translate>
<div style={{"text-align": "center"}}>
<strong>{c.summary}</strong>
</div>
<strong></strong>
{products}
{(this.state.payStatus && this.state.payStatus.coinSelection) {(this.state.payStatus && this.state.payStatus.coinSelection)
? <p> ? <p>
The total price is <span>{amount}</span>{" "} The total price is <span>{amount}</span>{" "}
@ -280,7 +309,7 @@ class ContractPrompt extends React.Component<ContractPromptProps, ContractPrompt
{(this.state.error ? <p className="errorbox">{this.state.error}</p> : <p />)} {(this.state.error ? <p className="errorbox">{this.state.error}</p> : <p />)}
</div> </div>
<Details exchanges={this.state.exchanges} contractTerms={c} collapsed={!this.state.error}/> <Details exchanges={this.state.exchanges} contractTerms={c} collapsed={!this.state.error}/>
</div> </>
); );
} }
} }
@ -296,10 +325,8 @@ document.addEventListener("DOMContentLoaded", () => {
} catch { } catch {
// ignore error // ignore error
} }
const sessionId = query.sessionId; const sessionId = query.sessionId;
const contractUrl = query.contractUrl; const contractUrl = query.contractUrl;
const resourceUrl = query.resourceUrl; const resourceUrl = query.resourceUrl;
ReactDOM.render( ReactDOM.render(

View File

@ -39,11 +39,11 @@ import {
} from "../renderHtml"; } from "../renderHtml";
import * as Amounts from "../../amounts"; import * as Amounts from "../../amounts";
import { TipToken } from "../../talerTypes";
import { TipStatus } from "../../walletTypes"; import { TipStatus } from "../../walletTypes";
interface TipDisplayProps { interface TipDisplayProps {
merchantDomain: string; tipToken: TipToken;
tipId: string;
} }
interface TipDisplayState { interface TipDisplayState {
@ -58,7 +58,7 @@ class TipDisplay extends React.Component<TipDisplayProps, TipDisplayState> {
} }
async update() { async update() {
const tipStatus = await getTipStatus(this.props.merchantDomain, this.props.tipId); const tipStatus = await getTipStatus(this.props.tipToken);
this.setState({ tipStatus }); this.setState({ tipStatus });
} }
@ -96,7 +96,7 @@ class TipDisplay extends React.Component<TipDisplayProps, TipDisplayState> {
accept() { accept() {
this.setState({ working: true}); this.setState({ working: true});
acceptTip(this.props.merchantDomain, this.props.tipId); acceptTip(this.props.tipToken);
} }
renderButtons() { renderButtons() {
@ -126,7 +126,7 @@ class TipDisplay extends React.Component<TipDisplayProps, TipDisplayState> {
<div> <div>
<h2>Tip Received!</h2> <h2>Tip Received!</h2>
<p>You received a tip of <strong>{renderAmount(ts.tip.amount)}</strong> from <span> </span> <p>You received a tip of <strong>{renderAmount(ts.tip.amount)}</strong> from <span> </span>
<strong>{this.props.merchantDomain}</strong>.</p> <strong>{ts.tip.merchantDomain}</strong>.</p>
{ts.tip.accepted {ts.tip.accepted
? <p>You've accepted this tip! <a href={ts.tip.nextUrl}>Go back to merchant</a></p> ? <p>You've accepted this tip! <a href={ts.tip.nextUrl}>Go back to merchant</a></p>
: this.renderButtons() : this.renderButtons()
@ -142,11 +142,9 @@ async function main() {
const url = new URI(document.location.href); const url = new URI(document.location.href);
const query: any = URI.parseQuery(url.query()); const query: any = URI.parseQuery(url.query());
const merchantDomain = query.merchant_domain; const tipToken = TipToken.checked(JSON.parse(query.tip_token));
const tipId = query.tip_id;
const props: TipDisplayProps = { tipId, merchantDomain };
ReactDOM.render(<TipDisplay {...props} />, ReactDOM.render(<TipDisplay tipToken={tipToken} />,
document.getElementById("container")!); document.getElementById("container")!);
} catch (e) { } catch (e) {

View File

@ -35,7 +35,6 @@ import {
import { import {
CheckPayResult, CheckPayResult,
ConfirmPayResult, ConfirmPayResult,
QueryPaymentResult,
ReserveCreationInfo, ReserveCreationInfo,
SenderWireInfos, SenderWireInfos,
TipStatus, TipStatus,
@ -44,8 +43,7 @@ import {
import { import {
RefundPermission, RefundPermission,
TipPlanchetDetail, TipToken,
TipResponse,
} from "../talerTypes"; } from "../talerTypes";
import { MessageMap, MessageType } from "./messages"; import { MessageMap, MessageType } from "./messages";
@ -221,6 +219,13 @@ export function confirmPay(proposalId: number, sessionId: string | undefined): P
return callBackend("confirm-pay", { proposalId, sessionId }); return callBackend("confirm-pay", { proposalId, sessionId });
} }
/**
* Replay paying for a purchase.
*/
export function submitPay(contractTermsHash: string, sessionId: string | undefined): Promise<ConfirmPayResult> {
return callBackend("submit-pay", { contractTermsHash, sessionId });
}
/** /**
* Hash a contract. Throws if its not a valid contract. * Hash a contract. Throws if its not a valid contract.
*/ */
@ -238,7 +243,7 @@ export function confirmReserve(reservePub: string): Promise<void> {
/** /**
* Query for a payment by fulfillment URL. * Query for a payment by fulfillment URL.
*/ */
export function queryPaymentByFulfillmentUrl(url: string): Promise<QueryPaymentResult> { export function queryPaymentByFulfillmentUrl(url: string): Promise<PurchaseRecord> {
return callBackend("query-payment", { url }); return callBackend("query-payment", { url });
} }
@ -323,38 +328,20 @@ export function getFullRefundFees(args: { refundPermissions: RefundPermission[]
} }
/**
* Get or generate planchets to give the merchant that wants to tip us.
*/
export function getTipPlanchets(merchantDomain: string,
tipId: string,
amount: AmountJson,
deadline: number,
exchangeUrl: string,
nextUrl: string): Promise<TipPlanchetDetail[]> {
return callBackend("get-tip-planchets", { merchantDomain, tipId, amount, deadline, exchangeUrl, nextUrl });
}
/** /**
* Get the status of processing a tip. * Get the status of processing a tip.
*/ */
export function getTipStatus(merchantDomain: string, tipId: string): Promise<TipStatus> { export function getTipStatus(tipToken: TipToken): Promise<TipStatus> {
return callBackend("get-tip-status", { merchantDomain, tipId }); return callBackend("get-tip-status", { tipToken });
} }
/** /**
* Mark a tip as accepted by the user. * Mark a tip as accepted by the user.
*/ */
export function acceptTip(merchantDomain: string, tipId: string): Promise<TipStatus> { export function acceptTip(tipToken: TipToken): Promise<TipStatus> {
return callBackend("accept-tip", { merchantDomain, tipId }); return callBackend("accept-tip", { tipToken });
} }
/**
* Process a response from the merchant for a tip request.
*/
export function processTipResponse(merchantDomain: string, tipId: string, tipResponse: TipResponse): Promise<void> {
return callBackend("process-tip-response", { merchantDomain, tipId, tipResponse });
}
/** /**
* Clear notifications that the wallet shows to the user. * Clear notifications that the wallet shows to the user.

View File

@ -34,15 +34,10 @@ import {
import { AmountJson } from "../amounts"; import { AmountJson } from "../amounts";
import { import {
AcceptTipRequest,
ConfirmReserveRequest, ConfirmReserveRequest,
CreateReserveRequest, CreateReserveRequest,
GetTipPlanchetsRequest,
Notifier, Notifier,
ProcessTipResponseRequest,
QueryPaymentFound,
ReturnCoinsRequest, ReturnCoinsRequest,
TipStatusRequest,
} from "../walletTypes"; } from "../walletTypes";
import { import {
@ -50,6 +45,7 @@ import {
} from "../wallet"; } from "../wallet";
import { import {
PurchaseRecord,
Stores, Stores,
WALLET_DB_VERSION, WALLET_DB_VERSION,
} from "../dbTypes"; } from "../dbTypes";
@ -136,6 +132,12 @@ function handleMessage(sender: MessageSender,
} }
return needsWallet().confirmPay(detail.proposalId, detail.sessionId); return needsWallet().confirmPay(detail.proposalId, detail.sessionId);
} }
case "submit-pay": {
if (typeof detail.contractTermsHash !== "string") {
throw Error("contractTermsHash must be a string");
}
return needsWallet().submitPay(detail.contractTermsHash, detail.sessionId);
}
case "check-pay": { case "check-pay": {
if (typeof detail.proposalId !== "number") { if (typeof detail.proposalId !== "number") {
throw Error("proposalId must be number"); throw Error("proposalId must be number");
@ -291,25 +293,12 @@ function handleMessage(sender: MessageSender,
case "get-full-refund-fees": case "get-full-refund-fees":
return needsWallet().getFullRefundFees(detail.refundPermissions); return needsWallet().getFullRefundFees(detail.refundPermissions);
case "get-tip-status": { case "get-tip-status": {
const req = TipStatusRequest.checked(detail); const tipToken = TipToken.checked(detail.tipToken);
return needsWallet().getTipStatus(req.merchantDomain, req.tipId); return needsWallet().getTipStatus(tipToken);
} }
case "accept-tip": { case "accept-tip": {
const req = AcceptTipRequest.checked(detail); const tipToken = TipToken.checked(detail.tipToken);
return needsWallet().acceptTip(req.merchantDomain, req.tipId); return needsWallet().acceptTip(tipToken);
}
case "process-tip-response": {
const req = ProcessTipResponseRequest.checked(detail);
return needsWallet().processTipResponse(req.merchantDomain, req.tipId, req.tipResponse);
}
case "get-tip-planchets": {
const req = GetTipPlanchetsRequest.checked(detail);
return needsWallet().getTipPlanchets(req.merchantDomain,
req.tipId,
req.amount,
req.deadline,
req.exchangeUrl,
req.nextUrl);
} }
case "clear-notification": { case "clear-notification": {
return needsWallet().clearNotification(); return needsWallet().clearNotification();
@ -410,7 +399,7 @@ async function talerPay(fields: any, url: string, tabId: number): Promise<string
const w = currentWallet; const w = currentWallet;
const goToPayment = (p: QueryPaymentFound): string => { const goToPayment = (p: PurchaseRecord): string => {
const nextUrl = new URI(p.contractTerms.fulfillment_url); const nextUrl = new URI(p.contractTerms.fulfillment_url);
nextUrl.addSearch("order_id", p.contractTerms.order_id); nextUrl.addSearch("order_id", p.contractTerms.order_id);
if (p.lastSessionSig) { if (p.lastSessionSig) {
@ -422,14 +411,7 @@ async function talerPay(fields: any, url: string, tabId: number): Promise<string
if (fields.resource_url) { if (fields.resource_url) {
const p = await w.queryPaymentByFulfillmentUrl(fields.resource_url); const p = await w.queryPaymentByFulfillmentUrl(fields.resource_url);
console.log("query for resource url", fields.resource_url, "result", p); console.log("query for resource url", fields.resource_url, "result", p);
if (p.found && (fields.session_id === undefined || fields.session_id === p.lastSessionId)) { if (p && (fields.session_id === undefined || fields.session_id === p.lastSessionId)) {
return goToPayment(p);
}
}
if (fields.contract_hash) {
const p = await w.queryPaymentByContractTermsHash(fields.contract_hash);
if (p.found) {
goToPayment(p);
return goToPayment(p); return goToPayment(p);
} }
} }
@ -452,15 +434,8 @@ async function talerPay(fields: any, url: string, tabId: number): Promise<string
return chrome.extension.getURL(`/src/webex/pages/refund.html?contractTermsHash=${hc}`); return chrome.extension.getURL(`/src/webex/pages/refund.html?contractTermsHash=${hc}`);
} }
if (fields.tip) { if (fields.tip) {
const tipToken = TipToken.checked(fields.tip);
w.processTip(tipToken);
// Go to tip dialog page, where the user can confirm the tip or
// decline if they are not happy with the exchange.
const merchantDomain = new URI(url).origin();
const uri = new URI(chrome.extension.getURL("/src/webex/pages/tip.html")); const uri = new URI(chrome.extension.getURL("/src/webex/pages/tip.html"));
const params = { tip_id: tipToken.tip_id, merchant_domain: merchantDomain }; return uri.query({ tip_token: fields.tip }).href();
const redirectUrl = uri.query(params).href();
return redirectUrl;
} }
return undefined; return undefined;
} }
@ -486,7 +461,6 @@ function handleHttpPayment(headerList: chrome.webRequest.HttpHeader[], url: stri
} }
const fields = { const fields = {
contract_hash: headers["x-taler-contract-hash"],
contract_url: headers["x-taler-contract-url"], contract_url: headers["x-taler-contract-url"],
offer_url: headers["x-taler-offer-url"], offer_url: headers["x-taler-offer-url"],
refund_url: headers["x-taler-refund-url"], refund_url: headers["x-taler-refund-url"],
@ -506,15 +480,15 @@ function handleHttpPayment(headerList: chrome.webRequest.HttpHeader[], url: stri
console.log("got pay detail", fields); console.log("got pay detail", fields);
// Fast path for existing payment // Synchronous fast path for existing payment
if (fields.resource_url) { if (fields.resource_url) {
const result = currentWallet.getNextUrlFromResourceUrl(fields.resource_url); const result = currentWallet.getNextUrlFromResourceUrl(fields.resource_url);
if (result && (fields.session_id === undefined || fields.session_id === result.lastSessionId)) { if (result && (fields.session_id === undefined || fields.session_id === result.lastSessionId)) {
return { redirectUrl: result.nextUrl }; return { redirectUrl: result.nextUrl };
} }
} }
// Fast path for new contract // Synchronous fast path for new contract
if (!fields.contract_hash && fields.contract_url) { if (fields.contract_url) {
const uri = new URI(chrome.extension.getURL("/src/webex/pages/confirm-contract.html")); const uri = new URI(chrome.extension.getURL("/src/webex/pages/confirm-contract.html"));
uri.addSearch("contractUrl", fields.contract_url); uri.addSearch("contractUrl", fields.contract_url);
if (fields.session_id) { if (fields.session_id) {
@ -526,6 +500,13 @@ function handleHttpPayment(headerList: chrome.webRequest.HttpHeader[], url: stri
return { redirectUrl: uri.href() }; return { redirectUrl: uri.href() };
} }
// Synchronous fast path for tip
if (fields.tip) {
const uri = new URI(chrome.extension.getURL("/src/webex/pages/tip.html"));
uri.query({ tip_token: fields.tip });
return { redirectUrl: uri.href() };
}
// We need to do some asynchronous operation, we can't directly redirect // We need to do some asynchronous operation, we can't directly redirect
talerPay(fields, url, tabId).then((nextUrl) => { talerPay(fields, url, tabId).then((nextUrl) => {
if (nextUrl) { if (nextUrl) {