This repository has been archived by the owner on Feb 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
order_management.go
274 lines (221 loc) · 9.99 KB
/
order_management.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
package go_klarna
import (
"encoding/json"
"fmt"
)
const (
OrderManagementEndpoint = "/ordermanagement/v1/orders"
Authorized = "AUTHORIZED"
PartCaptured = "PART_CAPTURED"
Captured = "CAPTURED"
Cancelled = "CANCELLED"
Expired = "EXPIRED"
Closed = "LOSED"
Accepted = "ACCEPTED"
Pending = "PENDING"
Rejected = "REJECTED"
)
type (
OrderManagementSrv interface {
// Order Management - order end-points
GetOrder(string) (*OrderManagementOrder, error)
AcknowledgeOrder(string) error
SetOrderAmountLines(string, *OrderAmountLines) error
AdjustOrderAmountLines(string, *AdjustAmountLines) error
CancelOrder(string) error
UpdateCustomerAddress(string, *CustomerAddress) error
ExtendAuthorizationTime(string) error
UpdateMerchantReferences(string, *MerchantReferences) error
ReleaseRemainingAuthorization(string) error
// Order Management - capture end-points
GetRefund(string, string) error
CreateRefund(string, *OrderManagementRefund) error
GetAllCaptures(string) ([]*Capture, error)
TriggerResendCustomerCommunication(string, string) error
AddCaptureShippingInfo(string, string, []*OrderManagementShippingInfo) error
GetCapture(string, string) (*Capture, error)
CreateCapture(string, *CreateCapture) error
}
orderManagementSrv struct {
client Client
}
OrderManagementOrder struct {
ID string `json:"order_id,omitempty"`
Status string `json:"status,omitempty"`
FraudStatus string `json:"fraud_status,omitempty"`
OrderAmount int `json:"order_amount,omitempty"`
OriginalOrderAmount int `json:"original_order_amount,omitempty"`
CapturedAmount int `json:"captured_amount,omitmepty"`
RefundedAmount int `json:",omitempty"`
RemainingAuthorizedAmount int `json:"remaining_authorized_amount,omitempty"`
PurchaseCurrency string `json:"purchase_currency,omitempty"`
Locale string `json:",omitempty"`
OrderLines []*Line `json:"order_lines,omitempty"`
MerchantReference1 string `json:"merchant_reference1,omitempty"`
MerchantReference2 string `json:"merchant_reference2,omitempty"`
KlarnaReference string `json:"klarna_reference"`
Customer *OrderManagementCustomer `json:"customer,omitempty"`
BillingAddress *Address `json:"billing_address,omitempty"`
ShippingAddress *Address `json:"shipping_address,omitempty"`
CreatedAt string `json:"created_at,omitempty"` // DateTime string of ISO 8601
PurchaseCountry string `json:"purchase_country,omitempty"`
ExpiresAt string `json:"expires_at,omitempty"` // DateTime string of ISO 8601
Captures *[]Capture `json:"captures,omitempty"`
Refunds *OrderManagementRefund `json:"refunds,omitempty"`
MerchantData string `json:"merchant_data,omitempty"`
}
OrderManagementCustomer struct {
DateOfBirth string `json:"date_of_birth,omitempty"`
NationalIdentificationNumber string `json:"national_identification_number,omitempty"`
}
Capture struct {
ID string `json:"capture_id,omitempty"`
KlarnaReference string `json:"klarna_reference,omitempty"`
CaptureAmount int `json:"capture_amount,omitempty"`
CapturedAt string `json:"captured_at,omitempty"` // DateTime string of ISO 8601
Description string `json:"description,omitempty"`
OrderLines []*Line `json:"order_lines,omitempty"`
RefundedAmount int `json:"refunded_amount,omitempty"`
BillingAddress *Address `json:"billing_address,omitempty"`
ShippingAddress *Address `json:"shipping_address,omitempty"`
ShippingInfo *OrderManagementShippingInfo `json:"shipping_info,omitempty"`
}
OrderManagementShippingInfo struct {
ShippingCompany string `json:"shipping_company,omitempty"`
ShippingMethod string `json:"shipping_method,omitempty"`
TrackingNumber string `json:"tracking_number,omitempty"`
TrackingUri string `json:"tracking_uri,omitempty"`
ReturnShippingCompany string `json:"return_shipping_company,omitempty"`
ReturnTrackingNumber string `json:"return_tracking_number,omitempty"`
ReturnTrackingUri string `json:"return_tracking_uri,omitempty"`
}
OrderManagementRefund struct {
RefundAmount int `json:"refund_amount,omitempty"`
RefundedAt string `json:"refunded_at,omitempty"` // DateTime string of ISO 8601
Description string `json:"description,omitempty"`
OrderLines []*Line `json:"order_lines,omitempty"`
}
OrderAmountLines struct {
OrderAmount int `json:"order_amount"`
Description string `json:"description,omitempty"`
OrderLines []*Line `json:"order_lines,omitempty"`
}
AdjustAmountLines struct {
AdjustAmount int `json:"adjust_amount"`
Description string `json:"description,omitempty"`
OrderLines []*Line `json:"order_lines,omitempty"`
}
CustomerAddress struct {
ShippingAddress *Address `json:"shipping_address,omitempty"`
BillingAddress *Address `json:"billing_address,omitempty"`
}
MerchantReferences struct {
MerchantReference1 string `json:"merchant_reference1,omitmepty"`
MerchantReference2 string `json:"merchant_reference2,omitmepty"`
}
CreateCapture struct {
CapturedAmount int `json:"captured_amount"`
Description string `json:"description,omitempty"`
OrderLines []*Line `json:"order_lines,omitempty"`
ShippingInfo []*OrderManagementShippingInfo `json:"shipping_info,omitempty"`
ShippingDelay int `json:"shipping_delay,omitempty"`
}
)
func (srv *orderManagementSrv) GetRefund(oid, rid string) error {
path := fmt.Sprintf("%s/%s/refunds/%s", OrderManagementEndpoint, oid, rid)
_, err := srv.client.Get(path)
return err
}
func (srv *orderManagementSrv) CreateRefund(oid string, rf *OrderManagementRefund) error {
path := fmt.Sprintf("%s/%s/refunds", OrderManagementEndpoint, oid)
_, err := srv.client.Post(path, rf)
return err
}
func (srv *orderManagementSrv) TriggerResendCustomerCommunication(oid, cid string) error {
path := fmt.Sprintf("%s/%s/captures/%s/trigger-send-out", OrderManagementEndpoint, oid, cid)
_, err := srv.client.Post(path, nil)
return err
}
func (srv *orderManagementSrv) AddCaptureShippingInfo(oid, cid string, si []*OrderManagementShippingInfo) error {
path := fmt.Sprintf("%s/%s/captures/%s/shipping-info", OrderManagementEndpoint, oid, cid)
_, err := srv.client.Post(path, si)
return err
}
func (srv *orderManagementSrv) GetCapture(oid, cid string) (*Capture, error) {
path := fmt.Sprintf("%s/%s/captures/%s", OrderManagementEndpoint, oid, cid)
res, err := srv.client.Get(path)
if nil != err {
return nil, err
}
var capture *Capture
err = json.NewDecoder(res.Body).Decode(&capture)
return capture, err
}
func (srv *orderManagementSrv) CreateCapture(oid string, c *CreateCapture) error {
path := fmt.Sprintf("%s/%s/captures", OrderManagementEndpoint, oid)
_, err := srv.client.Post(path, c)
return err
}
func (srv *orderManagementSrv) GetAllCaptures(oid string) ([]*Capture, error) {
path := fmt.Sprintf("%s/%s/captures", OrderManagementEndpoint, oid)
res, err := srv.client.Get(path)
if nil != err {
return nil, err
}
var captures []*Capture
err = json.NewDecoder(res.Body).Decode(&captures)
return captures, err
}
func (srv *orderManagementSrv) GetOrder(id string) (*OrderManagementOrder, error) {
path := OrderManagementEndpoint + "/" + id
res, err := srv.client.Get(path)
if nil != err {
return nil, err
}
o := new(OrderManagementOrder)
err = json.NewDecoder(res.Body).Decode(o)
return o, err
}
func (srv *orderManagementSrv) AcknowledgeOrder(oid string) error {
path := fmt.Sprintf("%s/%s/acknowledge", OrderManagementEndpoint, oid)
_, err := srv.client.Post(path, nil)
return err
}
func (srv *orderManagementSrv) SetOrderAmountLines(oid string, oal *OrderAmountLines) error {
path := fmt.Sprintf("%s/%s/authorization", OrderManagementEndpoint, oid)
_, err := srv.client.Patch(path, oal)
return err
}
func (srv *orderManagementSrv) AdjustOrderAmountLines(oid string, adjust *AdjustAmountLines) error {
path := fmt.Sprintf("%s/%s/authorization-adjustments", OrderManagementEndpoint, oid)
_, err := srv.client.Post(path, adjust)
return err
}
func (srv *orderManagementSrv) CancelOrder(oid string) error {
path := fmt.Sprintf("%s/%s/cancel", OrderManagementEndpoint, oid)
_, err := srv.client.Post(path, nil)
return err
}
func (srv *orderManagementSrv) UpdateCustomerAddress(oid string, ca *CustomerAddress) error {
path := fmt.Sprintf("%s/%s/customer-details", OrderManagementEndpoint, oid)
_, err := srv.client.Post(path, ca)
return err
}
func (srv *orderManagementSrv) ExtendAuthorizationTime(oid string) error {
path := fmt.Sprintf("%s/%s/extend-authorization-time", OrderManagementEndpoint, oid)
_, err := srv.client.Post(path, nil)
return err
}
func (srv *orderManagementSrv) UpdateMerchantReferences(oid string, mr *MerchantReferences) error {
path := fmt.Sprintf("%s/%s/merchant-references", OrderManagementEndpoint, oid)
_, err := srv.client.Post(path, mr)
return err
}
func (srv *orderManagementSrv) ReleaseRemainingAuthorization(oid string) error {
path := fmt.Sprintf("%s/%s/release-remaining-authorization", OrderManagementEndpoint, oid)
_, err := srv.client.Post(path, nil)
return err
}
func NewOrderManagement(c Client) OrderManagementSrv {
return &orderManagementSrv{c}
}