-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
219 lines (173 loc) · 5.88 KB
/
types.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package client
import (
"encoding/json"
"net/http"
"time"
log "github.com/sirupsen/logrus"
)
//ChargeStatus represent the status of requested Charge
type ChargeStatus int
const (
//Required Charge sent to a user waitng for acceptance
Required ChargeStatus = iota
//Success Charge accepted by the user
Success
//Failure Charge failed, more details can be found on ChargeStatusDetail
Failure
)
//ChargeStatusDetail represent the detail regarding a failure of Charge operation
type ChargeStatusDetail int
const (
//DeclinedByPayer user declined the Charge
DeclinedByPayer ChargeStatusDetail = iota
//DeclinedByPayerNotRequired user declined the Charge because he did not request it
DeclinedByPayerNotRequired
//CancelByNewCharge same Charge sent to the same user, the second will override the first
CancelByNewCharge
//InternalFailure generic error
InternalFailure
//Expired the Charge has expired
Expired
)
//RefundReason indicating the reason for the refund.
type RefundReason int
const (
//Duplicate means a charge payed twice for some reason
Duplicate RefundReason = iota
//Fraudolent means that a charge is fraudolent
Fraudolent
//RequestedByCustomer for other reason requested by customer
RequestedByCustomer
)
//Client is the main structure of this library, represent the main client in order to interact with Satispay platform
type Client struct {
bearerToken string
endpoint string
httpClient *http.Client
}
//Represent satispay error message
type SatispayError struct {
Code int `json:"code"`
Message string `json:"message"`
}
//User represent a Satispay user resource
type User struct {
ID string `json:"id"`
PhoneNumber string `json:"phone_number"`
}
//ChargeRequest represent a Satispay charge request for a target user identified by its id
type ChargeRequest struct {
UserID string `json:"user_id"`
Description string `json:"description"`
Currency string `json:"currency"`
CallBackURL string `json:"callback_url"`
Amount int64 `json:"amount"`
Metdata map[string]string `json:"metadata"`
RequiredSuccessEmail bool `json:"required_success_email"`
ExpireIn int `json:"expire_in"`
}
//Charge represent a Satispay charge
type Charge struct {
ID string `json:"id"`
Description string `json:"description"`
Currency string `json:"currency"`
Amount int64 `json:"amount"`
Status ChargeStatus `json:"status"`
StatusDetail ChargeStatusDetail `json:"status_detail"`
UserID string `json:"user_id"`
UserShortName string `json:"user_short_name"`
Metadata map[string]string `json:"metadata"`
RequiredSuccessEmail bool `json:"required_success_email"`
ExpireDate time.Time `json:"expire_date"`
CallbackURL string `json:"callback_url"`
}
//RefundRequest represent the request for refund in Satispay Platform
type RefundRequest struct {
ChargeID string `json:"charge_id"`
Description string `json:"description"`
Amount int64 `json:"amount"`
Currency string `json:"currency"`
Reason RefundReason `json:"reason"`
Metadata map[string]string `json:"metadata"`
}
type Refund struct {
ID string `json:"id"`
*RefundRequest
}
type TotalAmount struct {
TotalChargeAmountUnit int64 `json:"total_charge_amount_unit"`
TotalRefundAmountUnit int64 `json:"total_refund_amount_unit"`
Currency string `json:"currency"`
}
// Private types and constants
const (
productionEndpoint = "https://authservices.satispay.com/online"
sandBoxAPIEndpoint = "https://staging.authservices.satispay.com/online"
)
type newUser struct {
PhoneNumber string `json:"phone_number"`
}
type userListResponse struct {
HasMore bool `json:"has_more"`
List []User `json:"list"`
}
type chargeListResponse struct {
HasMore bool `json:"has_more"`
List []Charge `json:"list"`
}
type chargeUpdate struct {
Description string `json:"description,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
ChangeState string `json:"change_state,omitempty"`
}
type refundListResponse struct {
HasMore bool `json:"has_more"`
List []Refund `json:"list"`
}
type refundUpdate struct {
Metadata map[string]string `json:"metadata"`
}
//String is the implementation of Stringer interface for ChargeRequest
func (request *ChargeRequest) String() string {
jsonifiedRequest, err := json.Marshal(request)
if err != nil {
log.Errorf("Got error while marshaling request %v", err)
return ""
}
return string(jsonifiedRequest)
}
func (charge Charge) String() string {
jsonifiedRequest, err := json.Marshal(&charge)
if err != nil {
log.Errorf("Got error while marshaling request %v", err)
return ""
}
return string(jsonifiedRequest)
}
//String is the implementation of Stringer interface for RefundRequest
func (request *RefundRequest) String() string {
jsonifiedRequest, err := json.Marshal(request)
if err != nil {
log.Errorf("Got error while marshaling request %v", err)
return ""
}
return string(jsonifiedRequest)
}
//String is the implementation of Stringer interface for chargeUpdate
func (update chargeUpdate) String() string {
jsonifiedUpdate, err := json.Marshal(update)
if err != nil {
log.Errorf("Got error while marshaling update %v", err)
return ""
}
return string(jsonifiedUpdate)
}
//String is the implementation of Stringer interface for refundUpdate
func (update refundUpdate) String() string {
jsonifiedUpdate, err := json.Marshal(update)
if err != nil {
log.Errorf("Got error while marshaling update %v", err)
return ""
}
return string(jsonifiedUpdate)
}