-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathaccount.go
66 lines (61 loc) · 2.5 KB
/
account.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
package ravepay
import (
"encoding/json"
"log"
)
// Account is a type that encapsulates rave's account description
// It has all account attributes necessary for rave api card references
// It also implements the chargable interface required for making charge requests and validating them
type Account struct {
AccountBank string `json:"account_bank"` // TODO: account_bank vs accountbank
AccountIsBlacklisted int `json:"account_is_blacklisted"`
AccountNumber string `json:"account_number"`
AccountToken struct {
Token string `json:"token"`
} `json:"account_token"`
ChargeAccountURL string `json:"-"`
ValidateAccountChargeURL string `json:"-"`
Country string `json:"country"`
CreatedAt string `json:"createdAt"`
Currency string `json:"currency"`
DeletedAt interface{} `json:"deletedAt"`
FirstName string `json:"first_name"`
ID int `json:"id"`
LastName string `json:"last_name"`
Passcode string `json:"passcode"`
UpdatedAt string `json:"updatedAt"`
}
// ChargeURL is an implemenation of the Chargeable interface
// it returns the url to be used for charging the given account
func (a *Account) ChargeURL() string {
if a.ChargeAccountURL == "" {
a.ChargeAccountURL = buildURL(defaultChargeURL)
}
return a.ChargeAccountURL
}
// ValidateChargeURL is an implemenation of the Chargeable interface
// it returns the url to be used for validating charge on the given account
func (a *Account) ValidateChargeURL() string {
if a.ValidateAccountChargeURL == "" {
a.ValidateAccountChargeURL = buildURL(validateAccountChargeURL)
}
return a.ValidateAccountChargeURL
}
// BuildChargeRequestPayload is an implemenation of the Chargeable interface
// it returns the byte representation of the charge request client
// at the ChargeRequest level, chargeables are merely interface objects
// so trying to compose a struct with an interface object results in go adding the interface name key to the result bytes
// see https://play.golang.com/p/MFfbuPLrjo6
// so here we upend it so the individual concrete types do the marshalling
func (a *Account) BuildChargeRequestPayload(creq *ChargeRequest) []byte {
creq.PaymentType = "account"
payload := struct {
*Account
*ChargeRequest
}{a, creq}
b, err := json.Marshal(payload)
if err != nil {
log.Println("couldn't marshal payload: ", err)
}
return b
}