1 import { Table } from "docs-ui"
2
3 export const metadata = {
4 title: `Transactions`,
5 }
6
7 # {metadata.title}
8
9 In this guide, you’ll learn about order transactions, how to check an order’s outstanding amount, and how transactions relate to payments and refunds.
10
11 ## What is a Transaction?
12
13 A transaction represents any order payment process, such as capturing or refunding an amount. It’s represented by the [OrderTransaction data model](/references/order/models/OrderTransaction).
14
15 The transaction’s main purpose is to ensure a correct balance between paid and outstanding amounts.
16
17 Transactions are also associated with returns, claims, and exchanges when additional payment or refunds are required.
18
19 ---
20
21 ## Checking Outstanding Amount
22
23 The order’s total amounts are stored in the `OrderSummary`'s `totals` property, which is a JSON object holding the total details of the order.
24
25 ```json
26 {
27 "totals": {
28 "total": 30,
29 "subtotal": 30,
30 // ...
31 }
32 }
33 ```
34
35 To check the outstanding amount of an order, the transaction amounts from the `Transaction` records are summed. Then, the following conditions are evaluated:
36
37 <Table>
38 <Table.Header>
39 <Table.Row>
40 <Table.HeaderCell>
41 Condition
42 </Table.HeaderCell>
43 <Table.HeaderCell>
44 Result
45 </Table.HeaderCell>
46 </Table.Row>
47 </Table.Header>
48 <Table.Body>
49 <Table.Row>
50 <Table.Cell>
51
52 summary’s total - transaction amounts total = 0
53
54 </Table.Cell>
55 <Table.Cell>
56
57 There’s no outstanding amount.
58
59 </Table.Cell>
60 </Table.Row>
61 <Table.Row>
62 <Table.Cell>
63
64 summary’s total - transaction amounts total > 0
65
66 </Table.Cell>
67 <Table.Cell>
68
69 The customer owes additional payment to the merchant.
70
71 </Table.Cell>
72 </Table.Row>
73 <Table.Row>
74 <Table.Cell>
75
76 summary’s total - transaction amounts total < 0
77
78 </Table.Cell>
79 <Table.Cell>
80
81 The merchant owes the customer a refund.
82
83 </Table.Cell>
84 </Table.Row>
85 </Table.Body>
86 </Table>
87
88 ---
89
90 ## Transaction Reference
91
92 The Order Module doesn’t provide payment processing functionalities, so it doesn’t store payments that can be processed. Payment functionalities are provided by the [Payment Module](../../payment/page.mdx).
93
94 The `OrderTransaction` data model has two properties that determine which data model and record holds the actual payment’s details:
95
96 - `reference`: indicates the table’s name in the database. For example, `capture` from the [Payment Module](../../payment/page.mdx).
97 - `reference_id`: indicates the ID of the record in the table. For example, `capt_123`.
98
99 ### Refund Transactions
100
101 When a refund is created for an order, an `OrderTransaction` record is created with its `reference` property set to `refund`. The `reference_id` property is set to the ID of the refund record in the [Payment Module](../../payment/page.mdx).
102
103 The transaction's `amount` for refunds will be a negative value, indicating that the merchant owes the customer a refund.
104
105 #### Example: Retrieve Order's Refunds
106
107 To retrieve an order's refunds, you can retrieve the order's transactions using [Query](!docs!/learn/fundamentals/query) and filter the transactions by their `reference` property.
108
109 For example:
110
111 ```ts
112 const query = container.resolve(
113 ContainerRegistrationKeys.QUERY
114 ) // or req.scope.resolve("query")
115
116 const { data: [order] } = await query.graph({
117 entity: "order",
118 fields: ["*", "transactions.*"],
119 filters: {
120 id: "order_123",
121 },
122 })
123
124 const refundTransactions = order.transactions?.filter(
125 (t) => t?.reference === "refund"
126 )
127
128 // calculate total refunded amount as a positive value
129 const totalRefunded = refundTransactions?.reduce((acc, t) => acc + Math.abs(t!.amount || 0), 0)
130 ```