Skip to content

Commit

Permalink
Merge branch 'use-time-structs-for-times' of git://github.com/jszwedk…
Browse files Browse the repository at this point in the history
…o/braintree-go into jszwedko-use-time-structs-for-times
  • Loading branch information
lionelbarrow committed Apr 3, 2015
2 parents bcefd12 + 3a39f6b commit 7d7ddf6
Show file tree
Hide file tree
Showing 10 changed files with 125 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 @@ -80,10 +80,10 @@ func TestAddress(t *testing.T) {
if addr2.CountryName != addr.CountryName {
t.Fatal("country names do not match")
}
if addr2.CreatedAt == "" {
if addr2.CreatedAt == nil {
t.Fatal("generated created at is empty")
}
if addr2.UpdatedAt == "" {
if addr2.UpdatedAt == nil {
t.Fatal("generated updated at is empty")
}

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
33 changes: 33 additions & 0 deletions date.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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
}

// UnmarshalXML interprets Braintree's date format from XML to initialize the Date
// e.g. "2014-02-09"
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
}

// MarshalXML Outputs the date in a format the Braintree expects to see date
// e.g. "2014-02-09"
func (d *Date) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return e.EncodeElement(d.Format("2006-01-02"), start)
}
34 changes: 34 additions & 0 deletions date_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package braintree

import (
"encoding/xml"
"testing"
"time"
)

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)
}
}

func TestDateMarshalXML(t *testing.T) {
date := &Date{Time: time.Date(2014, 2, 9, 0, 0, 0, 0, time.Local)}
expected := `<Date>2014-02-09</Date>`

b, err := xml.Marshal(date)
if err != nil {
t.Fatal(err)
}

if string(b) != expected {
t.Fatalf("expected %s got %s", expected, string(b))
}
}
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 @@ -18,8 +20,8 @@ type Transaction struct {
ShippingAddress *Address `xml:"shipping,omitempty"`
Options *TransactionOptions `xml:"options,omitempty"`
ServiceFeeAmount *Decimal `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 7d7ddf6

Please sign in to comment.