forked from pagarme/pagarme-golang-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createSubscriptionRequest.go
205 lines (199 loc) · 8.62 KB
/
createSubscriptionRequest.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
package models
import (
"encoding/json"
"log"
"time"
)
// Request for creating a subcription
type CreateSubscriptionRequest struct {
Customer CreateCustomerRequest `json:"customer"`
Card CreateCardRequest `json:"card"`
Code string `json:"code"`
PaymentMethod string `json:"payment_method"`
BillingType string `json:"billing_type"`
StatementDescriptor string `json:"statement_descriptor"`
Description string `json:"description"`
Currency string `json:"currency"`
Interval string `json:"interval"`
IntervalCount int `json:"interval_count"`
PricingScheme CreatePricingSchemeRequest `json:"pricing_scheme"`
Items []CreateSubscriptionItemRequest `json:"items"`
Shipping CreateShippingRequest `json:"shipping"`
Discounts []CreateDiscountRequest `json:"discounts"`
Metadata map[string]string `json:"metadata"`
Setup *CreateSetupRequest `json:"setup,omitempty"`
PlanId *string `json:"plan_id,omitempty"`
CustomerId *string `json:"customer_id,omitempty"`
CardId *string `json:"card_id,omitempty"`
BillingDay *int `json:"billing_day,omitempty"`
Installments *int `json:"installments,omitempty"`
StartAt *time.Time `json:"start_at,omitempty"`
MinimumPrice *int `json:"minimum_price,omitempty"`
Cycles *int `json:"cycles,omitempty"`
CardToken *string `json:"card_token,omitempty"`
GatewayAffiliationId *string `json:"gateway_affiliation_id,omitempty"`
Quantity *int `json:"quantity,omitempty"`
BoletoDueDays *int `json:"boleto_due_days,omitempty"`
Increments []CreateIncrementRequest `json:"increments"`
Period *CreatePeriodRequest `json:"period,omitempty"`
Submerchant *CreateSubMerchantRequest `json:"submerchant,omitempty"`
Split *CreateSubscriptionSplitRequest `json:"split,omitempty"`
Boleto *CreateSubscriptionBoletoRequest `json:"boleto,omitempty"`
}
func (c *CreateSubscriptionRequest) MarshalJSON() (
[]byte,
error) {
return json.Marshal(c.toMap())
}
func (c *CreateSubscriptionRequest) toMap() map[string]any {
structMap := make(map[string]any)
structMap["customer"] = c.Customer
structMap["card"] = c.Card
structMap["code"] = c.Code
structMap["payment_method"] = c.PaymentMethod
structMap["billing_type"] = c.BillingType
structMap["statement_descriptor"] = c.StatementDescriptor
structMap["description"] = c.Description
structMap["currency"] = c.Currency
structMap["interval"] = c.Interval
structMap["interval_count"] = c.IntervalCount
structMap["pricing_scheme"] = c.PricingScheme
structMap["items"] = c.Items
structMap["shipping"] = c.Shipping
structMap["discounts"] = c.Discounts
structMap["metadata"] = c.Metadata
if c.Setup != nil {
structMap["setup"] = c.Setup
}
if c.PlanId != nil {
structMap["plan_id"] = c.PlanId
}
if c.CustomerId != nil {
structMap["customer_id"] = c.CustomerId
}
if c.CardId != nil {
structMap["card_id"] = c.CardId
}
if c.BillingDay != nil {
structMap["billing_day"] = c.BillingDay
}
if c.Installments != nil {
structMap["installments"] = c.Installments
}
if c.StartAt != nil {
structMap["start_at"] = c.StartAt.Format(time.RFC3339)
}
if c.MinimumPrice != nil {
structMap["minimum_price"] = c.MinimumPrice
}
if c.Cycles != nil {
structMap["cycles"] = c.Cycles
}
if c.CardToken != nil {
structMap["card_token"] = c.CardToken
}
if c.GatewayAffiliationId != nil {
structMap["gateway_affiliation_id"] = c.GatewayAffiliationId
}
if c.Quantity != nil {
structMap["quantity"] = c.Quantity
}
if c.BoletoDueDays != nil {
structMap["boleto_due_days"] = c.BoletoDueDays
}
structMap["increments"] = c.Increments
if c.Period != nil {
structMap["period"] = c.Period
}
if c.Submerchant != nil {
structMap["submerchant"] = c.Submerchant
}
if c.Split != nil {
structMap["split"] = c.Split
}
if c.Boleto != nil {
structMap["boleto"] = c.Boleto
}
return structMap
}
func (c *CreateSubscriptionRequest) UnmarshalJSON(input []byte) error {
temp := &struct {
Customer CreateCustomerRequest `json:"customer"`
Card CreateCardRequest `json:"card"`
Code string `json:"code"`
PaymentMethod string `json:"payment_method"`
BillingType string `json:"billing_type"`
StatementDescriptor string `json:"statement_descriptor"`
Description string `json:"description"`
Currency string `json:"currency"`
Interval string `json:"interval"`
IntervalCount int `json:"interval_count"`
PricingScheme CreatePricingSchemeRequest `json:"pricing_scheme"`
Items []CreateSubscriptionItemRequest `json:"items"`
Shipping CreateShippingRequest `json:"shipping"`
Discounts []CreateDiscountRequest `json:"discounts"`
Metadata map[string]string `json:"metadata"`
Setup *CreateSetupRequest `json:"setup,omitempty"`
PlanId *string `json:"plan_id,omitempty"`
CustomerId *string `json:"customer_id,omitempty"`
CardId *string `json:"card_id,omitempty"`
BillingDay *int `json:"billing_day,omitempty"`
Installments *int `json:"installments,omitempty"`
StartAt *string `json:"start_at,omitempty"`
MinimumPrice *int `json:"minimum_price,omitempty"`
Cycles *int `json:"cycles,omitempty"`
CardToken *string `json:"card_token,omitempty"`
GatewayAffiliationId *string `json:"gateway_affiliation_id,omitempty"`
Quantity *int `json:"quantity,omitempty"`
BoletoDueDays *int `json:"boleto_due_days,omitempty"`
Increments []CreateIncrementRequest `json:"increments"`
Period *CreatePeriodRequest `json:"period,omitempty"`
Submerchant *CreateSubMerchantRequest `json:"submerchant,omitempty"`
Split *CreateSubscriptionSplitRequest `json:"split,omitempty"`
Boleto *CreateSubscriptionBoletoRequest `json:"boleto,omitempty"`
}{}
err := json.Unmarshal(input, &temp)
if err != nil {
return err
}
c.Customer = temp.Customer
c.Card = temp.Card
c.Code = temp.Code
c.PaymentMethod = temp.PaymentMethod
c.BillingType = temp.BillingType
c.StatementDescriptor = temp.StatementDescriptor
c.Description = temp.Description
c.Currency = temp.Currency
c.Interval = temp.Interval
c.IntervalCount = temp.IntervalCount
c.PricingScheme = temp.PricingScheme
c.Items = temp.Items
c.Shipping = temp.Shipping
c.Discounts = temp.Discounts
c.Metadata = temp.Metadata
c.Setup = temp.Setup
c.PlanId = temp.PlanId
c.CustomerId = temp.CustomerId
c.CardId = temp.CardId
c.BillingDay = temp.BillingDay
c.Installments = temp.Installments
if temp.StartAt != nil {
StartAtVal, err := time.Parse(time.RFC3339, *temp.StartAt)
if err != nil {
log.Fatalf("Cannot Parse start_at as % s format.", time.RFC3339)
}
c.StartAt = &StartAtVal
}
c.MinimumPrice = temp.MinimumPrice
c.Cycles = temp.Cycles
c.CardToken = temp.CardToken
c.GatewayAffiliationId = temp.GatewayAffiliationId
c.Quantity = temp.Quantity
c.BoletoDueDays = temp.BoletoDueDays
c.Increments = temp.Increments
c.Period = temp.Period
c.Submerchant = temp.Submerchant
c.Split = temp.Split
c.Boleto = temp.Boleto
return nil
}