Skip to content

Commit

Permalink
Merge pull request #838 from lucasabond/CancellationRequestService
Browse files Browse the repository at this point in the history
Added support for Fulfillment Order Cancellation Request
  • Loading branch information
nozzlegear committed May 31, 2023
2 parents 2190cc5 + 7293be1 commit f019a04
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 5 deletions.
18 changes: 18 additions & 0 deletions ShopifySharp/Entities/CancellationRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Newtonsoft.Json;
using System.Collections.Generic;

namespace ShopifySharp
{
/// <summary>
/// An object representing a Shopify fulfillment cancellation request.
/// </summary>
public class CancellationRequest
{
/// <summary>
/// An optional reason for the cancellation request.
/// </summary>
[JsonProperty("message")]
public string Message { get; set; }

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using ShopifySharp.Infrastructure;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;

namespace ShopifySharp
{
/// <summary>
/// The CancellationRequest resource represents a cancellation request made by the merchant or an order management app to a fulfillment service for a fulfillment order.
/// </summary>
public class CancellationRequestService : ShopifyService, ICancellationRequestService
{
/// <summary>
/// Creates a new instance of <see cref="CancellationRequestService" />.
/// </summary>
/// <param name="myShopifyUrl">The shop's *.myshopify.com URL.</param>
/// <param name="shopAccessToken">An API access token for the shop.</param>
public CancellationRequestService(string myShopifyUrl, string shopAccessToken) : base(myShopifyUrl, shopAccessToken) { }

public virtual async Task<FulfillmentOrder> CreateAsync(long fulfillmentOrderId, string message, CancellationToken cancellationToken = default)
{
var req = PrepareRequest($@"fulfillment_orders/{fulfillmentOrderId}/cancellation_request.json");
var cancellationRequest = new CancellationRequest { Message = message };
var body = cancellationRequest.ToDictionary();

var content = new JsonContent(new
{
cancellation_request = body
});

var response = await ExecuteRequestAsync<FulfillmentOrder>(req, HttpMethod.Post, cancellationToken, content, "fulfillment_order");
return response.Result;
}

public virtual async Task<FulfillmentOrder> AcceptAsync(long fulfillmentOrderId, string message, CancellationToken cancellationToken = default)
{
var req = PrepareRequest($"fulfillment_orders/{fulfillmentOrderId}/cancellation_request/accept.json");
var cancellationRequest = new CancellationRequest { Message = message };
var body = cancellationRequest.ToDictionary();

var content = new JsonContent(new
{
cancellation_request = body
});

var response = await ExecuteRequestAsync<FulfillmentOrder>(req, HttpMethod.Post, cancellationToken, content, "fulfillment_order");
return response.Result;
}

public virtual async Task<FulfillmentOrder> RejectAsync(long fulfillmentOrderId, string message, CancellationToken cancellationToken = default)
{
var req = PrepareRequest($@"fulfillment_orders/{fulfillmentOrderId}/cancellation_request/reject.json");
var cancellationRequest = new CancellationRequest { Message = message };
var body = cancellationRequest.ToDictionary();

var content = new JsonContent(new
{
cancellation_request = body
});

var response = await ExecuteRequestAsync<FulfillmentOrder>(req, HttpMethod.Post, cancellationToken, content, "fulfillment_order");
return response.Result;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Threading;
using System.Threading.Tasks;

namespace ShopifySharp
{
/// <summary>
/// The CancellationRequest resource represents a cancellation request made by the merchant or an order management app to a fulfillment service for a fulfillment order.
/// <see href="https://shopify.dev/docs/api/admin-rest/2022-07/resources/cancellationrequest">API Reference</see>
/// </summary>
public interface ICancellationRequestService
{
/// <summary>
/// Sends a cancellation request to the fulfillment service of a fulfillment order.
/// <see href="https://shopify.dev/docs/api/admin-rest/2022-07/resources/cancellationrequest#post-fulfillment-orders-fulfillment-order-id-cancellation-request">API Reference</see>
/// </summary>
/// <param name="fulfillmentOrderId"></param>
/// <param name="message">An optional reason for the cancellation request</param>
/// <param name="cancellationToken">Cancellation Token</param>
/// <exception cref="ShopifyException"></exception>
Task<FulfillmentOrder> CreateAsync(long fulfillmentOrderId, string message, CancellationToken cancellationToken = default);

/// <summary>
/// Accepts a cancellation request sent to a fulfillment service for a fulfillment order.
/// <see href="https://shopify.dev/docs/api/admin-rest/2022-07/resources/cancellationrequest#post-fulfillment-orders-fulfillment-order-id-cancellation-request-accept">API Reference</see>
/// </summary>
/// <param name="fulfillmentOrderId"></param>
/// <param name="message">An optional reason for accepting the cancellation request</param>
/// <param name="cancellationToken">Cancellation Token</param>
/// <exception cref="ShopifyException"></exception>
Task<FulfillmentOrder> AcceptAsync(long fulfillmentOrderId, string message, CancellationToken cancellationToken = default);

/// <summary>
/// Rejects a cancellation request sent to a fulfillment service for a fulfillment order.
/// <see href="https://shopify.dev/docs/api/admin-rest/2022-07/resources/cancellationrequest#post-fulfillment-orders-fulfillment-order-id-cancellation-request-reject">API Reference</see>
/// </summary>
/// <param name="fulfillmentOrderId"></param>
/// <param name="message">An optional reason for rejecting the cancellation request</param>
/// <param name="cancellationToken">Cancellation Token</param>
/// <exception cref="ShopifyException"></exception>
Task<FulfillmentOrder> RejectAsync(long fulfillmentOrderId, string message, CancellationToken cancellationToken = default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace ShopifySharp
{
/// <summary>
/// The FulfillmentRequest resource represents a fulfillment request made by the merchant to a fulfillment service for a fulfillment order.
/// <see href="https://shopify.dev/api/admin/rest/reference/shipping-and-fulfillment/fulfillmentrequest">API Reference</see>
/// <see href="https://shopify.dev/docs/api/admin-rest/2022-07/resources/fulfillmentrequest">API Reference</see>
/// </summary>
public interface IFulfillmentRequestService
{

/// <summary>
/// Accepts a fulfillment request sent to a fulfillment service for a fulfillment order.
/// <see href="https://shopify.dev/api/admin/rest/reference/shipping-and-fulfillment/fulfillmentrequest#accept-2021-07">API Reference</see>
/// <see href="https://shopify.dev/docs/api/admin-rest/2022-07/resources/fulfillmentrequest#post-fulfillment-orders-fulfillment-order-id-fulfillment-request-accept">API Reference</see>
/// </summary>
/// <param name="fulfillmentOrderId"></param>
/// <param name="message">An optional reason for accepting the fulfillment request</param>
Expand All @@ -22,7 +22,7 @@ public interface IFulfillmentRequestService

/// <summary>
/// Sends a fulfillment request to the fulfillment service of a fulfillment order.
/// <see href="https://shopify.dev/api/admin/rest/reference/shipping-and-fulfillment/fulfillmentrequest#create-2021-07">API Reference</see>
/// <see href="https://shopify.dev/docs/api/admin-rest/2022-07/resources/fulfillmentrequest#post-fulfillment-orders-fulfillment-order-id-fulfillment-request">API Reference</see>
/// </summary>
/// <param name="fulfillmentOrderId"></param>
/// <param name="fulfillmentRequest">A new <see cref="FulfillmentRequest"/>.</param>
Expand All @@ -33,7 +33,7 @@ public interface IFulfillmentRequestService

/// <summary>
/// Rejects a fulfillment request sent to a fulfillment service for a fulfillment order.
/// <see href="https://shopify.dev/api/admin/rest/reference/shipping-and-fulfillment/assignedfulfillmentorder#index-2021-07 ">API Reference</see>
/// <see href="https://shopify.dev/docs/api/admin-rest/2022-07/resources/fulfillmentrequest#post-fulfillment-orders-fulfillment-order-id-fulfillment-request-reject">API Reference</see>
/// </summary>
/// <param name="fulfillmentOrderId"></param>
/// <param name="message">An optional reason for accepting the fulfillment request</param>
Expand Down
36 changes: 35 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ ShopifySharp currently supports the following Shopify APIs:
- Resource Feedback (not implimented yet)
- Shipping and Fulfillment
- [Assigned Fulfillment Orders](#assigned-fulfillment-orders)
- Cancellation Request (not implimented yet)
- [Cancellation Requests](#cancellation-requests)
- Carrier Service (docs not yet written)
- [Fulfillments](#fulfillments)
- [Fulfillment Events](#fulfillment-events)
Expand Down Expand Up @@ -1379,6 +1379,40 @@ var assignedFulfillments = await service.ListAsync(filterStatus);

---

## Cancellation Requests

The CancellationRequest resource represents a cancellation request made by the merchant or
an order management app to a fulfillment service for a fulfillment order.

### Create A Cancellation Request

Send a cancellation request to the fulfillment service of a fulfillment order.

```c#
var service = new CancellationRequestService(myShopifyUrl, shopAccessToken);
var fulfillmentOrder = await service.CreateAsync(fulfillmentOrderId, "The customer changed his mind.");
```

### Accept A Cancellation Request

Accept a cancellation request sent to a fulfillment service for a fulfillment order.

```c#
var service = new CancellationRequestService(myShopifyUrl, shopAccessToken);
var fulfillmentOrder = await service.AcceptAsync(fulfillmentOrderId, "We had not started any processing yet.");
```

### Reject A Cancellation Request

Reject a cancellation request sent to a fulfillment service for a fulfillment order.

```c#
var service = new CancellationRequestService(myShopifyUrl, shopAccessToken);
var fulfillmentOrder = await service.AcceptAsync(fulfillmentOrderId, "We have already sent the shipment out.");
```

---

## Fulfillment Events

The FulfillmentEvent resource represents tracking events that belong to a fulfillment of one or more items in an order.
Expand Down

0 comments on commit f019a04

Please sign in to comment.