generate refund in integration test
This commit is contained in:
parent
9ad8c69782
commit
fcb0565323
@ -22,30 +22,93 @@ import { getDefaultNodeWallet, withdrawTestBalance } from "./helpers";
|
|||||||
import { MerchantBackendConnection } from "./merchant";
|
import { MerchantBackendConnection } from "./merchant";
|
||||||
import { Logger } from "../util/logging";
|
import { Logger } from "../util/logging";
|
||||||
import { NodeHttpLib } from "./NodeHttpLib";
|
import { NodeHttpLib } from "./NodeHttpLib";
|
||||||
|
import * as Amounts from "../util/amounts";
|
||||||
|
import { Wallet } from "../wallet";
|
||||||
|
|
||||||
const logger = new Logger("integrationtest.ts");
|
const logger = new Logger("integrationtest.ts");
|
||||||
|
|
||||||
export async function runIntegrationTest(args: {
|
export interface IntegrationTestArgs {
|
||||||
exchangeBaseUrl: string;
|
exchangeBaseUrl: string;
|
||||||
bankBaseUrl: string;
|
bankBaseUrl: string;
|
||||||
merchantBaseUrl: string;
|
merchantBaseUrl: string;
|
||||||
merchantApiKey: string;
|
merchantApiKey: string;
|
||||||
amountToWithdraw: string;
|
amountToWithdraw: string;
|
||||||
amountToSpend: string;
|
amountToSpend: string;
|
||||||
}) {
|
}
|
||||||
|
|
||||||
|
async function makePayment(
|
||||||
|
wallet: Wallet,
|
||||||
|
merchant: MerchantBackendConnection,
|
||||||
|
amount: string,
|
||||||
|
summary: string,
|
||||||
|
): Promise<{ orderId: string }> {
|
||||||
|
let orderResp = await merchant.createOrder(
|
||||||
|
amount,
|
||||||
|
summary,
|
||||||
|
"taler://fulfillment-success/thx",
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log("created order with orderId", orderResp.orderId);
|
||||||
|
|
||||||
|
let paymentStatus = await merchant.checkPayment(orderResp.orderId);
|
||||||
|
|
||||||
|
console.log("payment status", paymentStatus);
|
||||||
|
|
||||||
|
const talerPayUri = paymentStatus.taler_pay_uri;
|
||||||
|
if (!talerPayUri) {
|
||||||
|
throw Error("no taler://pay/ URI in payment response");
|
||||||
|
}
|
||||||
|
|
||||||
|
let preparePayResult = await wallet.preparePayForUri(talerPayUri);
|
||||||
|
|
||||||
|
console.log("prepare pay result", preparePayResult);
|
||||||
|
|
||||||
|
if (preparePayResult.status != "payment-possible") {
|
||||||
|
throw Error("payment not possible");
|
||||||
|
}
|
||||||
|
|
||||||
|
let confirmPayResult = await wallet.confirmPay(
|
||||||
|
preparePayResult.proposalId,
|
||||||
|
undefined,
|
||||||
|
);
|
||||||
|
|
||||||
|
console.log("confirmPayResult", confirmPayResult);
|
||||||
|
|
||||||
|
paymentStatus = await merchant.checkPayment(orderResp.orderId);
|
||||||
|
|
||||||
|
console.log("payment status after wallet payment:", paymentStatus);
|
||||||
|
|
||||||
|
if (!paymentStatus.paid) {
|
||||||
|
throw Error("payment did not succeed");
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
orderId: orderResp.orderId,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function runIntegrationTest(args: IntegrationTestArgs) {
|
||||||
logger.info("running test with arguments", args);
|
logger.info("running test with arguments", args);
|
||||||
|
|
||||||
|
const parsedSpendAmount = Amounts.parseOrThrow(args.amountToSpend);
|
||||||
|
const currency = parsedSpendAmount.currency;
|
||||||
|
|
||||||
const myHttpLib = new NodeHttpLib();
|
const myHttpLib = new NodeHttpLib();
|
||||||
myHttpLib.setThrottling(false);
|
myHttpLib.setThrottling(false);
|
||||||
|
|
||||||
const myWallet = await getDefaultNodeWallet({ httpLib: myHttpLib });
|
const myWallet = await getDefaultNodeWallet({ httpLib: myHttpLib });
|
||||||
|
|
||||||
myWallet.runRetryLoop().catch((e) => {
|
myWallet.runRetryLoop().catch(e => {
|
||||||
console.error("exception during retry loop:", e);
|
console.error("exception during retry loop:", e);
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info("withdrawing test balance");
|
logger.info("withdrawing test balance");
|
||||||
await withdrawTestBalance(myWallet, args.amountToWithdraw, args.bankBaseUrl, args.exchangeBaseUrl);
|
await withdrawTestBalance(
|
||||||
|
myWallet,
|
||||||
|
args.amountToWithdraw,
|
||||||
|
args.bankBaseUrl,
|
||||||
|
args.exchangeBaseUrl,
|
||||||
|
);
|
||||||
logger.info("done withdrawing test balance");
|
logger.info("done withdrawing test balance");
|
||||||
|
|
||||||
const balance = await myWallet.getBalances();
|
const balance = await myWallet.getBalances();
|
||||||
@ -57,42 +120,68 @@ export async function runIntegrationTest(args: {
|
|||||||
args.merchantApiKey,
|
args.merchantApiKey,
|
||||||
);
|
);
|
||||||
|
|
||||||
const orderResp = await myMerchant.createOrder(
|
await makePayment(myWallet, myMerchant, args.amountToSpend, "hello world");
|
||||||
args.amountToSpend,
|
|
||||||
"hello world",
|
// Wait until the refresh is done
|
||||||
"https://example.com/",
|
await myWallet.runUntilDone();
|
||||||
|
|
||||||
|
console.log("withdrawing test balance for refund");
|
||||||
|
const withdrawAmountTwo: Amounts.AmountJson = {
|
||||||
|
currency,
|
||||||
|
value: 18,
|
||||||
|
fraction: 0,
|
||||||
|
};
|
||||||
|
const spendAmountTwo: Amounts.AmountJson = {
|
||||||
|
currency,
|
||||||
|
value: 7,
|
||||||
|
fraction: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
const refundAmount: Amounts.AmountJson = {
|
||||||
|
currency,
|
||||||
|
value: 6,
|
||||||
|
fraction: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
const spendAmountThree: Amounts.AmountJson = {
|
||||||
|
currency,
|
||||||
|
value: 3,
|
||||||
|
fraction: 0,
|
||||||
|
};
|
||||||
|
await withdrawTestBalance(
|
||||||
|
myWallet,
|
||||||
|
Amounts.toString(withdrawAmountTwo),
|
||||||
|
args.bankBaseUrl,
|
||||||
|
args.exchangeBaseUrl,
|
||||||
);
|
);
|
||||||
|
|
||||||
console.log("created order with orderId", orderResp.orderId);
|
// Wait until the withdraw is done
|
||||||
|
await myWallet.runUntilDone();
|
||||||
|
|
||||||
const paymentStatus = await myMerchant.checkPayment(orderResp.orderId);
|
let { orderId: refundOrderId } = await makePayment(
|
||||||
|
myWallet,
|
||||||
|
myMerchant,
|
||||||
|
Amounts.toString(spendAmountTwo),
|
||||||
|
"order that will be refunded",
|
||||||
|
);
|
||||||
|
|
||||||
console.log("payment status", paymentStatus);
|
const refundUri = await myMerchant.refund(
|
||||||
|
refundOrderId,
|
||||||
|
"test refund",
|
||||||
|
Amounts.toString(refundAmount),
|
||||||
|
);
|
||||||
|
|
||||||
const talerPayUri = paymentStatus.taler_pay_uri;
|
await myWallet.applyRefund(refundUri);
|
||||||
if (!talerPayUri) {
|
|
||||||
throw Error("no taler://pay/ URI in payment response");
|
|
||||||
}
|
|
||||||
|
|
||||||
const preparePayResult = await myWallet.preparePayForUri(talerPayUri);
|
// Wait until the refund is done
|
||||||
|
await myWallet.runUntilDone();
|
||||||
|
|
||||||
console.log("prepare pay result", preparePayResult);
|
await makePayment(
|
||||||
|
myWallet,
|
||||||
if (preparePayResult.status != "payment-possible") {
|
myMerchant,
|
||||||
throw Error("payment not possible");
|
Amounts.toString(spendAmountThree),
|
||||||
}
|
"payment after refund",
|
||||||
|
);
|
||||||
const confirmPayResult = await myWallet.confirmPay(preparePayResult.proposalId, undefined);
|
|
||||||
|
|
||||||
console.log("confirmPayResult", confirmPayResult);
|
|
||||||
|
|
||||||
const paymentStatus2 = await myMerchant.checkPayment(orderResp.orderId);
|
|
||||||
|
|
||||||
console.log("payment status after wallet payment:", paymentStatus2);
|
|
||||||
|
|
||||||
if (!paymentStatus2.paid) {
|
|
||||||
throw Error("payment did not succeed");
|
|
||||||
}
|
|
||||||
|
|
||||||
await myWallet.runUntilDone();
|
await myWallet.runUntilDone();
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ export class MerchantBackendConnection {
|
|||||||
orderId: string,
|
orderId: string,
|
||||||
reason: string,
|
reason: string,
|
||||||
refundAmount: string,
|
refundAmount: string,
|
||||||
): Promise<void> {
|
): Promise<string> {
|
||||||
const reqUrl = new URL("refund", this.merchantBaseUrl);
|
const reqUrl = new URL("refund", this.merchantBaseUrl);
|
||||||
const refundReq = {
|
const refundReq = {
|
||||||
order_id: orderId,
|
order_id: orderId,
|
||||||
|
Loading…
Reference in New Issue
Block a user