forked from pagarme/pagarme-golang-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createChargeRequest.go
95 lines (88 loc) · 2.9 KB
/
createChargeRequest.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
package models
import (
"encoding/json"
"log"
"time"
"github.com/apimatic/go-core-runtime/types"
)
// Request for creating a new charge
type CreateChargeRequest struct {
Code types.Optional[string] `json:"code"`
Amount int `json:"amount"`
CustomerId types.Optional[string] `json:"customer_id"`
Customer types.Optional[CreateCustomerRequest] `json:"customer"`
Payment CreatePaymentRequest `json:"payment"`
Metadata types.Optional[map[string]string] `json:"metadata"`
DueAt types.Optional[time.Time] `json:"due_at"`
Antifraud types.Optional[CreateAntifraudRequest] `json:"antifraud"`
OrderId string `json:"order_id"`
}
func (c *CreateChargeRequest) MarshalJSON() (
[]byte,
error) {
return json.Marshal(c.toMap())
}
func (c *CreateChargeRequest) toMap() map[string]any {
structMap := make(map[string]any)
if c.Code.IsValueSet() {
structMap["code"] = c.Code.Value()
}
structMap["amount"] = c.Amount
if c.CustomerId.IsValueSet() {
structMap["customer_id"] = c.CustomerId.Value()
}
if c.Customer.IsValueSet() {
structMap["customer"] = c.Customer.Value()
}
structMap["payment"] = c.Payment
if c.Metadata.IsValueSet() {
structMap["metadata"] = c.Metadata.Value()
}
if c.DueAt.IsValueSet() {
var DueAtVal *string = nil
if c.DueAt.Value() != nil {
val := c.DueAt.Value().Format(time.RFC3339)
DueAtVal = &val
}
structMap["due_at"] = DueAtVal
}
if c.Antifraud.IsValueSet() {
structMap["antifraud"] = c.Antifraud.Value()
}
structMap["order_id"] = c.OrderId
return structMap
}
func (c *CreateChargeRequest) UnmarshalJSON(input []byte) error {
temp := &struct {
Code types.Optional[string] `json:"code"`
Amount int `json:"amount"`
CustomerId types.Optional[string] `json:"customer_id"`
Customer types.Optional[CreateCustomerRequest] `json:"customer"`
Payment CreatePaymentRequest `json:"payment"`
Metadata types.Optional[map[string]string] `json:"metadata"`
DueAt types.Optional[string] `json:"due_at"`
Antifraud types.Optional[CreateAntifraudRequest] `json:"antifraud"`
OrderId string `json:"order_id"`
}{}
err := json.Unmarshal(input, &temp)
if err != nil {
return err
}
c.Code = temp.Code
c.Amount = temp.Amount
c.CustomerId = temp.CustomerId
c.Customer = temp.Customer
c.Payment = temp.Payment
c.Metadata = temp.Metadata
c.DueAt.ShouldSetValue(temp.DueAt.IsValueSet())
if temp.DueAt.Value() != nil {
DueAtVal, err := time.Parse(time.RFC3339, (*temp.DueAt.Value()))
if err != nil {
log.Fatalf("Cannot Parse due_at as % s format.", time.RFC3339)
}
c.DueAt.SetValue(&DueAtVal)
}
c.Antifraud = temp.Antifraud
c.OrderId = temp.OrderId
return nil
}