forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settlement_integration_test.go
90 lines (81 loc) · 2.17 KB
/
settlement_integration_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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))
}
}