aboutsummaryrefslogtreecommitdiff
path: root/packages/taler-util/src/MerchantApiClient.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/taler-util/src/MerchantApiClient.ts')
-rw-r--r--packages/taler-util/src/MerchantApiClient.ts116
1 files changed, 88 insertions, 28 deletions
diff --git a/packages/taler-util/src/MerchantApiClient.ts b/packages/taler-util/src/MerchantApiClient.ts
index 86c72651e..561226ba9 100644
--- a/packages/taler-util/src/MerchantApiClient.ts
+++ b/packages/taler-util/src/MerchantApiClient.ts
@@ -16,31 +16,55 @@
import { codecForAny } from "./codec.js";
import {
+ TalerMerchantApi,
+ codecForMerchantConfig,
+} from "./http-client/types.js";
+import { HttpStatusCode } from "./http-status-codes.js";
+import {
createPlatformHttpLib,
expectSuccessResponseOrThrow,
readSuccessResponseJsonOrThrow,
} from "./http.js";
import { FacadeCredentials } from "./libeufin-api-types.js";
+import { LibtoolVersion } from "./libtool-version.js";
import { Logger } from "./logging.js";
import {
- MerchantReserveCreateConfirmation,
- codecForMerchantReserveCreateConfirmation,
- TippingReserveStatus,
MerchantInstancesResponse,
+ MerchantOrderPrivateStatusResponse,
MerchantPostOrderRequest,
MerchantPostOrderResponse,
- codecForMerchantPostOrderResponse,
- MerchantOrderPrivateStatusResponse,
- codecForMerchantOrderPrivateStatusResponse,
- RewardCreateRequest,
- RewardCreateConfirmation,
+ MerchantReserveCreateConfirmation,
MerchantTemplateAddDetails,
+ RewardCreateConfirmation,
+ RewardCreateRequest,
+ TippingReserveStatus,
+ codecForMerchantOrderPrivateStatusResponse,
+ codecForMerchantPostOrderResponse,
+ codecForMerchantReserveCreateConfirmation,
} from "./merchant-api-types.js";
+import {
+ FailCasesByMethod,
+ OperationFail,
+ OperationOk,
+ ResultByMethod,
+ opEmptySuccess,
+ opKnownHttpFailure,
+ opSuccess,
+ opUnknownFailure,
+} from "./operation.js";
import { AmountString } from "./taler-types.js";
import { TalerProtocolDuration } from "./time.js";
const logger = new Logger("MerchantApiClient.ts");
+// FIXME: Explain!
+export type TalerMerchantResultByMethod<prop extends keyof MerchantApiClient> =
+ ResultByMethod<MerchantApiClient, prop>;
+
+// FIXME: Explain!
+export type TalerMerchantErrorsByMethod<prop extends keyof MerchantApiClient> =
+ FailCasesByMethod<MerchantApiClient, prop>;
+
export interface MerchantAuthConfiguration {
method: "external" | "token";
token?: string;
@@ -69,23 +93,6 @@ export interface CreateMerchantTippingReserveRequest {
wire_method: string;
}
-interface OtpDeviceAddDetails {
- // Device ID to use.
- otp_device_id: string;
-
- // Human-readable description for the device.
- otp_device_description: string;
-
- // A base64-encoded key
- otp_key: string;
-
- // Algorithm for computing the POS confirmation.
- otp_algorithm: number;
-
- // Counter for counter-based OTP devices.
- otp_ctr?: number;
-}
-
export interface DeleteTippingReserveArgs {
reservePub: string;
purge?: boolean;
@@ -123,6 +130,23 @@ export interface PrivateOrderStatusQuery {
sessionId?: string;
}
+export interface OtpDeviceAddDetails {
+ // Device ID to use.
+ otp_device_id: string;
+
+ // Human-readable description for the device.
+ otp_device_description: string;
+
+ // A base64-encoded key
+ otp_key: string;
+
+ // Algorithm for computing the POS confirmation.
+ otp_algorithm: number;
+
+ // Counter for counter-based OTP devices.
+ otp_ctr?: number;
+}
+
/**
* Client for the GNU Taler merchant backend.
*/
@@ -135,10 +159,15 @@ export class MerchantApiClient {
readonly auth: MerchantAuthConfiguration;
- constructor(baseUrl: string, auth?: MerchantAuthConfiguration) {
+ public readonly PROTOCOL_VERSION = "6:0:2";
+
+ constructor(
+ baseUrl: string,
+ options: { auth?: MerchantAuthConfiguration } = {},
+ ) {
this.baseUrl = baseUrl;
- this.auth = auth ?? {
+ this.auth = options?.auth ?? {
method: "external",
};
}
@@ -338,13 +367,44 @@ export class MerchantApiClient {
}
}
- async createOtpDevice(req: OtpDeviceAddDetails): Promise<void> {
+ isCompatible(version: string): boolean {
+ const compare = LibtoolVersion.compare(this.PROTOCOL_VERSION, version);
+ return compare?.compatible ?? false;
+ }
+ /**
+ * https://docs.taler.net/core/api-merchant.html#get--config
+ *
+ */
+ async getConfig(): Promise<OperationOk<TalerMerchantApi.VersionResponse>> {
+ const url = new URL(`config`, this.baseUrl);
+ const resp = await this.httpClient.fetch(url.href, {
+ method: "GET",
+ });
+ switch (resp.status) {
+ case HttpStatusCode.Ok:
+ return opSuccess(resp, codecForMerchantConfig());
+ default:
+ return opUnknownFailure(resp, await resp.text());
+ }
+ }
+
+ async createOtpDevice(
+ req: OtpDeviceAddDetails,
+ ): Promise<OperationOk<void> | OperationFail<HttpStatusCode.NotFound>> {
let url = new URL("private/templates", this.baseUrl);
const resp = await this.httpClient.fetch(url.href, {
method: "POST",
body: req,
headers: this.makeAuthHeader(),
});
+ switch (resp.status) {
+ case HttpStatusCode.Ok:
+ return opEmptySuccess(resp);
+ case HttpStatusCode.NotFound:
+ return opKnownHttpFailure(resp.status, resp);
+ default:
+ return opUnknownFailure(resp, await resp.text());
+ }
}
private makeAuthHeader(): Record<string, string> {