Skip to content

Commit b7d68c3

Browse files
Merge pull request #1972 from stripe/mbroshi/time-dot-time-support
Support time.Time instead of int64 for date fields
2 parents 7383ac8 + 26cce11 commit b7d68c3

File tree

134 files changed

+9715
-866
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+9715
-866
lines changed

account.go

Lines changed: 291 additions & 15 deletions
Large diffs are not rendered by default.

account_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package stripe
33
import (
44
"encoding/json"
55
"testing"
6+
"time"
67

78
assert "github.com/stretchr/testify/require"
89
"github.com/stripe/stripe-go/v81/form"
@@ -138,7 +139,7 @@ func TestAccount_Unmarshal(t *testing.T) {
138139
assert.Equal(t, "value1", account.Metadata["key1"])
139140
assert.Equal(t, "value2", account.Metadata["key2"])
140141

141-
assert.Equal(t, int64(1234567890), account.Requirements.CurrentDeadline)
142+
assert.Equal(t, time.Unix(1234567890, 0), account.Requirements.CurrentDeadline)
142143
assert.Equal(t, 2, len(account.Requirements.CurrentlyDue))
143144
assert.Equal(t, AccountRequirementsDisabledReasonRejectedFraud, account.Requirements.DisabledReason)
144145
assert.Equal(t, 1, len(account.Requirements.Errors))
@@ -156,7 +157,7 @@ func TestAccount_Unmarshal(t *testing.T) {
156157
assert.Equal(t, int64(2), account.Settings.Payouts.Schedule.DelayDays)
157158
assert.Equal(t, AccountSettingsPayoutsScheduleIntervalWeekly, account.Settings.Payouts.Schedule.Interval)
158159

159-
assert.Equal(t, int64(1528573382), account.TOSAcceptance.Date)
160+
assert.Equal(t, time.Unix(1528573382, 0), account.TOSAcceptance.Date)
160161
assert.Equal(t, "127.0.0.1", account.TOSAcceptance.IP)
161162
assert.Equal(t, "user agent", account.TOSAcceptance.UserAgent)
162163
assert.Equal(t, AccountTOSAcceptanceServiceAgreementRecipient, account.TOSAcceptance.ServiceAgreement)

accountlink.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
package stripe
88

9+
import (
10+
"encoding/json"
11+
"time"
12+
)
13+
914
// Specifies the requirements that Stripe collects from connected accounts in the Connect Onboarding flow.
1015
type AccountLinkCollectionOptionsParams struct {
1116
// Specifies whether the platform collects only currently_due requirements (`currently_due`) or both currently_due and eventually_due requirements (`eventually_due`). If you don't specify `collection_options`, the default value is `currently_due`.
@@ -63,11 +68,51 @@ func (p *AccountLinkParams) AddExpand(f string) {
6368
type AccountLink struct {
6469
APIResource
6570
// Time at which the object was created. Measured in seconds since the Unix epoch.
66-
Created int64 `json:"created"`
71+
Created time.Time `json:"created"`
6772
// The timestamp at which this account link will expire.
68-
ExpiresAt int64 `json:"expires_at"`
73+
ExpiresAt time.Time `json:"expires_at"`
6974
// String representing the object's type. Objects of the same type share the same value.
7075
Object string `json:"object"`
7176
// The URL for the account link.
7277
URL string `json:"url"`
7378
}
79+
80+
// UnmarshalJSON handles deserialization of an AccountLink.
81+
// This custom unmarshaling is needed to handle the time fields correctly.
82+
func (a *AccountLink) UnmarshalJSON(data []byte) error {
83+
type accountLink AccountLink
84+
v := struct {
85+
Created int64 `json:"created"`
86+
ExpiresAt int64 `json:"expires_at"`
87+
*accountLink
88+
}{
89+
accountLink: (*accountLink)(a),
90+
}
91+
if err := json.Unmarshal(data, &v); err != nil {
92+
return err
93+
}
94+
95+
a.Created = time.Unix(v.Created, 0)
96+
a.ExpiresAt = time.Unix(v.ExpiresAt, 0)
97+
return nil
98+
}
99+
100+
// MarshalJSON handles serialization of an AccountLink.
101+
// This custom marshaling is needed to handle the time fields correctly.
102+
func (a AccountLink) MarshalJSON() ([]byte, error) {
103+
type accountLink AccountLink
104+
v := struct {
105+
Created int64 `json:"created"`
106+
ExpiresAt int64 `json:"expires_at"`
107+
accountLink
108+
}{
109+
accountLink: (accountLink)(a),
110+
Created: a.Created.Unix(),
111+
ExpiresAt: a.ExpiresAt.Unix(),
112+
}
113+
b, err := json.Marshal(v)
114+
if err != nil {
115+
return nil, err
116+
}
117+
return b, err
118+
}

accountnotice.go

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
package stripe
88

9+
import (
10+
"encoding/json"
11+
"time"
12+
)
13+
914
// Reason the notice is being sent. The reason determines what copy the notice must contain. See the [regulated customer notices](https://stripe.com/docs/issuing/compliance-us/issuing-regulated-customer-notices) guide. All reasons might not apply to your integration, and Stripe might add new reasons in the future, so we recommend an internal warning when you receive an unknown reason.
1015
type AccountNoticeReason string
1116

@@ -50,7 +55,7 @@ type AccountNoticeParams struct {
5055
// Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object. This can be useful for storing additional information about the object in a structured format. Individual keys can be unset by posting an empty value to them. All keys can be unset by posting an empty value to `metadata`.
5156
Metadata map[string]string `form:"metadata"`
5257
// Date when you sent the notice.
53-
SentAt *int64 `form:"sent_at"`
58+
SentAt *time.Time `form:"sent_at"`
5459
}
5560

5661
// AddExpand appends a new field to expand.
@@ -103,9 +108,9 @@ type AccountNoticeLinkedObjects struct {
103108
type AccountNotice struct {
104109
APIResource
105110
// Time at which the object was created. Measured in seconds since the Unix epoch.
106-
Created int64 `json:"created"`
111+
Created time.Time `json:"created"`
107112
// When present, the deadline for sending the notice to meet the relevant regulations.
108-
Deadline int64 `json:"deadline"`
113+
Deadline time.Time `json:"deadline"`
109114
// Information about the email when sent.
110115
Email *AccountNoticeEmail `json:"email"`
111116
// Unique identifier for the object.
@@ -121,7 +126,7 @@ type AccountNotice struct {
121126
// Reason the notice is being sent. The reason determines what copy the notice must contain. See the [regulated customer notices](https://stripe.com/docs/issuing/compliance-us/issuing-regulated-customer-notices) guide. All reasons might not apply to your integration, and Stripe might add new reasons in the future, so we recommend an internal warning when you receive an unknown reason.
122127
Reason AccountNoticeReason `json:"reason"`
123128
// Date when the notice was sent. When absent, you must send the notice, update the content of the email and date when it was sent.
124-
SentAt int64 `json:"sent_at"`
129+
SentAt time.Time `json:"sent_at"`
125130
}
126131

127132
// AccountNoticeList is a list of AccountNotices as retrieved from a list endpoint.
@@ -130,3 +135,47 @@ type AccountNoticeList struct {
130135
ListMeta
131136
Data []*AccountNotice `json:"data"`
132137
}
138+
139+
// UnmarshalJSON handles deserialization of an AccountNotice.
140+
// This custom unmarshaling is needed to handle the time fields correctly.
141+
func (a *AccountNotice) UnmarshalJSON(data []byte) error {
142+
type accountNotice AccountNotice
143+
v := struct {
144+
Created int64 `json:"created"`
145+
Deadline int64 `json:"deadline"`
146+
SentAt int64 `json:"sent_at"`
147+
*accountNotice
148+
}{
149+
accountNotice: (*accountNotice)(a),
150+
}
151+
if err := json.Unmarshal(data, &v); err != nil {
152+
return err
153+
}
154+
155+
a.Created = time.Unix(v.Created, 0)
156+
a.Deadline = time.Unix(v.Deadline, 0)
157+
a.SentAt = time.Unix(v.SentAt, 0)
158+
return nil
159+
}
160+
161+
// MarshalJSON handles serialization of an AccountNotice.
162+
// This custom marshaling is needed to handle the time fields correctly.
163+
func (a AccountNotice) MarshalJSON() ([]byte, error) {
164+
type accountNotice AccountNotice
165+
v := struct {
166+
Created int64 `json:"created"`
167+
Deadline int64 `json:"deadline"`
168+
SentAt int64 `json:"sent_at"`
169+
accountNotice
170+
}{
171+
accountNotice: (accountNotice)(a),
172+
Created: a.Created.Unix(),
173+
Deadline: a.Deadline.Unix(),
174+
SentAt: a.SentAt.Unix(),
175+
}
176+
b, err := json.Marshal(v)
177+
if err != nil {
178+
return nil, err
179+
}
180+
return b, err
181+
}

accountsession.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
package stripe
88

9+
import (
10+
"encoding/json"
11+
"time"
12+
)
13+
914
// The list of features enabled in the embedded component.
1015
type AccountSessionComponentsAccountManagementFeaturesParams struct {
1116
// Disables Stripe user authentication for this embedded component. This value can only be true for accounts where `controller.requirement_collection` is `application`. The default value is the opposite of the `external_account_collection` value. For example, if you don't set `external_account_collection`, it defaults to true and `disable_stripe_user_authentication` defaults to false.
@@ -679,9 +684,45 @@ type AccountSession struct {
679684
ClientSecret string `json:"client_secret"`
680685
Components *AccountSessionComponents `json:"components"`
681686
// The timestamp at which this AccountSession will expire.
682-
ExpiresAt int64 `json:"expires_at"`
687+
ExpiresAt time.Time `json:"expires_at"`
683688
// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
684689
Livemode bool `json:"livemode"`
685690
// String representing the object's type. Objects of the same type share the same value.
686691
Object string `json:"object"`
687692
}
693+
694+
// UnmarshalJSON handles deserialization of an AccountSession.
695+
// This custom unmarshaling is needed to handle the time fields correctly.
696+
func (a *AccountSession) UnmarshalJSON(data []byte) error {
697+
type accountSession AccountSession
698+
v := struct {
699+
ExpiresAt int64 `json:"expires_at"`
700+
*accountSession
701+
}{
702+
accountSession: (*accountSession)(a),
703+
}
704+
if err := json.Unmarshal(data, &v); err != nil {
705+
return err
706+
}
707+
708+
a.ExpiresAt = time.Unix(v.ExpiresAt, 0)
709+
return nil
710+
}
711+
712+
// MarshalJSON handles serialization of an AccountSession.
713+
// This custom marshaling is needed to handle the time fields correctly.
714+
func (a AccountSession) MarshalJSON() ([]byte, error) {
715+
type accountSession AccountSession
716+
v := struct {
717+
ExpiresAt int64 `json:"expires_at"`
718+
accountSession
719+
}{
720+
accountSession: (accountSession)(a),
721+
ExpiresAt: a.ExpiresAt.Unix(),
722+
}
723+
b, err := json.Marshal(v)
724+
if err != nil {
725+
return nil, err
726+
}
727+
return b, err
728+
}

applepaydomain.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
package stripe
88

9+
import (
10+
"encoding/json"
11+
"time"
12+
)
13+
914
// Delete an apple pay domain.
1015
type ApplePayDomainParams struct {
1116
Params `form:"*"`
@@ -35,9 +40,9 @@ func (p *ApplePayDomainListParams) AddExpand(f string) {
3540
type ApplePayDomain struct {
3641
APIResource
3742
// Time at which the object was created. Measured in seconds since the Unix epoch.
38-
Created int64 `json:"created"`
39-
Deleted bool `json:"deleted"`
40-
DomainName string `json:"domain_name"`
43+
Created time.Time `json:"created"`
44+
Deleted bool `json:"deleted"`
45+
DomainName string `json:"domain_name"`
4146
// Unique identifier for the object.
4247
ID string `json:"id"`
4348
// Has the value `true` if the object exists in live mode or the value `false` if the object exists in test mode.
@@ -52,3 +57,39 @@ type ApplePayDomainList struct {
5257
ListMeta
5358
Data []*ApplePayDomain `json:"data"`
5459
}
60+
61+
// UnmarshalJSON handles deserialization of an ApplePayDomain.
62+
// This custom unmarshaling is needed to handle the time fields correctly.
63+
func (a *ApplePayDomain) UnmarshalJSON(data []byte) error {
64+
type applePayDomain ApplePayDomain
65+
v := struct {
66+
Created int64 `json:"created"`
67+
*applePayDomain
68+
}{
69+
applePayDomain: (*applePayDomain)(a),
70+
}
71+
if err := json.Unmarshal(data, &v); err != nil {
72+
return err
73+
}
74+
75+
a.Created = time.Unix(v.Created, 0)
76+
return nil
77+
}
78+
79+
// MarshalJSON handles serialization of an ApplePayDomain.
80+
// This custom marshaling is needed to handle the time fields correctly.
81+
func (a ApplePayDomain) MarshalJSON() ([]byte, error) {
82+
type applePayDomain ApplePayDomain
83+
v := struct {
84+
Created int64 `json:"created"`
85+
applePayDomain
86+
}{
87+
applePayDomain: (applePayDomain)(a),
88+
Created: a.Created.Unix(),
89+
}
90+
b, err := json.Marshal(v)
91+
if err != nil {
92+
return nil, err
93+
}
94+
return b, err
95+
}

applicationfee.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
package stripe
88

9-
import "encoding/json"
9+
import (
10+
"encoding/json"
11+
"time"
12+
)
1013

1114
// Type of object that created the application fee, either `charge` or `payout`.
1215
type ApplicationFeeFeeSourceType string
@@ -71,7 +74,7 @@ type ApplicationFee struct {
7174
// ID of the charge that the application fee was taken from.
7275
Charge *Charge `json:"charge"`
7376
// Time at which the object was created. Measured in seconds since the Unix epoch.
74-
Created int64 `json:"created"`
77+
Created time.Time `json:"created"`
7578
// Three-letter [ISO currency code](https://www.iso.org/iso-4217-currency-codes.html), in lowercase. Must be a [supported currency](https://stripe.com/docs/currencies).
7679
Currency Currency `json:"currency"`
7780
// Polymorphic source of the application fee. Includes the ID of the object the application fee was created from.
@@ -107,11 +110,34 @@ func (a *ApplicationFee) UnmarshalJSON(data []byte) error {
107110
}
108111

109112
type applicationFee ApplicationFee
110-
var v applicationFee
113+
v := struct {
114+
Created int64 `json:"created"`
115+
*applicationFee
116+
}{
117+
applicationFee: (*applicationFee)(a),
118+
}
111119
if err := json.Unmarshal(data, &v); err != nil {
112120
return err
113121
}
114122

115-
*a = ApplicationFee(v)
123+
a.Created = time.Unix(v.Created, 0)
116124
return nil
117125
}
126+
127+
// MarshalJSON handles serialization of an ApplicationFee.
128+
// This custom marshaling is needed to handle the time fields correctly.
129+
func (a ApplicationFee) MarshalJSON() ([]byte, error) {
130+
type applicationFee ApplicationFee
131+
v := struct {
132+
Created int64 `json:"created"`
133+
applicationFee
134+
}{
135+
applicationFee: (applicationFee)(a),
136+
Created: a.Created.Unix(),
137+
}
138+
b, err := json.Marshal(v)
139+
if err != nil {
140+
return nil, err
141+
}
142+
return b, err
143+
}

0 commit comments

Comments
 (0)