This repository has been archived by the owner on Jun 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
OrderManagementStore.cs
281 lines (237 loc) · 13.9 KB
/
OrderManagementStore.cs
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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Klarna.Rest.Core.Common;
using Klarna.Rest.Core.Commuication;
using Klarna.Rest.Core.Commuication.Dto;
using Klarna.Rest.Core.Model;
using Klarna.Rest.Core.Serialization;
namespace Klarna.Rest.Core.Store
{
/// <summary>
/// The Order Management API is used for handling an order after the customer has completed the purchase. It is used for updating, capturing and refunding an order as well as to see the history of events that have affected this order.
/// </summary>
public class OrderManagementStore : BaseStore
{
internal OrderManagementStore(ApiSession apiSession, IJsonSerializer jsonSerializer) : base(apiSession, ApiControllers.OrderManagement, jsonSerializer) { }
/// <summary>
/// Release remaining authorization
/// <a href="https://developers.klarna.com/api/#order-management-api-release-remaining-authorization">https://developers.klarna.com/api/#order-management-api-release-remaining-authorization</a>
/// </summary>
/// <param name="orderId">Id of order to release</param>
/// <returns></returns>
public async Task ReleaseRemainingAuthorization(string orderId)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/release-remaining_authorization");
await Post(url);
}
/// <summary>
/// Extend authorization time
/// <a href="https://developers.klarna.com/api/#order-management-api-extend-authorization-time">https://developers.klarna.com/api/#order-management-api-extend-authorization-time</a>
/// </summary>
/// <param name="orderId">Id of order to extend</param>
/// <returns></returns>
public async Task ExtendAuthorizationTime(string orderId)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/extend-authorization-time");
await Post(url);
}
/// <summary>
/// Update customer addresses
/// <a href="https://developers.klarna.com/api/#order-management-api-update-customer-addresses">https://developers.klarna.com/api/#order-management-api-update-customer-addresses</a>
/// </summary>
/// <param name="orderId">Id of order to update</param>
/// <param name="customerAddresses">The <see cref="UpdateCustomerAddresses"/> object</param>
/// <returns></returns>
public async Task UpdateCustomerAddresses(string orderId, OrderManagementCustomerAddresses customerAddresses)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/customer-details");
await Patch(url, customerAddresses);
}
/// <summary>
/// Cancel order
/// <a href="https://developers.klarna.com/api/#order-management-api-cancel-order">https://developers.klarna.com/api/#order-management-api-cancel-order</a>
/// </summary>
/// <param name="orderId">Id of order to cancel</param>
/// <returns></returns>
public async Task CancelOrder(string orderId)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/cancel");
await Post(url);
}
/// <summary>
/// Update merchant references
/// <a href="https://developers.klarna.com/api/#order-management-api-update-merchant-references">https://developers.klarna.com/api/#order-management-api-update-merchant-references</a>
/// </summary>
/// <param name="orderId">Id of order to update</param>
/// <param name="merchantReferences">The <see cref="OrderManagementMerchantReferences"/> object</param>
/// <returns></returns>
public async Task UpdateMerchantReferences(string orderId, OrderManagementMerchantReferences merchantReferences)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/merchant-references");
await Patch(url, merchantReferences);
}
/// <summary>
/// Acknowledge order
/// <a href="https://developers.klarna.com/api/#order-management-api-acknowledge-order">https://developers.klarna.com/api/#order-management-api-acknowledge-order</a>
/// </summary>
/// <param name="orderId">Id of order to acknowledge</param>
/// <returns></returns>
public async Task AcknowledgeOrder(string orderId)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/acknowledge");
await Post(url);
}
/// <summary>
/// Get order
/// <a href="https://developers.klarna.com/api/#order-management-api-get-order">https://developers.klarna.com/api/#order-management-api-get-order</a>
/// </summary>
/// <param name="orderId">Id of order to retrieve</param>
/// <returns><see cref="OrderManagementOrder"/></returns>
public async Task<OrderManagementOrder> GetOrder(string orderId)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}");
var response = await Get<OrderManagementOrder>(url);
return response;
}
/// <summary>
/// Set new order amount and order lines
/// <a href="https://developers.klarna.com/api/#order-management-api-set-new-order-amount-and-order-lines">https://developers.klarna.com/api/#order-management-api-set-new-order-amount-and-order-lines</a>
/// </summary>
/// <param name="orderId">Id of order to update</param>
/// <param name="newOrderAmountAndLines">The <see cref="OrderManagementSetNewOrderAmountAndLines"/> object</param>
/// <returns></returns>
public async Task SetNewOrderAmountAndOrderLines(string orderId, OrderManagementSetNewOrderAmountAndLines newOrderAmountAndLines)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/authorization");
await Patch(url, newOrderAmountAndLines);
}
/// <summary>
/// Get one capture
/// <a href="https://developers.klarna.com/api/#order-management-api-get-one-capture">https://developers.klarna.com/api/#order-management-api-get-one-capture</a>
/// </summary>
/// <param name="orderId">Id of order that contains the capture</param>
/// <param name="captureId">Id of capture to retrieve</param>
/// <returns><see cref="OrderManagementCapture"/></returns>
public async Task<OrderManagementCapture> GetCapture(string orderId, string captureId)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/captures/{captureId}");
var response = await Get<OrderManagementCapture>(url);
return response;
}
/// <summary>
/// Trigger resend of customer communication
/// <a href="https://developers.klarna.com/api/#order-management-api-trigger-resend-of-customer-communication">https://developers.klarna.com/api/#order-management-api-trigger-resend-of-customer-communication</a>
/// </summary>
/// <param name="orderId">Id of order that contains the capture</param>
/// <param name="captureId">Id of capture to resend</param>
/// <returns></returns>
public async Task TriggerResendOfCustomerCommunication(string orderId, string captureId)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/captures/{captureId}/trigger-send-out");
await Post(url);
}
/// <summary>
/// Get all captures for one order
/// <a href="https://developers.klarna.com/api/#order-management-api-get-all-captures-for-one-order">https://developers.klarna.com/api/#order-management-api-get-all-captures-for-one-order</a>
/// </summary>
/// <param name="orderId">Id of order to retrieve captures</param>
/// <returns>Collection of <see cref="OrderManagementCapture"/></returns>
public async Task<ICollection<OrderManagementCapture>> GetCapturesForOrder(string orderId)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/captures");
var response = await Get<ICollection<OrderManagementCapture>>(url);
return response;
}
/// <summary>
/// Create capture
/// <a href="https://developers.klarna.com/api/#order-management-api-create-capture">https://developers.klarna.com/api/#order-management-api-create-capture</a>
/// </summary>
/// <param name="orderId">Id of order to create capture</param>
/// <param name="capture">The <see cref="OrderManagementCapture"/> object</param>
/// <returns>Object of <see cref="OrderManagementCapture"/> </returns>
public async Task CreateCapture(string orderId, OrderManagementCreateCapture capture)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/captures");
await Post(url, capture);
}
/// <summary>
/// Create capture and follow the Location header to fetch the data
/// <a href="https://developers.klarna.com/api/#order-management-api-create-capture">https://developers.klarna.com/api/#order-management-api-create-capture</a>
/// </summary>
/// <param name="orderId">Id of order to create capture</param>
/// <param name="capture">The <see cref="OrderManagementCapture"/> object</param>
/// <returns>Object of <see cref="OrderManagementCapture"/> </returns>
public async Task<OrderManagementCapture> CreateAndFetchCapture(string orderId, OrderManagementCreateCapture capture)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/captures");
var response = new Ref<HttpResponseMessage>();
await Post(url, capture, null, response);
var headers = response.Value.Headers;
url = headers.Location.ToString();
if (!string.IsNullOrEmpty(url))
{
return await Get<OrderManagementCapture>(url);
}
return default(OrderManagementCapture);
}
/// <summary>
/// Add shipping info to a capture
/// <a href="https://developers.klarna.com/api/#order-management-api-add-shipping-info-to-a-capture">https://developers.klarna.com/api/#order-management-api-add-shipping-info-to-a-capture</a>
/// </summary>
/// <param name="orderId">Id of order to add shipping info</param>
/// <param name="captureId">Id of capture to add shipping info</param>
/// <param name="shippingInfo">The <see cref="OrderManagementAddShippingInfo"/> object</param>
/// <returns></returns>
public async Task AddShippingInfoToCapture(string orderId, string captureId, OrderManagementAddShippingInfo shippingInfo)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/captures/{captureId}/shipping-info");
await Post(url, shippingInfo);
}
/// <summary>
/// Create a refund
/// <a href="https://developers.klarna.com/api/#order-management-api-create-a-refund">https://developers.klarna.com/api/#order-management-api-create-a-refund</a>
/// </summary>
/// <param name="orderId">Id of order to create a refund</param>
/// <param name="refund">The <see cref="OrderManagementRefund"/> object</param>
/// <returns></returns>
public async Task CreateRefund(string orderId, OrderManagementRefund refund)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/refunds");
await Post(url, refund);
}
/// <summary>
/// Create a refund and follow the Location header to fetch the data
/// <a href="https://developers.klarna.com/api/#order-management-api-create-a-refund">https://developers.klarna.com/api/#order-management-api-create-a-refund</a>
/// </summary>
/// <param name="orderId">Id of order to create a refund</param>
/// <param name="refund">The <see cref="OrderManagementRefund"/> object</param>
/// <returns></returns>
public async Task<OrderManagementGetRefundResponse> CreateAndFetchRefund(string orderId, OrderManagementRefund refund)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/refunds");
var response = new Ref<HttpResponseMessage>();
await Post(url, refund, null, response);
var headers = response.Value.Headers;
url = headers.Location.ToString();
if (!string.IsNullOrEmpty(url))
{
return await Get<OrderManagementGetRefundResponse>(url);
}
return default(OrderManagementGetRefundResponse);
}
/// <summary>
/// Get refund
/// <a href="https://developers.klarna.com/api/#order-management-api-get-refund">https://developers.klarna.com/api/#order-management-api-get-refund</a>
/// </summary>
/// <param name="orderId">Id of order to get refund</param>
/// <param name="refundId">Id of refund</param>
/// <returns></returns>
public async Task<OrderManagementGetRefundResponse> GetRefundForOrder(string orderId, string refundId)
{
var url = ApiUrlHelper.GetApiUrlForController(ApiSession.ApiUrl, ApiControllerUri, $"{orderId}/refunds/{refundId}");
return await Get<OrderManagementGetRefundResponse>(url);
}
}
}