Skip to content

Commit

Permalink
Added settlement integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
ulisesflynn committed Mar 2, 2016
1 parent a2b69e6 commit 29e4a3c
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 2 deletions.
4 changes: 4 additions & 0 deletions braintree.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,7 @@ func (g *Braintree) Discount() *DiscountGateway {
func (g *Braintree) WebhookNotification() *WebhookNotificationGateway {
return &WebhookNotificationGateway{g}
}

func (g *Braintree) Settlement() *SettlementGateway {
return &SettlementGateway{g}
}
1 change: 0 additions & 1 deletion settlement.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ type SettlementBatchSummary struct {
type Settlement struct {
XMLName string `xml:"settlement_batch_summary"`
Date string `xml:"settlement_date"`
//CustomField string `xml:group_by_custom_field"`
}
90 changes: 90 additions & 0 deletions settlement_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package braintree

import (
"fmt"
"reflect"
"strings"
"testing"
"time"
)

const (
cardToUse = "Discover"
)

func TestSettlementBatch(t *testing.T) {
// Get current batch summary
y, m, d := time.Now().Date()
date := fmt.Sprintf("%d-%d-%d", y, m, d)
batchSummary, err := testGateway.Settlement().Generate(&Settlement{Date: date})
if err != nil {
t.Fatal(err)
}
t.Log(batchSummary)

// Get the card types
cardTypes := []string{}
for _, record := range batchSummary.Records.Type {
cardTypes = append(cardTypes, record.CardType)
}

// Create a new transaction to add 12.34 to the summary
tx, err := testGateway.Transaction().Create(&Transaction{
Type: "sale",
Amount: NewDecimal(1234, 2),
CreditCard: &CreditCard{
Number: testCreditCards[strings.ToLower(cardToUse)].Number,
ExpirationDate: "05/14",
},
})
if err != nil {
t.Fatal(err)
}
t.Log(tx)
if tx.Id == "" {
t.Fatal("Received invalid ID on new transaction")
}
if tx.Status != "authorized" {
t.Fatal(tx.Status)
}

// Submit for settlement
ten := NewDecimal(1234, 2)
tx2, err := testGateway.Transaction().SubmitForSettlement(tx.Id, ten)
if err != nil {
t.Fatal(err)
}
t.Log(tx2)
if x := tx2.Status; x != "submitted_for_settlement" {
t.Fatal(x)
}
if amount := tx2.Amount; amount.Cmp(ten) != 0 {
t.Fatalf("transaction settlement amount (%s) did not equal amount requested (%s)", amount, ten)
}

// Settle
tx3, err := testGateway.Transaction().Settle(tx.Id)
t.Log(tx3)
if err != nil {
t.Fatal(err)
}
if x := tx3.Status; x != "settled" {
t.Fatal(x)
}

// Generate Settlement Batch Summary which will include new transaction
batchSummary, err = testGateway.Settlement().Generate(&Settlement{Date: date})
if err != nil {
t.Fatal(fmt.Sprintf("Unable to get settlement batch: err is %s", err.Error()))
}
t.Log(batchSummary)

// Since these tests are run concurrently, we will not test the amount only the card types.
foundTypes := []string{}
for _, record := range batchSummary.Records.Type {
foundTypes = append(foundTypes, record.CardType)
}
if !reflect.DeepEqual(cardTypes, foundTypes) {
t.Fatal(fmt.Sprintf("Expected card types: %s, got: %s", cardTypes, foundTypes))
}
}
2 changes: 1 addition & 1 deletion transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Transaction struct {
UpdatedAt *time.Time `xml:"updated-at,omitempty"`
DisbursementDetails *DisbursementDetails `xml:"disbursement-details,omitempty"`
RefundId string `xml:"refund-id,omitempty"`
RefundIds *[]string `xml:"refund-ids,omitempty"`
RefundIds *[]string `xml:"refund-ids>item,omitempty"`
RefundedTransactionId *string `xml:"refunded-transaction-id,omitempty"`
ProcessorResponseCode int `xml:"processor-response-code,omitempty"`
ProcessorResponseText string `xml:"processor-response-text,omitempty"`
Expand Down

0 comments on commit 29e4a3c

Please sign in to comment.