forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payout.go
114 lines (93 loc) · 3.77 KB
/
payout.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
package goshopify
import (
// "encoding/json"
"fmt"
"net/http"
// "time"
"github.com/shopspring/decimal"
)
const payoutsBasePath = "payouts"
const payoutsResourceName = "payouts"
// PayoutService is an interface for interfacing with the payouts endpoints of
// the Shopify API.
// See: https://help.shopify.com/api/reference/shopify_payments
type PayoutService interface {
List(interface{}) ([]Payout, error)
ListWithPagination(interface{}) ([]Payout, *Pagination, error)
Get(int64, interface{}) (*Payout, error)
}
// PayoutServiceOp handles communication with the payout related methods of the
// Shopify API.
type PayoutServiceOp struct {
client *Client
}
// A struct for all available payout list options.
// See: https://help.shopify.com/api/reference/payout#index
type PayoutListOptions struct {
SinceId int64 `url:"since_id,omitempty"`
LastId int64 `url:"last_id,omitempty"`
DateMin string `url:"date_min,omitempty"`
DateMax string `url:"date_max,omitempty"`
Date string `url:"date,omitempty"`
Status string `url:"status,omitempty"`
}
// Payout represents a Shopify payout
type Payout struct {
ID int64 `json:"id,omitempty"`
Status string `json:"status,omitempty"`
Date string `json:"date,omitempty"`
Currency string `json:"currency,omitempty"`
Amount *decimal.Decimal `json:"amount,omitempty"`
Summary *Summary `json:"summary,omitempty"`
}
type Summary struct {
AdjustmentsFeeAmount *decimal.Decimal `json:"adjustments_fee_amount,omitempty"`
AdjustmentsGrossAmount *decimal.Decimal `json:"adjustments_gross_amount,omitempty"`
ChargesFeeAmount *decimal.Decimal `json:"charges_fee_amount,omitempty"`
ChargesGrossAmount *decimal.Decimal `json:"charges_gross_amount,omitempty"`
RefundsFeeAmount *decimal.Decimal `json:"refunds_fee_amount,omitempty"`
RefundsGrossAmount *decimal.Decimal `json:"refunds_gross_amount,omitempty"`
ReservedFundsFeeAmount *decimal.Decimal `json:"reserved_funds_fee_amount,omitempty"`
ReservedFundsGrossAmount *decimal.Decimal `json:"reserved_funds_gross_amount,omitempty"`
RetriedPayoutsFeeAmount *decimal.Decimal `json:"retried_payouts_fee_amount,omitempty"`
RetriedPayoutsGrossAmount *decimal.Decimal `json:"retried_payouts_gross_amount,omitempty"`
}
// Represents the result from the shopify_payments/payouts/X.json endpoint
type PayoutResource struct {
Payout *Payout `json:"payout"`
}
// Represents the result from the shopify_payments/payouts.json endpoint
type PayoutsResource struct {
Payouts []Payout `json:"payouts"`
}
// List payouts
func (s *PayoutServiceOp) List(options interface{}) ([]Payout, error) {
payouts, _, err := s.ListWithPagination(options)
if err != nil {
return nil, err
}
return payouts, nil
}
func (s *PayoutServiceOp) ListWithPagination(options interface{}) ([]Payout, *Pagination, error) {
path := fmt.Sprintf("shopify_payments/%s.json", payoutsBasePath)
resource := new(PayoutsResource)
headers := http.Header{}
headers, err := s.client.createAndDoGetHeaders("GET", path, nil, options, resource)
if err != nil {
return nil, nil, err
}
// Extract pagination info from header
linkHeader := headers.Get("Link")
pagination, err := extractPagination(linkHeader)
if err != nil {
return nil, nil, err
}
return resource.Payouts, pagination, nil
}
// Get individual payout
func (s *PayoutServiceOp) Get(payoutID int64, options interface{}) (*Payout, error) {
path := fmt.Sprintf("shopify_payments/%s/%d.json", payoutsBasePath, payoutID)
resource := new(PayoutResource)
err := s.client.Get(path, resource, options)
return resource.Payout, err
}