forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
refund.go
63 lines (54 loc) · 2.09 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
61
62
63
package goshopify
import "fmt"
// RefundService is an interface for interfacing with the refunds endpoints of
// the Shopify API.
// See: https://shopify.dev/docs/admin-api/rest/reference/orders/refund
type RefundService interface {
List(int64, interface{}) ([]Refund, error)
Get(int64, int64, interface{}) (*Refund, error)
Calculate(int64, Refund) (*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
}
// 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
}
// Calculate a new refund
func (s *RefundServiceOp) Calculate(orderID int64, refund Refund) (*Refund, error) {
path := fmt.Sprintf("%s/%d/refunds/calculate.json", ordersBasePath, orderID)
wrappedData := RefundResource{Refund: &refund}
resource := new(RefundResource)
err := s.client.Post(path, wrappedData, resource)
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
}