-
Notifications
You must be signed in to change notification settings - Fork 5
/
mpesa.go
69 lines (62 loc) · 2.26 KB
/
mpesa.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
package ravepay
import (
"encoding/json"
"log"
)
// Mpesa is a type that encapsulates rave's mpesa description
// It has all mpesa attributes necessary for rave api mpesa references
// It also implements the chargable interface required for making charge requests and validating them
type Mpesa struct {
ChargeMpesaURL string `json:"-"`
Currency string `json:"currency"`
Country string `json:"country"`
LastName string `json:"lastname,omitempty"`
FirstName string `json:"firstname,omitempty"`
IsMpesa string `json:"is_mpesa,omitempty"`
}
// MpesaPaymentInfo is the information necessary for completing mpesa payment
type MpesaPaymentInfo struct {
AccountNumber string
Amount int
BusinessNumber string
}
// ChargeURL is an implemenation of the Chargeable interface
// it returns the url to be used for charging the given card
func (m *Mpesa) ChargeURL() string {
if m.ChargeMpesaURL == "" {
m.ChargeMpesaURL = buildURL(defaultChargeURL)
}
return m.ChargeMpesaURL
}
// ValidateChargeURL is an implemenation of the Chargeable interface
// it returns the url to be used for charging the given card
func (m *Mpesa) 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 (m *Mpesa) BuildChargeRequestPayload(cReq *ChargeRequest) []byte {
cReq.PaymentType = "mpesa"
payload := struct {
*Mpesa
*ChargeRequest
}{m, cReq}
b, err := json.Marshal(payload)
if err != nil {
log.Println("couldn't marshal payload: ", err)
}
return b
}
// MpesaPaymentInstruction parses the given charge response
// and returns the payment info: business number and account number for completing the payment
func MpesaPaymentInstruction(cr *ChargeResponse) *MpesaPaymentInfo {
return &MpesaPaymentInfo{
Amount: cr.Data.Amount,
AccountNumber: cr.Data.OrderRef,
BusinessNumber: cr.Data.BusinessNumber,
}
}