# @moirei/complex-pricing

> Ecommerce complex pricing package for the frontend and node.js.

Latest version **1.0.1** (published 2021-12-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install @moirei/complex-pricing
pnpm add @moirei/complex-pricing
yarn add @moirei/complex-pricing
bun add @moirei/complex-pricing
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2021-12-31 |
| First published | 2021-07-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 42.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Augustus Okoye |
| Maintainers | augustusnaz |
| Keywords | moirei, ecommerce, pricing, volume, graduated, volume pricing, graduated pricing, typescript |

## Links

- npm: https://www.npmjs.com/package/@moirei/complex-pricing
- Repository: https://github.com/moirei/complex-pricing
- Homepage: https://github.com/moirei/complex-pricing#readme
- Issues: https://github.com/moirei/complex-pricing/issues
- npm.io page: https://npm.io/package/@moirei/complex-pricing

## Dependencies (4)

- [chai](https://npm.io/package/chai.md) ^4.2.0
- [@types/mocha](https://npm.io/package/@types/mocha.md) ^8.0.3
- [@types/expect](https://npm.io/package/@types/expect.md) ^24.3.0
- [@types/lodash](https://npm.io/package/@types/lodash.md) ^4.14.161

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2021-12-31
- 1.0.0 — 2021-07-04

## README

# @moirei/complex-pricing

This package is a port of the [laravel-pricing](https://github.com/moirei/laravel-pricing) package with typescript support for the frontend and node.js.

## Installation

```bash
$ npm i @moirei/complex-pricing
# or
$ yarn add @moirei/complex-pricing
```



## Usage

```typescript
import Pricing from '@moirei/complex-pricing'

const pricing = new Pricing({
    model: "package",
    unit_amount: 25,
    units: 5,
});
// or
const pricing = Pricing.make(...)

const price = pricing.price(4); // returns 25.0
```

### Get export content

To export/save and re-instantiate the underlying data, use the `get` method.

```typescript
const content = pricing.get();

pricing = pricing.make(content);
```



## Pricing Models

In large applications, pricing for provided goods or service are often not straight forward. For instance, you might want to charge $10 on an item for every 5 units purchased in AU, while at the same time, for your customers in US, regressively charge $50, $40, $30 for every quantity ranged between 0-30, 31-40, 50-infinity respectively.

This package has the concept of `standard`, `package`, `volume`, and `graduated` pricing intended to cover most (if not all) complex pricing scenarios. It also allows naming for multi-currency and multi-region use cases.



### Standard

This is the classic pricing model where your price result is a linear multiple of the unit price.

```typescript
const pricing = Pricing.make({
    model: "standard",
    unit_amount: 25,
});
// or
const pricing = Pricing.make().standard(25)

const price = pricing.price(4); // returns 100.0
```



### Package

`Package` pricing computes the total result in package groups. For example, an amount of $25.0 for every 5 units. Results are rounded up such that 8 units returns $50.0.

```typescript
const pricing = Pricing.make({
    model: "package",
    unit_amount: 25,
    units: 5,
});
// or
const pricing = Pricing.make().package(25, 5)

const price = pricing.price(4); // returns 25.0
const price = pricing.price(8); // returns 100.0
```



### Volume

Use `volume` pricing to apply charges based on tier of the `quantity`. For example, with the tiers below; charges on 1-5 units fall in the first tier, 6-10 within the second tier, and within the third for 11 units and above.

```typescript
const tiers = [
    {
        max: 5,
        unit_amount: 3,
    },
    {
        max: 10,
        unit_amount: 2,
    },
    {
        max: 'infinity',
        unit_amount: 1,
        flat_amount: 0.3,
    }
];
const pricing = Pricing.make({
    model: "volume",
    tiers: tiers,
});
// or
const pricing = Pricing.make().volume(tiers)

const price = pricing.price(4); // returns 4 x 3 = 12.0
const price = pricing.price(8); // returns 8 x 2 = 16.0
const price = pricing.price(12); // returns (12 x 1) + 0.3 = 12.3
```



## Graduated

Use `graduated` pricing to progressively calculate a charge based on all applicable tiers. For example, with the tiers below, a unit of 6 falls between tiers 0-1, 12 falls between 0-2, and so on.

```typescript
const tiers = [
    {
        max: 5,
        unit_amount: 4,
    },
    {
        max: 10,
        unit_amount: 3,
        flat_amount: 0.1,
    },
    {
        max: 15,
        unit_amount: 2,
        flat_amount: 0.2,
    },
    {
        max: 'infinity',
        unit_amount: 1,
        flat_amount: 0.3,
    }
];
const pricing = Pricing.make({
    model: "graduated",
    tiers: tiers,
});
// or
const pricing = Pricing.make().graduated(tiers)

const price = pricing.price(4);  // returns 4 x 4 = 16.0
const price = pricing.price(8);  // returns (5 x 4) + (3 x 3 + 0.1) = 29.1
const price = pricing.price(12); // returns (5 x 4) + (5 x 3 + 0.1) + (2 x 2 + 0.2) = 39.3
```



##  Flat Fees

For `volume` and `graduated` pricing, use `flat_amount` for the provided tiers to include a flat fee for every charge.



## Miscellaneous Data

Use the `data` method to update or get the content miscellaneous data.

```typescript
// set data
pricing.data({
    currency: 'AUD'
}) // returns the instance so it may be chainable
pricing.data('meta.tiers_count', 4); // returns 4

const currency = pricing.data('currency') // returns 'AUD'
const tiers_count = pricing.data('meta.tiers_count') // returns 4

const data = pricing.data(); // dump all
```





## Contribution Guidelines

Any pull requests or discussions are welcome.
Note that every pull request providing new feature or correcting a bug should be created with appropriate unit tests.



## Changelog

Please see [CHANGELOG](./CHANGELOG.md).

---
_Source: https://npm.io/package/@moirei/complex-pricing · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
