forked from pagarme/pagarme-golang-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createAnticipationRequest.go
49 lines (43 loc) · 1.14 KB
/
createAnticipationRequest.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
package models
import (
"encoding/json"
"log"
"time"
)
// Request for creating an anticipation
type CreateAnticipationRequest struct {
Amount int `json:"amount"`
Timeframe string `json:"timeframe"`
PaymentDate time.Time `json:"payment_date"`
}
func (c *CreateAnticipationRequest) MarshalJSON() (
[]byte,
error) {
return json.Marshal(c.toMap())
}
func (c *CreateAnticipationRequest) toMap() map[string]any {
structMap := make(map[string]any)
structMap["amount"] = c.Amount
structMap["timeframe"] = c.Timeframe
structMap["payment_date"] = c.PaymentDate.Format(time.RFC3339)
return structMap
}
func (c *CreateAnticipationRequest) UnmarshalJSON(input []byte) error {
temp := &struct {
Amount int `json:"amount"`
Timeframe string `json:"timeframe"`
PaymentDate string `json:"payment_date"`
}{}
err := json.Unmarshal(input, &temp)
if err != nil {
return err
}
c.Amount = temp.Amount
c.Timeframe = temp.Timeframe
PaymentDateVal, err := time.Parse(time.RFC3339, temp.PaymentDate)
if err != nil {
log.Fatalf("Cannot Parse payment_date as % s format.", time.RFC3339)
}
c.PaymentDate = PaymentDateVal
return nil
}