/
balanced.go
71 lines (59 loc) · 1.66 KB
/
balanced.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
// A Go package that provides bindings to Balanced Payemnts API.
//
// See https://www.balancedpayments.com/
package balanced
import (
"flag"
"github.com/stathat/jconfig"
"log"
"os"
)
const (
testApiRoot = "https://api.balancedpayments.com"
)
// Basic information needed to connect to the balanced REST API.
var apiRoot, apiKey, marketplaceId string
func init() {
var stage string
// Get stage flag passed in. Default to test
flag.StringVar(&stage, "stage", "test", "flag for deployment stage")
flag.Parse()
if stage == "test" {
setupTestEnvironment()
} else {
// Retrieve config from balanced.conf
config := jconfig.LoadConfig(stage + "/balanced.conf")
apiRoot = config.GetString("balanced_api_root")
apiKey = config.GetString("balanced_api_key")
marketplaceId = config.GetString("balanced_marketplace_id")
}
}
// Setup basic information needed to connect to balanced. Overrides any config
// set during init.
func SetupEnvironment(root, key, marketId string) {
apiRoot = root
apiKey = key
marketplaceId = marketId
}
// Used when running test, or when no config file was specified.
// The api invoked by this function is not a public endpoint at balanced.
// May not work in the future.
func setupTestEnvironment() {
apiRoot = testApiRoot
// Get test api key from balanced
key := ApiKey{}
err := post(apiKeyUri, nil, &key)
if err != nil {
log.Println("Unable to generate test key")
os.Exit(1)
}
apiKey = key.Secret
// Get test marketplace from balanced
marketplace := Marketplace{}
err = post(marketplaceUri, nil, &marketplace)
if err != nil {
log.Println("Unable to generate test marketplace")
os.Exit(1)
}
marketplaceId = marketplace.Id
}