-
Notifications
You must be signed in to change notification settings - Fork 6
/
orders.go
281 lines (224 loc) · 9.64 KB
/
orders.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
// Copyright 2021-present Airheart, Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package duffel
import (
"context"
"net/url"
"time"
"github.com/bojanz/currency"
"github.com/gorilla/schema"
)
const orderIDPrefix = "ord_"
type (
ListOrdersSort string
Order struct {
ID string `json:"id"`
LiveMode bool `json:"live_mode"`
Metadata Metadata `json:"metadata"`
RawBaseAmount *string `json:"base_amount,omitempty"`
RawBaseCurrency *string `json:"base_currency,omitempty"`
BookingReference string `json:"booking_reference"`
CancelledAt *time.Time `json:"cancelled_at,omitempty"`
CreatedAt time.Time `json:"created_at"`
Conditions Conditions `json:"conditions,omitempty"`
Documents []Document `json:"documents,omitempty"`
Owner Airline `json:"owner"`
Passengers []OrderPassenger `json:"passengers,omitempty"`
PaymentStatus PaymentStatus `json:"payment_status"`
Services []Service `json:"services,omitempty"`
Slices []Slice `json:"slices,omitempty"`
SyncedAt time.Time `json:"synced_at"`
RawTaxAmount *string `json:"tax_amount,omitempty"`
RawTaxCurrency *string `json:"tax_currency,omitempty"`
RawTotalAmount string `json:"total_amount"`
RawTotalCurrency string `json:"total_currency"`
}
SliceConditions struct {
ChangeBeforeDeparture *ChangeCondition `json:"change_before_departure,omitempty"`
}
Conditions struct {
RefundBeforeDeparture *ChangeCondition `json:"refund_before_departure,omitempty"`
ChangeBeforeDeparture *ChangeCondition `json:"change_before_departure,omitempty"`
}
ChangeCondition struct {
Allowed bool `json:"allowed"`
RawPenaltyAmount *string `json:"penalty_amount,omitempty"`
RawPenaltyCurrency *string `json:"penalty_currency,omitempty"`
}
Document struct {
Type string `json:"type"`
UniqueIdentifier string `json:"unique_identifier"`
}
// NOTE: If you receive a 500 Internal Server Error when trying to create an order,
// it may have still been created on the airline’s side.
// Please contact Duffel support before trying the request again.
OrderType string
CreateOrderInput struct {
Type OrderType `json:"type"`
// Metadata contains a set of key-value pairs that you can attach to an object.
// It can be useful for storing additional information about the object, in a
// structured format. Duffel does not use this information.
//
// You should not store sensitive information in this field.
Metadata Metadata `json:"metadata,omitempty"`
// The personal details of the passengers, expanding on
// the information initially provided when creating the offer request.
Passengers []OrderPassenger `json:"passengers"`
Payments []PaymentCreateInput `json:"payments,omitempty"`
// The ids of the offers you want to book. You must specify an array containing exactly one selected offer.
SelectedOffers []string `json:"selected_offers"`
Services []ServiceCreateInput `json:"services,omitempty"`
}
// The services you want to book along with the first selected offer. This key should be omitted when the order’s type is hold, as we do not support services for hold orders yet.
ServiceCreateInput struct {
// The id of the service from the offer's available_services that you want to book
ID string `json:"id"`
// The quantity of the service to book. This will always be 1 for seat services.
Quantity int `json:"quantity"`
}
Service struct {
// Duffel's unique identifier for the booked service
ID string `json:"id"`
// The metadata varies by the type of service.
// It includes further data about the service. For example, for
// baggages, it may have data about size and weight restrictions.
Metadata Metadata `json:"metadata"`
// List of passenger ids the service applies to.
// The service applies to all the passengers in this list.
PassengerIDs []string `json:"passenger_ids"`
// The quantity of the service that was booked
Quantity int `json:"quantity"`
// List of segment ids the service applies to. The service applies to all the segments in this list.
SegmentIDs []string `json:"segment_ids"`
// The total price of the service for all passengers and segments it applies to, accounting for quantity and including taxes
RawTotalAmount string `json:"total_amount,omitempty"`
RawTotalCurrency string `json:"total_currency,omitempty"`
// Possible values: "baggage" or "seat"
Type string `json:"type"`
}
// OrderUpdateParams is used as the input to UpdateOrder.
// Only certain order fields are updateable.
// Each field that can be updated is detailed in the `OrderUpdateParams` object.
OrderUpdateParams struct {
Metadata map[string]any
}
ListOrdersParams struct {
// Filters orders by their booking reference.
// The filter requires an exact match but is case insensitive.
BookingReference string `url:"booking_reference,omitempty"`
// Whether to filter orders that are awaiting payment or not.
// If not specified, all orders regardless of their payment state will be returned.
AwaitingPayment bool `url:"awaiting_payment,omitempty"`
// By default, orders aren't returned in any specific order.
// This parameter allows you to sort the list of orders by the payment_required_by date
Sort ListOrdersSort `url:"sort,omitempty"`
// Filters the returned orders by owner.id. Values must be valid airline.ids.
// Check the Airline schema for details.
OwnerIDs []string `url:"owner_id,omitempty"`
// Filters the returned orders by origin. Values must be valid origin identifiers.
// Check the Order schema for details.
OriginIDs []string `url:"origin_id,omitempty"`
// Filters the returned orders by destination. Values must be valid destination identifiers.
// Check the Order schema for details.
DestinationIDs []string `url:"destination_id,omitempty"`
// Filters the returned orders by departure datetime.
// Orders will be included if any of their segments matches the given criteria
DepartingAt *TimeFilter `url:"departing_at,omitempty"`
// Filters the returned orders by arrival datetime.
// Orders will be included if any of their segments matches the given criteria.
ArrivingAt *TimeFilter `url:"arriving_at,omitempty"`
// Filters the returned orders by creation datetime.
CreatedAt *TimeFilter `url:"created_at,omitempty"`
// Orders will be included if any of their passengers matches any of the given names.
// Matches are case insensitive, and include partial matches.
PassengerNames []string `url:"passenger_name,omitempty"`
}
Metadata map[string]any
TimeFilter struct {
Before *time.Time `url:"before,omitempty"`
After *time.Time `url:"after,omitempty"`
}
OrderClient interface {
// Get a single order by ID.
GetOrder(ctx context.Context, id string) (*Order, error)
// Update a single order by ID.
UpdateOrder(ctx context.Context, id string, params OrderUpdateParams) (*Order, error)
// List orders.
ListOrders(ctx context.Context, params ...ListOrdersParams) *Iter[Order]
// Create an order.
CreateOrder(ctx context.Context, input CreateOrderInput) (*Order, error)
}
)
const (
ListOrdersSortPaymentRequiredByAsc ListOrdersSort = "payment_required_by"
ListOrdersSortPaymentRequiredByDesc ListOrdersSort = "-payment_required_by"
OrderTypeHold OrderType = "hold"
OrderTypeInstant OrderType = "instant"
)
// CreateOrder creates a new order.
func (a *API) CreateOrder(ctx context.Context, input CreateOrderInput) (*Order, error) {
return newRequestWithAPI[CreateOrderInput, Order](a).Post("/air/orders", &input).Single(ctx)
}
func (a *API) UpdateOrder(ctx context.Context, id string, params OrderUpdateParams) (*Order, error) {
return newRequestWithAPI[OrderUpdateParams, Order](a).Patch("/air/orders/"+id, ¶ms).Single(ctx)
}
// CreateOrder creates a new order.
func (a *API) GetOrder(ctx context.Context, id string) (*Order, error) {
return newRequestWithAPI[EmptyPayload, Order](a).Get("/air/orders/" + id).Single(ctx)
}
func (a *API) ListOrders(ctx context.Context, params ...ListOrdersParams) *Iter[Order] {
return newRequestWithAPI[ListOrdersParams, Order](a).
Get("/air/orders").
WithParams(normalizeParams(params)...).
Iter(ctx)
}
func (o *Order) BaseAmount() *currency.Amount {
if o.RawBaseAmount != nil && o.RawBaseCurrency != nil {
amount, err := currency.NewAmount(*o.RawBaseAmount, *o.RawBaseCurrency)
if err != nil {
return nil
}
return &amount
}
return nil
}
func (o *Order) TaxAmount() *currency.Amount {
if o.RawTaxAmount != nil && o.RawTaxCurrency != nil {
amount, err := currency.NewAmount(*o.RawTaxAmount, *o.RawTaxCurrency)
if err != nil {
return nil
}
return &amount
}
return nil
}
func (o *Order) TotalAmount() currency.Amount {
amount, err := currency.NewAmount(o.RawTotalAmount, o.RawTotalCurrency)
if err != nil {
return currency.Amount{}
}
return amount
}
func (c *ChangeCondition) PenaltyAmount() *currency.Amount {
if c.RawPenaltyAmount != nil && c.RawPenaltyCurrency != nil {
amount, err := currency.NewAmount(*c.RawPenaltyAmount, *c.RawPenaltyCurrency)
if err != nil {
return nil
}
return &amount
}
return nil
}
func (s *Service) TotalAmount() currency.Amount {
amount, err := currency.NewAmount(s.RawTotalAmount, s.RawTotalCurrency)
if err != nil {
return currency.Amount{}
}
return amount
}
func (o ListOrdersParams) Encode(q url.Values) error {
enc := schema.NewEncoder()
enc.SetAliasTag("url")
return enc.Encode(o, q)
}