-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayout.go
155 lines (125 loc) · 4.6 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
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
package pawapay
import (
"fmt"
"net/http"
"os"
"time"
"github.com/pariz/gountries"
)
// Config represents the pawapay config
type Config struct {
BaseURL string
APIKey string
LogRequest bool
LogResponse bool
}
// Service is a representation of a pawapay service
type Service struct {
config Config
client *http.Client
}
// ConfigProvider pawapay config provider
type ConfigProvider func() Config
// GetConfigFromEnvVars returns config configurations from environment variables
func GetConfigFromEnvVars() Config {
return Config{
BaseURL: os.Getenv("PAWAPAY_API_URL"),
APIKey: os.Getenv("PAWAPAY_API_KEY"),
}
}
// functionality to allow you log request payload. This is only necessary for debugging as all response
// objects return the api annotation giving all the required details of the request
func (c *Config) AllowRequestLogging() { c.LogRequest = true }
// functionality to allow you log response payload. This is only necessary for debugging as all response
// objects return the api annotation giving all the required details of the request
func (c *Config) AllowResponseLogging() { c.LogResponse = true }
// functionality to allow the package log request and responses
func (c *Config) AllowLogging() {
c.AllowRequestLogging()
c.AllowResponseLogging()
}
// NewService returns a new pawapay service
func NewService(c Config) Service {
return Service{
config: c,
client: &http.Client{Timeout: 60 * time.Second},
}
}
// CreatePayout provides the functionality of creating a payout
// See docs https://docs.pawapay.co.uk/#operation/createPayout for more details
func (s *Service) CreatePayout(timeProvider TimeProviderFunc, payoutReq PayoutRequest) (CreatePayoutResponse, error) {
query := gountries.New()
se, err := query.FindCountryByCallingCode(payoutReq.PhoneNumber.CountryCode)
if err != nil {
return CreatePayoutResponse{}, err
}
countryCode := se.Alpha3
resource := "payouts"
payload := s.newCreatePayoutRequest(timeProvider, payoutReq.PayoutId, payoutReq.Amount, countryCode,
payoutReq.Correspondent, payoutReq.Description, payoutReq.PhoneNumber)
var response CreatePayoutResponse
annotation, err := s.makeRequest(http.MethodPost, resource, payload, &response)
if err != nil {
return CreatePayoutResponse{}, err
}
response.Annotation = annotation
return response, nil
}
// CreateBulkPayout provides the functionality of creating a bulk payout
// See docs https://docs.pawapay.co.uk/#operation/createPayout for more details
func (s *Service) CreateBulkPayout(timeProvider TimeProviderFunc, data []PayoutRequest) (CreateBulkPayoutResponse, error) {
resource := "payouts/bulk"
payload, err := s.newCreateBulkPayoutRequest(timeProvider, data)
if err != nil {
return CreateBulkPayoutResponse{}, err
}
var response []CreatePayoutResponse
annotation, err := s.makeRequest(http.MethodPost, resource, payload, &response)
if err != nil {
return CreateBulkPayoutResponse{}, err
}
return CreateBulkPayoutResponse{Result: response, Annotation: annotation}, nil
}
// GetPayout provides the functionality of retrieving a payout
// See docs https://docs.pawapay.co.uk/#operation/getPayout for more details
func (s *Service) GetPayout(payoutId string) (Payout, error) {
resource := fmt.Sprintf("payouts/%s", payoutId)
var (
response []Payout
result Payout
)
annotation, err := s.makeRequest(http.MethodGet, resource, nil, &response)
if err != nil {
return Payout{}, err
}
if len(response) > 0 {
result = response[0]
}
result.Annotation = annotation
return result, nil
}
// ResendPayoutCallback provides the functionality of resending a callback (webhook)
// See docs https://docs.pawapay.co.uk/#operation/payoutsResendCallback for more details
func (s *Service) ResendPayoutCallback(payoutId string) (PayoutStatusResponse, error) {
resource := "payouts/resend-callback"
payload := ResendCallbackRequest{PayoutId: payoutId}
var response PayoutStatusResponse
annotation, err := s.makeRequest(http.MethodPost, resource, payload, &response)
if err != nil {
return PayoutStatusResponse{}, err
}
response.Annotation = annotation
return response, nil
}
// FailEnqueued provides the functionality of failing an already created payout
// See docs https://docs.pawapay.co.uk/#operation/payoutsFailEnqueued for more details
func (s *Service) FailEnqueued(payoutId string) (PayoutStatusResponse, error) {
resource := fmt.Sprintf("payouts/fail-enqueued/%s", payoutId)
var response PayoutStatusResponse
annotation, err := s.makeRequest(http.MethodPost, resource, nil, &response)
if err != nil {
return PayoutStatusResponse{}, err
}
response.Annotation = annotation
return response, nil
}