-
Notifications
You must be signed in to change notification settings - Fork 5
/
ussd.go
71 lines (64 loc) · 2.36 KB
/
ussd.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
package ravepay
import (
"encoding/json"
"log"
)
// USSD is a type that encapsulates rave's ussd description
// It has all ussd attributes necessary for rave api ussd referencess
// It also implements the chargable interface required for making charge requests
type USSD struct {
AccountBank string `json:"accountbank"` // vs account_bank
AccountNumber string `json:"accountnumber"`
ChargeRequestURL string `json:"-"`
ValidateCardChargeURL string `json:"-"`
Country string `json:"country"`
Currency string `json:"currency"`
FirstName string `json:"firstname,omitempty"`
LastName string `json:"lastname,omitempty"`
IsUSSD string `json:"is_ussd,omitempty"`
OrderRef string `json:"orderRef,omitempty"`
}
// USSDPaymentInfo is the information necessary for completing mpesa payment
type USSDPaymentInfo struct {
FlwRef string
Amount int
}
// ChargeURL is an implemenation of the Chargeable interface
// it returns the url to be used for charging the given card
func (c *USSD) ChargeURL() string {
if c.ChargeRequestURL == "" {
c.ChargeRequestURL = buildURL(defaultChargeURL)
}
return c.ChargeRequestURL
}
// ValidateChargeURL is an implemenation of the Chargeable interface
// it returns the url to be used for charging the given card
func (c *USSD) ValidateChargeURL() string {
return ""
}
// 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 *USSD) BuildChargeRequestPayload(cReq *ChargeRequest) []byte {
cReq.PaymentType = "ussd"
payload := struct {
*USSD
*ChargeRequest
}{c, cReq}
b, err := json.Marshal(payload)
if err != nil {
log.Println("couldn't marshal payload: ", err)
}
return b
}
// USSDPaymentInstruction parses the given charge response
// and returns the payment info for completing the payment
func USSDPaymentInstruction(cr *ChargeResponse) *USSDPaymentInfo {
return &USSDPaymentInfo{
Amount: cr.Data.Amount,
FlwRef: cr.Data.FlwRef,
}
}