-
Notifications
You must be signed in to change notification settings - Fork 1
/
c2b.go
72 lines (62 loc) · 2.77 KB
/
c2b.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
72
package darajago
// C2BPayload Simulate paying bills online
type C2BPayload struct {
ShortCode string `json:"ShortCode"`
CommandID string `json:"CommandID"`
Amount string `json:"Amount"`
Msisdn string `json:"Msisdn"`
BillRefNumber string `json:"BillRefNumber"`
}
// C2BResponse is the response from the C2BPayload API
type C2BResponse struct {
OriginatorConversationID string `json:"OriginatorConversationID"`
ConversationID string `json:"ConversationID"`
ResponseDescription string `json:"ResponseDescription"`
}
// C2BRegistrationPayload is the payload for C2BPayload shortcode registration.
type C2BRegistrationPayload struct {
// ValidationURL is the URL that receives the validation request from the API upon payment submission.
// The validation URL is only called if the external validation on the registered shortcode is enabled.
ValidationURL string `json:"ValidationURL"`
// ConfirmationURL is the URL that receives the confirmation request from the API upon payment completion.
ConfirmationURL string `json:"ConfirmationURL"`
// ResponseType specifies what is to happen if the validation URL is not reachable.
// Only two values are allowed: Completed or Cancelled.
// Completed means MPesa will automatically complete the transaction,
// whereas Cancelled means MPesa will automatically cancel the transaction.
ResponseType string `json:"ResponseType"`
// ShortCode is the short code of the organization.
ShortCode string `json:"ShortCode"`
}
// C2BRegistrationResponse is the response from the C2BPayload API
type C2BRegistrationResponse struct {
OriginatorConversationID string `json:"OriginatorConversationID"`
ConversationID string `json:"ConversationID"`
// ResponseDescription This is the status of the request.
ResponseDescription string `json:"ResponseDescription"`
}
func (d *DarajaApi) RegisterC2BCallback(payload C2BRegistrationPayload) (*C2BRegistrationResponse, *ErrorResponse) {
secureResponse, err := performSecurePostRequest[*C2BRegistrationResponse](payload, endpointRegisterConfirmValidation, d)
if err != nil {
return nil, err
}
return secureResponse.Body, nil
}
func (d *DarajaApi) MakeC2BPayment(c2b C2BPayload) (*C2BResponse, *ErrorResponse) {
c2b.CommandID = "CustomerPayBillOnline"
// marshal the struct into a map
secureResponse, err := performSecurePostRequest[*C2BResponse](c2b, endpointSimulatePmtC2B, d)
if err != nil {
return nil, err
}
return secureResponse.Body, nil
}
func (d *DarajaApi) MakeC2BPaymentV2(c2b C2BPayload) (*C2BResponse, *ErrorResponse) {
c2b.CommandID = "CustomerPayBillOnline"
// marshal the struct into a map
secureResponse, err := performSecurePostRequest[*C2BResponse](c2b, endpointSimulatePmtC2BV2, d)
if err != nil {
return nil, err
}
return secureResponse.Body, nil
}