@pax2pay/model-banking 0.1.456 → 0.1.457

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.
Files changed (76) hide show
  1. package/Card/Operation/index.ts +3 -11
  2. package/Operation/Changes.ts +1 -1
  3. package/Operation/Creatable.ts +1 -1
  4. package/Settlement/Entry/Creatable.ts +111 -11
  5. package/Settlement/Entry/Failed.ts +27 -0
  6. package/Settlement/Entry/{Cancel.ts → Legacy/Cancel.ts} +5 -5
  7. package/Settlement/Entry/{Capture.ts → Legacy/Capture.ts} +5 -5
  8. package/Settlement/Entry/Legacy/Creatable.ts +15 -0
  9. package/Settlement/Entry/{Refund.ts → Legacy/Refund.ts} +8 -8
  10. package/Settlement/Entry/{Unknown.ts → Legacy/Unknown.ts} +3 -3
  11. package/Settlement/Entry/Legacy/index.ts +55 -0
  12. package/Settlement/Entry/Succeeded.ts +26 -0
  13. package/Settlement/Entry/fromCreatable.ts +42 -0
  14. package/Settlement/Entry/fromLegacy.ts +95 -0
  15. package/Settlement/Entry/index.ts +19 -50
  16. package/Settlement/Entry/type.ts +6 -0
  17. package/Settlement/index.ts +1 -1
  18. package/Transaction/Incoming.ts +1 -1
  19. package/Transaction/index.ts +1 -1
  20. package/Warning/Settlement/UnknownEntry.ts +6 -3
  21. package/dist/Card/Operation/index.d.ts +0 -1
  22. package/dist/Card/Operation/index.js +3 -12
  23. package/dist/Card/Operation/index.js.map +1 -1
  24. package/dist/Operation/Changes.d.ts +1 -1
  25. package/dist/Operation/Creatable.d.ts +1 -1
  26. package/dist/Settlement/Entry/Creatable.d.ts +51 -6
  27. package/dist/Settlement/Entry/Creatable.js +88 -5
  28. package/dist/Settlement/Entry/Creatable.js.map +1 -1
  29. package/dist/Settlement/Entry/Failed.d.ts +11 -0
  30. package/dist/Settlement/Entry/Failed.js +12 -0
  31. package/dist/Settlement/Entry/Failed.js.map +1 -0
  32. package/dist/Settlement/Entry/{Cancel.d.ts → Legacy/Cancel.d.ts} +5 -5
  33. package/dist/Settlement/Entry/{Cancel.js → Legacy/Cancel.js} +5 -5
  34. package/dist/Settlement/Entry/Legacy/Cancel.js.map +1 -0
  35. package/dist/Settlement/Entry/{Capture.d.ts → Legacy/Capture.d.ts} +5 -5
  36. package/dist/Settlement/Entry/{Capture.js → Legacy/Capture.js} +5 -5
  37. package/dist/Settlement/Entry/Legacy/Capture.js.map +1 -0
  38. package/dist/Settlement/Entry/Legacy/Creatable.d.ts +9 -0
  39. package/dist/Settlement/Entry/Legacy/Creatable.js +10 -0
  40. package/dist/Settlement/Entry/Legacy/Creatable.js.map +1 -0
  41. package/dist/Settlement/Entry/{Refund.d.ts → Legacy/Refund.d.ts} +8 -8
  42. package/dist/Settlement/Entry/{Refund.js → Legacy/Refund.js} +7 -7
  43. package/dist/Settlement/Entry/Legacy/Refund.js.map +1 -0
  44. package/dist/Settlement/Entry/{Unknown.d.ts → Legacy/Unknown.d.ts} +3 -3
  45. package/dist/Settlement/Entry/{Unknown.js → Legacy/Unknown.js} +3 -3
  46. package/dist/Settlement/Entry/Legacy/Unknown.js.map +1 -0
  47. package/dist/Settlement/Entry/Legacy/index.d.ts +19 -0
  48. package/dist/Settlement/Entry/Legacy/index.js +50 -0
  49. package/dist/Settlement/Entry/Legacy/index.js.map +1 -0
  50. package/dist/Settlement/Entry/Succeeded.d.ts +18 -0
  51. package/dist/Settlement/Entry/Succeeded.js +21 -0
  52. package/dist/Settlement/Entry/Succeeded.js.map +1 -0
  53. package/dist/Settlement/Entry/fromCreatable.d.ts +6 -0
  54. package/dist/Settlement/Entry/fromCreatable.js +34 -0
  55. package/dist/Settlement/Entry/fromCreatable.js.map +1 -0
  56. package/dist/Settlement/Entry/fromLegacy.d.ts +3 -0
  57. package/dist/Settlement/Entry/fromLegacy.js +87 -0
  58. package/dist/Settlement/Entry/fromLegacy.js.map +1 -0
  59. package/dist/Settlement/Entry/index.d.ts +19 -16
  60. package/dist/Settlement/Entry/index.js +13 -45
  61. package/dist/Settlement/Entry/index.js.map +1 -1
  62. package/dist/Settlement/Entry/type.d.ts +3 -0
  63. package/dist/Settlement/Entry/type.js +5 -0
  64. package/dist/Settlement/Entry/type.js.map +1 -0
  65. package/dist/Settlement/index.d.ts +1 -1
  66. package/dist/Settlement/index.js.map +1 -1
  67. package/dist/Transaction/Incoming.d.ts +1 -1
  68. package/dist/Transaction/index.d.ts +1 -1
  69. package/dist/Warning/Settlement/UnknownEntry.d.ts +6 -2
  70. package/dist/Warning/Settlement/UnknownEntry.js +2 -1
  71. package/dist/Warning/Settlement/UnknownEntry.js.map +1 -1
  72. package/package.json +1 -1
  73. package/dist/Settlement/Entry/Cancel.js.map +0 -1
  74. package/dist/Settlement/Entry/Capture.js.map +0 -1
  75. package/dist/Settlement/Entry/Refund.js.map +0 -1
  76. package/dist/Settlement/Entry/Unknown.js.map +0 -1
@@ -17,22 +17,14 @@ export namespace Operation {
17
17
  }
18
18
  }
19
19
  export function fromEntry(entry: Entry): Operation | undefined {
20
- return entry.type == "unknown"
20
+ return entry.status == "failed"
21
21
  ? undefined
22
22
  : {
23
23
  type: "authorization",
24
- id: (entry.type != "refund" ? entry.authorization?.id : entry.transaction?.id) ?? "unknown",
25
- status: Operation.fromEntryStatus(entry.type),
24
+ id: entry.transaction.id,
25
+ status: entry.type == "capture" ? "captured" : "refunded",
26
26
  created: isoly.DateTime.now(),
27
27
  }
28
28
  }
29
- export function fromEntryStatus(status: Exclude<Entry.Type, "unknown">): OperationAuthorization.Status {
30
- const statusConverter: Record<Exclude<Entry.Type, "unknown">, OperationAuthorization.Status> = {
31
- capture: "captured",
32
- cancel: "cancelled",
33
- refund: "refunded",
34
- }
35
- return statusConverter[status]
36
- }
37
29
  export const type = isly.union(Card.type, OperationAuthorization.type)
38
30
  }
@@ -114,7 +114,7 @@ export namespace Changes {
114
114
  }
115
115
  export function fromRefund(
116
116
  settlement: string | undefined, // FIXME: remove | undefined when we're sure we send the id
117
- refund: Settlement.Entry.Refund.Creatable,
117
+ refund: Settlement.Entry.Creatable.Refund,
118
118
  charge: number | undefined,
119
119
  sum: Sum
120
120
  ): Changes {
@@ -36,7 +36,7 @@ export namespace Creatable {
36
36
  changes: Changes.type,
37
37
  type: isly.string(types),
38
38
  })
39
- export function fromRefund(account: string, settlement: string, entry: Settlement.Entry.Refund.Creatable): Creatable {
39
+ export function fromRefund(account: string, settlement: string, entry: Settlement.Entry.Creatable.Refund): Creatable {
40
40
  // The Entry.Refund.Creatable has negative amount and fee
41
41
  // The operation amounts should always be positive
42
42
  const [currency, entryAmount] = entry.amount
@@ -1,15 +1,115 @@
1
1
  import { isly } from "isly"
2
- import { Cancel as EntryCancel } from "./Cancel"
3
- import { Capture as EntryCapture } from "./Capture"
4
- import { Refund as EntryRefund } from "./Refund"
5
- import { Unknown as EntryUnknown } from "./Unknown"
2
+ import { Acquirer } from "../../Acquirer"
3
+ import { Amount } from "../../Amount"
4
+ import { Merchant } from "../../Merchant"
5
+ import { Batch } from "../Batch"
6
+ import { Fee } from "../Fee"
7
+ import { Identifier as SettlementIdentifier } from "../Identifier"
8
+ import { Entry as LegacyEntry } from "./Legacy"
6
9
 
7
- export type Creatable = EntryCancel.Creatable | EntryCapture.Creatable | EntryRefund.Creatable | EntryUnknown.Creatable
10
+ export type Creatable = Creatable.Known | Creatable.Unknown
8
11
  export namespace Creatable {
9
- export const type = isly.union(
10
- EntryCancel.Creatable.type,
11
- EntryCapture.Creatable.type,
12
- EntryRefund.Creatable.type,
13
- EntryUnknown.Creatable.type
14
- )
12
+ export interface Base {
13
+ card: string
14
+ transaction?: string
15
+ account: string
16
+ approvalCode: string
17
+ merchant: Merchant
18
+ acquirer: Acquirer
19
+ reference: string
20
+ batch: Batch
21
+ fee: Fee
22
+ amount: Amount
23
+ settlement: SettlementIdentifier
24
+ }
25
+ export namespace Base {
26
+ export const type = isly.object<Base>({
27
+ card: isly.string(),
28
+ transaction: isly.string().optional(),
29
+ account: isly.string(),
30
+ approvalCode: isly.string(),
31
+ merchant: Merchant.type,
32
+ acquirer: Acquirer.type,
33
+ reference: isly.string(),
34
+ batch: Batch.type,
35
+ fee: Fee.type,
36
+ amount: Amount.type,
37
+ settlement: SettlementIdentifier.type,
38
+ })
39
+ }
40
+ export interface Capture extends Base {
41
+ type: "capture"
42
+ }
43
+ export namespace Capture {
44
+ export const type = Base.type.extend<Capture>({
45
+ type: isly.string("capture"),
46
+ })
47
+ export function fromLegacy(maybeLegacy: Capture | LegacyEntry.Capture.Creatable): Capture {
48
+ return type.is(maybeLegacy)
49
+ ? maybeLegacy
50
+ : {
51
+ type: maybeLegacy.type,
52
+ card: maybeLegacy.authorization.card.id,
53
+ transaction: maybeLegacy.authorization.transaction?.id,
54
+ account: maybeLegacy.authorization.account || "unknown",
55
+ approvalCode: maybeLegacy.authorization.approvalCode ?? "unknown",
56
+ merchant: maybeLegacy.authorization.merchant,
57
+ acquirer: maybeLegacy.authorization.acquirer,
58
+ reference: maybeLegacy.reference,
59
+ batch: maybeLegacy.batch,
60
+ fee: maybeLegacy.fee,
61
+ amount: maybeLegacy.amount,
62
+ settlement: maybeLegacy.settlement ?? "unknown",
63
+ }
64
+ }
65
+ }
66
+ export interface Refund extends Base {
67
+ type: "refund"
68
+ }
69
+ export namespace Refund {
70
+ export const type = Base.type.extend<Refund>({
71
+ type: isly.string("refund"),
72
+ })
73
+ export function fromLegacy(maybeLegacy: Refund | LegacyEntry.Refund.Creatable): Refund {
74
+ return type.is(maybeLegacy)
75
+ ? maybeLegacy
76
+ : {
77
+ type: maybeLegacy.type,
78
+ card: maybeLegacy.card,
79
+ account: maybeLegacy.account ?? "unknown",
80
+ approvalCode: maybeLegacy.authorization.approvalCode ?? "unknown",
81
+ merchant: maybeLegacy.merchant,
82
+ acquirer: maybeLegacy.acquirer,
83
+ reference: maybeLegacy.reference,
84
+ batch: maybeLegacy.batch,
85
+ fee: maybeLegacy.fee,
86
+ amount: maybeLegacy.amount,
87
+ settlement: maybeLegacy.settlement ?? "unknown",
88
+ }
89
+ }
90
+ }
91
+ export type Known = Capture | Refund
92
+
93
+ export namespace Known {
94
+ export const type = isly.union(Capture.type, Refund.type)
95
+ export function fromLegacy(
96
+ maybeLegacy: Creatable.Known | LegacyEntry.Capture.Creatable | LegacyEntry.Refund.Creatable
97
+ ): Creatable.Known {
98
+ return maybeLegacy.type == "capture" ? Capture.fromLegacy(maybeLegacy) : Refund.fromLegacy(maybeLegacy)
99
+ }
100
+ }
101
+ export interface Unknown extends Partial<Base> {
102
+ type: "unknown"
103
+ data: Record<string, unknown>
104
+ }
105
+ export namespace Unknown {
106
+ export const type = isly.object<Unknown>({
107
+ ...(Object.fromEntries(
108
+ Object.entries(Base.type.getProperties()).map(([k, v]) => [k, v.optional()])
109
+ ) as isly.object.Properties<Partial<Base>>), // TODO: Add "Partial" to isly
110
+ type: isly.string("unknown"),
111
+ data: isly.record<Record<string, unknown>>(isly.string(), isly.any()),
112
+ })
113
+ }
114
+ export const type = isly.union(Known.type, Unknown.type)
15
115
  }
@@ -0,0 +1,27 @@
1
+ import { isoly } from "isoly"
2
+ import { isly } from "isly"
3
+ import { Creatable } from "./Creatable"
4
+
5
+ export type Failed = Creatable & {
6
+ status: "failed"
7
+ reason: string
8
+ created: isoly.DateTime
9
+ }
10
+ export namespace Failed {
11
+ export const type = isly.intersection<
12
+ Failed,
13
+ Creatable,
14
+ {
15
+ status: "failed"
16
+ reason: string
17
+ created: isoly.DateTime
18
+ }
19
+ >(
20
+ Creatable.type,
21
+ isly.object({
22
+ status: isly.string<"failed">("failed"),
23
+ reason: isly.string(),
24
+ created: isly.fromIs("isoly.DateTime", isoly.DateTime.is),
25
+ })
26
+ )
27
+ }
@@ -1,10 +1,10 @@
1
1
  import { isoly } from "isoly"
2
2
  import { isly } from "isly"
3
- import { Amount } from "../../Amount"
4
- import { Authorization } from "../../Authorization"
5
- import { Identifier as SettlementIdentifier } from "../../Settlement/Identifier"
6
- import { Batch } from "../Batch"
7
- import { Fee } from "../Fee"
3
+ import { Amount } from "../../../Amount"
4
+ import { Authorization } from "../../../Authorization"
5
+ import { Batch } from "../../Batch"
6
+ import { Fee } from "../../Fee"
7
+ import { Identifier as SettlementIdentifier } from "../../Identifier"
8
8
 
9
9
  export interface Cancel extends Omit<Cancel.Creatable, "settlement"> {
10
10
  status: "succeeded" | "failed"
@@ -1,10 +1,10 @@
1
1
  import { isoly } from "isoly"
2
2
  import { isly } from "isly"
3
- import { Amount } from "../../Amount"
4
- import { Authorization } from "../../Authorization"
5
- import { Identifier as SettlementIdentifier } from "../../Settlement/Identifier"
6
- import { Batch } from "../Batch"
7
- import { Fee } from "../Fee"
3
+ import { Amount } from "../../../Amount"
4
+ import { Authorization } from "../../../Authorization"
5
+ import { Batch } from "../../Batch"
6
+ import { Fee } from "../../Fee"
7
+ import { Identifier as SettlementIdentifier } from "../../Identifier"
8
8
 
9
9
  export interface Capture extends Omit<Capture.Creatable, "settlement"> {
10
10
  status: "succeeded" | "failed"
@@ -0,0 +1,15 @@
1
+ import { isly } from "isly"
2
+ import { Cancel as EntryCancel } from "./Cancel"
3
+ import { Capture as EntryCapture } from "./Capture"
4
+ import { Refund as EntryRefund } from "./Refund"
5
+ import { Unknown as EntryUnknown } from "./Unknown"
6
+
7
+ export type Creatable = EntryCancel.Creatable | EntryCapture.Creatable | EntryRefund.Creatable | EntryUnknown.Creatable
8
+ export namespace Creatable {
9
+ export const type = isly.union(
10
+ EntryCancel.Creatable.type,
11
+ EntryCapture.Creatable.type,
12
+ EntryRefund.Creatable.type,
13
+ EntryUnknown.Creatable.type
14
+ )
15
+ }
@@ -1,13 +1,13 @@
1
1
  import { isoly } from "isoly"
2
2
  import { isly } from "isly"
3
- import { Acquirer } from "../../Acquirer"
4
- import { Amount } from "../../Amount"
5
- import { Authorization } from "../../Authorization"
6
- import { Merchant } from "../../Merchant"
7
- import { Identifier as SettlementIdentifier } from "../../Settlement/Identifier"
8
- import { Transaction } from "../../Transaction"
9
- import { Batch } from "../Batch"
10
- import { Fee } from "../Fee"
3
+ import { Acquirer } from "../../../Acquirer"
4
+ import { Amount } from "../../../Amount"
5
+ import { Authorization } from "../../../Authorization"
6
+ import { Merchant } from "../../../Merchant"
7
+ import { Transaction } from "../../../Transaction"
8
+ import { Batch } from "../../Batch"
9
+ import { Fee } from "../../Fee"
10
+ import { Identifier as SettlementIdentifier } from "../../Identifier"
11
11
 
12
12
  export interface Refund extends Omit<Refund.Creatable, "settlement"> {
13
13
  status: "succeeded" | "failed"
@@ -1,8 +1,8 @@
1
1
  import { isoly } from "isoly"
2
2
  import { isly } from "isly"
3
- import { Authorization } from "../../Authorization"
4
- import { Identifier as SettlementIdentifier } from "../../Settlement/Identifier"
5
- import { Batch } from "../Batch"
3
+ import { Authorization } from "../../../Authorization"
4
+ import { Batch } from "../../Batch"
5
+ import { Identifier as SettlementIdentifier } from "../../Identifier"
6
6
 
7
7
  export interface Unknown extends Omit<Unknown.Creatable, "settlement"> {
8
8
  status: "succeeded" | "failed"
@@ -0,0 +1,55 @@
1
+ import { gracely } from "gracely"
2
+ import { isoly } from "isoly"
3
+ import { isly } from "isly"
4
+ import { Transaction } from "../../../Transaction"
5
+ import { Cancel as EntryCancel } from "./Cancel"
6
+ import { Capture as EntryCapture } from "./Capture"
7
+ import { Creatable as EntryCreatable } from "./Creatable"
8
+ import { Refund as EntryRefund } from "./Refund"
9
+ import { Unknown as EntryUnknown } from "./Unknown"
10
+
11
+ export type Entry = Entry.Cancel | Entry.Capture | Entry.Refund | Entry.Unknown
12
+ export namespace Entry {
13
+ export import Cancel = EntryCancel
14
+ export import Capture = EntryCapture
15
+ export import Refund = EntryRefund
16
+ export import Unknown = EntryUnknown
17
+ export type Type = "unknown" | "refund" | "capture" | "cancel"
18
+ export import Creatable = EntryCreatable
19
+ export function from(creatable: Entry.Creatable, transaction: Transaction | gracely.Error | string): Entry {
20
+ let result: Entry
21
+ const created = isoly.DateTime.now()
22
+ if (!Transaction.type.is(transaction) || transaction.status != "finalized")
23
+ result = { status: "failed", reason: reason(creatable, transaction), ...creatable, created }
24
+ else
25
+ switch (creatable.type) {
26
+ case "capture":
27
+ result = Capture.from(creatable)
28
+ break
29
+ case "refund":
30
+ result = Refund.from(creatable, transaction)
31
+ break
32
+ default:
33
+ result = { ...creatable, status: "failed", reason: "Entry type not implemented yet.", created }
34
+ break
35
+ }
36
+ return result
37
+ }
38
+ function reason(creatable: Entry.Creatable, transaction: Transaction | gracely.Error | string): string {
39
+ const result: string[] = []
40
+ !creatable.authorization && result.push("Missing authorization.")
41
+ if (gracely.Error.is(transaction))
42
+ result.push(`gracely error: ${JSON.stringify(transaction)}`)
43
+ else if (typeof transaction != "string")
44
+ result.push(`Transaction ${transaction.id} on account ${transaction.accountId} unable to be finalized.`)
45
+ else
46
+ result.push(transaction || "No reason provided")
47
+ return result.join("\n")
48
+ }
49
+ export const type = isly.union<Entry, Entry.Cancel, Entry.Capture, Entry.Refund, Entry.Unknown>(
50
+ Entry.Cancel.type,
51
+ Entry.Capture.type,
52
+ Entry.Refund.type,
53
+ Entry.Unknown.type
54
+ )
55
+ }
@@ -0,0 +1,26 @@
1
+ import { isoly } from "isoly"
2
+ import { isly } from "isly"
3
+ import { Rail } from "../../Rail"
4
+ import { Creatable } from "./Creatable"
5
+
6
+ export interface Succeeded extends Omit<Creatable.Known, "transaction" | "card"> {
7
+ status: "succeeded"
8
+ card: Rail.Address.Card
9
+ transaction: { id: string; posted: isoly.DateTime; description: string }
10
+ created: isoly.DateTime
11
+ }
12
+ export namespace Succeeded {
13
+ export const type = Creatable.Base.type
14
+ .omit<"transaction" | "card">(["card", "transaction"])
15
+ .extend<Succeeded & { type: "capture" | "refund" }>({
16
+ type: isly.string(["capture", "refund"]),
17
+ status: isly.string<"succeeded">("succeeded"),
18
+ card: Rail.Address.Card.type,
19
+ transaction: isly.object({
20
+ id: isly.string(),
21
+ posted: isly.fromIs("isoly.DateTime", isoly.DateTime.is),
22
+ description: isly.string(),
23
+ }),
24
+ created: isly.fromIs("isoly.DateTime", isoly.DateTime.is),
25
+ })
26
+ }
@@ -0,0 +1,42 @@
1
+ import { gracely } from "gracely"
2
+ import { isoly } from "isoly"
3
+ import { Card } from "../../Card"
4
+ import { Rail } from "../../Rail"
5
+ import { Transaction } from "../../Transaction"
6
+ import type { Entry } from "."
7
+ import { Creatable } from "./Creatable"
8
+
9
+ export function fromCreatable(
10
+ creatable: Creatable,
11
+ transaction: Transaction.CardTransaction | gracely.Error | string,
12
+ card?: Card
13
+ ): Entry {
14
+ let result: Entry
15
+ const reasons: string[] = []
16
+ const created = isoly.DateTime.now()
17
+ if (creatable.type == "unknown")
18
+ reasons.push("Unknown entry type.")
19
+ if (gracely.Error.is(transaction))
20
+ reasons.push(`gracely error: ${JSON.stringify(transaction)}`)
21
+ else if (typeof transaction == "string")
22
+ reasons.push(transaction || "No reason provided.")
23
+ else if (transaction.status != "finalized")
24
+ reasons.push(`Transaction ${transaction.id} on account ${transaction.accountId} unable to be finalized.`)
25
+ if (reasons.length > 0)
26
+ result = { status: "failed", reason: reasons.join("\n"), ...creatable, created }
27
+ else if (!card)
28
+ result = { status: "failed", reason: "Missing card", ...creatable, created }
29
+ else
30
+ result = {
31
+ status: "succeeded",
32
+ ...(creatable as Creatable.Known),
33
+ created,
34
+ card: Rail.Address.Card.from(card),
35
+ transaction: {
36
+ id: (transaction as Transaction.CardTransaction).id,
37
+ posted: (transaction as Transaction.CardTransaction).posted,
38
+ description: (transaction as Transaction.CardTransaction).description,
39
+ },
40
+ }
41
+ return result
42
+ }
@@ -0,0 +1,95 @@
1
+ import { isoly } from "isoly"
2
+ import { Card } from "../../Card"
3
+ import { Rail } from "../../Rail"
4
+ import { Identifier as SettlementIdentifier } from "../Identifier"
5
+ import type { Entry } from "."
6
+ import type { Failed } from "./Failed"
7
+ import { Entry as LegacyEntry } from "./Legacy"
8
+ import { type } from "./type"
9
+
10
+ function toFailed(legacy: LegacyEntry.Unknown | LegacyEntry.Cancel): Failed {
11
+ return {
12
+ type: "unknown",
13
+ status: "failed",
14
+ data: legacy.type == "cancel" ? legacy : legacy.data,
15
+ reason: legacy.reason ?? "unknown",
16
+ created: legacy.created ?? isoly.DateTime.now(),
17
+ settlement: legacy.settlement ?? "unknown",
18
+ reference: ("reference" in legacy && legacy.reference) || "unknown",
19
+ batch: legacy.batch,
20
+ ...("authorization" in legacy &&
21
+ legacy.authorization && {
22
+ account: legacy.authorization.account ?? "unknown",
23
+ approvalCode: legacy.authorization.approvalCode,
24
+ merchant: legacy.authorization.merchant,
25
+ acquirer: legacy.authorization.acquirer,
26
+ card: legacy.authorization.card.id,
27
+ transaction: legacy.authorization.transaction?.id,
28
+ }),
29
+ ...("fee" in legacy && { fee: legacy.fee }),
30
+ ...("amount" in legacy && { amount: legacy.amount }),
31
+ }
32
+ }
33
+ function toEntry(legacy: LegacyEntry.Capture | LegacyEntry.Refund, card?: Rail.Address.Card): Entry {
34
+ return {
35
+ type: legacy.type,
36
+ created: legacy.created ?? isoly.DateTime.now(),
37
+ ...(legacy.status == "failed"
38
+ ? {
39
+ card: legacy.type == "refund" ? legacy.card : legacy.authorization.card.id,
40
+ status: "failed",
41
+ reason: legacy.reason ?? "unknown",
42
+ transaction:
43
+ ("transaction" in legacy
44
+ ? legacy.transaction?.id
45
+ : "authorization" in legacy && "transaction" in legacy.authorization
46
+ ? legacy.authorization.transaction?.id
47
+ : "unknown") ?? "unknown",
48
+ }
49
+ : {
50
+ card: card ?? {
51
+ type: "card",
52
+ scheme: (stack => (stack ? Card.Stack.toScheme(stack) : "unknown"))(
53
+ legacy.settlement && SettlementIdentifier.toProcessor(legacy.settlement)
54
+ ) as Card.Scheme,
55
+ ...(legacy.type == "refund"
56
+ ? { id: legacy.card, iin: "unknown", last4: "unknown" }
57
+ : {
58
+ id: legacy.authorization.card.id,
59
+ iin: legacy.authorization.card.iin ?? "unknown",
60
+ last4: legacy.authorization.card.last4 ?? "unknown",
61
+ }),
62
+ expiry: [0, 0] as unknown as Card.Expiry,
63
+ holder: "unknown",
64
+ },
65
+ status: "succeeded",
66
+ transaction: ("transaction" in legacy && legacy.transaction
67
+ ? {
68
+ id: legacy.transaction.id,
69
+ posted: legacy.transaction.posted,
70
+ description: legacy.transaction.description,
71
+ }
72
+ : "authorization" in legacy && "transaction" in legacy.authorization
73
+ ? legacy.authorization.transaction
74
+ : undefined) ?? { id: "unknown", posted: "", description: "" },
75
+ }),
76
+ account: legacy.account ?? (("transaction" in legacy && legacy.transaction?.accountId) || "unknown"),
77
+ approvalCode: legacy.authorization.approvalCode ?? "unknown",
78
+ ...(legacy.type == "refund"
79
+ ? { merchant: legacy.merchant, acquirer: legacy.acquirer }
80
+ : { merchant: legacy.authorization.merchant, acquirer: legacy.authorization.acquirer }),
81
+ reference: legacy.reference,
82
+ batch: legacy.batch,
83
+ fee: legacy.fee,
84
+ amount: legacy.amount,
85
+ settlement: legacy.settlement ?? "unknown",
86
+ }
87
+ }
88
+
89
+ export function fromLegacy(maybeLegacy: LegacyEntry | Entry): Entry {
90
+ return type.is(maybeLegacy)
91
+ ? maybeLegacy
92
+ : maybeLegacy.type == "cancel" || maybeLegacy.type == "unknown"
93
+ ? toFailed(maybeLegacy)
94
+ : toEntry(maybeLegacy)
95
+ }
@@ -1,57 +1,26 @@
1
- import { gracely } from "gracely"
2
- import { isoly } from "isoly"
3
- import { isly } from "isly"
4
- import { Transaction } from "../../Transaction"
5
- import { Cancel as EntryCancel } from "./Cancel"
6
- import { Capture as EntryCapture } from "./Capture"
7
1
  import { Creatable as EntryCreatable } from "./Creatable"
8
- import { Refund as EntryRefund } from "./Refund"
2
+ import { Failed as EntryFailed } from "./Failed"
3
+ import { fromCreatable } from "./fromCreatable"
4
+ import { fromLegacy as entryFromLegacy } from "./fromLegacy"
5
+ import { Entry as LegacyEntry } from "./Legacy"
6
+ import { Succeeded as EntrySucceeded } from "./Succeeded"
9
7
  import { Summary as EntrySummary } from "./Summary"
10
- import { Unknown as EntryUnknown } from "./Unknown"
8
+ import { type as entryType } from "./type"
11
9
 
12
- export type Entry = Entry.Cancel | Entry.Capture | Entry.Refund | Entry.Unknown
10
+ export type Entry = EntrySucceeded | EntryFailed
13
11
  export namespace Entry {
14
- export import Cancel = EntryCancel
15
- export import Capture = EntryCapture
16
- export import Refund = EntryRefund
17
- export import Unknown = EntryUnknown
18
- export type Type = "unknown" | "refund" | "capture" | "cancel"
19
- export import Summary = EntrySummary
20
- export import Creatable = EntryCreatable
21
- export function from(creatable: Entry.Creatable, transaction: Transaction | gracely.Error | string): Entry {
22
- let result: Entry
23
- const created = isoly.DateTime.now()
24
- if (!Transaction.type.is(transaction) || transaction.status != "finalized")
25
- result = { status: "failed", reason: reason(creatable, transaction), ...creatable, created }
26
- else
27
- switch (creatable.type) {
28
- case "capture":
29
- result = Capture.from(creatable)
30
- break
31
- case "refund":
32
- result = Refund.from(creatable, transaction)
33
- break
34
- default:
35
- result = { ...creatable, status: "failed", reason: "Entry type not implemented yet.", created }
36
- break
37
- }
38
- return result
12
+ export interface Capture extends EntrySucceeded {
13
+ type: "capture"
39
14
  }
40
- function reason(creatable: Entry.Creatable, transaction: Transaction | gracely.Error | string): string {
41
- const result = []
42
- !creatable.authorization && result.push("Missing authorization.")
43
- if (gracely.Error.is(transaction))
44
- result.push(`gracely error: ${JSON.stringify(transaction)}`)
45
- else if (typeof transaction != "string")
46
- result.push(`Transaction ${transaction.id} on account ${transaction.accountId} unable to be finalized.`)
47
- else
48
- result.push(transaction || "No reason provided")
49
- return result.join("\n")
15
+ export interface Refund extends EntrySucceeded {
16
+ type: "refund"
50
17
  }
51
- export const type = isly.union<Entry, Entry.Cancel, Entry.Capture, Entry.Refund, Entry.Unknown>(
52
- Entry.Cancel.type,
53
- Entry.Capture.type,
54
- Entry.Refund.type,
55
- Entry.Unknown.type
56
- )
18
+ export import Creatable = EntryCreatable
19
+ export import Failed = EntryFailed
20
+ export import Succeeded = EntrySucceeded
21
+ export import Summary = EntrySummary
22
+ export import Legacy = LegacyEntry
23
+ export const type = entryType
24
+ export const fromLegacy = entryFromLegacy
25
+ export const from = fromCreatable
57
26
  }
@@ -0,0 +1,6 @@
1
+ import { isly } from "isly"
2
+ import type { Entry } from "."
3
+ import { Failed } from "./Failed"
4
+ import { Succeeded } from "./Succeeded"
5
+
6
+ export const type = isly.union<Entry, Succeeded, Failed>(Succeeded.type, Failed.type)
@@ -40,7 +40,7 @@ export namespace Settlement {
40
40
  entries: { count: 0 },
41
41
  }
42
42
  }
43
- export function compile(settlement: Settlement, entries: Settlement.Entry[]): Settlement {
43
+ export function compile(settlement: Settlement, entries: (Settlement.Entry | Settlement.Entry.Failed)[]): Settlement {
44
44
  const result = { ...settlement }
45
45
  for (const entry of entries) {
46
46
  switch (entry.status) {
@@ -26,7 +26,7 @@ export namespace Incoming {
26
26
  reference: TransactionReference.type.optional(),
27
27
  })
28
28
 
29
- export function fromRefund(entry: Settlement.Entry.Refund.Creatable, card: Rail.Address.Card): Incoming {
29
+ export function fromRefund(entry: Settlement.Entry.Creatable.Refund, card: Rail.Address.Card): Incoming {
30
30
  const [currency, amount] = entry.amount
31
31
  return {
32
32
  account: card,
@@ -335,7 +335,7 @@ export namespace Transaction {
335
335
  }
336
336
  }
337
337
  export function fromRefund(
338
- refund: Settlement.Entry.Refund.Creatable,
338
+ refund: Settlement.Entry.Creatable.Refund,
339
339
  id: string,
340
340
  account: { id: string; name: string; organization: string },
341
341
  card: Rail.Address.Card,
@@ -2,14 +2,16 @@ import { cryptly } from "cryptly"
2
2
  import { isoly } from "isoly"
3
3
  import { isly } from "isly"
4
4
  import { Authorization } from "../../Authorization"
5
- import { Unknown } from "../../Settlement/Entry/Unknown"
5
+ import { Entry } from "../../Settlement/Entry"
6
6
  import { Identifier } from "../../Settlement/Identifier"
7
+ import { Transaction } from "../../Transaction"
7
8
  import { Base } from "../Base"
8
9
 
9
10
  export interface UnknownEntry extends Base {
10
11
  type: "unknown-entry"
11
12
  resource: Identifier
12
13
  authorization?: Authorization["id"]
14
+ transaction?: Transaction["id"]
13
15
  }
14
16
 
15
17
  export namespace UnknownEntry {
@@ -17,12 +19,13 @@ export namespace UnknownEntry {
17
19
  type: isly.string("unknown-entry"),
18
20
  resource: Identifier.type,
19
21
  authorization: isly.fromIs("Authorization.id", cryptly.Identifier.is).optional(),
22
+ transaction: isly.string().optional(),
20
23
  })
21
- export function create(entry: Unknown, resource: Identifier): UnknownEntry {
24
+ export function create(entry: Extract<Entry.Failed, { type: "unknown" }>, resource: Identifier): UnknownEntry {
22
25
  return {
23
26
  type: "unknown-entry",
24
27
  resource,
25
- authorization: entry.authorization?.id,
28
+ transaction: entry.transaction,
26
29
  date: isoly.Date.now(),
27
30
  }
28
31
  }