forked from getconversio/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 256
/
order_risk.go
153 lines (126 loc) · 5.56 KB
/
order_risk.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
package goshopify
import (
"context"
"fmt"
)
const (
ordersRiskBasePath = "orders"
ordersRiskResourceName = "risks"
)
// OrderRiskService is an interface for interfacing with the orders Risk endpoints of
// the Shopify API.
// See: https://shopify.dev/docs/api/admin-rest/2023-10/resources/order-risk
type OrderRiskService interface {
List(context.Context, uint64, interface{}) ([]OrderRisk, error)
ListAll(context.Context, uint64, interface{}) ([]OrderRisk, error)
ListWithPagination(context.Context, uint64, interface{}) ([]OrderRisk, *Pagination, error)
Get(context.Context, uint64, uint64, interface{}) (*OrderRisk, error)
Create(context.Context, uint64, OrderRisk) (*OrderRisk, error)
Update(context.Context, uint64, uint64, OrderRisk) (*OrderRisk, error)
Delete(context.Context, uint64, uint64) error
}
// OrderRiskServiceOp handles communication with the order related methods of the
// Shopify API.
type OrderRiskServiceOp struct {
client *Client
}
// Represents the result from the orders-risk/X.json endpoint
type OrderRiskResource struct {
OrderRisk *OrderRisk `json:"risk"`
}
// Represents the result from the orders-risk.json endpoint
type OrdersRisksResource struct {
OrderRisk []OrderRisk `json:"risks"`
}
// OrderRiskRecommendation The recommended action given to the merchant.
//
// https://shopify.dev/docs/api/admin-rest/2024-04/resources/order-risk#resource-object
type OrderRiskRecommendation string
const (
// OrderRecommendationCancel There is a high level of risk that this order is fraudulent. The merchant should cancel the order.
OrderRecommendationCancel OrderRiskRecommendation = "cancel"
// OrderRecommendationInvestigate There is a medium level of risk that this order is fraudulent. The merchant should investigate the order.
OrderRecommendationInvestigate OrderRiskRecommendation = "investigate"
// OrderRecommendationAccept There is a low level of risk that this order is fraudulent. The order risk found no indication of fraud.
OrderRecommendationAccept OrderRiskRecommendation = "accept"
)
// A struct for all available order Risk list options.
// See: https://shopify.dev/docs/api/admin-rest/2023-10/resources/order-risk#index
type OrderRiskListOptions struct {
ListOptions
}
// OrderRisk represents a Shopify order risk
type OrderRisk struct {
Id uint64 `json:"id,omitempty"`
CheckoutId uint64 `json:"checkout_id,omitempty"`
OrderId uint64 `json:"order_id,omitempty"`
CauseCancel bool `json:"cause_cancel,omitempty"`
Display bool `json:"display,omitempty"`
MerchantMessage string `json:"merchant_message,omitempty"`
Message string `json:"message,omitempty"`
Score string `json:"score,omitempty"`
Source string `json:"source,omitempty"`
Recommendation OrderRiskRecommendation `json:"recommendation,omitempty"`
}
// List OrderRisk
func (s *OrderRiskServiceOp) List(ctx context.Context, orderId uint64, options interface{}) ([]OrderRisk, error) {
orders, _, err := s.ListWithPagination(ctx, orderId, options)
if err != nil {
return nil, err
}
return orders, nil
}
// ListAll Lists all OrderRisk, iterating over pages
func (s *OrderRiskServiceOp) ListAll(ctx context.Context, orderId uint64, options interface{}) ([]OrderRisk, error) {
collector := []OrderRisk{}
for {
entities, pagination, err := s.ListWithPagination(ctx, orderId, options)
if err != nil {
return collector, err
}
collector = append(collector, entities...)
if pagination.NextPageOptions == nil {
break
}
options = pagination.NextPageOptions
}
return collector, nil
}
func (s *OrderRiskServiceOp) ListWithPagination(ctx context.Context, orderId uint64, options interface{}) ([]OrderRisk, *Pagination, error) {
path := fmt.Sprintf("%s/%d/%s.json", ordersRiskBasePath, orderId, ordersRiskResourceName)
resource := new(OrdersRisksResource)
pagination, err := s.client.ListWithPagination(ctx, path, resource, options)
if err != nil {
return nil, nil, err
}
return resource.OrderRisk, pagination, nil
}
// Get individual order
func (s *OrderRiskServiceOp) Get(ctx context.Context, orderId uint64, riskId uint64, options interface{}) (*OrderRisk, error) {
path := fmt.Sprintf("%s/%d/%s/%d.json", ordersRiskBasePath, orderId, ordersRiskResourceName, riskId)
resource := new(OrderRiskResource)
err := s.client.Get(ctx, path, resource, options)
return resource.OrderRisk, err
}
// Create order
func (s *OrderRiskServiceOp) Create(ctx context.Context, orderId uint64, orderRisk OrderRisk) (*OrderRisk, error) {
path := fmt.Sprintf("%s/%d/%s.json", ordersRiskBasePath, orderId, ordersRiskResourceName)
wrappedData := OrderRiskResource{OrderRisk: &orderRisk}
resource := new(OrderRiskResource)
err := s.client.Post(ctx, path, wrappedData, resource)
return resource.OrderRisk, err
}
// Update order
func (s *OrderRiskServiceOp) Update(ctx context.Context, orderId uint64, riskId uint64, orderRisk OrderRisk) (*OrderRisk, error) {
path := fmt.Sprintf("%s/%d/%s/%d.json", ordersRiskBasePath, orderId, ordersRiskResourceName, riskId)
wrappedData := OrderRiskResource{OrderRisk: &orderRisk}
resource := new(OrderRiskResource)
err := s.client.Put(ctx, path, wrappedData, resource)
return resource.OrderRisk, err
}
// Delete order
func (s *OrderRiskServiceOp) Delete(ctx context.Context, orderId uint64, riskId uint64) error {
path := fmt.Sprintf("%s/%d/%s/%d.json", ordersRiskBasePath, orderId, ordersRiskResourceName, riskId)
err := s.client.Delete(ctx, path)
return err
}