Skip to content

Commit

Permalink
Merge pull request #231 from VictorAvelar/feature/manage-order-lines
Browse files Browse the repository at this point in the history
feature(orders): add manage order lines implementation
  • Loading branch information
VictorAvelar committed Mar 6, 2023
2 parents c499033 + 1e4bf64 commit a4a0603
Show file tree
Hide file tree
Showing 4 changed files with 521 additions and 2 deletions.
101 changes: 99 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# mollie

--

import "github.com/VictorAvelar/mollie-api-golang/v3/mollie"
import "github.com/VictorAvelar/mollie-api-go/v3/mollie"

Package mollie is a wrapper around Mollie's REST API.

Expand Down Expand Up @@ -2153,6 +2152,18 @@ type OrderLine struct {

OrderLine contain the actual things the customer bought.

#### type OrderLineChangeInstruction

```go
type OrderLineChangeInstruction struct {
Operation OrderLineOperation `json:"operation,omitempty"`
Data *OrderLineOperationData `json:"data,omitempty"`
}
```

OrderLineChangeInstruction contains details on what needs to be changed when
managing order lines.

#### type OrderLineLinks

```go
Expand All @@ -2165,6 +2176,78 @@ type OrderLineLinks struct {
OrderLineLinks describes object with several URL objects relevant to the order
line.

#### type OrderLineOperation

```go
type OrderLineOperation string
```

OrderLineOperation describes supported operations when managing order lines.

```go
const (
AddOrderLine OrderLineOperation = "add"
UpdateOrderLine OrderLineOperation = "update"
CancelOrderLine OrderLineOperation = "cancel"
)
```

Supported order lines operation types.

#### type OrderLineOperationData

```go
type OrderLineOperationData struct {
Quantity int `json:"quantity,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
SKU string `json:"sku,omitempty"`
ImageURL string `json:"imageUrl,omitempty"`
ProductURL string `json:"productUrl,omitempty"`
VATRate string `json:"vatRate,omitempty"`
Type string `json:"type,omitempty"`
Category OrderLineOperationProductCategory `json:"category,omitempty"`
Amount *Amount `json:"amount,omitempty"`
UnitPrice *Amount `json:"unitPrice,omitempty"`
DiscountAmount *Amount `json:"discountAmount,omitempty"`
VATAmount *Amount `json:"vatAmount,omitempty"`
TotalAmount *Amount `json:"totalAmount,omitempty"`
Metadata interface{} `json:"metadata,omitempty"`
}
```

OrderLineOperationData contains the order line’s details for an update
operation.

#### type OrderLineOperationProductCategory

```go
type OrderLineOperationProductCategory string
```

OrderLineOperationProductCategory contains the product category.

```go
const (
MealProductCategory OrderLineOperationProductCategory = "meal"
EcoProductCategory OrderLineOperationProductCategory = "eco"
GiftProductCategory OrderLineOperationProductCategory = "gift"
)
```

Product category possible values.

#### type OrderLineOperations

```go
type OrderLineOperations struct {
Operations []*OrderLineChangeInstruction `json:"operations,omitempty"`
}
```

OrderLineOperations contains the operations to be performed when managing order
lines.

#### type OrderLineStatus

```go
Expand Down Expand Up @@ -2427,6 +2510,20 @@ ListOrderRefunds retrieve all order refunds.

See <https://docs.mollie.com/reference/v2/orders-api/list-order-refunds>

#### func (*OrdersService) ManageOrderLines

```go
func (ors *OrdersService) ManageOrderLines(ctx context.Context, orderID string, operations *OrderLineOperations) (
res *Response,
order *Order,
err error,
)
```

ManageOrderLines allows to update, cancel, or add one or more order lines.

See: <https://docs.mollie.com/reference/v2/orders-api/manage-order-lines>

#### func (*OrdersService) Update

```go
Expand Down
72 changes: 72 additions & 0 deletions mollie/orders.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,56 @@ type OrderLine struct {
Metadata interface{} `json:"metadata,omitempty"`
}

// OrderLineOperation describes supported operations when managing order lines.
type OrderLineOperation string

// Supported order lines operation types.
const (
AddOrderLine OrderLineOperation = "add"
UpdateOrderLine OrderLineOperation = "update"
CancelOrderLine OrderLineOperation = "cancel"
)

// OrderLineOperationProductCategory contains the product category.
type OrderLineOperationProductCategory string

// Product category possible values.
const (
MealProductCategory OrderLineOperationProductCategory = "meal"
EcoProductCategory OrderLineOperationProductCategory = "eco"
GiftProductCategory OrderLineOperationProductCategory = "gift"
)

// OrderLineOperationData contains the order line’s details for an update operation.
type OrderLineOperationData struct {
Quantity int `json:"quantity,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
SKU string `json:"sku,omitempty"`
ImageURL string `json:"imageUrl,omitempty"`
ProductURL string `json:"productUrl,omitempty"`
VATRate string `json:"vatRate,omitempty"`
Type string `json:"type,omitempty"`
Category OrderLineOperationProductCategory `json:"category,omitempty"`
Amount *Amount `json:"amount,omitempty"`
UnitPrice *Amount `json:"unitPrice,omitempty"`
DiscountAmount *Amount `json:"discountAmount,omitempty"`
VATAmount *Amount `json:"vatAmount,omitempty"`
TotalAmount *Amount `json:"totalAmount,omitempty"`
Metadata interface{} `json:"metadata,omitempty"`
}

// OrderLineChangeInstruction contains details on what needs to be changed when managing order lines.
type OrderLineChangeInstruction struct {
Operation OrderLineOperation `json:"operation,omitempty"`
Data *OrderLineOperationData `json:"data,omitempty"`
}

// OrderLineOperations contains the operations to be performed when managing order lines.
type OrderLineOperations struct {
Operations []*OrderLineChangeInstruction `json:"operations,omitempty"`
}

// OrderList for containing the response of list orders.
type OrderList struct {
Count int `json:"count,omitempty"`
Expand Down Expand Up @@ -421,3 +471,25 @@ func (ors *OrdersService) ListOrderRefunds(ctx context.Context, orderID string,

return
}

// ManageOrderLines allows to update, cancel, or add one or more order lines.
//
// See: https://docs.mollie.com/reference/v2/orders-api/manage-order-lines
func (ors *OrdersService) ManageOrderLines(ctx context.Context, orderID string, operations *OrderLineOperations) (
res *Response,
order *Order,
err error,
) {
u := fmt.Sprintf("v2/orders/%s/lines", orderID)

res, err = ors.client.patch(ctx, u, operations, nil)
if err != nil {
return
}

if err = json.Unmarshal(res.content, &order); err != nil {
return
}

return
}
166 changes: 166 additions & 0 deletions mollie/orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,172 @@ func (os *ordersServiceSuite) TestOrdersService_ListOrderRefund() {
})
}
}

func (os *ordersServiceSuite) TestOrdersService_ManageOrderLines() {
type args struct {
ctx context.Context
order string
operations *OrderLineOperations
}
cases := []struct {
name string
args args
wantErr bool
err error
pre func()
handler http.HandlerFunc
}{
{
"manage order line works as expected.",
args{
context.Background(),
"ord_pbjz8x",
&OrderLineOperations{
Operations: []*OrderLineChangeInstruction{
{
Operation: AddOrderLine,
Data: &OrderLineOperationData{
ID: "odl_1.1l9vx0",
Name: "new order line",
},
},
},
},
},
false,
nil,
noPre,
func(w http.ResponseWriter, r *http.Request) {
testHeader(os.T(), r, AuthHeader, "Bearer token_X12b31ggg23")
testMethod(os.T(), r, "PATCH")

if _, ok := r.Header[AuthHeader]; !ok {
w.WriteHeader(http.StatusUnauthorized)
}
_, _ = w.Write([]byte(testdata.ManageOrderLinesResponse))
},
},
{
"update order lines works as expected.",
args{
context.Background(),
"ord_kEn1PlbGa",
&OrderLineOperations{
Operations: []*OrderLineChangeInstruction{
{
Operation: AddOrderLine,
Data: &OrderLineOperationData{
ID: "odl_1.1l9vx0",
Name: "new order line",
},
},
},
},
},
false,
nil,
func() {
tClient.WithAuthenticationValue("access_token_test")
},
func(w http.ResponseWriter, r *http.Request) {
testHeader(os.T(), r, AuthHeader, "Bearer access_token_test")
testMethod(os.T(), r, "PATCH")

if _, ok := r.Header[AuthHeader]; !ok {
w.WriteHeader(http.StatusUnauthorized)
}
_, _ = w.Write([]byte(testdata.UpdateOrderlineResponse))
},
},
{
"update order lines, an error is returned from the server",
args{
context.Background(),
"ord_kEn1PlbGa",
&OrderLineOperations{
Operations: []*OrderLineChangeInstruction{
{
Operation: AddOrderLine,
Data: &OrderLineOperationData{
ID: "odl_1.1l9vx0",
Name: "new order line",
},
},
},
},
},
true,
fmt.Errorf("500 Internal Server Error: An internal server error occurred while processing your request."),
noPre,
errorHandler,
},
{
"update order lines, an error occurs when parsing json",
args{
context.Background(),
"ord_kEn1PlbGa",
&OrderLineOperations{
Operations: []*OrderLineChangeInstruction{
{
Operation: AddOrderLine,
Data: &OrderLineOperationData{
ID: "odl_1.1l9vx0",
Name: "new order line",
},
},
},
},
},
true,
fmt.Errorf("invalid character 'h' looking for beginning of object key string"),
noPre,
encodingHandler,
},
{
"update order lines, invalid url when building request",
args{
context.Background(),
"ord_kEn1PlbGa",
&OrderLineOperations{
Operations: []*OrderLineChangeInstruction{
{
Operation: AddOrderLine,
Data: &OrderLineOperationData{
ID: "odl_1.1l9vx0",
Name: "new order line",
},
},
},
},
},
true,
errBadBaseURL,
crashSrv,
errorHandler,
},
}

for _, c := range cases {
setup()
defer teardown()

os.T().Run(c.name, func(t *testing.T) {
c.pre()
tMux.HandleFunc(fmt.Sprintf("/v2/orders/%s/lines", c.args.order), c.handler)

res, m, err := tClient.Orders.ManageOrderLines(c.args.ctx, c.args.order, c.args.operations)
if c.wantErr {
os.NotNil(err)
os.EqualError(err, c.err.Error())
} else {
os.Nil(err)
os.IsType(&Order{}, m)
os.IsType(&http.Response{}, res.Response)
}
})
}
}

func TestOrdersService(t *testing.T) {
suite.Run(t, new(ordersServiceSuite))
}

0 comments on commit a4a0603

Please sign in to comment.