@fire-spark/payments 0.3.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 +45 -1
- package/dist/index.js +85 -1
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -464,6 +464,50 @@ declare const isBatchCloseDue: (input: {
|
|
|
464
464
|
}) => boolean;
|
|
465
465
|
declare const batchCloseDay: (now: Date) => string;
|
|
466
466
|
//#endregion
|
|
467
|
+
//#region src/redemptions.d.ts
|
|
468
|
+
/** `details` of a REDEMPTION tender: one per `payment_id` (D15). */
|
|
469
|
+
type RedemptionDetails = Extract<TenderDetails, {
|
|
470
|
+
method: "REDEMPTION";
|
|
471
|
+
}>;
|
|
472
|
+
/** One reward inside a grouped REDEMPTION tender (entry totals). */
|
|
473
|
+
type RedemptionReward = RedemptionDetails["rewards"][number];
|
|
474
|
+
/**
|
|
475
|
+
* Payment redeem as stored in `orders.redeem`. `amount` and `points` are
|
|
476
|
+
* totals of the entry (every unit); `quantity` is informational.
|
|
477
|
+
*/
|
|
478
|
+
type PaymentRedeemLike = {
|
|
479
|
+
reward_id: string;
|
|
480
|
+
quantity?: number;
|
|
481
|
+
payment_id?: string | null;
|
|
482
|
+
amount?: number;
|
|
483
|
+
points?: number;
|
|
484
|
+
program?: string;
|
|
485
|
+
};
|
|
486
|
+
/** Payment redeems of one `payment_id`, ready to seed its tender. */
|
|
487
|
+
type RedemptionGroup = {
|
|
488
|
+
payment_id: string;
|
|
489
|
+
/** Tender value: Σ `amount` of the grouped redeems. */
|
|
490
|
+
amount: number;
|
|
491
|
+
details: RedemptionDetails;
|
|
492
|
+
};
|
|
493
|
+
/**
|
|
494
|
+
* Groups payment redeems (`payment_id` set) into one entry per
|
|
495
|
+
* `payment_id`, in first-appearance order. Discount redeems are skipped.
|
|
496
|
+
*/
|
|
497
|
+
declare const groupPaymentRedeems: (redeem: readonly PaymentRedeemLike[]) => RedemptionGroup[];
|
|
498
|
+
/**
|
|
499
|
+
* REDEMPTION `details` in the grouped shape, or null for other methods.
|
|
500
|
+
* Single-reward details written before grouping read as one reward.
|
|
501
|
+
*/
|
|
502
|
+
declare const redemptionDetailsOf: (details: unknown) => RedemptionDetails | null;
|
|
503
|
+
/** Rewards of a REDEMPTION tender; empty for other methods. */
|
|
504
|
+
declare const redemptionRewardsOf: (details: unknown) => {
|
|
505
|
+
reward_id: string;
|
|
506
|
+
quantity: number;
|
|
507
|
+
points: number;
|
|
508
|
+
points_value: number;
|
|
509
|
+
}[];
|
|
510
|
+
//#endregion
|
|
467
511
|
//#region src/reference.d.ts
|
|
468
512
|
/** ULID per attempt; sent to the provider as merchant/idempotency reference. */
|
|
469
513
|
declare const createAttemptReference: (now?: number) => string;
|
|
@@ -625,4 +669,4 @@ declare const scopedPaymentStorage: (storage: PaymentStorage, paymentId: string)
|
|
|
625
669
|
/** For tests and web previews; apps provide AsyncStorage or localStorage. */
|
|
626
670
|
declare const memoryPaymentStorage: () => PaymentStorage;
|
|
627
671
|
//#endregion
|
|
628
|
-
export { ACTIVE_INTENT_STATUSES, AllocateTenderInput, AllocateTendersInput, AllocateTendersResult, AllocatedTender, AllocationError, CURRENCY_DECIMALS, ChargeConversion, DEFAULT_PAYMENT_POLICY, DELIVERY_FEE_BASED_ON, DIGITAL_COLLECT_AT, type DeviceField, MergePaymentTenderOutcome, MergePaymentTenderResult, NEXT_STATUSES, PARTIAL_APPROVAL_ERROR, PAYMENT_ERROR_CODES, PAYMENT_ERROR_DEFAULTS, type PaymentAction, type PaymentAdapter, type PaymentAdapterCapabilities, type PaymentAmount, type PaymentCardType, type PaymentCollectAt, type PaymentContext, type PaymentCustomer, type PaymentEntryMode, type PaymentEnvironment, PaymentError, PaymentErrorCode, PaymentErrorDictionary, PaymentErrorEntry, PaymentErrorReason, type PaymentIntentStatus, type PaymentMetadata, type PaymentMetadataPayment, type PaymentOperation, type PaymentPolicy, type PaymentResult, type PaymentReversal, type PaymentServer, type PaymentStorage, PaymentTenderUpdate, type PaymentUI, REDEMPTION_METHOD, RawPaymentError, RulesTender, StandardPaymentError, TERMINAL_TENDER_STATUSES, TIP_FEE_BASED_ON, type TaxBreakdown, TaxLine, type TerminalContext, type TerminalOperation, type TerminalOperations, type TerminalResult, ValidateTendersResult, activeIntent, allocateTenders, amountTolerance, batchCloseDay, countTenderItems, createAttemptReference, createPaymentError, currencyDecimals, currentIntent, defaultReversalFor, definePaymentErrors, derivePaymentStatus, formatPaymentError, isBatchCloseDue, isDeliveryFee, isIntentCovered, isMixedTenders, isPaymentError, isRealPaymentMethod, isRedemptionReserved, isScopePaid, isTenderSettled, isTerminalLocked, isTipFee, liveRealTenders, liveTenders, memoryPaymentStorage, mergePaymentTender, missingDeviceFields, nextIntentStatus, primaryTender, primaryTenderOf, prorateMinor, realTenders, resolvePaymentPolicy, resolveProviderEnvironment, roundTo, scopedPaymentStorage, taxBreakdown, tendersCoverage, tendersInScope, toChargeAmount, toPaymentAmount, toPaymentError, validateTendersAgainstRules, withTerminalLock };
|
|
672
|
+
export { ACTIVE_INTENT_STATUSES, AllocateTenderInput, AllocateTendersInput, AllocateTendersResult, AllocatedTender, AllocationError, CURRENCY_DECIMALS, ChargeConversion, DEFAULT_PAYMENT_POLICY, DELIVERY_FEE_BASED_ON, DIGITAL_COLLECT_AT, type DeviceField, MergePaymentTenderOutcome, MergePaymentTenderResult, NEXT_STATUSES, PARTIAL_APPROVAL_ERROR, PAYMENT_ERROR_CODES, PAYMENT_ERROR_DEFAULTS, type PaymentAction, type PaymentAdapter, type PaymentAdapterCapabilities, type PaymentAmount, type PaymentCardType, type PaymentCollectAt, type PaymentContext, type PaymentCustomer, type PaymentEntryMode, type PaymentEnvironment, PaymentError, PaymentErrorCode, PaymentErrorDictionary, PaymentErrorEntry, PaymentErrorReason, type PaymentIntentStatus, type PaymentMetadata, type PaymentMetadataPayment, type PaymentOperation, type PaymentPolicy, PaymentRedeemLike, type PaymentResult, type PaymentReversal, type PaymentServer, type PaymentStorage, PaymentTenderUpdate, type PaymentUI, REDEMPTION_METHOD, RawPaymentError, RedemptionDetails, RedemptionGroup, RedemptionReward, RulesTender, StandardPaymentError, TERMINAL_TENDER_STATUSES, TIP_FEE_BASED_ON, type TaxBreakdown, TaxLine, type TerminalContext, type TerminalOperation, type TerminalOperations, type TerminalResult, ValidateTendersResult, activeIntent, allocateTenders, amountTolerance, batchCloseDay, countTenderItems, createAttemptReference, createPaymentError, currencyDecimals, currentIntent, defaultReversalFor, definePaymentErrors, derivePaymentStatus, formatPaymentError, groupPaymentRedeems, isBatchCloseDue, isDeliveryFee, isIntentCovered, isMixedTenders, isPaymentError, isRealPaymentMethod, isRedemptionReserved, isScopePaid, isTenderSettled, isTerminalLocked, isTipFee, liveRealTenders, liveTenders, memoryPaymentStorage, mergePaymentTender, missingDeviceFields, nextIntentStatus, primaryTender, primaryTenderOf, prorateMinor, realTenders, redemptionDetailsOf, redemptionRewardsOf, resolvePaymentPolicy, resolveProviderEnvironment, roundTo, scopedPaymentStorage, taxBreakdown, tendersCoverage, tendersInScope, toChargeAmount, toPaymentAmount, toPaymentError, validateTendersAgainstRules, withTerminalLock };
|
package/dist/index.js
CHANGED
|
@@ -1170,6 +1170,90 @@ const isBatchCloseDue = (input) => {
|
|
|
1170
1170
|
};
|
|
1171
1171
|
const batchCloseDay = (now) => localDay(now);
|
|
1172
1172
|
//#endregion
|
|
1173
|
+
//#region src/redemptions.ts
|
|
1174
|
+
const SUM_DECIMALS = 6;
|
|
1175
|
+
const add = (a, b) => roundTo(a + b, SUM_DECIMALS);
|
|
1176
|
+
const rewardOf = (redeem) => {
|
|
1177
|
+
const r1 = {
|
|
1178
|
+
reward_id: redeem.reward_id,
|
|
1179
|
+
quantity: redeem.quantity ?? 1
|
|
1180
|
+
};
|
|
1181
|
+
const r2 = {
|
|
1182
|
+
points: redeem.points ?? 0,
|
|
1183
|
+
points_value: redeem.amount ?? 0
|
|
1184
|
+
};
|
|
1185
|
+
return {
|
|
1186
|
+
...r1,
|
|
1187
|
+
...r2
|
|
1188
|
+
};
|
|
1189
|
+
};
|
|
1190
|
+
const detailsOf = (rewards, program, loyalty_transaction_id) => {
|
|
1191
|
+
const d1 = {
|
|
1192
|
+
method: REDEMPTION_METHOD,
|
|
1193
|
+
rewards,
|
|
1194
|
+
points: rewards.reduce((sum, item) => add(sum, item.points), 0),
|
|
1195
|
+
points_value: rewards.reduce((sum, item) => add(sum, item.points_value), 0)
|
|
1196
|
+
};
|
|
1197
|
+
const d2 = program === void 0 ? {} : { program };
|
|
1198
|
+
const d3 = loyalty_transaction_id ? { loyalty_transaction_id } : {};
|
|
1199
|
+
return {
|
|
1200
|
+
...d1,
|
|
1201
|
+
...d2,
|
|
1202
|
+
...d3
|
|
1203
|
+
};
|
|
1204
|
+
};
|
|
1205
|
+
/**
|
|
1206
|
+
* Groups payment redeems (`payment_id` set) into one entry per
|
|
1207
|
+
* `payment_id`, in first-appearance order. Discount redeems are skipped.
|
|
1208
|
+
*/
|
|
1209
|
+
const groupPaymentRedeems = (redeem) => {
|
|
1210
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1211
|
+
for (const item of redeem) {
|
|
1212
|
+
if (!item.payment_id) continue;
|
|
1213
|
+
const list = groups.get(item.payment_id) ?? [];
|
|
1214
|
+
list.push(item);
|
|
1215
|
+
groups.set(item.payment_id, list);
|
|
1216
|
+
}
|
|
1217
|
+
return [...groups].map(([payment_id, items]) => {
|
|
1218
|
+
const program = items.find((item) => item.program)?.program;
|
|
1219
|
+
const details = detailsOf(items.map(rewardOf), program);
|
|
1220
|
+
return {
|
|
1221
|
+
payment_id,
|
|
1222
|
+
amount: details.points_value,
|
|
1223
|
+
details
|
|
1224
|
+
};
|
|
1225
|
+
});
|
|
1226
|
+
};
|
|
1227
|
+
const isRecord = (value) => {
|
|
1228
|
+
return typeof value === "object" && value !== null;
|
|
1229
|
+
};
|
|
1230
|
+
/**
|
|
1231
|
+
* REDEMPTION `details` in the grouped shape, or null for other methods.
|
|
1232
|
+
* Single-reward details written before grouping read as one reward.
|
|
1233
|
+
*/
|
|
1234
|
+
const redemptionDetailsOf = (details) => {
|
|
1235
|
+
if (!isRecord(details) || details.method !== "REDEMPTION") return null;
|
|
1236
|
+
if (Array.isArray(details.rewards)) return details;
|
|
1237
|
+
if (typeof details.reward_id !== "string") return null;
|
|
1238
|
+
const legacy = details;
|
|
1239
|
+
const r1 = {
|
|
1240
|
+
reward_id: legacy.reward_id,
|
|
1241
|
+
quantity: 1
|
|
1242
|
+
};
|
|
1243
|
+
const r2 = { points: legacy.points ?? 0 };
|
|
1244
|
+
const reward = {
|
|
1245
|
+
...r1,
|
|
1246
|
+
...r2,
|
|
1247
|
+
points_value: legacy.points_value ?? 0
|
|
1248
|
+
};
|
|
1249
|
+
const id = legacy.loyalty_transaction_id;
|
|
1250
|
+
return detailsOf([reward], legacy.program, id);
|
|
1251
|
+
};
|
|
1252
|
+
/** Rewards of a REDEMPTION tender; empty for other methods. */
|
|
1253
|
+
const redemptionRewardsOf = (details) => {
|
|
1254
|
+
return redemptionDetailsOf(details)?.rewards ?? [];
|
|
1255
|
+
};
|
|
1256
|
+
//#endregion
|
|
1173
1257
|
//#region src/reference.ts
|
|
1174
1258
|
const ENCODING = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
1175
1259
|
const randomBytes = (size) => {
|
|
@@ -1267,4 +1351,4 @@ const memoryPaymentStorage = () => {
|
|
|
1267
1351
|
};
|
|
1268
1352
|
};
|
|
1269
1353
|
//#endregion
|
|
1270
|
-
export { ACTIVE_INTENT_STATUSES, CURRENCY_DECIMALS, DEFAULT_PAYMENT_POLICY, DELIVERY_FEE_BASED_ON, DIGITAL_COLLECT_AT, NEXT_STATUSES, PARTIAL_APPROVAL_ERROR, PAYMENT_ERROR_CODES, PAYMENT_ERROR_DEFAULTS, PaymentError, REDEMPTION_METHOD, TERMINAL_TENDER_STATUSES, TIP_FEE_BASED_ON, activeIntent, allocateTenders, amountTolerance, batchCloseDay, countTenderItems, createAttemptReference, createPaymentError, currencyDecimals, currentIntent, defaultReversalFor, definePaymentErrors, derivePaymentStatus, formatPaymentError, isBatchCloseDue, isDeliveryFee, isIntentCovered, isMixedTenders, isPaymentError, isRealPaymentMethod, isRedemptionReserved, isScopePaid, isTenderSettled, isTerminalLocked, isTipFee, liveRealTenders, liveTenders, memoryPaymentStorage, mergePaymentTender, missingDeviceFields, nextIntentStatus, primaryTender, primaryTenderOf, prorateMinor, realTenders, resolvePaymentPolicy, resolveProviderEnvironment, roundTo, scopedPaymentStorage, taxBreakdown, tendersCoverage, tendersInScope, toChargeAmount, toPaymentAmount, toPaymentError, validateTendersAgainstRules, withTerminalLock };
|
|
1354
|
+
export { ACTIVE_INTENT_STATUSES, CURRENCY_DECIMALS, DEFAULT_PAYMENT_POLICY, DELIVERY_FEE_BASED_ON, DIGITAL_COLLECT_AT, NEXT_STATUSES, PARTIAL_APPROVAL_ERROR, PAYMENT_ERROR_CODES, PAYMENT_ERROR_DEFAULTS, PaymentError, REDEMPTION_METHOD, TERMINAL_TENDER_STATUSES, TIP_FEE_BASED_ON, activeIntent, allocateTenders, amountTolerance, batchCloseDay, countTenderItems, createAttemptReference, createPaymentError, currencyDecimals, currentIntent, defaultReversalFor, definePaymentErrors, derivePaymentStatus, formatPaymentError, groupPaymentRedeems, isBatchCloseDue, isDeliveryFee, isIntentCovered, isMixedTenders, isPaymentError, isRealPaymentMethod, isRedemptionReserved, isScopePaid, isTenderSettled, isTerminalLocked, isTipFee, liveRealTenders, liveTenders, memoryPaymentStorage, mergePaymentTender, missingDeviceFields, nextIntentStatus, primaryTender, primaryTenderOf, prorateMinor, realTenders, redemptionDetailsOf, redemptionRewardsOf, resolvePaymentPolicy, resolveProviderEnvironment, roundTo, scopedPaymentStorage, taxBreakdown, tendersCoverage, tendersInScope, toChargeAmount, toPaymentAmount, toPaymentError, validateTendersAgainstRules, withTerminalLock };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fire-spark/payments",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Fire Spark payments contract: adapters, errors, amounts and policy",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@fire-spark/types": "^0.3.
|
|
20
|
+
"@fire-spark/types": "^0.3.1"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"tsdown": "^0.22.14",
|