-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathorderchanges.go
180 lines (157 loc) · 6.61 KB
/
orderchanges.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
// 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.
// Order change flow:
// 1. Get an existing order by ID using client.GetOrder(...)
// 2. Create a new order change request using client.CreateOrderChangeRequest(...)
// 3. Get the order change offer using client.CreatePendingOrderChange(...)
package duffel
import (
"context"
"fmt"
"strings"
"time"
"github.com/bojanz/currency"
)
const (
orderChangeRequestIDPrefix = "ocr_"
orderChangeOfferIDPrefix = "oco_"
orderChangeIDPrefix = "oce_"
)
type (
// OrderChangeRequest is the input to the OrderChange API.
// To change an order, you'll need to create an order change request.
// An order change request describes the slices of an existing paid order
// that you want to remove and search criteria for new slices you want to add.
OrderChangeRequest struct {
ID string `json:"id"`
OrderID string `json:"order_id"`
Slices SliceChange `json:"slices"`
OrderChangeOffers []OrderChangeOffer `json:"order_change_offers"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
LiveMode bool `json:"live_mode"`
}
OrderChangeOffer struct {
ID string `json:"id"`
// OrderChangeID is the ID for an order change if one has already been created from this order change offer
OrderChangeID string `json:"order_change_id"`
Slices SliceChangeset `json:"slices"`
RefundTo PaymentMethod `json:"refund_to"`
RawPenaltyTotalCurrency string `json:"penalty_total_currency"`
RawPenaltyTotalAmount string `json:"penalty_total_amount"`
RawNewTotalCurrency string `json:"new_total_currency"`
RawNewTotalAmount string `json:"new_total_amount"`
RawChangeTotalCurrency string `json:"change_total_currency"`
RawChangeTotalAmount string `json:"change_total_amount"`
ExpiresAt time.Time `json:"expires_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
LiveMode bool `json:"live_mode"`
}
OrderChange struct {
ID string `json:"id"`
OrderID string `json:"order_id"`
Slices SliceChangeset `json:"slices"`
RefundTo PaymentMethod `json:"refund_to"`
RawPenaltyTotalCurrency string `json:"penalty_total_currency"`
RawPenaltyTotalAmount string `json:"penalty_total_amount"`
RawNewTotalCurrency string `json:"new_total_currency"`
RawNewTotalAmount string `json:"new_total_amount"`
RawChangeTotalCurrency string `json:"change_total_currency"`
RawChangeTotalAmount string `json:"change_total_amount"`
ExpiresAt string `json:"expires_at"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
LiveMode bool `json:"live_mode"`
}
SliceChangeset struct {
Add []Slice `json:"add"`
Remove []Slice `json:"remove"`
}
OrderChangeRequestParams struct {
OrderID string `json:"order_id"`
Slices SliceChange `json:"slices,omitempty"`
}
SliceAdd struct {
DepartureDate Date `json:"departure_date"`
Destination string `json:"destination"`
Origin string `json:"origin"`
CabinClass CabinClass `json:"cabin_class"`
}
SliceRemove struct {
SliceID string `json:"slice_id"`
}
SliceChange struct {
Add []SliceAdd `json:"add,omitempty"`
Remove []SliceRemove `json:"remove,omitempty"`
}
OrderChangeClient interface {
CreateOrderChangeRequest(ctx context.Context, params OrderChangeRequestParams) (*OrderChangeRequest, error)
GetOrderChangeRequest(ctx context.Context, id string) (*OrderChangeRequest, error)
CreatePendingOrderChange(ctx context.Context, orderChangeRequestID string) (*OrderChange, error)
ConfirmOrderChange(ctx context.Context, id string, payment PaymentCreateInput) (*OrderChange, error)
}
)
func (a *API) CreateOrderChangeRequest(ctx context.Context, params OrderChangeRequestParams) (*OrderChangeRequest, error) {
return newRequestWithAPI[OrderChangeRequestParams, OrderChangeRequest](a).
Post("/air/order_change_requests", ¶ms).
Single(ctx)
}
func (a *API) GetOrderChangeRequest(ctx context.Context, orderChangeRequestID string) (*OrderChangeRequest, error) {
if err := validateID(orderChangeRequestID, orderChangeRequestIDPrefix); err != nil {
return nil, err
}
return newRequestWithAPI[EmptyPayload, OrderChangeRequest](a).
Getf("/air/order_change_requests/%s", orderChangeRequestID).
Single(ctx)
}
func (a *API) CreatePendingOrderChange(ctx context.Context, offerID string) (*OrderChange, error) {
if err := validateID(offerID, orderChangeOfferIDPrefix); err != nil {
return nil, err
}
return newRequestWithAPI[map[string]string, OrderChange](a).
Postf("/air/order_changes").
Body(&map[string]string{"selected_order_change_offer": offerID}).
Single(ctx)
}
func (a *API) ConfirmOrderChange(ctx context.Context, orderChangeRequestID string, payment PaymentCreateInput) (*OrderChange, error) {
if err := validateID(orderChangeRequestID, orderChangeRequestIDPrefix); err != nil {
return nil, err
}
return newRequestWithAPI[PaymentCreateInput, OrderChange](a).
Postf("/air/order_changes/%s/actions/confirm", orderChangeRequestID).
Body(&payment).
Single(ctx)
}
var _ OrderChangeClient = (*API)(nil)
func validateID(id, prefix string) error {
if id == "" {
return fmt.Errorf("id param is required")
} else if !strings.HasPrefix(id, prefix) {
return fmt.Errorf("id should begin with %s", prefix)
}
return nil
}
func (o *OrderChangeOffer) ChangeTotalAmount() currency.Amount {
amount, err := currency.NewAmount(o.RawChangeTotalAmount, o.RawChangeTotalCurrency)
if err != nil {
return currency.Amount{}
}
return amount
}
func (o *OrderChangeOffer) NewTotalAmount() currency.Amount {
amount, err := currency.NewAmount(o.RawNewTotalAmount, o.RawNewTotalCurrency)
if err != nil {
return currency.Amount{}
}
return amount
}
// PenaltyTotalAmount returns the penalty imposed by the airline for making this change.
func (o *OrderChangeOffer) PenaltyTotalAmount() currency.Amount {
amount, err := currency.NewAmount(o.RawPenaltyTotalAmount, o.RawPenaltyTotalCurrency)
if err != nil {
return currency.Amount{}
}
return amount
}