-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcard.go
67 lines (62 loc) · 2.35 KB
/
card.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
package ravepay
import (
"encoding/json"
"log"
)
// Card is a type that encapsulates rave's card description
// It has all card attributes necessary for rave api card references
// It also implements the chargable interface required for making charge requests and validating them
type Card struct {
Brand string `json:"brand,omitempty"`
CardBIN string `json:"cardBIN,omitempty"`
CardNo string `json:"cardno"`
CardTokens []struct {
Embedtoken string `json:"embedtoken"`
Shortcode string `json:"shortcode"`
} `json:"card_tokens,omitempty"`
ChargeCardURL string `json:"-"`
Country string `json:"country"`
Currency string `json:"currency"`
Cvv string `json:"cvv"`
Expirymonth string `json:"expirymonth"`
Expiryyear string `json:"expiryyear"`
FirstName string `json:"firstname,omitempty"`
Last4digits string `json:"last4digits,omitempty"`
LastName string `json:"lastname,omitempty"`
Pin string `json:"pin"`
ValidateCardChargeURL string `json:"-"`
}
// ChargeURL is an implemenation of the Chargeable interface
// it returns the url to be used for charging the given card
func (c *Card) ChargeURL() string {
if c.ChargeCardURL == "" {
c.ChargeCardURL = buildURL(defaultChargeURL)
}
return c.ChargeCardURL
}
// ValidateChargeURL is an implemenation of the Chargeable interface
// it returns the url to be used for charging the given card
func (c *Card) ValidateChargeURL() string {
if c.ValidateCardChargeURL == "" {
c.ValidateCardChargeURL = buildURL(validateCardChargeURL)
}
return c.ValidateCardChargeURL
}
// 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 (c *Card) BuildChargeRequestPayload(creq *ChargeRequest) []byte {
creq.PaymentType = "card"
payload := struct {
*Card
*ChargeRequest
}{c, creq}
b, err := json.Marshal(payload)
if err != nil {
log.Println("couldn't marshal payload: ", err)
}
return b
}