@fire-spark/payments-cash 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,11 +1,23 @@
1
- import { PaymentAdapter } from "@fire-spark/payments";
1
+ import { PaymentAdapter, PaymentAdapterCapabilities, PaymentContext } from "@fire-spark/payments";
2
+ import { TenderDetails } from "@fire-spark/types";
2
3
  //#region src/index.d.ts
3
4
  /** Cash has no provider: nothing can fail remotely. */
4
5
  declare const cashErrors: Readonly<Record<string, import("@fire-spark/payments").PaymentErrorEntry>>;
6
+ /** Cash is collected by a person and never captured later. */
7
+ declare const cashCapabilities: PaymentAdapterCapabilities;
8
+ type CashDetails = Extract<TenderDetails, {
9
+ method: "CASH";
10
+ }>;
11
+ /** `details` of a CASH tender; `tendered` below the amount is clamped. */
12
+ declare const cashDetails: (amount: number, tendered: number | null | undefined, currency: string) => CashDetails;
13
+ type CashPaymentsOptions = {
14
+ /** Cash the customer hands over (e.g. typed by the cashier). */
15
+ tendered?: (ctx: PaymentContext) => number | null | Promise<number | null>;
16
+ };
5
17
  /**
6
18
  * Cash collected by staff at the counter (kiosk) or at the door (delivery).
7
- * The intent stays IDLE until the store marks the order as paid.
19
+ * The tender stays IDLE until the store marks the order as paid.
8
20
  */
9
- declare const cashPayments: () => PaymentAdapter;
21
+ declare const cashPayments: (options?: CashPaymentsOptions) => PaymentAdapter;
10
22
  //#endregion
11
- export { cashErrors, cashPayments };
23
+ export { CashDetails, CashPaymentsOptions, cashCapabilities, cashDetails, cashErrors, cashPayments };
package/dist/index.js CHANGED
@@ -1,25 +1,44 @@
1
- import { definePaymentErrors } from "@fire-spark/payments";
1
+ import { currencyDecimals, definePaymentErrors, roundTo } from "@fire-spark/payments";
2
2
  //#region src/index.ts
3
3
  /** Cash has no provider: nothing can fail remotely. */
4
4
  const cashErrors = definePaymentErrors({});
5
- const idle = (ctx) => {
6
- const metadata = { attempt_reference: ctx.attemptReference };
5
+ /** Cash is collected by a person and never captured later. */
6
+ const cashCapabilities = {
7
+ deferredCapture: false,
8
+ inPerson: true
9
+ };
10
+ /** `details` of a CASH tender; `tendered` below the amount is clamped. */
11
+ const cashDetails = (amount, tendered, currency) => {
12
+ const decimals = currencyDecimals(currency);
13
+ const given = roundTo(Math.max(tendered ?? amount, amount), decimals);
7
14
  return {
8
- status: "IDLE",
9
- amount: ctx.amount.value,
10
- metadata
15
+ method: "CASH",
16
+ tendered: given,
17
+ change: roundTo(given - amount, decimals)
11
18
  };
12
19
  };
13
20
  /**
14
21
  * Cash collected by staff at the counter (kiosk) or at the door (delivery).
15
- * The intent stays IDLE until the store marks the order as paid.
22
+ * The tender stays IDLE until the store marks the order as paid.
16
23
  */
17
- const cashPayments = () => {
24
+ const cashPayments = (options = {}) => {
25
+ const start = async (ctx) => {
26
+ const metadata = { attempt_reference: ctx.attemptReference };
27
+ const amount = ctx.amount.value;
28
+ const tendered = await options.tendered?.(ctx) ?? null;
29
+ return {
30
+ status: "IDLE",
31
+ amount,
32
+ metadata,
33
+ details: cashDetails(amount, tendered, ctx.amount.currency)
34
+ };
35
+ };
18
36
  return {
19
37
  provider: "firespark",
20
38
  method: "CASH",
21
39
  errors: cashErrors,
22
- start: async (ctx) => idle(ctx),
40
+ capabilities: cashCapabilities,
41
+ start,
23
42
  cancel: async (_ctx, result) => ({
24
43
  ...result,
25
44
  status: "CANCELLED"
@@ -27,4 +46,4 @@ const cashPayments = () => {
27
46
  };
28
47
  };
29
48
  //#endregion
30
- export { cashErrors, cashPayments };
49
+ export { cashCapabilities, cashDetails, cashErrors, cashPayments };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fire-spark/payments-cash",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Fire Spark cash payments adapter (collected at the counter or the door)",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -17,8 +17,8 @@
17
17
  }
18
18
  },
19
19
  "dependencies": {
20
- "@fire-spark/types": "^0.2.0",
21
- "@fire-spark/payments": "^0.2.0"
20
+ "@fire-spark/payments": "^0.3.1",
21
+ "@fire-spark/types": "^0.3.1"
22
22
  },
23
23
  "devDependencies": {
24
24
  "tsdown": "^0.22.14",