UNPKG

pcp-server-nodejs-sdk

Version:

[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=PAYONE-GmbH_PCP-server-nodeJS-SDK&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=PAYONE-GmbH_PCP-server-nodeJS-SDK) [![Coverage](https://sonarcloud.io/api/pr

117 lines 4.8 kB
import { URLSearchParams } from 'url'; import { Headers } from 'node-fetch'; import { CommunicatorConfiguration } from '../CommunicatorConfiguration.js'; import { GetCheckoutsQuery } from '../queries/index.js'; import { BaseApiClient, CHECKOUT_ID_REQUIRED_ERROR, COMMERCE_CASE_ID_REQUIRED_ERROR, MERCHANT_ID_REQUIRED_ERROR, } from './BaseApiClient.js'; export class CheckoutApiClient extends BaseApiClient { constructor(config) { super(config); } async createCheckoutRequest(merchantId, commerceCaseId, payload) { if (!merchantId) { throw new TypeError(MERCHANT_ID_REQUIRED_ERROR); } if (!commerceCaseId) { throw new TypeError(COMMERCE_CASE_ID_REQUIRED_ERROR); } const url = new URL(`/v1/${merchantId}/commerce-cases/${commerceCaseId}/checkouts`, this.getConfig().getHost()); const requestInit = { method: 'POST', headers: new Headers({ 'Content-Type': 'application/json', }), body: JSON.stringify(payload), }; return this.makeApiCall(url.toString(), requestInit); } async getCheckoutRequest(merchantId, commerceCaseId, checkoutId) { if (!merchantId) { throw new TypeError(MERCHANT_ID_REQUIRED_ERROR); } if (!commerceCaseId) { throw new TypeError(COMMERCE_CASE_ID_REQUIRED_ERROR); } if (!checkoutId) { throw new TypeError(CHECKOUT_ID_REQUIRED_ERROR); } const url = new URL(`/v1/${merchantId}/commerce-cases/${commerceCaseId}/checkouts/${checkoutId}`, this.getConfig().getHost()); const requestInit = { method: 'GET', headers: new Headers(), }; return this.makeApiCall(url.toString(), requestInit); } async getCheckoutsRequest(merchantId, queryParams) { if (!merchantId) { throw new TypeError(MERCHANT_ID_REQUIRED_ERROR); } const url = new URL(`/v1/${merchantId}/checkouts`, this.getConfig().getHost()); if (queryParams) { const params = new URLSearchParams(queryParams.toQueryMap()); url.search = params.toString(); } const requestInit = { method: 'GET', headers: new Headers(), }; return this.makeApiCall(url.toString(), requestInit); } async updateCheckoutRequest(merchantId, commerceCaseId, checkoutId, payload) { if (!merchantId) { throw new TypeError(MERCHANT_ID_REQUIRED_ERROR); } if (!commerceCaseId) { throw new TypeError(COMMERCE_CASE_ID_REQUIRED_ERROR); } if (!checkoutId) { throw new TypeError(CHECKOUT_ID_REQUIRED_ERROR); } const url = new URL(`/v1/${merchantId}/commerce-cases/${commerceCaseId}/checkouts/${checkoutId}`, this.getConfig().getHost()); const requestInit = { method: 'PATCH', headers: new Headers({ 'Content-Type': 'application/json', }), body: JSON.stringify(payload), }; await this.makeApiCall(url.toString(), requestInit, BaseApiClient.parseVoid); } async removeCheckoutRequest(merchantId, commerceCaseId, checkoutId) { if (!merchantId) { throw new TypeError(MERCHANT_ID_REQUIRED_ERROR); } if (!commerceCaseId) { throw new TypeError(COMMERCE_CASE_ID_REQUIRED_ERROR); } if (!checkoutId) { throw new TypeError(CHECKOUT_ID_REQUIRED_ERROR); } const url = new URL(`/v1/${merchantId}/commerce-cases/${commerceCaseId}/checkouts/${checkoutId}`, this.getConfig().getHost()); const requestInit = { method: 'DELETE', headers: new Headers(), }; await this.makeApiCall(url.toString(), requestInit, BaseApiClient.parseVoid); } async completeCheckoutRequest(merchantId, commerceCaseId, checkoutId, payload) { if (!merchantId) { throw new TypeError(MERCHANT_ID_REQUIRED_ERROR); } if (!commerceCaseId) { throw new TypeError(COMMERCE_CASE_ID_REQUIRED_ERROR); } if (!checkoutId) { throw new TypeError(CHECKOUT_ID_REQUIRED_ERROR); } const url = new URL(`/v1/${merchantId}/commerce-cases/${commerceCaseId}/checkouts/${checkoutId}/complete-order`, this.getConfig().getHost()); const requestInit = { method: 'POST', headers: new Headers({ 'Content-Type': 'application/json', }), body: JSON.stringify(payload), }; return this.makeApiCall(url.toString(), requestInit); } } //# sourceMappingURL=CheckoutApiClient.js.map