PostalPoint® supports plugin extensions. Plugins can hook into PostalPoint to add features.
## What plugins can do
* Process card payments and handle saved payment methods
* Add additional carriers and provide shipping rates
* Print to label and receipt printers, letting PostalPoint handle hardware support and drivers
* Extend support for prepaid label acceptance, prepaid barcode recognition, and carrier dropoff QR codes
* Install pages in the Tools interface, creating new interfaces and features
* Receive transaction receipts for ingestion into third-party accounting or business software
* Run both Node.JS and browser code.
## Plugin Package Structure
A plugin is distributed as a simple ZIP file, containing a folder. That folder's name is the plugin
name. The folder then has at least one file, named `plugin.js`.
he `exports.init` function in `plugin.js` is executed when PostalPoint launches, allowing the plugin
to request involvement with various events in PostalPoint.
PostalPoint installs plugin packages by unzipping their contents into a plugins folder.
Plugins are uninstalled by deleting their folder.
## Minimal Plugin Code
```javascript
// plugin-name/plugin.js
exports.init = function () {
global.apis.alert("This message appears when PostalPoint launches.", "Hello!");
};
```
Yes, the smallest plugin really is just two lines of code, and accessing PostalPoint features
really is that easy.
## Example Plugins
Check out the `plugins` folder for fully-functional plugins.
## PostalPoint Plugin API
The PostalPoint plugin API is a globally-available object.
### API List
#### Core
`global.apis.`:
*`alert(text, title)`: Display an alert dialog.
*`getAppFolder()`: Get the filesystem path to the PostalPoint installation folder.
*`getPluginFolder(pluginName)`: Get the filesystem path to a plugin's folder.
If used without arguments, gets the root plugin storage folder.
*`f7`: The Framework7 app instance for PostalPoint's entire UI, created by `new Framework7()`. See https://framework7.io/docs/app for details. Be very careful.
#### Barcode
`global.apis.barcode.`:
*`TrackingBarcode`: A class defining a prepaid barcode and related information. See docs/TrackingBarcode.md for details.
*`onPrepaidScan(function (codeString) {})`: The function passed to onPrepaidScan is run
when a barcode is scanned on the Prepaid page. The function is passed one argument, a string
containing the raw barcode data. The function shall return boolean `false` if unable or unwilling
to handle the barcode. If the barcode is handled by this function, it shall return a TrackingBarcode object.
*`addPrepaidBarcode(trackingBarcodeData)`: Add a TrackingBarcode object to the transaction receipt at any time other than `onPrepaidScan`.
#### Database
PostalPoint supports multiple SQL databases, currently SQLite and MariaDB.
`global.apis.database.`:
*`async getConnection()`: Returns a database driver. See docs/Database.md for details.
#### Graphics
PostalPoint uses the Jimp library version 1.6 for creating and manipulating images and shipping labels.
`global.apis.graphics.`:
*`Jimp()`: The [JavaScript Image Manipulation Program](https://jimp-dev.github.io/jimp/). Access like so: `const {Jimp} = global.apis.graphics.Jimp();`.
*`async loadFont(filename)`: Replacement for [Jimp's loadFont](https://jimp-dev.github.io/jimp/api/jimp/functions/loadfont/), which gets very confused about our JS environment and ends up crashing everything.
#### Kiosk
`global.apis.kiosk.`:
*`isKiosk()`: Returns a boolean to indicate if PostalPoint is running in kiosk mode.
#### Point of Sale
`global.apis.pos.`:
*`addReceiptItem(item)`: Add a `ReceiptItem` to the current transaction.
*`addReceiptPayment(item)`: Add a `ReceiptPayment` to the current transaction.
*`addOnscreenPaymentLog(string)`: Append a line of text to the onscreen log displayed during credit card processing. Not shown in kiosk mode.
*`onReceiptChange(function (receipt) {})`: Add a function to be called whenever the transaction data/receipt is changed.
*`onTransactionFinished(function (receipt) {})`: Same as `onReceiptChange` except run when a transaction is completed.
*`addToolsPage(page, title, id = "", description = "", cardTitle = "", icon = "")`: Add a page to the Tools screen. See examples/basic-demo for example usage.
*`showProgressSpinner(title, text = "", subtitle = "")`: Show a Framework7 notification with a loading icon.
*`hideProgressSpinner()`: hide the UI element created by `showProgressSpinner`.
*`openInternalWebBrowser(url)`: Open a web browser UI, navigating to the URL. The browser has forward/back/close buttons.
*`openSystemWebBrowser(url)`: Open the native OS default browser to the URL given.
#### Utilities
Various useful helper functions.
`global.apis.util.`:
*`barcode.getBuffer(data, type = "code128", height = 10, scale = 2, includetext = false)`: Get a PNG image buffer of a barcode. Uses library "bwip-js".
*`clipboard.copy(text, showNotification = false)`: Copy a string to the system clipboard, optionally showing a "copied" notification to the user.
*`async delay(ms = 1000)`: Pause execution for some amount of time in an async function, i.e., returns a Promise that resolves in some number of milliseconds.
*`async http.fetch(url, responseType = "text", timeout = 15)`: Fetch a URL. `responseType` can be "text", "blob", "buffer", or "json". Timeout is in seconds.
*`async http.post(url, data, responseType = "text", headers = {"Content-Type": "application/json"}, method = "POST", continueOnBadStatusCode = false)`: POST to a URL. `data` is sent as a JSON body.
*`objectEquals(a, b)`: Compare two objects for equality.
*`string.chunk(str, chunksize)`: Split a string into an array of strings of length `chunksize`.
*`string.split(input, separator, limit)`: Split a string with a RegExp separator an optionally limited number of times.
*`time.diff(compareto)`: Get the number of seconds between now and the given UNIX timestamp.
*`time.format(format, timestamp = time.now())`: Take a UNIX timestamp in seconds and format it to a string. (Mostly) compatible with PHP's date() function.
*`time.now()`: Get the current UNIX timestamp in seconds.
*`time.strtotime(str)`: Parse a string date and return a UNIX timestamp.
*`time.toDateString(timestamp)`: Get a localized date string for a UNIX timestamp.
*`time.toTimeString(timestamp)`: Get a time string for a UNIX timestamp, for example, "2:01 PM".
*`uuid.v4()`: Generate a version 4 UUID string, for example, "fcca5b12-6a11-46eb-96e4-5ed6365de977".
*`uuid.short()`: Generate a 16-character random alphanumeric string, for example, "4210cd8f584e6f6c".