Skip to content

Commit

Permalink
Use real time.Time structs for CreatedAt/UpdatedAt
Browse files Browse the repository at this point in the history
Also introduce Date struct to handle values from Braintree that are
dates rather than datetimes.
  • Loading branch information
jszwedko committed Jan 23, 2015
1 parent bbc2bbd commit eb9f7c1
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 51 deletions.
33 changes: 17 additions & 16 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ package braintree

import (
"encoding/xml"
"time"
)

type Address struct {
XMLName xml.Name
Id string `xml:"id,omitempty"`
CustomerId string `xml:"customer-id,omitempty"`
FirstName string `xml:"first-name,omitempty"`
LastName string `xml:"last-name,omitempty"`
Company string `xml:"company,omitempty"`
StreetAddress string `xml:"street-address,omitempty"`
ExtendedAddress string `xml:"extended-address,omitempty"`
Locality string `xml:"locality,omitempty"`
Region string `xml:"region,omitempty"`
PostalCode string `xml:"postal-code,omitempty"`
CountryCodeAlpha2 string `xml:"country-code-alpha2,omitempty"`
CountryCodeAlpha3 string `xml:"country-code-alpha3,omitempty"`
CountryCodeNumeric string `xml:"country-code-numeric,omitempty"`
CountryName string `xml:"country-name,omitempty"`
CreatedAt string `xml:"created-at,omitempty"`
UpdatedAt string `xml:"updated-at,omitempty"`
Id string `xml:"id,omitempty"`
CustomerId string `xml:"customer-id,omitempty"`
FirstName string `xml:"first-name,omitempty"`
LastName string `xml:"last-name,omitempty"`
Company string `xml:"company,omitempty"`
StreetAddress string `xml:"street-address,omitempty"`
ExtendedAddress string `xml:"extended-address,omitempty"`
Locality string `xml:"locality,omitempty"`
Region string `xml:"region,omitempty"`
PostalCode string `xml:"postal-code,omitempty"`
CountryCodeAlpha2 string `xml:"country-code-alpha2,omitempty"`
CountryCodeAlpha3 string `xml:"country-code-alpha3,omitempty"`
CountryCodeNumeric string `xml:"country-code-numeric,omitempty"`
CountryName string `xml:"country-name,omitempty"`
CreatedAt *time.Time `xml:"created-at,omitempty"`
UpdatedAt *time.Time `xml:"updated-at,omitempty"`
}

type Addresses struct {
Expand Down
4 changes: 2 additions & 2 deletions address_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ func TestAddress(t *testing.T) {
if addr2.CountryName != addr.CountryName {
t.Fatal()
}
if addr2.CreatedAt == "" {
if addr2.CreatedAt == nil {
t.Fatal()
}
if addr2.UpdatedAt == "" {
if addr2.UpdatedAt == nil {
t.Fatal()
}

Expand Down
6 changes: 4 additions & 2 deletions credit_card.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package braintree

import "time"

type CreditCard struct {
CustomerId string `xml:"customer-id,omitempty"`
Token string `xml:"token,omitempty"`
Expand All @@ -12,8 +14,8 @@ type CreditCard struct {
VenmoSDKPaymentMethodCode string `xml:"venmo-sdk-payment-method-code,omitempty"`
VenmoSDK bool `xml:"venmo-sdk,omitempty"`
Options *CreditCardOptions `xml:"options,omitempty"`
CreatedAt string `xml:"created-at,omitempty"`
UpdatedAt string `xml:"updated-at,omitempty"`
CreatedAt *time.Time `xml:"created-at,omitempty"`
UpdatedAt *time.Time `xml:"updated-at,omitempty"`
Bin string `xml:"bin,omitempty"`
CardType string `xml:"card-type,omitempty"`
CardholderName string `xml:"cardholder-name,omitempty"`
Expand Down
25 changes: 25 additions & 0 deletions date.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package braintree

import (
"encoding/xml"
"time"
)

// Date wraps a time object but handles deserializing dates returned from the Braintree API
// e.g. "2014-02-09"
type Date struct {
*time.Time
}

func (d *Date) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
var v string
dec.DecodeElement(&v, &start)

parse, err := time.Parse("2006-01-02", v)
if err != nil {
return err
}

*d = Date{Time: &parse}
return nil
}
19 changes: 19 additions & 0 deletions date_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package braintree

import (
"encoding/xml"
"testing"
)

func TestDateUnmarshalXML(t *testing.T) {
date := &Date{}

dateXML := []byte(`<?xml version="1.0" encoding="UTF-8"?><foo>2014-02-09</foo></xml>`)
if err := xml.Unmarshal(dateXML, date); err != nil {
t.Fatal(err)
}

if date.Format("2006-01-02") != "2014-02-09" {
t.Fatalf("expected 2014-02-09 got %s", date)
}
}
6 changes: 2 additions & 4 deletions disbursement.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package braintree

import (
"encoding/xml"
)
import "encoding/xml"

type Disbursement struct {
XMLName xml.Name `xml:"disbursement"`
Id string `xml:"id"`
ExceptionMessage string `xml:"exception-message"`
DisbursementDate string `xml:"disbursement-date"`
DisbursementDate *Date `xml:"disbursement-date"`
FollowUpAction string `xml:"follow-up-action"`
Success bool `xml:"success"`
Retry bool `xml:"retry"`
Expand Down
18 changes: 10 additions & 8 deletions modification.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package braintree

import "time"

const (
ModificationKindDiscount = "discount"
ModificationKindAddOn = "add_on"
)

type Modification struct {
Id string `xml:"id,omitempty"`
Amount float64 `xml:"amount,omitempty"`
Description string `xml:"description,omitempty"`
Kind string `xml:"kind,omitempty"`
Name string `xml:"name,omitempty"`
NeverExpires bool `xml:"never-expires,omitempty"`
Quantity int `xml:"quantity,omitempty"`
UpdatedAt string `xml:"updated_at,omitempty"`
Id string `xml:"id,omitempty"`
Amount float64 `xml:"amount,omitempty"`
Description string `xml:"description,omitempty"`
Kind string `xml:"kind,omitempty"`
Name string `xml:"name,omitempty"`
NeverExpires bool `xml:"never-expires,omitempty"`
Quantity int `xml:"quantity,omitempty"`
UpdatedAt *time.Time `xml:"updated_at,omitempty"`
}
32 changes: 17 additions & 15 deletions plan.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package braintree

import "time"

type Plan struct {
XMLName string `xml:"plan"`
Id string `xml:"id"`
MerchantId string `xml:"merchant-id"`
BillingDayOfMonth string `xml:"billing-day-of-month"` // int
BillingFrequency string `xml:"billing-frequency"` // int
CurrencyISOCode string `xml:"currency-iso-code"`
Description string `xml:"description"`
Name string `xml:"name"`
NumberOfBillingCycles string `xml:"number-of-billing-cycles"` // int
Price float64 `xml:"price"`
TrialDuration string `xml:"trial-duration"` // int
TrialDurationUnit string `xml:"trial-duration-unit"`
TrialPeriod string `xml:"trial-period"` // bool
CreatedAt string `xml:"created-at"`
UpdatedAt string `xml:"updated-at"`
XMLName string `xml:"plan"`
Id string `xml:"id"`
MerchantId string `xml:"merchant-id"`
BillingDayOfMonth string `xml:"billing-day-of-month"` // int
BillingFrequency string `xml:"billing-frequency"` // int
CurrencyISOCode string `xml:"currency-iso-code"`
Description string `xml:"description"`
Name string `xml:"name"`
NumberOfBillingCycles string `xml:"number-of-billing-cycles"` // int
Price float64 `xml:"price"`
TrialDuration string `xml:"trial-duration"` // int
TrialDurationUnit string `xml:"trial-duration-unit"`
TrialPeriod string `xml:"trial-period"` // bool
CreatedAt *time.Time `xml:"created-at"`
UpdatedAt *time.Time `xml:"updated-at"`
// AddOns []interface{} `xml:"add-ons"`
// Discounts []interface{} `xml:"discounts"`
}
Expand Down
4 changes: 2 additions & 2 deletions plan_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ func TestPlan(t *testing.T) {
if x := plan.TrialPeriod; x != "true" {
t.Fatal(x)
}
if x := plan.CreatedAt; x == "" {
if x := plan.CreatedAt; x == nil {
t.Fatal(x)
}
if x := plan.UpdatedAt; x == "" {
if x := plan.UpdatedAt; x == nil {
t.Fatal(x)
}

Expand Down
6 changes: 4 additions & 2 deletions transaction.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package braintree

import "time"

type Transaction struct {
XMLName string `xml:"transaction"`
Id string `xml:"id,omitempty"`
Expand All @@ -17,8 +19,8 @@ type Transaction struct {
ShippingAddress *Address `xml:"shipping,omitempty"`
Options *TransactionOptions `xml:"options,omitempty"`
ServiceFeeAmount float64 `xml:"service-fee-amount,attr,omitempty"`
CreatedAt string `xml:"created-at,omitempty"`
UpdatedAt string `xml:"updated-at,omitempty"`
CreatedAt *time.Time `xml:"created-at,omitempty"`
UpdatedAt *time.Time `xml:"updated-at,omitempty"`
DisbursementDetails *DisbursementDetails `xml:"disbursement-details,omitempty"`
}

Expand Down

0 comments on commit eb9f7c1

Please sign in to comment.