forked from BoltApp/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merchant_account_gateway.go
62 lines (56 loc) · 1.68 KB
/
merchant_account_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
package braintree
import "context"
type MerchantAccountGateway struct {
*Braintree
}
// Create a sub merchant account.
func (g *MerchantAccountGateway) Create(ctx context.Context, ma *MerchantAccount) (*MerchantAccount, error) {
pruneAddress(ma)
resp, err := g.execute(ctx, "POST", "merchant_accounts/create_via_api", ma)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 201:
return resp.merchantAccount()
}
return nil, &invalidResponseError{resp}
}
// Find finds the merchant account with the specified id.
func (g *MerchantAccountGateway) Find(ctx context.Context, id string) (*MerchantAccount, error) {
resp, err := g.execute(ctx, "GET", "merchant_accounts/"+id, nil)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 200:
return resp.merchantAccount()
}
return nil, &invalidResponseError{resp}
}
// Update a sub merchant account.
func (g *MerchantAccountGateway) Update(ctx context.Context, ma *MerchantAccount) (*MerchantAccount, error) {
pruneAddress(ma)
resp, err := g.execute(ctx, "PUT", "merchant_accounts/"+ma.Id+"/update_via_api", ma)
if err != nil {
return nil, err
}
switch resp.StatusCode {
case 201:
return resp.merchantAccount()
}
return nil, &invalidResponseError{resp}
}
// Merchant Accounts only have one address entry so join the components if needed
func pruneAddress(ma *MerchantAccount) {
var addr *Address
if ma.Individual != nil && ma.Individual.Address != nil {
addr = ma.Individual.Address
} else if ma.Business != nil && ma.Business.Address != nil {
addr = ma.Business.Address
}
if addr != nil && len(addr.ExtendedAddress) > 0 {
addr.StreetAddress += " " + addr.ExtendedAddress
addr.ExtendedAddress = ""
}
}