internetmarke
Version:
A node implementation to use the Internetmarke web service of Deutsche Post.
296 lines (295 loc) • 12.2 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Internetmarke = exports.DinPaper = exports.DinEnvelope = exports.PaymentMethod = exports.JournalEntryType = exports.JournalEntryState = exports.VoucherLayout = exports.VoucherFormat = exports.PageFormatPageType = exports.PageFormatOrientation = exports.CountryCode = void 0;
const countryCode_1 = __importDefault(require("./1c4a/countryCode"));
exports.CountryCode = countryCode_1.default;
const download_1 = require("./1c4a/download");
const inversify_config_1 = __importDefault(require("./di/inversify-config"));
const types_1 = require("./di/types");
const Error_1 = require("./Error");
const product_1 = require("./prodWs/product");
var pageFormat_1 = require("./1c4a/pageFormat");
Object.defineProperty(exports, "PageFormatOrientation", { enumerable: true, get: function () { return pageFormat_1.PageFormatOrientation; } });
Object.defineProperty(exports, "PageFormatPageType", { enumerable: true, get: function () { return pageFormat_1.PageFormatPageType; } });
var voucher_1 = require("./1c4a/voucher");
Object.defineProperty(exports, "VoucherFormat", { enumerable: true, get: function () { return voucher_1.VoucherFormat; } });
Object.defineProperty(exports, "VoucherLayout", { enumerable: true, get: function () { return voucher_1.VoucherLayout; } });
// Portokasse
var journal_1 = require("./portokasse/journal");
Object.defineProperty(exports, "JournalEntryState", { enumerable: true, get: function () { return journal_1.JournalEntryState; } });
Object.defineProperty(exports, "JournalEntryType", { enumerable: true, get: function () { return journal_1.JournalEntryType; } });
var Service_1 = require("./portokasse/Service");
Object.defineProperty(exports, "PaymentMethod", { enumerable: true, get: function () { return Service_1.PaymentMethod; } });
var paper_1 = require("./prodWs/paper");
Object.defineProperty(exports, "DinEnvelope", { enumerable: true, get: function () { return paper_1.DinEnvelope; } });
Object.defineProperty(exports, "DinPaper", { enumerable: true, get: function () { return paper_1.DinPaper; } });
/**
* Main class of the internetmarke package with access to all available methods.
*/
class Internetmarke {
constructor() {
this.init();
}
//
// 1C4A
//
/**
* Initializes the connection to the OneClickPerApp service and authenticates
* the user.
*/
initOneClickForAppService(options) {
return __awaiter(this, void 0, void 0, function* () {
yield this.oneClick4AppService.init(options);
return this.oneClick4AppService;
});
}
/**
* Retrieves all available information about the Portokasse user.
*/
getUserInfo() {
return __awaiter(this, void 0, void 0, function* () {
let info = null;
if (this.portokasseService.isInitialized()) {
info = yield this.portokasseService.getUserInfo();
}
if (!info && !this.oneClick4AppService.isInitialized()) {
throw new Error_1.InternetmarkeError('Cannot get user info before initializing OneClickForApp service');
}
if (this.oneClick4AppService.isInitialized()) {
const extended = yield this.oneClick4AppService.getUserInfo();
extended.walletBalance = info === null || info === void 0 ? void 0 : info.walletBalance;
info = extended;
}
return info;
});
}
/**
* Retrieves the page formats available for pdf voucher format.
*/
retrievePageFormats() {
return this.oneClick4AppService.retrievePageFormats();
}
/**
* Retrieves the page formats with the given id if existing.
*/
retrievePageFormat(id) {
return this.oneClick4AppService.retrievePageFormat(id);
}
/**
* Creates a globally unique order id to pass during checkout.
*/
createShopOrderId() {
return this.oneClick4AppService.createShopOrderId();
}
/**
* Retrieves all available gallery categories and images from the public
* gallery provided by Deutsche Post.
*/
retrievePublicGallery() {
return this.oneClick4AppService.retrievePublicGallery();
}
/**
* Retrieves all images from the private gallery of the authenticated user.
*/
retrievePrivateGallery() {
return this.oneClick4AppService.retrievePrivateGallery();
}
/**
* Generates a preview what the voucher will look like. A pageFormat will
* result in a pdf voucher. ImageItems can only be used in FrankingZone
* layouts.
*
* @param product The product that shoud be previewed.
* @param options Additional formatting options to customize the voucher.
*/
retrievePreviewVoucher(product, options = {}) {
return this.oneClick4AppService.retrievePreviewVoucher(product, options);
}
/**
* Adds the given product to virtual local shopping cart and attaches the
* given options to it. The shopping cart is used to store vouchers prior to
* checkout.
*
* @param product The product that should be ordered.
* @param options Options to customize the given product.
* @returns The index of the item within the shopping cart.
*/
addItemToShoppingCart(product, options = {}) {
return this.oneClick4AppService.addItemToShoppingCart(product, options);
}
/**
* Retrieves a copy of the shopping cart item at the give index if existing.
*
* @param index The index of the desired shopping cart item.
*/
getItemFromShoppingCart(index) {
return this.oneClick4AppService.getItemFromShoppingCart(index);
}
/**
* Removes the shopping cart item from the list if available. This will not
* change affect other item indices.
*
* @param index The index of the item that should be removed
* @returns The removed item if found.
*/
removeItemFromShoppingCart(index) {
return this.oneClick4AppService.removeItemFromShoppingCart(index);
}
/**
* Generates a brief summary of the items in the shopping cart.
*/
getShoppingCartSummary() {
return this.oneClick4AppService.getShoppingCartSummary();
}
/**
* Performs a checkout and retrieves the ordered vouchers.
* This will charge your Portokasse account (User) if successful!
* Add the dryrun option to simulate the checkout only and validate the
* shopping cart.
*
* @param options The checkout options to customize the vouchers.
*/
checkoutShoppingCart(options = {}) {
return this.oneClick4AppService.checkoutShoppingCart(options);
}
/**
* Retrieves the order information of an existing order with the given id.
*
* @param shopOrderId The order information that hold the data about the
* vouchers.
*/
retrieveOrder(shopOrderId) {
return __awaiter(this, void 0, void 0, function* () {
return this.oneClick4AppService.retrieveOrder(shopOrderId);
});
}
/**
* Downloads the file corresponding to the given order. This will also extract
* archives if the order contains PNG vouchers.
*
* @param order The order information as retrieved from the 1C4A service.
* @param options Download options to customize the download.
*/
/* eslint-disable class-methods-use-this */
downloadOrder(order, options = {}) {
return __awaiter(this, void 0, void 0, function* () {
return (0, download_1.downloadOrder)(order, options);
});
}
//
// ProdWS
//
/**
* Initializes the product service and makes it ready to use with the
* registered client account.
*
* @param options Product service options to manipulate the default behaviour.
*/
initProductService(options) {
return __awaiter(this, void 0, void 0, function* () {
yield this.productService.init(options);
return this.productService;
});
}
/**
* Retrieves the list of available catalogs from the service.
*/
getCatalogList() {
return this.productService.getCatalogList();
}
/**
* Retrieves the catalog with the given name if existing.
*
* @param name The name of the catalog that should be retrieved.
*/
getCatalog(name) {
return this.productService.getCatalog(name);
}
/**
* Retrieves the list of available products from the service.
*
* @param date An optional date that loads the product list at the given date.
*/
getProductList(date) {
return this.productService.getProductList(date);
}
/**
* Retrieves the product with the given id if existing.
*
* @param id The id of the product that should be retrieved.
*/
getProduct(id) {
return this.productService.getProduct(id);
}
/**
* Matches the given options to the right product.
*
* @param options Match options to retrieve the right product.
*/
matchProduct(options) {
return __awaiter(this, void 0, void 0, function* () {
const products = yield this.productService.getProductList();
return (0, product_1.matchProduct)(products, options);
});
}
//
// Portokasse
//
/**
* Initializes the portokasse service and makes it ready to use with the
* registered user account.
*
* @param options Portokasse service options to manipulate the default
* behaviour.
*/
initPortokasseService(options) {
return __awaiter(this, void 0, void 0, function* () {
yield this.portokasseService.init(options);
return this.portokasseService;
});
}
/**
* Tops up the Portokasse account with the given amount of money and the
* defined payment provider or type.
*
* @param amount The amout you want to charge as amount object or as number in
* in Euro cents. Note: The minimum amount id EUR 10.
* @param paymentMethod The type of provider you want to choose. PayPal and
* GiroPay both return a callback url to proceed the transaction, DirectDebit
* requires a one time registration at the Portokasse website prior it can
* be used.
* @param bic Optional BIC of the bank account you want to be charged. This
* value is only used for GiroPay transactions.
*/
topUp(amount, paymentMethod, bic) {
return this.portokasseService.topUp(amount, paymentMethod, bic);
}
/**
* Get the purchase and top up journal of your account.
*
* @param daysOrDateRange Eigher a days or a date range option with optional
* offset and rows information.
*/
getJournal(daysOrDateRange) {
return this.portokasseService.getJournal(daysOrDateRange);
}
init() {
this.oneClick4AppService = inversify_config_1.default.get(types_1.TYPES.OneClickForAppService);
this.portokasseService = inversify_config_1.default.get(types_1.TYPES.PortokasseService);
this.productService = inversify_config_1.default.get(types_1.TYPES.ProductService);
}
}
exports.Internetmarke = Internetmarke;
exports.default = Internetmarke;