Skip to content

Commit e0418a5

Browse files
Support time.Time
1 parent 2167b71 commit e0418a5

File tree

7 files changed

+51
-11
lines changed

7 files changed

+51
-11
lines changed

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)

form/form.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strconv"
1010
"strings"
1111
"sync"
12+
"time"
1213
)
1314

1415
const tagName = "form"
@@ -325,6 +326,14 @@ func intEncoder(values *Values, v reflect.Value, keyParts []string, encodeZero b
325326
values.Add(FormatKey(keyParts), strconv.FormatInt(val, 10))
326327
}
327328

329+
func timeEncoder(values *Values, v reflect.Value, keyParts []string, encodeZero bool, _ *formOptions) {
330+
val := v.Interface().(time.Time)
331+
if val.IsZero() && !encodeZero {
332+
return
333+
}
334+
values.Add(FormatKey(keyParts), strconv.FormatInt(val.Unix(), 10))
335+
}
336+
328337
func interfaceEncoder(values *Values, v reflect.Value, keyParts []string, encodeZero bool, _ *formOptions) {
329338
// interfaceEncoder never encodes a `nil`, but it will pass through an
330339
// `encodeZero` value into its chained encoder
@@ -383,7 +392,6 @@ func uintEncoder(values *Values, v reflect.Value, keyParts []string, encodeZero
383392
// interface{}.
384393
func reflectValue(values *Values, v reflect.Value, encodeZero bool, keyParts []string) {
385394
t := v.Type()
386-
387395
f := getCachedOrBuildTypeEncoder(t)
388396
if f != nil {
389397
f(values, v, keyParts, encodeZero || v.Kind() == reflect.Ptr, nil)
@@ -461,6 +469,12 @@ func makeStructEncoder(t reflect.Type) *structEncoder {
461469
}
462470

463471
func makeTypeEncoder(t reflect.Type) encoderFunc {
472+
// For time.Time, we want to encode it as a Unix timestamp,
473+
// and don't want to recurse into it.
474+
if t == reflect.TypeOf(time.Time{}) {
475+
return timeEncoder
476+
}
477+
464478
switch t.Kind() {
465479
case reflect.Array, reflect.Slice:
466480
return buildArrayOrSliceEncoder(t)

issuing/dispute/client_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ func TestIssuingDisputeNew(t *testing.T) {
3535
Evidence: &stripe.IssuingDisputeEvidenceParams{
3636
Canceled: &stripe.IssuingDisputeEvidenceCanceledParams{
3737
AdditionalDocumentation: stripe.String("file_123"),
38-
CanceledAt: stripe.Int64(1577836800),
38+
CanceledAt: stripe.UnixTime(1528573382),
3939
CancellationPolicyProvided: stripe.Bool(true),
4040
CancellationReason: stripe.String("reason for cancellation"),
41-
ExpectedAt: stripe.Int64(1577836800),
41+
ExpectedAt: stripe.UnixTime(1528573382),
4242
Explanation: stripe.String("explanation"),
4343
ProductDescription: stripe.String("product description"),
4444
ProductType: stripe.String(string(stripe.IssuingDisputeEvidenceCanceledProductTypeMerchandise)),
4545
ReturnStatus: stripe.String(string(stripe.IssuingDisputeEvidenceCanceledReturnStatusMerchantRejected)),
46-
ReturnedAt: stripe.Int64(1577836800),
46+
ReturnedAt: stripe.UnixTime(1528573382),
4747
},
4848
Reason: stripe.String(string(stripe.IssuingDisputeEvidenceReasonCanceled)),
4949
},

source/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ func TestSourceNew(t *testing.T) {
2323
Mandate: &stripe.SourceMandateParams{
2424
Amount: stripe.Int64(1000),
2525
Acceptance: &stripe.SourceMandateAcceptanceParams{
26-
Date: stripe.Int64(1528573382),
26+
Date: stripe.UnixTime(1528573382),
2727
IP: stripe.String("127.0.0.1"),
2828
Online: &stripe.SourceMandateAcceptanceOnlineParams{
29-
Date: stripe.Int64(1528573382),
29+
Date: stripe.UnixTime(1528573382),
3030
IP: stripe.String("127.0.0.1"),
3131
UserAgent: stripe.String("User-Agent"),
3232
},

stripe.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,32 @@ func AddBetaVersion(betaName string, betaVersion string) error {
13891389
return nil
13901390
}
13911391

1392+
// Time returns a pointer to the time.Time value passed in.
1393+
func Time(v time.Time) *time.Time {
1394+
return &v
1395+
}
1396+
1397+
// TimeValue returns the value of the time.Time pointer passed in or
1398+
// time.Time{} if the pointer is nil.
1399+
func TimeValue(v *time.Time) time.Time {
1400+
if v != nil {
1401+
return *v
1402+
}
1403+
return time.Time{}
1404+
}
1405+
1406+
// UnixTime returns a pointer to the time.Time value associated to the
1407+
// Unix epoch timestamp passed in.
1408+
func UnixTime(v int64) *time.Time {
1409+
return Time(time.Unix(v, 0))
1410+
}
1411+
1412+
// UnixTimeValue returns the Unix epoch timestamp value of the time.Time
1413+
// pointer passed in or 0 if the pointer is nil.
1414+
func UnixTimeValue(v *time.Time) int64 {
1415+
return TimeValue(v).Unix()
1416+
}
1417+
13921418
//
13931419
// Private constants
13941420
//

usagerecord/client_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ import (
1010
)
1111

1212
func TestUsageRecordNew(t *testing.T) {
13-
now := int64(time.Now().Unix())
1413
usageRecord, err := New(&stripe.UsageRecordParams{
1514
Quantity: stripe.Int64(123),
16-
Timestamp: stripe.Int64(now),
15+
Timestamp: stripe.Time(time.Now()),
1716
Action: stripe.String(stripe.UsageRecordActionIncrement),
1817
SubscriptionItem: stripe.String("si_123"),
1918
})

usagerecord_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestUsageRecordParams_AppendTo(t *testing.T) {
1717
{"action", &UsageRecordParams{Action: String("increment")}, "increment"},
1818
{"quantity", &UsageRecordParams{Quantity: Int64(2000)}, strconv.FormatUint(2000, 10)},
1919
{"quantity", &UsageRecordParams{Quantity: Int64(0)}, strconv.FormatUint(0, 10)},
20-
{"timestamp", &UsageRecordParams{Timestamp: Int64(123123123)}, strconv.FormatUint(123123123, 10)},
20+
{"timestamp", &UsageRecordParams{Timestamp: UnixTime(123123123)}, strconv.FormatUint(123123123, 10)},
2121
}
2222
for _, tc := range testCases {
2323
t.Run(tc.field, func(t *testing.T) {

0 commit comments

Comments
 (0)