forked from checkout/checkout-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payouts.go
170 lines (141 loc) · 3.83 KB
/
payouts.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package accounts
import (
"encoding/json"
"fmt"
"github.com/PaddleHQ/checkout-sdk-go/common"
"github.com/PaddleHQ/checkout-sdk-go/errors"
)
type Frequency string
const (
Weekly Frequency = "Weekly"
Daily Frequency = "Daily"
Monthly Frequency = "Monthly"
)
type DaySchedule string
const (
Monday DaySchedule = "Monday"
Tuesday DaySchedule = "Tuesday"
Wednesday DaySchedule = "Wednesday"
Thursday DaySchedule = "Thursday"
Friday DaySchedule = "Friday"
Saturday DaySchedule = "Saturday"
Sunday DaySchedule = "Sunday"
)
type (
Recurrence interface {
GetSchedule() Frequency
}
scheduleFrequencyDaily struct {
Frequency
}
scheduleFrequencyWeekly struct {
Frequency
ByDay []DaySchedule `json:"by_day,omitempty"`
}
scheduleFrequencyMonthly struct {
Frequency
ByMonthDay []int `json:"by_month_day,omitempty"`
}
)
func NewScheduleFrequencyDailyRequest() scheduleFrequencyDaily {
return scheduleFrequencyDaily{
Frequency: Daily,
}
}
func NewScheduleFrequencyWeeklyRequest(days []DaySchedule) scheduleFrequencyWeekly {
return scheduleFrequencyWeekly{
Frequency: Weekly,
ByDay: days,
}
}
func NewScheduleFrequencyMonthlyRequest(days []int) scheduleFrequencyMonthly {
return scheduleFrequencyMonthly{
Frequency: Monthly,
ByMonthDay: days,
}
}
func (s scheduleFrequencyDaily) GetSchedule() Frequency {
return s.Frequency
}
func (s scheduleFrequencyWeekly) GetSchedule() Frequency {
return s.Frequency
}
func (s scheduleFrequencyMonthly) GetSchedule() Frequency {
return s.Frequency
}
type (
CurrencySchedule struct {
Enabled bool `json:"enabled,omitempty"`
Threshold int `json:"threshold,omitempty"`
Recurrence Recurrence `json:"recurrence,omitempty"`
}
PayoutSchedule struct {
HttpMetadata common.HttpMetadata `json:"http_metadata,omitempty"`
Currency map[common.Currency]CurrencySchedule
Links map[string]common.Link `json:"_links"`
}
)
func (p *PayoutSchedule) UnmarshalJSON(data []byte) error {
p.Currency = make(map[common.Currency]CurrencySchedule)
var currencyMap map[common.Currency]currencyUnmarshaler
if err := json.Unmarshal(data, ¤cyMap); err != nil {
return err
}
var currency CurrencySchedule
for k := range currencyMap {
if k != "_links" {
switch currencyMap[k].Recurrence.Frequency {
case Daily:
var schedule map[common.Currency]dailyScheduleUnmarshaler
if err := json.Unmarshal(data, &schedule); err != nil {
return err
}
currency.Recurrence = schedule[k].Recurrence
case Weekly:
var schedule map[common.Currency]weeklyScheduleUnmarshaler
if err := json.Unmarshal(data, &schedule); err != nil {
return err
}
currency.Recurrence = schedule[k].Recurrence
case Monthly:
var schedule map[common.Currency]monthlyScheduleUnmarshaler
if err := json.Unmarshal(data, &schedule); err != nil {
return err
}
currency.Recurrence = schedule[k].Recurrence
default:
return errors.UnsupportedTypeError(fmt.Sprintf("%s currency frequency is unsupported", k))
}
currency.Enabled = currencyMap[k].Enabled
currency.Threshold = currencyMap[k].Threshold
p.Currency[k] = currency
}
}
var links linksUnmarshaler
if err := json.Unmarshal(data, &links); err != nil {
return err
}
p.Links = links.Links
return nil
}
type (
currencyUnmarshaler struct {
Enabled bool `json:"enabled,omitempty"`
Threshold int `json:"threshold,omitempty"`
Recurrence struct {
Frequency
}
}
linksUnmarshaler struct {
Links map[string]common.Link `json:"_links"`
}
dailyScheduleUnmarshaler struct {
Recurrence scheduleFrequencyDaily `json:"recurrence,omitempty"`
}
weeklyScheduleUnmarshaler struct {
Recurrence scheduleFrequencyWeekly `json:"recurrence,omitempty"`
}
monthlyScheduleUnmarshaler struct {
Recurrence scheduleFrequencyMonthly `json:"recurrence,omitempty"`
}
)