forked from lukecyca/go-shopify
/
refund.go
60 lines (51 loc) · 1.94 KB
/
refund.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
package goshopify
import "fmt"
// RefundService is an interface for interfacing with the refunds endpoints of
// the Shopify API.
// See: https://help.shopify.com/api/reference/refund
type RefundService interface {
List(int64, interface{}) ([]Refund, error)
Count(int64, interface{}) (int, error)
Get(int64, int64, interface{}) (*Refund, error)
Create(int64, Refund) (*Refund, error)
}
// RefundServiceOp handles communication with the refund related methods of the
// Shopify API.
type RefundServiceOp struct {
client *Client
}
// RefundResource represents the result from the orders/X/refunds/Y.json endpoint
type RefundResource struct {
Refund *Refund `json:"refund"`
}
// RefundsResource represents the result from the orders/X/refunds.json endpoint
type RefundsResource struct {
Refunds []Refund `json:"refunds"`
}
// List refunds
func (s *RefundServiceOp) List(orderID int64, options interface{}) ([]Refund, error) {
path := fmt.Sprintf("%s/%d/refunds.json", ordersBasePath, orderID)
resource := new(RefundsResource)
err := s.client.Get(path, resource, options)
return resource.Refunds, err
}
// Count refunds
func (s *RefundServiceOp) Count(orderID int64, options interface{}) (int, error) {
path := fmt.Sprintf("%s/%d/refunds/count.json", ordersBasePath, orderID)
return s.client.Count(path, options)
}
// Get individual refund
func (s *RefundServiceOp) Get(orderID int64, refundID int64, options interface{}) (*Refund, error) {
path := fmt.Sprintf("%s/%d/refunds/%d.json", ordersBasePath, orderID, refundID)
resource := new(RefundResource)
err := s.client.Get(path, resource, options)
return resource.Refund, err
}
// Create a new refund
func (s *RefundServiceOp) Create(orderID int64, refund Refund) (*Refund, error) {
path := fmt.Sprintf("%s/%d/refunds.json", ordersBasePath, orderID)
wrappedData := RefundResource{Refund: &refund}
resource := new(RefundResource)
err := s.client.Post(path, wrappedData, resource)
return resource.Refund, err
}