change applyRefund return signature

This commit is contained in:
Florian Dold 2020-03-30 16:04:16 +05:30
parent ad661a63f7
commit 15e18440db
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
2 changed files with 29 additions and 23 deletions

View File

@ -64,7 +64,7 @@ async function incrementPurchaseQueryRefundRetry(
err: OperationError | undefined, err: OperationError | undefined,
): Promise<void> { ): Promise<void> {
console.log("incrementing purchase refund query retry with error", err); console.log("incrementing purchase refund query retry with error", err);
await ws.db.runWithWriteTransaction([Stores.purchases], async tx => { await ws.db.runWithWriteTransaction([Stores.purchases], async (tx) => {
const pr = await tx.get(Stores.purchases, proposalId); const pr = await tx.get(Stores.purchases, proposalId);
if (!pr) { if (!pr) {
return; return;
@ -86,7 +86,7 @@ async function incrementPurchaseApplyRefundRetry(
err: OperationError | undefined, err: OperationError | undefined,
): Promise<void> { ): Promise<void> {
console.log("incrementing purchase refund apply retry with error", err); console.log("incrementing purchase refund apply retry with error", err);
await ws.db.runWithWriteTransaction([Stores.purchases], async tx => { await ws.db.runWithWriteTransaction([Stores.purchases], async (tx) => {
const pr = await tx.get(Stores.purchases, proposalId); const pr = await tx.get(Stores.purchases, proposalId);
if (!pr) { if (!pr) {
return; return;
@ -161,7 +161,7 @@ export async function acceptRefundResponse(
const refundGroupId = encodeCrock(randomBytes(32)); const refundGroupId = encodeCrock(randomBytes(32));
await ws.db.runWithWriteTransaction([Stores.purchases], async tx => { await ws.db.runWithWriteTransaction([Stores.purchases], async (tx) => {
const p = await tx.get(Stores.purchases, proposalId); const p = await tx.get(Stores.purchases, proposalId);
if (!p) { if (!p) {
console.error("purchase not found, not adding refunds"); console.error("purchase not found, not adding refunds");
@ -239,7 +239,7 @@ async function startRefundQuery(
): Promise<void> { ): Promise<void> {
const success = await ws.db.runWithWriteTransaction( const success = await ws.db.runWithWriteTransaction(
[Stores.purchases], [Stores.purchases],
async tx => { async (tx) => {
const p = await tx.get(Stores.purchases, proposalId); const p = await tx.get(Stores.purchases, proposalId);
if (!p) { if (!p) {
console.log("no purchase found for refund URL"); console.log("no purchase found for refund URL");
@ -271,7 +271,7 @@ async function startRefundQuery(
export async function applyRefund( export async function applyRefund(
ws: InternalWalletState, ws: InternalWalletState,
talerRefundUri: string, talerRefundUri: string,
): Promise<string> { ): Promise<{ contractTermsHash: string }> {
const parseResult = parseRefundUri(talerRefundUri); const parseResult = parseRefundUri(talerRefundUri);
console.log("applying refund", parseResult); console.log("applying refund", parseResult);
@ -286,13 +286,15 @@ export async function applyRefund(
]); ]);
if (!purchase) { if (!purchase) {
throw Error(`no purchase for the taler://refund/ URI (${talerRefundUri}) was found`); throw Error(
`no purchase for the taler://refund/ URI (${talerRefundUri}) was found`,
);
} }
console.log("processing purchase for refund"); console.log("processing purchase for refund");
await startRefundQuery(ws, purchase.proposalId); await startRefundQuery(ws, purchase.proposalId);
return purchase.contractData.contractTermsHash; return { contractTermsHash: purchase.contractData.contractTermsHash };
} }
export async function processPurchaseQueryRefund( export async function processPurchaseQueryRefund(
@ -312,7 +314,7 @@ async function resetPurchaseQueryRefundRetry(
ws: InternalWalletState, ws: InternalWalletState,
proposalId: string, proposalId: string,
) { ) {
await ws.db.mutate(Stores.purchases, proposalId, x => { await ws.db.mutate(Stores.purchases, proposalId, (x) => {
if (x.refundStatusRetryInfo.active) { if (x.refundStatusRetryInfo.active) {
x.refundStatusRetryInfo = initRetryInfo(); x.refundStatusRetryInfo = initRetryInfo();
} }
@ -336,10 +338,7 @@ async function processPurchaseQueryRefundImpl(
return; return;
} }
const refundUrlObj = new URL( const refundUrlObj = new URL("refund", purchase.contractData.merchantBaseUrl);
"refund",
purchase.contractData.merchantBaseUrl,
);
refundUrlObj.searchParams.set("order_id", purchase.contractData.orderId); refundUrlObj.searchParams.set("order_id", purchase.contractData.orderId);
const refundUrl = refundUrlObj.href; const refundUrl = refundUrlObj.href;
let resp; let resp;
@ -353,7 +352,9 @@ async function processPurchaseQueryRefundImpl(
throw Error(`unexpected status code (${resp.status}) for /refund`); throw Error(`unexpected status code (${resp.status}) for /refund`);
} }
const refundResponse = codecForMerchantRefundResponse().decode(await resp.json()); const refundResponse = codecForMerchantRefundResponse().decode(
await resp.json(),
);
await acceptRefundResponse( await acceptRefundResponse(
ws, ws,
proposalId, proposalId,
@ -379,7 +380,7 @@ async function resetPurchaseApplyRefundRetry(
ws: InternalWalletState, ws: InternalWalletState,
proposalId: string, proposalId: string,
) { ) {
await ws.db.mutate(Stores.purchases, proposalId, x => { await ws.db.mutate(Stores.purchases, proposalId, (x) => {
if (x.refundApplyRetryInfo.active) { if (x.refundApplyRetryInfo.active) {
x.refundApplyRetryInfo = initRetryInfo(); x.refundApplyRetryInfo = initRetryInfo();
} }
@ -452,7 +453,7 @@ async function processPurchaseApplyRefundImpl(
let allRefundsProcessed = false; let allRefundsProcessed = false;
await ws.db.runWithWriteTransaction( await ws.db.runWithWriteTransaction(
[Stores.purchases, Stores.coins, Stores.refreshGroups, Stores.refundEvents], [Stores.purchases, Stores.coins, Stores.refreshGroups, Stores.refundEvents],
async tx => { async (tx) => {
const p = await tx.get(Stores.purchases, proposalId); const p = await tx.get(Stores.purchases, proposalId);
if (!p) { if (!p) {
return; return;
@ -471,8 +472,12 @@ async function processPurchaseApplyRefundImpl(
return; return;
} }
refreshCoinsMap[c.coinPub] = { coinPub: c.coinPub }; refreshCoinsMap[c.coinPub] = { coinPub: c.coinPub };
logger.trace(`commiting refund ${perm.merchant_sig} to coin ${c.coinPub}`); logger.trace(
logger.trace(`coin amount before is ${Amounts.toString(c.currentAmount)}`) `commiting refund ${perm.merchant_sig} to coin ${c.coinPub}`,
);
logger.trace(
`coin amount before is ${Amounts.toString(c.currentAmount)}`,
);
logger.trace(`refund amount (via merchant) is ${perm.refund_amount}`); logger.trace(`refund amount (via merchant) is ${perm.refund_amount}`);
logger.trace(`refund fee (via merchant) is ${perm.refund_fee}`); logger.trace(`refund fee (via merchant) is ${perm.refund_fee}`);
const refundAmount = Amounts.parseOrThrow(perm.refund_amount); const refundAmount = Amounts.parseOrThrow(perm.refund_amount);
@ -480,7 +485,9 @@ async function processPurchaseApplyRefundImpl(
c.status = CoinStatus.Dormant; c.status = CoinStatus.Dormant;
c.currentAmount = Amounts.add(c.currentAmount, refundAmount).amount; c.currentAmount = Amounts.add(c.currentAmount, refundAmount).amount;
c.currentAmount = Amounts.sub(c.currentAmount, refundFee).amount; c.currentAmount = Amounts.sub(c.currentAmount, refundFee).amount;
logger.trace(`coin amount after is ${Amounts.toString(c.currentAmount)}`) logger.trace(
`coin amount after is ${Amounts.toString(c.currentAmount)}`,
);
await tx.put(Stores.coins, c); await tx.put(Stores.coins, c);
}; };
@ -511,7 +518,7 @@ async function processPurchaseApplyRefundImpl(
for (const g of Object.keys(groups)) { for (const g of Object.keys(groups)) {
let groupDone = true; let groupDone = true;
for (const pk of Object.keys(p.refundState.refundsPending)) { for (const pk of Object.keys(p.refundState.refundsPending)) {
const r = p.refundState.refundsPending[pk]; const r = p.refundState.refundsPending[pk];
if (r.refundGroupId == g) { if (r.refundGroupId == g) {
groupDone = false; groupDone = false;
} }
@ -521,7 +528,7 @@ async function processPurchaseApplyRefundImpl(
proposalId, proposalId,
refundGroupId: g, refundGroupId: g,
timestamp: now, timestamp: now,
} };
await tx.put(Stores.refundEvents, refundEvent); await tx.put(Stores.refundEvents, refundEvent);
} }
} }
@ -533,8 +540,7 @@ async function processPurchaseApplyRefundImpl(
} }
await tx.put(Stores.purchases, p); await tx.put(Stores.purchases, p);
const coinsPubsToBeRefreshed = Object.values(refreshCoinsMap); const coinsPubsToBeRefreshed = Object.values(refreshCoinsMap);
if (coinsPubsToBeRefreshed.length > 0) if (coinsPubsToBeRefreshed.length > 0) {
{
await createRefreshGroup( await createRefreshGroup(
tx, tx,
coinsPubsToBeRefreshed, coinsPubsToBeRefreshed,

View File

@ -640,7 +640,7 @@ export class Wallet {
* Accept a refund, return the contract hash for the contract * Accept a refund, return the contract hash for the contract
* that was involved in the refund. * that was involved in the refund.
*/ */
async applyRefund(talerRefundUri: string): Promise<string> { async applyRefund(talerRefundUri: string): Promise<{ contractTermsHash: string }> {
return applyRefund(this.ws, talerRefundUri); return applyRefund(this.ws, talerRefundUri);
} }