forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction_gateway.go
134 lines (123 loc) · 3.53 KB
/
transaction_gateway.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package braintree
import (
"encoding/xml"
"fmt"
)
type TransactionGateway struct {
*Braintree
}
// Create initiates a transaction.
func (g *TransactionGateway) Create(tx *Transaction) (*Transaction, error) {
resp, err := g.execute("POST", "transactions", tx)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 201:
return resp.transaction()
}
return nil, &invalidResponseError{resp}
}
// SubmitForSettlement submits the transaction with the specified id for settlement.
// If the amount is omitted, the full amount is settled.
func (g *TransactionGateway) SubmitForSettlement(id string, amount ...*Decimal) (*Transaction, error) {
var tx *Transaction
if len(amount) > 0 {
tx = &Transaction{
Amount: amount[0],
}
}
resp, err := g.execute("PUT", "transactions/"+id+"/submit_for_settlement", tx)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 200:
return resp.transaction()
}
return nil, &invalidResponseError{resp}
}
// Settle settles a transaction.
// This action is only available in the sandbox environment.
func (g *TransactionGateway) Settle(id string) (*Transaction, error) {
if g.Environment != Production {
resp, err := g.execute("PUT", "transactions/"+id+"/settle", nil)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 200:
return resp.transaction()
}
return nil, &invalidResponseError{resp}
} else {
return nil, &testOperationPerformedInProductionError{}
}
}
// Void voids the transaction with the specified id if it has a status of authorized or
// submitted_for_settlement. When the transaction is voided Braintree will do an authorization
// reversal if possible so that the customer won’t have a pending charge on their card
func (g *TransactionGateway) Void(id string) (*Transaction, error) {
resp, err := g.execute("PUT", "transactions/"+id+"/void", nil)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 200:
return resp.transaction()
}
return nil, &invalidResponseError{resp}
}
// A transaction can be refunded if it is settled or settling.
// If the transaction has not yet begun settlement, use Void() instead.
// If you do not specify an amount to refund, the entire transaction amount will be refunded.
func (g *TransactionGateway) Refund(id string, amount ...*Decimal) (*Transaction, error) {
var tx *Transaction
if len(amount) > 0 {
tx = &Transaction{
Amount: amount[0],
}
}
resp, err := g.execute("POST", "transactions/"+id+"/refund", tx)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 200:
return resp.transaction()
case 201:
return resp.transaction()
}
return nil, &invalidResponseError{resp}
}
// Find finds the transaction with the specified id.
func (g *TransactionGateway) Find(id string) (*Transaction, error) {
resp, err := g.execute("GET", "transactions/"+id, nil)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 200:
return resp.transaction()
}
return nil, &invalidResponseError{resp}
}
// Search finds all transactions matching the search query.
func (g *TransactionGateway) Search(query *SearchQuery) (*TransactionSearchResult, error) {
resp, err := g.execute("POST", "transactions/advanced_search", query)
if err != nil {
return nil, err
}
var v TransactionSearchResult
err = xml.Unmarshal(resp.Body, &v)
if err != nil {
return nil, err
}
return &v, err
}
type testOperationPerformedInProductionError struct {
error
}
func (e *testOperationPerformedInProductionError) Error() string {
return fmt.Sprint("Operation not allowed in production environment")
}