-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepayment.go
134 lines (126 loc) · 5.09 KB
/
prepayment.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
package models
import (
"encoding/json"
"errors"
"log"
"strings"
"time"
)
// Prepayment represents a Prepayment struct.
type Prepayment struct {
Id int `json:"id"`
SubscriptionId int `json:"subscription_id"`
AmountInCents int64 `json:"amount_in_cents"`
RemainingAmountInCents int64 `json:"remaining_amount_in_cents"`
RefundedAmountInCents *int64 `json:"refunded_amount_in_cents,omitempty"`
Details *string `json:"details,omitempty"`
External bool `json:"external"`
Memo string `json:"memo"`
// The payment type of the prepayment.
PaymentType *PrepaymentMethod `json:"payment_type,omitempty"`
CreatedAt time.Time `json:"created_at"`
AdditionalProperties map[string]any `json:"_"`
}
// MarshalJSON implements the json.Marshaler interface for Prepayment.
// It customizes the JSON marshaling process for Prepayment objects.
func (p Prepayment) MarshalJSON() (
[]byte,
error) {
return json.Marshal(p.toMap())
}
// toMap converts the Prepayment object to a map representation for JSON marshaling.
func (p Prepayment) toMap() map[string]any {
structMap := make(map[string]any)
MapAdditionalProperties(structMap, p.AdditionalProperties)
structMap["id"] = p.Id
structMap["subscription_id"] = p.SubscriptionId
structMap["amount_in_cents"] = p.AmountInCents
structMap["remaining_amount_in_cents"] = p.RemainingAmountInCents
if p.RefundedAmountInCents != nil {
structMap["refunded_amount_in_cents"] = p.RefundedAmountInCents
}
if p.Details != nil {
structMap["details"] = p.Details
}
structMap["external"] = p.External
structMap["memo"] = p.Memo
if p.PaymentType != nil {
structMap["payment_type"] = p.PaymentType
}
structMap["created_at"] = p.CreatedAt.Format(time.RFC3339)
return structMap
}
// UnmarshalJSON implements the json.Unmarshaler interface for Prepayment.
// It customizes the JSON unmarshaling process for Prepayment objects.
func (p *Prepayment) UnmarshalJSON(input []byte) error {
var temp prepayment
err := json.Unmarshal(input, &temp)
if err != nil {
return err
}
err = temp.validate()
if err != nil {
return err
}
additionalProperties, err := UnmarshalAdditionalProperties(input, "id", "subscription_id", "amount_in_cents", "remaining_amount_in_cents", "refunded_amount_in_cents", "details", "external", "memo", "payment_type", "created_at")
if err != nil {
return err
}
p.AdditionalProperties = additionalProperties
p.Id = *temp.Id
p.SubscriptionId = *temp.SubscriptionId
p.AmountInCents = *temp.AmountInCents
p.RemainingAmountInCents = *temp.RemainingAmountInCents
p.RefundedAmountInCents = temp.RefundedAmountInCents
p.Details = temp.Details
p.External = *temp.External
p.Memo = *temp.Memo
p.PaymentType = temp.PaymentType
CreatedAtVal, err := time.Parse(time.RFC3339, *temp.CreatedAt)
if err != nil {
log.Fatalf("Cannot Parse created_at as % s format.", time.RFC3339)
}
p.CreatedAt = CreatedAtVal
return nil
}
// prepayment is a temporary struct used for validating the fields of Prepayment.
type prepayment struct {
Id *int `json:"id"`
SubscriptionId *int `json:"subscription_id"`
AmountInCents *int64 `json:"amount_in_cents"`
RemainingAmountInCents *int64 `json:"remaining_amount_in_cents"`
RefundedAmountInCents *int64 `json:"refunded_amount_in_cents,omitempty"`
Details *string `json:"details,omitempty"`
External *bool `json:"external"`
Memo *string `json:"memo"`
PaymentType *PrepaymentMethod `json:"payment_type,omitempty"`
CreatedAt *string `json:"created_at"`
}
func (p *prepayment) validate() error {
var errs []string
if p.Id == nil {
errs = append(errs, "required field `id` is missing for type `Prepayment`")
}
if p.SubscriptionId == nil {
errs = append(errs, "required field `subscription_id` is missing for type `Prepayment`")
}
if p.AmountInCents == nil {
errs = append(errs, "required field `amount_in_cents` is missing for type `Prepayment`")
}
if p.RemainingAmountInCents == nil {
errs = append(errs, "required field `remaining_amount_in_cents` is missing for type `Prepayment`")
}
if p.External == nil {
errs = append(errs, "required field `external` is missing for type `Prepayment`")
}
if p.Memo == nil {
errs = append(errs, "required field `memo` is missing for type `Prepayment`")
}
if p.CreatedAt == nil {
errs = append(errs, "required field `created_at` is missing for type `Prepayment`")
}
if len(errs) == 0 {
return nil
}
return errors.New(strings.Join(errs, "\n"))
}