@fire-spark/payments-cashea-financing 0.0.0-stage → 0.20.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 +171 -0
- package/dist/index.js +350 -0
- package/package.json +56 -3
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { PaymentAdapter, PaymentContext, PaymentPolicy, PaymentStorage, PaymentUI } from "@fire-spark/payments";
|
|
2
|
+
//#region src/constants.d.ts
|
|
3
|
+
declare const CASHEA_PROVIDER = "cashea";
|
|
4
|
+
/** Key prefix of the confirmed quote in `PaymentStorage` (per order). */
|
|
5
|
+
declare const CASHEA_QUOTE_KEY = "fire-spark:cashea:quote:";
|
|
6
|
+
/** Times `casheaQuote` asks Cashea to confirm before giving up. */
|
|
7
|
+
declare const CASHEA_CONFIRM_TRIES = 5;
|
|
8
|
+
/**
|
|
9
|
+
* Closing the Cashea order is one call and Cashea has no status query: one
|
|
10
|
+
* automatic attempt, a retry is decided by the cashier.
|
|
11
|
+
*/
|
|
12
|
+
declare const CASHEA_POLICY: Partial<PaymentPolicy>;
|
|
13
|
+
//#endregion
|
|
14
|
+
//#region src/errors.d.ts
|
|
15
|
+
/**
|
|
16
|
+
* SiTef de Venezuela publishes no Cashea error catalog (doc v1.9.5): every
|
|
17
|
+
* failure answers HTTP 200 with a `trx_status` other than the expected
|
|
18
|
+
* one. These are the Spark operation codes the adapter sees.
|
|
19
|
+
*/
|
|
20
|
+
declare const casheaErrors: Readonly<Record<string, import("@fire-spark/payments").PaymentErrorEntry>> & {
|
|
21
|
+
AMOUNT_MISMATCH: {
|
|
22
|
+
code: "INVALID_REQUEST";
|
|
23
|
+
source: string;
|
|
24
|
+
};
|
|
25
|
+
CLOSED: {
|
|
26
|
+
code: "REVERSAL_NOT_ALLOWED";
|
|
27
|
+
source: string;
|
|
28
|
+
};
|
|
29
|
+
INVALID_CREDENTIALS: {
|
|
30
|
+
code: "INVALID_CREDENTIALS";
|
|
31
|
+
source: string;
|
|
32
|
+
};
|
|
33
|
+
AMBIGUOUS: {
|
|
34
|
+
code: "AMBIGUOUS";
|
|
35
|
+
source: string;
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/types.d.ts
|
|
40
|
+
/**
|
|
41
|
+
* Calls a Cashea storefront operation of the payment WITHOUT an attempt:
|
|
42
|
+
* `POST /storefront/v1/payments/:payment_id/operations/:operation` with
|
|
43
|
+
* `{ order_id, input }`. Built by the template before the tenders exist.
|
|
44
|
+
*/
|
|
45
|
+
type CasheaCall = <T>(operation: string, input: Record<string, unknown>) => Promise<T>;
|
|
46
|
+
/** `create_order` answer: the order the customer scans in the Cashea app. */
|
|
47
|
+
type CasheaCreated = {
|
|
48
|
+
cashea_order: string;
|
|
49
|
+
status: string;
|
|
50
|
+
/** What Cashea finances from: the order total in USD. */
|
|
51
|
+
amount_usd: number;
|
|
52
|
+
invoice_number: string;
|
|
53
|
+
dni: string;
|
|
54
|
+
/** Binds the Cashea order to this order; `confirm_order` needs it. */
|
|
55
|
+
signature: string;
|
|
56
|
+
};
|
|
57
|
+
/** `confirm_order` answer. */
|
|
58
|
+
type CasheaConfirmation = {
|
|
59
|
+
confirmed: true;
|
|
60
|
+
cashea_order: string;
|
|
61
|
+
dni: string;
|
|
62
|
+
financed_usd: number;
|
|
63
|
+
down_payment_usd: number;
|
|
64
|
+
amount_usd: number;
|
|
65
|
+
down_payment_ves: number | null;
|
|
66
|
+
rate: number | null;
|
|
67
|
+
signature: string;
|
|
68
|
+
} | {
|
|
69
|
+
confirmed: false;
|
|
70
|
+
cashea_order: string;
|
|
71
|
+
cashea_status: string | null;
|
|
72
|
+
code: number;
|
|
73
|
+
message: string | null;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* What Cashea confirmed, kept until the Cashea tender closes the order.
|
|
77
|
+
* The template splits the order with it: a tender of `financed_usd` with
|
|
78
|
+
* the Cashea payment (paid LAST) and the down payment (`down_payment_usd`,
|
|
79
|
+
* or `down_payment_ves` at `rate`) with other methods.
|
|
80
|
+
*/
|
|
81
|
+
type CasheaQuote = {
|
|
82
|
+
order_id: string;
|
|
83
|
+
cashea_order: string;
|
|
84
|
+
dni: string;
|
|
85
|
+
amount_usd: number;
|
|
86
|
+
financed_usd: number;
|
|
87
|
+
down_payment_usd: number;
|
|
88
|
+
down_payment_ves: number | null;
|
|
89
|
+
/** BCV rate Cashea used. */
|
|
90
|
+
rate: number | null;
|
|
91
|
+
/** Signs `financed_usd` / `down_payment_usd`; `close_order` checks it. */
|
|
92
|
+
signature: string;
|
|
93
|
+
/** Signature of the Cashea order; `cancel_order` needs it. */
|
|
94
|
+
order_signature: string;
|
|
95
|
+
};
|
|
96
|
+
/** `close_order` answer. */
|
|
97
|
+
type CasheaCloseOutcome = {
|
|
98
|
+
status: "PAID" | "FAILED";
|
|
99
|
+
cashea_order: string;
|
|
100
|
+
cashea_status: string | null;
|
|
101
|
+
financed_usd: number;
|
|
102
|
+
down_payment_usd: number;
|
|
103
|
+
processing_date: string | null;
|
|
104
|
+
message: string | null;
|
|
105
|
+
};
|
|
106
|
+
/** `cancel_order` answer. */
|
|
107
|
+
type CasheaCancelOutcome = {
|
|
108
|
+
cancelled: boolean;
|
|
109
|
+
cashea_order: string;
|
|
110
|
+
cashea_status: string | null;
|
|
111
|
+
message?: string | null;
|
|
112
|
+
};
|
|
113
|
+
//#endregion
|
|
114
|
+
//#region src/financing.d.ts
|
|
115
|
+
type CasheaFinancingOptions = {
|
|
116
|
+
/** Quote of this order. Default: the one `saveCasheaQuote` kept in `ctx.storage`. */
|
|
117
|
+
loadQuote?: (ctx: PaymentContext) => Promise<CasheaQuote | null>;
|
|
118
|
+
/**
|
|
119
|
+
* Installments of the Cashea plan, when the template knows them (the
|
|
120
|
+
* SiTef API does not answer them). Without it the tender has no
|
|
121
|
+
* `details` (`FINANCING` details require `installments`).
|
|
122
|
+
*/
|
|
123
|
+
installments?: number;
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* Cashea in store (Venezuela): buy now, pay later through the SiTef de
|
|
127
|
+
* Venezuela API. Before splitting the order the template runs
|
|
128
|
+
* `casheaQuote` (create + confirm the Cashea order) and keeps the quote
|
|
129
|
+
* with `saveCasheaQuote`. Then the order is split: the down payment with
|
|
130
|
+
* other methods FIRST and this tender, for exactly `financed_usd`, LAST.
|
|
131
|
+
* `start` closes the Cashea order (`close_order`), which tells Cashea the
|
|
132
|
+
* down payment was collected. A closed order is reversed only by the
|
|
133
|
+
* merchant with Cashea.
|
|
134
|
+
*/
|
|
135
|
+
declare const casheaFinancingPayments: (options?: CasheaFinancingOptions) => PaymentAdapter;
|
|
136
|
+
//#endregion
|
|
137
|
+
//#region src/quote.d.ts
|
|
138
|
+
type CasheaQuoteInput = {
|
|
139
|
+
/** Spark order being paid (the `order_id` the `call` sends). */
|
|
140
|
+
orderId: string;
|
|
141
|
+
/** Customer document affiliated to Cashea (`V12345678`, `12.345.678`). */
|
|
142
|
+
dni: string;
|
|
143
|
+
/** Messages and the default confirmation question. */
|
|
144
|
+
ui: Pick<PaymentUI, "message" | "confirm">;
|
|
145
|
+
/**
|
|
146
|
+
* Resolves once the customer accepted the order in the Cashea app
|
|
147
|
+
* (`true`) or gave up (`false`). Default: `ui.confirm`.
|
|
148
|
+
*/
|
|
149
|
+
waitForCustomer?: (created: CasheaCreated, attempt: number) => Promise<boolean>;
|
|
150
|
+
/** Times Cashea is asked to confirm (default 5). */
|
|
151
|
+
tries?: number;
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Pre-checkout Cashea flow, BEFORE the order is split in tenders:
|
|
155
|
+
* `create_order` for the order total in USD, "scan order N in the Cashea
|
|
156
|
+
* app", wait for the customer (template-driven), `confirm_order`. Answers
|
|
157
|
+
* what Cashea finances and the down payment the customer pays now. Gives
|
|
158
|
+
* up (and cancels the Cashea order) when the customer does or Cashea does
|
|
159
|
+
* not confirm after `tries`; throws a `PaymentError` then. Operation
|
|
160
|
+
* errors (HTTP) are thrown as the template's `call` throws them.
|
|
161
|
+
*/
|
|
162
|
+
declare const casheaQuote: (call: CasheaCall, input: CasheaQuoteInput) => Promise<CasheaQuote>;
|
|
163
|
+
/** Cancels a quoted Cashea order that will not be paid (e.g. the down payment failed). */
|
|
164
|
+
declare const cancelCasheaQuote: (call: CasheaCall, quote: CasheaQuote) => Promise<CasheaCancelOutcome>;
|
|
165
|
+
/** Keeps the quote on the device until the Cashea tender closes it. */
|
|
166
|
+
declare const saveCasheaQuote: (storage: PaymentStorage, orderId: string, quote: CasheaQuote) => Promise<void>;
|
|
167
|
+
/** The order's saved quote; `null` when absent or unreadable. */
|
|
168
|
+
declare const loadCasheaQuote: (storage: PaymentStorage, orderId: string) => Promise<CasheaQuote | null>;
|
|
169
|
+
declare const clearCasheaQuote: (storage: PaymentStorage, orderId: string) => Promise<void>;
|
|
170
|
+
//#endregion
|
|
171
|
+
export { CASHEA_CONFIRM_TRIES, CASHEA_POLICY, CASHEA_PROVIDER, CASHEA_QUOTE_KEY, type CasheaCall, type CasheaCancelOutcome, type CasheaCloseOutcome, type CasheaConfirmation, type CasheaCreated, CasheaFinancingOptions, type CasheaQuote, CasheaQuoteInput, cancelCasheaQuote, casheaErrors, casheaFinancingPayments, casheaQuote, clearCasheaQuote, loadCasheaQuote, saveCasheaQuote };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { PaymentError, createPaymentError, definePaymentErrors } from "@fire-spark/payments";
|
|
2
|
+
//#region src/constants.ts
|
|
3
|
+
const CASHEA_PROVIDER = "cashea";
|
|
4
|
+
/** Key prefix of the confirmed quote in `PaymentStorage` (per order). */
|
|
5
|
+
const CASHEA_QUOTE_KEY = "fire-spark:cashea:quote:";
|
|
6
|
+
/** Times `casheaQuote` asks Cashea to confirm before giving up. */
|
|
7
|
+
const CASHEA_CONFIRM_TRIES = 5;
|
|
8
|
+
/**
|
|
9
|
+
* Closing the Cashea order is one call and Cashea has no status query: one
|
|
10
|
+
* automatic attempt, a retry is decided by the cashier.
|
|
11
|
+
*/
|
|
12
|
+
const CASHEA_POLICY = {
|
|
13
|
+
timeout_ms: 12e4,
|
|
14
|
+
max_attempts: 1
|
|
15
|
+
};
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/errors.ts
|
|
18
|
+
const source = "docs/payments/cashea/financing.md#11-errores";
|
|
19
|
+
/**
|
|
20
|
+
* SiTef de Venezuela publishes no Cashea error catalog (doc v1.9.5): every
|
|
21
|
+
* failure answers HTTP 200 with a `trx_status` other than the expected
|
|
22
|
+
* one. These are the Spark operation codes the adapter sees.
|
|
23
|
+
*/
|
|
24
|
+
const casheaErrors = definePaymentErrors({
|
|
25
|
+
AMOUNT_MISMATCH: {
|
|
26
|
+
code: "INVALID_REQUEST",
|
|
27
|
+
source
|
|
28
|
+
},
|
|
29
|
+
CLOSED: {
|
|
30
|
+
code: "REVERSAL_NOT_ALLOWED",
|
|
31
|
+
source
|
|
32
|
+
},
|
|
33
|
+
INVALID_CREDENTIALS: {
|
|
34
|
+
code: "INVALID_CREDENTIALS",
|
|
35
|
+
source
|
|
36
|
+
},
|
|
37
|
+
AMBIGUOUS: {
|
|
38
|
+
code: "AMBIGUOUS",
|
|
39
|
+
source
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/quote.ts
|
|
44
|
+
const fail = (code, message) => {
|
|
45
|
+
return new PaymentError(createPaymentError(code, { provider_message: message }));
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Pre-checkout Cashea flow, BEFORE the order is split in tenders:
|
|
49
|
+
* `create_order` for the order total in USD, "scan order N in the Cashea
|
|
50
|
+
* app", wait for the customer (template-driven), `confirm_order`. Answers
|
|
51
|
+
* what Cashea finances and the down payment the customer pays now. Gives
|
|
52
|
+
* up (and cancels the Cashea order) when the customer does or Cashea does
|
|
53
|
+
* not confirm after `tries`; throws a `PaymentError` then. Operation
|
|
54
|
+
* errors (HTTP) are thrown as the template's `call` throws them.
|
|
55
|
+
*/
|
|
56
|
+
const casheaQuote = async (call, input) => {
|
|
57
|
+
const created = await call("create_order", { dni: input.dni });
|
|
58
|
+
const text = `Escanea la orden ${created.cashea_order} en la app Cashea`;
|
|
59
|
+
input.ui.message({
|
|
60
|
+
text,
|
|
61
|
+
kind: "waiting"
|
|
62
|
+
});
|
|
63
|
+
const ask = async (_, attempt) => {
|
|
64
|
+
const question = `${attempt > 1 ? "Cashea todavía no confirma. " : ""}¿El cliente ya aceptó la orden ${created.cashea_order} en la app Cashea?`;
|
|
65
|
+
return input.ui.confirm({ text: question });
|
|
66
|
+
};
|
|
67
|
+
const wait = input.waitForCustomer ?? ask;
|
|
68
|
+
const tries = input.tries ?? 5;
|
|
69
|
+
const cancel = () => {
|
|
70
|
+
return call("cancel_order", {
|
|
71
|
+
cashea_order: created.cashea_order,
|
|
72
|
+
dni: created.dni,
|
|
73
|
+
signature: created.signature
|
|
74
|
+
}).catch(() => null);
|
|
75
|
+
};
|
|
76
|
+
for (let attempt = 1; attempt <= tries; attempt++) {
|
|
77
|
+
if (!await wait(created, attempt)) {
|
|
78
|
+
await cancel();
|
|
79
|
+
throw fail("CANCELLED_BY_USER", "The customer left Cashea");
|
|
80
|
+
}
|
|
81
|
+
const answer = await call("confirm_order", {
|
|
82
|
+
cashea_order: created.cashea_order,
|
|
83
|
+
dni: created.dni,
|
|
84
|
+
signature: created.signature
|
|
85
|
+
});
|
|
86
|
+
if (answer.confirmed) {
|
|
87
|
+
const q1 = {
|
|
88
|
+
order_id: input.orderId,
|
|
89
|
+
cashea_order: answer.cashea_order
|
|
90
|
+
};
|
|
91
|
+
const q2 = {
|
|
92
|
+
dni: answer.dni,
|
|
93
|
+
amount_usd: answer.amount_usd
|
|
94
|
+
};
|
|
95
|
+
const q3 = { financed_usd: answer.financed_usd };
|
|
96
|
+
const q4 = { down_payment_usd: answer.down_payment_usd };
|
|
97
|
+
const q5 = {
|
|
98
|
+
down_payment_ves: answer.down_payment_ves,
|
|
99
|
+
rate: answer.rate
|
|
100
|
+
};
|
|
101
|
+
const q6 = {
|
|
102
|
+
signature: answer.signature,
|
|
103
|
+
order_signature: created.signature
|
|
104
|
+
};
|
|
105
|
+
return {
|
|
106
|
+
...q1,
|
|
107
|
+
...q2,
|
|
108
|
+
...q3,
|
|
109
|
+
...q4,
|
|
110
|
+
...q5,
|
|
111
|
+
...q6
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
const reason = answer.message ?? answer.cashea_status ?? "sin confirmar";
|
|
115
|
+
input.ui.message({
|
|
116
|
+
text: `Cashea no confirmó: ${reason}`,
|
|
117
|
+
kind: "error"
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
await cancel();
|
|
121
|
+
throw fail("FINANCING_EXPIRED", "Cashea did not confirm the order");
|
|
122
|
+
};
|
|
123
|
+
/** Cancels a quoted Cashea order that will not be paid (e.g. the down payment failed). */
|
|
124
|
+
const cancelCasheaQuote = (call, quote) => {
|
|
125
|
+
return call("cancel_order", {
|
|
126
|
+
cashea_order: quote.cashea_order,
|
|
127
|
+
dni: quote.dni,
|
|
128
|
+
signature: quote.order_signature
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
const keyOf = (orderId) => `${CASHEA_QUOTE_KEY}${orderId}`;
|
|
132
|
+
/** Keeps the quote on the device until the Cashea tender closes it. */
|
|
133
|
+
const saveCasheaQuote = (storage, orderId, quote) => {
|
|
134
|
+
return storage.set(keyOf(orderId), JSON.stringify(quote));
|
|
135
|
+
};
|
|
136
|
+
const isQuote = (value) => {
|
|
137
|
+
const q = value ?? {};
|
|
138
|
+
return [
|
|
139
|
+
"order_id",
|
|
140
|
+
"cashea_order",
|
|
141
|
+
"dni",
|
|
142
|
+
"signature",
|
|
143
|
+
"order_signature"
|
|
144
|
+
].every((key) => typeof q[key] === "string") && ["financed_usd", "down_payment_usd"].every((key) => typeof q[key] === "number");
|
|
145
|
+
};
|
|
146
|
+
/** The order's saved quote; `null` when absent or unreadable. */
|
|
147
|
+
const loadCasheaQuote = async (storage, orderId) => {
|
|
148
|
+
const raw = await storage.get(keyOf(orderId));
|
|
149
|
+
if (!raw) return null;
|
|
150
|
+
try {
|
|
151
|
+
const quote = JSON.parse(raw);
|
|
152
|
+
return isQuote(quote) && quote.order_id === orderId ? quote : null;
|
|
153
|
+
} catch {
|
|
154
|
+
return null;
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
const clearCasheaQuote = (storage, orderId) => {
|
|
158
|
+
return storage.remove(keyOf(orderId));
|
|
159
|
+
};
|
|
160
|
+
//#endregion
|
|
161
|
+
//#region src/financing.ts
|
|
162
|
+
/** Cents of tolerance between the tender and the financed amount. */
|
|
163
|
+
const TOLERANCE = .005;
|
|
164
|
+
const failedResult = (ctx, error, metadata) => {
|
|
165
|
+
const base = metadata ?? { attempt_reference: ctx.attemptReference };
|
|
166
|
+
const review = error.code === "AMBIGUOUS" ? { needs_review: true } : {};
|
|
167
|
+
const m = {
|
|
168
|
+
...base,
|
|
169
|
+
...review,
|
|
170
|
+
error
|
|
171
|
+
};
|
|
172
|
+
return {
|
|
173
|
+
status: "FAILED",
|
|
174
|
+
amount: ctx.amount.value,
|
|
175
|
+
metadata: m,
|
|
176
|
+
error
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
/** Spark operation failures; a lost answer may have closed the order. */
|
|
180
|
+
const serverErrorOf = (ctx, cause) => {
|
|
181
|
+
if (cause instanceof PaymentError) return cause.error;
|
|
182
|
+
const e = cause;
|
|
183
|
+
const code = e?.details?.code ?? e?.code;
|
|
184
|
+
const extra = {
|
|
185
|
+
provider_code: code ?? null,
|
|
186
|
+
provider_message: e?.message ?? "Cashea request failed",
|
|
187
|
+
attempt: ctx.attempt
|
|
188
|
+
};
|
|
189
|
+
const known = code ? casheaErrors[code] : void 0;
|
|
190
|
+
if (known) return createPaymentError(known.code, extra);
|
|
191
|
+
if (e?.status === 504) return createPaymentError("AMBIGUOUS", extra);
|
|
192
|
+
if (e?.status === 409) return createPaymentError("INVALID_REQUEST", extra);
|
|
193
|
+
if (e?.status === 400) return createPaymentError("FINANCING_REJECTED", extra);
|
|
194
|
+
return createPaymentError("PROVIDER_UNAVAILABLE", extra);
|
|
195
|
+
};
|
|
196
|
+
/**
|
|
197
|
+
* Tender metadata: the Cashea order (and the customer document the
|
|
198
|
+
* server's ledger `cancel` needs) in `provider`, never a card.
|
|
199
|
+
*/
|
|
200
|
+
const metadataOf = (ctx, quote, outcome) => {
|
|
201
|
+
const closed = outcome?.status === "PAID";
|
|
202
|
+
const r1 = {
|
|
203
|
+
cashea_order: quote.cashea_order,
|
|
204
|
+
dni: quote.dni
|
|
205
|
+
};
|
|
206
|
+
const r2 = { financed_usd: quote.financed_usd };
|
|
207
|
+
const r3 = {
|
|
208
|
+
down_payment_usd: quote.down_payment_usd,
|
|
209
|
+
rate: quote.rate
|
|
210
|
+
};
|
|
211
|
+
const r4 = {
|
|
212
|
+
order_signature: quote.order_signature,
|
|
213
|
+
closed
|
|
214
|
+
};
|
|
215
|
+
const r5 = { cashea_status: outcome?.cashea_status ?? null };
|
|
216
|
+
const provider = {
|
|
217
|
+
...r1,
|
|
218
|
+
...r2,
|
|
219
|
+
...r3,
|
|
220
|
+
...r4,
|
|
221
|
+
...r5
|
|
222
|
+
};
|
|
223
|
+
const p1 = { provider_transaction_id: quote.cashea_order };
|
|
224
|
+
const p2 = { reference: quote.cashea_order };
|
|
225
|
+
const p3 = {
|
|
226
|
+
financed_amount: quote.financed_usd,
|
|
227
|
+
exchange_rate: quote.rate
|
|
228
|
+
};
|
|
229
|
+
const p4 = { transaction_date: outcome?.processing_date ?? null };
|
|
230
|
+
const payment = {
|
|
231
|
+
...p1,
|
|
232
|
+
...p2,
|
|
233
|
+
...p3,
|
|
234
|
+
...p4
|
|
235
|
+
};
|
|
236
|
+
return {
|
|
237
|
+
attempt_reference: ctx.attemptReference,
|
|
238
|
+
payment,
|
|
239
|
+
provider
|
|
240
|
+
};
|
|
241
|
+
};
|
|
242
|
+
/**
|
|
243
|
+
* Cashea in store (Venezuela): buy now, pay later through the SiTef de
|
|
244
|
+
* Venezuela API. Before splitting the order the template runs
|
|
245
|
+
* `casheaQuote` (create + confirm the Cashea order) and keeps the quote
|
|
246
|
+
* with `saveCasheaQuote`. Then the order is split: the down payment with
|
|
247
|
+
* other methods FIRST and this tender, for exactly `financed_usd`, LAST.
|
|
248
|
+
* `start` closes the Cashea order (`close_order`), which tells Cashea the
|
|
249
|
+
* down payment was collected. A closed order is reversed only by the
|
|
250
|
+
* merchant with Cashea.
|
|
251
|
+
*/
|
|
252
|
+
const casheaFinancingPayments = (options = {}) => {
|
|
253
|
+
const loadQuote = (ctx) => {
|
|
254
|
+
if (options.loadQuote) return options.loadQuote(ctx);
|
|
255
|
+
return loadCasheaQuote(ctx.storage, ctx.order.id);
|
|
256
|
+
};
|
|
257
|
+
const detailsOf = (quote) => {
|
|
258
|
+
const count = options.installments;
|
|
259
|
+
if (!count || !Number.isInteger(count) || count < 1) return null;
|
|
260
|
+
return {
|
|
261
|
+
method: "FINANCING",
|
|
262
|
+
provider_reference: quote.cashea_order,
|
|
263
|
+
installments: count
|
|
264
|
+
};
|
|
265
|
+
};
|
|
266
|
+
const start = async (ctx) => {
|
|
267
|
+
const quote = await loadQuote(ctx).catch(() => null);
|
|
268
|
+
if (!quote) {
|
|
269
|
+
const error = createPaymentError("INVALID_REQUEST", { provider_message: "No Cashea quote for this order" });
|
|
270
|
+
return failedResult(ctx, error);
|
|
271
|
+
}
|
|
272
|
+
if (ctx.amount.currency.toUpperCase() === "USD" && Math.abs(ctx.amount.value - quote.financed_usd) > TOLERANCE) {
|
|
273
|
+
const error = createPaymentError("INVALID_REQUEST", {
|
|
274
|
+
provider_code: "AMOUNT_MISMATCH",
|
|
275
|
+
provider_message: "The Cashea tender must be the financed amount"
|
|
276
|
+
});
|
|
277
|
+
return failedResult(ctx, error, metadataOf(ctx, quote, null));
|
|
278
|
+
}
|
|
279
|
+
try {
|
|
280
|
+
const c1 = {
|
|
281
|
+
cashea_order: quote.cashea_order,
|
|
282
|
+
dni: quote.dni
|
|
283
|
+
};
|
|
284
|
+
const c2 = { financed_usd: quote.financed_usd };
|
|
285
|
+
const c3 = { down_payment_usd: quote.down_payment_usd };
|
|
286
|
+
const body = {
|
|
287
|
+
...c1,
|
|
288
|
+
...c2,
|
|
289
|
+
...c3,
|
|
290
|
+
signature: quote.signature
|
|
291
|
+
};
|
|
292
|
+
const outcome = await ctx.server.call("close_order", body);
|
|
293
|
+
const metadata = metadataOf(ctx, quote, outcome);
|
|
294
|
+
if (outcome.status === "PAID") {
|
|
295
|
+
await clearCasheaQuote(ctx.storage, ctx.order.id).catch(() => void 0);
|
|
296
|
+
const details = detailsOf(quote);
|
|
297
|
+
return {
|
|
298
|
+
status: "PAID",
|
|
299
|
+
amount: ctx.amount.value,
|
|
300
|
+
metadata,
|
|
301
|
+
details
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
const error = createPaymentError("FINANCING_REJECTED", {
|
|
305
|
+
provider_code: outcome.cashea_status,
|
|
306
|
+
provider_message: outcome.message,
|
|
307
|
+
attempt: ctx.attempt
|
|
308
|
+
});
|
|
309
|
+
return failedResult(ctx, error, metadata);
|
|
310
|
+
} catch (cause) {
|
|
311
|
+
const metadata = metadataOf(ctx, quote, null);
|
|
312
|
+
return failedResult(ctx, serverErrorOf(ctx, cause), metadata);
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
/** Cancels the Cashea order of an unpaid tender. */
|
|
316
|
+
const cancel = async (ctx, result) => {
|
|
317
|
+
const provider = result.metadata.provider ?? {};
|
|
318
|
+
const quote = await loadQuote(ctx).catch(() => null);
|
|
319
|
+
const order = quote?.cashea_order ?? provider.cashea_order;
|
|
320
|
+
const dni = quote?.dni ?? provider.dni;
|
|
321
|
+
const signature = quote?.order_signature ?? provider.order_signature;
|
|
322
|
+
if (order && dni && signature) {
|
|
323
|
+
const body = {
|
|
324
|
+
cashea_order: order,
|
|
325
|
+
dni,
|
|
326
|
+
signature
|
|
327
|
+
};
|
|
328
|
+
await ctx.server.call("cancel_order", body).catch(() => null);
|
|
329
|
+
}
|
|
330
|
+
await clearCasheaQuote(ctx.storage, ctx.order.id).catch(() => void 0);
|
|
331
|
+
return {
|
|
332
|
+
...result,
|
|
333
|
+
status: "CANCELLED"
|
|
334
|
+
};
|
|
335
|
+
};
|
|
336
|
+
const reversalFor = (result) => {
|
|
337
|
+
return result.status === "PAID" ? "server" : "cancel";
|
|
338
|
+
};
|
|
339
|
+
return {
|
|
340
|
+
provider: CASHEA_PROVIDER,
|
|
341
|
+
method: "FINANCING",
|
|
342
|
+
errors: casheaErrors,
|
|
343
|
+
policy: CASHEA_POLICY,
|
|
344
|
+
start,
|
|
345
|
+
cancel,
|
|
346
|
+
reversalFor
|
|
347
|
+
};
|
|
348
|
+
};
|
|
349
|
+
//#endregion
|
|
350
|
+
export { CASHEA_CONFIRM_TRIES, CASHEA_POLICY, CASHEA_PROVIDER, CASHEA_QUOTE_KEY, cancelCasheaQuote, casheaErrors, casheaFinancingPayments, casheaQuote, clearCasheaQuote, loadCasheaQuote, saveCasheaQuote };
|
package/package.json
CHANGED
|
@@ -1,6 +1,59 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fire-spark/payments-cashea-financing",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.20.1",
|
|
4
|
+
"description": "Fire Spark Cashea adapter (Venezuela): in-store buy now, pay later through the SiTef de Venezuela API; the down payment is charged with another method",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"main": "./dist/index.js",
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@fire-spark/payments": "^0.20.1",
|
|
21
|
+
"@fire-spark/types": "^0.20.1"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"tsdown": "^0.22.14",
|
|
25
|
+
"typescript": "^7.0.2",
|
|
26
|
+
"vitest": "^2.1.8"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"tsdown": {
|
|
32
|
+
"entry": [
|
|
33
|
+
"src/index.ts"
|
|
34
|
+
],
|
|
35
|
+
"format": [
|
|
36
|
+
"esm"
|
|
37
|
+
],
|
|
38
|
+
"platform": "neutral",
|
|
39
|
+
"dts": {
|
|
40
|
+
"tsconfig": "tsconfig.build.json"
|
|
41
|
+
},
|
|
42
|
+
"clean": true,
|
|
43
|
+
"deps": {
|
|
44
|
+
"neverBundle": [
|
|
45
|
+
"@fire-spark/payments",
|
|
46
|
+
"@fire-spark/types"
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsdown",
|
|
52
|
+
"dev": "tsdown --watch",
|
|
53
|
+
"check-types": "tsc --noEmit",
|
|
54
|
+
"test:utils": "vitest run",
|
|
55
|
+
"lint": "oxlint ./src --max-warnings=0",
|
|
56
|
+
"format": "oxfmt ./src",
|
|
57
|
+
"check-format": "oxfmt --check ./src"
|
|
58
|
+
}
|
|
6
59
|
}
|
package/README.md
DELETED