-
Notifications
You must be signed in to change notification settings - Fork 2
/
paymentorderreversal.go
348 lines (312 loc) · 16.6 KB
/
paymentorderreversal.go
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package moderntreasury
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
"github.com/Modern-Treasury/modern-treasury-go/v2/internal/apijson"
"github.com/Modern-Treasury/modern-treasury-go/v2/internal/apiquery"
"github.com/Modern-Treasury/modern-treasury-go/v2/internal/param"
"github.com/Modern-Treasury/modern-treasury-go/v2/internal/requestconfig"
"github.com/Modern-Treasury/modern-treasury-go/v2/option"
"github.com/Modern-Treasury/modern-treasury-go/v2/packages/pagination"
"github.com/Modern-Treasury/modern-treasury-go/v2/shared"
)
// PaymentOrderReversalService contains methods and other services that help with
// interacting with the Modern Treasury API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewPaymentOrderReversalService] method instead.
type PaymentOrderReversalService struct {
Options []option.RequestOption
}
// NewPaymentOrderReversalService generates a new service that applies the given
// options to each request. These options are applied after the parent client's
// options (if there is one), and before any request-specific options.
func NewPaymentOrderReversalService(opts ...option.RequestOption) (r *PaymentOrderReversalService) {
r = &PaymentOrderReversalService{}
r.Options = opts
return
}
// Create a reversal for a payment order.
func (r *PaymentOrderReversalService) New(ctx context.Context, paymentOrderID string, body PaymentOrderReversalNewParams, opts ...option.RequestOption) (res *Reversal, err error) {
opts = append(r.Options[:], opts...)
if paymentOrderID == "" {
err = errors.New("missing required payment_order_id parameter")
return
}
path := fmt.Sprintf("api/payment_orders/%s/reversals", paymentOrderID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Get details on a single reversal of a payment order.
func (r *PaymentOrderReversalService) Get(ctx context.Context, paymentOrderID string, reversalID string, opts ...option.RequestOption) (res *Reversal, err error) {
opts = append(r.Options[:], opts...)
if paymentOrderID == "" {
err = errors.New("missing required payment_order_id parameter")
return
}
if reversalID == "" {
err = errors.New("missing required reversal_id parameter")
return
}
path := fmt.Sprintf("api/payment_orders/%s/reversals/%s", paymentOrderID, reversalID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return
}
// Get a list of all reversals of a payment order.
func (r *PaymentOrderReversalService) List(ctx context.Context, paymentOrderID string, query PaymentOrderReversalListParams, opts ...option.RequestOption) (res *pagination.Page[Reversal], err error) {
var raw *http.Response
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
if paymentOrderID == "" {
err = errors.New("missing required payment_order_id parameter")
return
}
path := fmt.Sprintf("api/payment_orders/%s/reversals", paymentOrderID)
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}
// Get a list of all reversals of a payment order.
func (r *PaymentOrderReversalService) ListAutoPaging(ctx context.Context, paymentOrderID string, query PaymentOrderReversalListParams, opts ...option.RequestOption) *pagination.PageAutoPager[Reversal] {
return pagination.NewPageAutoPager(r.List(ctx, paymentOrderID, query, opts...))
}
type Reversal struct {
ID string `json:"id,required" format:"uuid"`
CreatedAt time.Time `json:"created_at,required" format:"date-time"`
// The ID of the ledger transaction linked to the reversal.
LedgerTransactionID string `json:"ledger_transaction_id,required,nullable" format:"uuid"`
// This field will be true if this object exists in the live environment or false
// if it exists in the test environment.
LiveMode bool `json:"live_mode,required"`
// Additional data represented as key-value pairs. Both the key and value must be
// strings.
Metadata map[string]string `json:"metadata,required"`
Object string `json:"object,required"`
// The ID of the relevant Payment Order.
PaymentOrderID string `json:"payment_order_id,required,nullable" format:"uuid"`
// The reason for the reversal.
Reason ReversalReason `json:"reason,required"`
// The current status of the reversal.
Status ReversalStatus `json:"status,required"`
UpdatedAt time.Time `json:"updated_at,required" format:"date-time"`
JSON reversalJSON `json:"-"`
}
// reversalJSON contains the JSON metadata for the struct [Reversal]
type reversalJSON struct {
ID apijson.Field
CreatedAt apijson.Field
LedgerTransactionID apijson.Field
LiveMode apijson.Field
Metadata apijson.Field
Object apijson.Field
PaymentOrderID apijson.Field
Reason apijson.Field
Status apijson.Field
UpdatedAt apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *Reversal) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r reversalJSON) RawJSON() string {
return r.raw
}
// The reason for the reversal.
type ReversalReason string
const (
ReversalReasonDuplicate ReversalReason = "duplicate"
ReversalReasonIncorrectAmount ReversalReason = "incorrect_amount"
ReversalReasonIncorrectReceivingAccount ReversalReason = "incorrect_receiving_account"
ReversalReasonDateEarlierThanIntended ReversalReason = "date_earlier_than_intended"
ReversalReasonDateLaterThanIntended ReversalReason = "date_later_than_intended"
)
func (r ReversalReason) IsKnown() bool {
switch r {
case ReversalReasonDuplicate, ReversalReasonIncorrectAmount, ReversalReasonIncorrectReceivingAccount, ReversalReasonDateEarlierThanIntended, ReversalReasonDateLaterThanIntended:
return true
}
return false
}
// The current status of the reversal.
type ReversalStatus string
const (
ReversalStatusCompleted ReversalStatus = "completed"
ReversalStatusFailed ReversalStatus = "failed"
ReversalStatusPending ReversalStatus = "pending"
ReversalStatusProcessing ReversalStatus = "processing"
ReversalStatusReturned ReversalStatus = "returned"
ReversalStatusSent ReversalStatus = "sent"
)
func (r ReversalStatus) IsKnown() bool {
switch r {
case ReversalStatusCompleted, ReversalStatusFailed, ReversalStatusPending, ReversalStatusProcessing, ReversalStatusReturned, ReversalStatusSent:
return true
}
return false
}
type PaymentOrderReversalNewParams struct {
// The reason for the reversal. Must be one of `duplicate`, `incorrect_amount`,
// `incorrect_receiving_account`, `date_earlier_than_intended`,
// `date_later_than_intended`.
Reason param.Field[PaymentOrderReversalNewParamsReason] `json:"reason,required"`
// Specifies a ledger transaction object that will be created with the reversal. If
// the ledger transaction cannot be created, then the reversal creation will fail.
// The resulting ledger transaction will mirror the status of the reversal.
LedgerTransaction param.Field[PaymentOrderReversalNewParamsLedgerTransaction] `json:"ledger_transaction"`
// Additional data represented as key-value pairs. Both the key and value must be
// strings.
Metadata param.Field[map[string]string] `json:"metadata"`
}
func (r PaymentOrderReversalNewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// The reason for the reversal. Must be one of `duplicate`, `incorrect_amount`,
// `incorrect_receiving_account`, `date_earlier_than_intended`,
// `date_later_than_intended`.
type PaymentOrderReversalNewParamsReason string
const (
PaymentOrderReversalNewParamsReasonDuplicate PaymentOrderReversalNewParamsReason = "duplicate"
PaymentOrderReversalNewParamsReasonIncorrectAmount PaymentOrderReversalNewParamsReason = "incorrect_amount"
PaymentOrderReversalNewParamsReasonIncorrectReceivingAccount PaymentOrderReversalNewParamsReason = "incorrect_receiving_account"
PaymentOrderReversalNewParamsReasonDateEarlierThanIntended PaymentOrderReversalNewParamsReason = "date_earlier_than_intended"
PaymentOrderReversalNewParamsReasonDateLaterThanIntended PaymentOrderReversalNewParamsReason = "date_later_than_intended"
)
func (r PaymentOrderReversalNewParamsReason) IsKnown() bool {
switch r {
case PaymentOrderReversalNewParamsReasonDuplicate, PaymentOrderReversalNewParamsReasonIncorrectAmount, PaymentOrderReversalNewParamsReasonIncorrectReceivingAccount, PaymentOrderReversalNewParamsReasonDateEarlierThanIntended, PaymentOrderReversalNewParamsReasonDateLaterThanIntended:
return true
}
return false
}
// Specifies a ledger transaction object that will be created with the reversal. If
// the ledger transaction cannot be created, then the reversal creation will fail.
// The resulting ledger transaction will mirror the status of the reversal.
type PaymentOrderReversalNewParamsLedgerTransaction struct {
// An array of ledger entry objects.
LedgerEntries param.Field[[]PaymentOrderReversalNewParamsLedgerTransactionLedgerEntry] `json:"ledger_entries,required"`
// An optional description for internal use.
Description param.Field[string] `json:"description"`
// The timestamp (ISO8601 format) at which the ledger transaction happened for
// reporting purposes.
EffectiveAt param.Field[time.Time] `json:"effective_at" format:"date-time"`
// The date (YYYY-MM-DD) on which the ledger transaction happened for reporting
// purposes.
EffectiveDate param.Field[time.Time] `json:"effective_date" format:"date"`
// A unique string to represent the ledger transaction. Only one pending or posted
// ledger transaction may have this ID in the ledger.
ExternalID param.Field[string] `json:"external_id"`
// If the ledger transaction can be reconciled to another object in Modern
// Treasury, the id will be populated here, otherwise null.
LedgerableID param.Field[string] `json:"ledgerable_id" format:"uuid"`
// If the ledger transaction can be reconciled to another object in Modern
// Treasury, the type will be populated here, otherwise null. This can be one of
// payment_order, incoming_payment_detail, expected_payment, return, paper_item, or
// reversal.
LedgerableType param.Field[PaymentOrderReversalNewParamsLedgerTransactionLedgerableType] `json:"ledgerable_type"`
// Additional data represented as key-value pairs. Both the key and value must be
// strings.
Metadata param.Field[map[string]string] `json:"metadata"`
// To post a ledger transaction at creation, use `posted`.
Status param.Field[PaymentOrderReversalNewParamsLedgerTransactionStatus] `json:"status"`
}
func (r PaymentOrderReversalNewParamsLedgerTransaction) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type PaymentOrderReversalNewParamsLedgerTransactionLedgerEntry struct {
// Value in specified currency's smallest unit. e.g. $10 would be represented
// as 1000. Can be any integer up to 36 digits.
Amount param.Field[int64] `json:"amount,required"`
// One of `credit`, `debit`. Describes the direction money is flowing in the
// transaction. A `credit` moves money from your account to someone else's. A
// `debit` pulls money from someone else's account to your own. Note that wire,
// rtp, and check payments will always be `credit`.
Direction param.Field[shared.TransactionDirection] `json:"direction,required"`
// The ledger account that this ledger entry is associated with.
LedgerAccountID param.Field[string] `json:"ledger_account_id,required" format:"uuid"`
// Use `gt` (>), `gte` (>=), `lt` (<), `lte` (<=), or `eq` (=) to lock on the
// account’s available balance. If any of these conditions would be false after the
// transaction is created, the entire call will fail with error code 422.
AvailableBalanceAmount param.Field[map[string]int64] `json:"available_balance_amount"`
// Lock version of the ledger account. This can be passed when creating a ledger
// transaction to only succeed if no ledger transactions have posted since the
// given version. See our post about Designing the Ledgers API with Optimistic
// Locking for more details.
LockVersion param.Field[int64] `json:"lock_version"`
// Additional data represented as key-value pairs. Both the key and value must be
// strings.
Metadata param.Field[map[string]string] `json:"metadata"`
// Use `gt` (>), `gte` (>=), `lt` (<), `lte` (<=), or `eq` (=) to lock on the
// account’s pending balance. If any of these conditions would be false after the
// transaction is created, the entire call will fail with error code 422.
PendingBalanceAmount param.Field[map[string]int64] `json:"pending_balance_amount"`
// Use `gt` (>), `gte` (>=), `lt` (<), `lte` (<=), or `eq` (=) to lock on the
// account’s posted balance. If any of these conditions would be false after the
// transaction is created, the entire call will fail with error code 422.
PostedBalanceAmount param.Field[map[string]int64] `json:"posted_balance_amount"`
// If true, response will include the balance of the associated ledger account for
// the entry.
ShowResultingLedgerAccountBalances param.Field[bool] `json:"show_resulting_ledger_account_balances"`
}
func (r PaymentOrderReversalNewParamsLedgerTransactionLedgerEntry) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// If the ledger transaction can be reconciled to another object in Modern
// Treasury, the type will be populated here, otherwise null. This can be one of
// payment_order, incoming_payment_detail, expected_payment, return, paper_item, or
// reversal.
type PaymentOrderReversalNewParamsLedgerTransactionLedgerableType string
const (
PaymentOrderReversalNewParamsLedgerTransactionLedgerableTypeExpectedPayment PaymentOrderReversalNewParamsLedgerTransactionLedgerableType = "expected_payment"
PaymentOrderReversalNewParamsLedgerTransactionLedgerableTypeIncomingPaymentDetail PaymentOrderReversalNewParamsLedgerTransactionLedgerableType = "incoming_payment_detail"
PaymentOrderReversalNewParamsLedgerTransactionLedgerableTypePaperItem PaymentOrderReversalNewParamsLedgerTransactionLedgerableType = "paper_item"
PaymentOrderReversalNewParamsLedgerTransactionLedgerableTypePaymentOrder PaymentOrderReversalNewParamsLedgerTransactionLedgerableType = "payment_order"
PaymentOrderReversalNewParamsLedgerTransactionLedgerableTypeReturn PaymentOrderReversalNewParamsLedgerTransactionLedgerableType = "return"
PaymentOrderReversalNewParamsLedgerTransactionLedgerableTypeReversal PaymentOrderReversalNewParamsLedgerTransactionLedgerableType = "reversal"
)
func (r PaymentOrderReversalNewParamsLedgerTransactionLedgerableType) IsKnown() bool {
switch r {
case PaymentOrderReversalNewParamsLedgerTransactionLedgerableTypeExpectedPayment, PaymentOrderReversalNewParamsLedgerTransactionLedgerableTypeIncomingPaymentDetail, PaymentOrderReversalNewParamsLedgerTransactionLedgerableTypePaperItem, PaymentOrderReversalNewParamsLedgerTransactionLedgerableTypePaymentOrder, PaymentOrderReversalNewParamsLedgerTransactionLedgerableTypeReturn, PaymentOrderReversalNewParamsLedgerTransactionLedgerableTypeReversal:
return true
}
return false
}
// To post a ledger transaction at creation, use `posted`.
type PaymentOrderReversalNewParamsLedgerTransactionStatus string
const (
PaymentOrderReversalNewParamsLedgerTransactionStatusArchived PaymentOrderReversalNewParamsLedgerTransactionStatus = "archived"
PaymentOrderReversalNewParamsLedgerTransactionStatusPending PaymentOrderReversalNewParamsLedgerTransactionStatus = "pending"
PaymentOrderReversalNewParamsLedgerTransactionStatusPosted PaymentOrderReversalNewParamsLedgerTransactionStatus = "posted"
)
func (r PaymentOrderReversalNewParamsLedgerTransactionStatus) IsKnown() bool {
switch r {
case PaymentOrderReversalNewParamsLedgerTransactionStatusArchived, PaymentOrderReversalNewParamsLedgerTransactionStatusPending, PaymentOrderReversalNewParamsLedgerTransactionStatusPosted:
return true
}
return false
}
type PaymentOrderReversalListParams struct {
AfterCursor param.Field[string] `query:"after_cursor"`
PerPage param.Field[int64] `query:"per_page"`
}
// URLQuery serializes [PaymentOrderReversalListParams]'s query parameters as
// `url.Values`.
func (r PaymentOrderReversalListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatBrackets,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}