/
Transaction.swift
132 lines (122 loc) · 4.89 KB
/
Transaction.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//
// Transaction.swift
// OmiseGO
//
// Created by Mederic Petit on 23/2/18.
// Copyright © 2017-2018 Omise Go Pte. Ltd. All rights reserved.
//
/// The status of a transaction
///
/// - pending: The transaction is pending
/// - confirmed: The transaction is confirmed
/// - failed: The transaction failed
public enum TransactionStatus: String, Decodable {
case pending
case confirmed
case failed
}
/// Represents a transaction
public struct Transaction {
/// The unique identifier of the transaction
public let id: String
/// The status of the transaction (pending, confirmed or failed)
public let status: TransactionConsumptionStatus
/// The source reprensenting the source of the funds
public let from: TransactionSource
/// The source representing the destination of the funds
public let to: TransactionSource
/// Contains info of the exchange made during the transaction (if any)
public let exchange: TransactionExchange
/// Additional metadata for the consumption
public let metadata: [String: Any]
/// Additional encrypted metadata for the consumption
public let encryptedMetadata: [String: Any]
/// The creation date of the transaction
public let createdAt: Date
/// An error code if the transaction encountered an error
public let errorCode: String?
/// The description of the encountered error
public let errorDescription: String?
}
extension Transaction: Decodable {
private enum CodingKeys: String, CodingKey {
case id
case status
case from
case to
case exchange
case metadata
case encryptedMetadata = "encrypted_metadata"
case createdAt = "created_at"
case errorCode = "error_code"
case errorDescription = "error_description"
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(String.self, forKey: .id)
status = try container.decode(TransactionConsumptionStatus.self, forKey: .status)
from = try container.decode(TransactionSource.self, forKey: .from)
to = try container.decode(TransactionSource.self, forKey: .to)
exchange = try container.decode(TransactionExchange.self, forKey: .exchange)
createdAt = try container.decode(Date.self, forKey: .createdAt)
metadata = try container.decode([String: Any].self, forKey: .metadata)
encryptedMetadata = try container.decode([String: Any].self, forKey: .encryptedMetadata)
errorCode = try container.decodeIfPresent(String.self, forKey: .errorCode)
errorDescription = try container.decodeIfPresent(String.self, forKey: .errorDescription)
}
}
extension Transaction: Retrievable {
@discardableResult
/// Create a new transaction
///
/// - Parameters:
/// - client: An API client.
/// This client need to be initialized with a ClientConfiguration struct before being used.
/// - params: The TransactionSendParams object to customize the transaction
/// - callback: The closure called when the request is completed
/// - Returns: An optional cancellable request.
public static func create(using client: HTTPClient,
params: TransactionCreateParams,
callback: @escaping Transaction.RetrieveRequestCallback) -> Transaction.RetrieveRequest? {
return self.retrieve(using: client, endpoint: .createTransaction(params: params), callback: callback)
}
}
extension Transaction: PaginatedListable {
@discardableResult
/// Get a paginated list of transaction for the current user
///
/// - Parameters:
/// - client: An API client.
/// This client need to be initialized with a ClientConfiguration struct before being used.
/// - params: The TransactionListParams object to use to scope the results
/// - callback: The closure called when the request is completed
/// - Returns: An optional cancellable request.
public static func list(using client: HTTPClient,
params: TransactionListParams,
callback: @escaping Transaction.ListRequestCallback) -> Transaction.ListRequest? {
return self.list(using: client, endpoint: .getTransactions(params: params), callback: callback)
}
}
extension Transaction: Paginable {
public enum SearchableFields: String, KeyEncodable {
case id
case status
case from
case to
}
public enum SortableFields: String, KeyEncodable {
case id
case status
case from
case to
case createdAt = "created_at"
}
}
extension Transaction: Hashable {
public var hashValue: Int {
return self.id.hashValue
}
public static func == (lhs: Transaction, rhs: Transaction) -> Bool {
return lhs.id == rhs.id
}
}