diff --git a/address.go b/address.go index d97b730f..e837af92 100644 --- a/address.go +++ b/address.go @@ -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 { diff --git a/address_integration_test.go b/address_integration_test.go index 55bd987e..ce95872a 100644 --- a/address_integration_test.go +++ b/address_integration_test.go @@ -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() } diff --git a/credit_card.go b/credit_card.go index 5a9df749..95df8a54 100644 --- a/credit_card.go +++ b/credit_card.go @@ -1,5 +1,7 @@ package braintree +import "time" + type CreditCard struct { CustomerId string `xml:"customer-id,omitempty"` Token string `xml:"token,omitempty"` @@ -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"` diff --git a/date.go b/date.go new file mode 100644 index 00000000..466136dc --- /dev/null +++ b/date.go @@ -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 +} diff --git a/date_test.go b/date_test.go new file mode 100644 index 00000000..49e80b66 --- /dev/null +++ b/date_test.go @@ -0,0 +1,19 @@ +package braintree + +import ( + "encoding/xml" + "testing" +) + +func TestDateUnmarshalXML(t *testing.T) { + date := &Date{} + + dateXML := []byte(`2014-02-09`) + 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) + } +} diff --git a/disbursement.go b/disbursement.go index a2cd21dd..b203fcf2 100644 --- a/disbursement.go +++ b/disbursement.go @@ -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"` diff --git a/modification.go b/modification.go index e8fe7e11..f13b86e5 100644 --- a/modification.go +++ b/modification.go @@ -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"` } diff --git a/plan.go b/plan.go index a4f228b3..67e1e618 100644 --- a/plan.go +++ b/plan.go @@ -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"` } diff --git a/plan_integration_test.go b/plan_integration_test.go index 08abe596..fa06e8be 100644 --- a/plan_integration_test.go +++ b/plan_integration_test.go @@ -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) } diff --git a/transaction.go b/transaction.go index 32282667..98632f29 100644 --- a/transaction.go +++ b/transaction.go @@ -1,5 +1,7 @@ package braintree +import "time" + type Transaction struct { XMLName string `xml:"transaction"` Id string `xml:"id,omitempty"` @@ -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"` }