Documentation ¶
Index ¶
- Constants
- func MapExpressGinCallBack(gingroup *gin.RouterGroup, callBackUrl string, callback ExpressCallBackFunc)
- type Authorization
- type B2BPayload
- type B2BResponse
- type B2CCommandID
- type B2CPayload
- type B2CResponse
- type BalanceQuery
- type C2BPayload
- type C2BRegistrationPayload
- type C2BRegistrationResponse
- type C2BResponse
- type C2BURLRegistration
- type CallbackResponse
- type DarajaApi
- func (d *DarajaApi) Authorize() (*Authorization, error)
- func (d *DarajaApi) MakeB2BPayment(b2c B2BPayload, certPath string) (*B2CResponse, *ErrorResponse)
- func (d *DarajaApi) MakeB2CPayment(b2c B2CPayload, certPath string) (*B2CResponse, *ErrorResponse)
- func (d *DarajaApi) MakeC2BPayment(c2b C2BPayload) (*C2BResponse, *ErrorResponse)
- func (d *DarajaApi) MakeC2BPaymentV2(c2b C2BPayload) (*C2BResponse, *ErrorResponse)
- func (d *DarajaApi) MakeQRCodeRequest(payload QRPayload) (*QRResponse, *ErrorResponse)
- func (d *DarajaApi) MakeSTKPushRequest(mpesaConfig LipaNaMpesaPayload) (*LipaNaMpesaResponse, *ErrorResponse)
- func (d *DarajaApi) QuerySTKPushStatus(mpesaConfig STKPushStatusPayload) (*STKPushStatusResponse, *ErrorResponse)
- func (d *DarajaApi) RegisterC2BCallback(payload C2BRegistrationPayload) (*C2BRegistrationResponse, *ErrorResponse)
- func (d *DarajaApi) ReverseTransaction(transation ReversalPayload, certPath string) (*ReversalResponse, *ErrorResponse)
- type Environment
- type ErrorResponse
- type ExpressCallBackFunc
- type LipaNaMpesaPayload
- type LipaNaMpesaResponse
- type QRPayload
- type QRResponse
- type RegisterURLConfig
- type ReversalPayload
- type ReversalResponse
- type STKPushStatusPayload
- type STKPushStatusResponse
- type TransactionStatus
- type TransactionType
Constants ¶
const ( // ENVIRONMENT_SANDBOX is the sandbox environment ENVIRONMENT_SANDBOX = "sandbox" // ENVIRONMENT_PRODUCTION is the production environment ENVIRONMENT_PRODUCTION = "production" )
const (
ExpressDefaultCallBackURL = "daraja-payments/mpesa"
)
Variables ¶
This section is empty.
Functions ¶
func MapExpressGinCallBack ¶
func MapExpressGinCallBack(gingroup *gin.RouterGroup, callBackUrl string, callback ExpressCallBackFunc)
MapExpressGinCallBack Register a callback for listening to MPESA requests
Types ¶
type Authorization ¶
type Authorization struct {
// contains filtered or unexported fields
}
type B2BPayload ¶
type B2BPayload struct { // InitiatorName is the initiator name. InitiatorName string `json:"InitiatorName"` // PassKey is the password generated on the Safaricom portal. PassKey string `json:"SecurityCredential"` // CommandID is the command ID. CommandID string `json:"CommandID"` // Amount is the amount to be transferred. Amount string `json:"Amount"` // PartyA is the party A (the organization making the payment). PartyA string `json:"PartyA"` // PartyB is the party B (the customer receiving the payment). PartyB string `json:"PartyB"` // Remarks are any remarks for the request. Remarks string `json:"Remarks"` // QueueTimeOutURL is the queue timeout URL. QueueTimeOutURL string `json:"QueueTimeOutURL"` // ResultURL is the result URL. ResultURL string `json:"ResultURL"` // Occasion is the occasion for the payment. Occasion string `json:"Occasion"` }
B2BPayload represents a request payload for the B2C API.
type B2BResponse ¶
type B2BResponse struct { OriginatorConversationID string `json:"OriginatorConversationID"` ConversationID string `json:"ConversationID"` ResponseDescription string `json:"ResponseDescription"` }
B2BResponse is the response from the C2BPayload API
type B2CCommandID ¶
type B2CCommandID string
B2CCommandID is a type that represents the command ID for a B2C payment.
const ( // B2CCommandIDSalaryPayment is a command ID for a salary payment. B2CCommandIDSalaryPayment B2CCommandID = "SalaryPayment" // B2CCommandIDBusinessPayment is a command ID for a business payment. B2CCommandIDBusinessPayment B2CCommandID = "BusinessPayment" // B2CCommandIDPromotionPayment is a command ID for a promotion payment. B2CCommandIDPromotionPayment B2CCommandID = "PromotionPayment" )
type B2CPayload ¶
type B2CPayload struct { // InitiatorName is the initiator name. // This is the API operator's username as set on the portal when the user was created. // For Sandbox users, the username is already created and assigned to them and is available // on the docs credentials page as Initiator Name (Shortcode 1). InitiatorName string `json:"InitiatorName"` // PassKey is the security credential. PassKey string `json:"PassKey"` // CommandID eg. // //· SalaryPayment // //· BusinessPayment // //· PromotionPayment CommandID B2CCommandID `json:"CommandID"` // Amount is the amount to be transferred. Amount string `json:"Amount"` // PartyA is the party A (the organization making the payment). // This is the B2C organization shortcode from which the money is sent from. PartyA string `json:"PartyA"` // PartyB is the party B (the customer receiving the payment). // This is the customer mobile number to receive the amount. //-The number should have the country code (254) without the plus sign. PartyB string `json:"PartyB"` // Remarks are any remarks for the request. Remarks string `json:"Remarks"` // QueueTimeOutURL is the queue timeout URL. // This is the URL to be specified in your request that will be used by API Proxy // to send notification in case the payment request is timed out while awaiting processing in the queue. QueueTimeOutURL string `json:"QueueTimeOutURL"` // ResultURL is the result URL. ResultURL string `json:"ResultURL"` // Occasion is the occasion for the payment. // Any additional information to be associated with the transaction. Occasion string `json:"Occasion"` }
B2CPayload represents a request payload for the B2C API.
type B2CResponse ¶
type B2CResponse struct { // OriginatorConversationID is the unique request ID for tracking a transaction. OriginatorConversationID string `json:"OriginatorConversationID"` // ConversationID is the unique request ID returned by Mpesa for each request made. ConversationID string `json:"ConversationID"` // ResponseDescription is the response description message. ResponseDescription string `json:"ResponseDescription"` }
B2CResponse is the response from the B2C API
type BalanceQuery ¶
type BalanceQuery struct { Initiator string `json:"Initiator"` SecurityCredential string `json:"SecurityCredential"` CommandID string `json:"CommandID"` PartyA string `json:"PartyA"` IdentifierType string `json:"IdentifierType"` Remarks string `json:"Remarks"` QueueTimeOutURL string `json:"QueueTimeOutURL"` ResultURL string `json:"ResultURL"` }
BalanceQuery is used to query the balance of an M-Pesa account
type C2BPayload ¶
type C2BPayload struct { ShortCode string `json:"ShortCode"` CommandID string `json:"CommandID"` Amount string `json:"Amount"` Msisdn string `json:"Msisdn"` BillRefNumber string `json:"BillRefNumber"` }
C2BPayload Simulate paying bills online
type C2BRegistrationPayload ¶
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"` }
C2BRegistrationPayload is the payload for C2BPayload shortcode registration.
type C2BRegistrationResponse ¶
type C2BRegistrationResponse struct { OriginatorConversationID string `json:"OriginatorConversationID"` ConversationID string `json:"ConversationID"` // ResponseDescription This is the status of the request. ResponseDescription string `json:"ResponseDescription"` }
C2BRegistrationResponse is the response from the C2BPayload API
type C2BResponse ¶
type C2BResponse struct { OriginatorConversationID string `json:"OriginatorConversationID"` ConversationID string `json:"ConversationID"` ResponseDescription string `json:"ResponseDescription"` }
C2BResponse is the response from the C2BPayload API
type C2BURLRegistration ¶
type C2BURLRegistration struct { ShortCode string `json:"ShortCode"` ResponseType string `json:"ResponseType"` ConfirmationURL string `json:"ConfirmationURL"` ValidationURL string `json:"ValidationURL"` }
C2BURLRegistration is used to register the confirmation and validation URLs
type CallbackResponse ¶
type CallbackResponse struct { Body struct { StkCallback struct { MerchantRequestID string `json:"MerchantRequestID"` CheckoutRequestID string `json:"CheckoutRequestID"` ResultCode int `json:"ResultCode"` ResultDesc string `json:"ResultDesc"` CallbackMetadata struct { Item []struct { Name string `json:"Name"` Value interface{} `json:"Value"` } `json:"Item"` } `json:"CallbackMetadata"` } `json:"stkCallback"` } `json:"Body"` }
type DarajaApi ¶
type DarajaApi struct { ConsumerKey string // Consumer key ConsumerSecret string // Consumer secret // contains filtered or unexported fields }
func NewDarajaApi ¶
func NewDarajaApi(consumerKey, consumerSecret string, environment Environment) *DarajaApi
NewDarajaApi is a function that creates a new DarajaApi struct. It takes a consumer key, a consumer secret, and an Environment as input, and returns a pointer to the DarajaApi struct.
func (*DarajaApi) Authorize ¶
func (d *DarajaApi) Authorize() (*Authorization, error)
Authorize is a function that retrieves an authorization token from the Mpesa API. It returns an Authorization struct representing the authorization token, or an error if an error occurred during the request.
func (*DarajaApi) MakeB2BPayment ¶
func (d *DarajaApi) MakeB2BPayment(b2c B2BPayload, certPath string) (*B2CResponse, *ErrorResponse)
MakeB2BPayment makes a B2C payment.
func (*DarajaApi) MakeB2CPayment ¶
func (d *DarajaApi) MakeB2CPayment(b2c B2CPayload, certPath string) (*B2CResponse, *ErrorResponse)
MakeB2CPayment is a function that makes a B2C payment using the Mpesa API. It takes a B2CPayload struct representing the payment request payload as input, and returns a pointer to a B2CResponse struct representing the payment response, or a pointer to an ErrorResponse struct representing an error that occurred during the request.
func (*DarajaApi) MakeC2BPayment ¶
func (d *DarajaApi) MakeC2BPayment(c2b C2BPayload) (*C2BResponse, *ErrorResponse)
func (*DarajaApi) MakeC2BPaymentV2 ¶
func (d *DarajaApi) MakeC2BPaymentV2(c2b C2BPayload) (*C2BResponse, *ErrorResponse)
func (*DarajaApi) MakeQRCodeRequest ¶
func (d *DarajaApi) MakeQRCodeRequest(payload QRPayload) (*QRResponse, *ErrorResponse)
MakeQRCodeRequest is a function that generates a QR code for a payment. It takes in a QRPayload struct representing the payment configuration, and returns a QRResponse struct representing the response from the QR code generation API, or an ErrorResponse struct representing an error that occurred during the request.
func (*DarajaApi) MakeSTKPushRequest ¶
func (d *DarajaApi) MakeSTKPushRequest(mpesaConfig LipaNaMpesaPayload) (*LipaNaMpesaResponse, *ErrorResponse)
MakeSTKPushRequest is a function that initiates a Lipa Na Mpesa Online payment. It takes in a LipaNaMpesaPayload struct representing the payment configuration, and returns a LipaNaMpesaResponse struct representing the response from the Lipa Na Mpesa API, or an ErrorResponse struct representing an error that occurred during the request.
func (*DarajaApi) QuerySTKPushStatus ¶
func (d *DarajaApi) QuerySTKPushStatus(mpesaConfig STKPushStatusPayload) (*STKPushStatusResponse, *ErrorResponse)
QuerySTKPushStatus is a function that queries the status of a Lipa Na Mpesa Online payment. It takes in a STKPushStatusPayload struct representing the payment configuration, and returns a STKPushStatusResponse struct representing the response from the Lipa Na Mpesa API, or an ErrorResponse struct representing an error that occurred during the request.
func (*DarajaApi) RegisterC2BCallback ¶
func (d *DarajaApi) RegisterC2BCallback(payload C2BRegistrationPayload) (*C2BRegistrationResponse, *ErrorResponse)
func (*DarajaApi) ReverseTransaction ¶
func (d *DarajaApi) ReverseTransaction(transation ReversalPayload, certPath string) (*ReversalResponse, *ErrorResponse)
type Environment ¶
type Environment string
type ErrorResponse ¶
type ErrorResponse struct { RequestID string `json:"requestId"` ErrorCode string `json:"errorCode"` ErrorMessage string `json:"errorMessage"` Raw []byte `json:"-"` // contains filtered or unexported fields }
func (ErrorResponse) Error ¶
func (e ErrorResponse) Error() string
type ExpressCallBackFunc ¶
type ExpressCallBackFunc func(response *CallbackResponse, request *http.Request, err error)
ExpressCallBackFunc A function called after MPESA API sends a request
type LipaNaMpesaPayload ¶
type LipaNaMpesaPayload struct { BusinessShortCode string `json:"BusinessShortCode"` Password string `json:"Password"` Timestamp string `json:"Timestamp"` TransactionType string `json:"TransactionType"` Amount string `json:"Amount"` PartyA string `json:"PartyA"` PartyB string `json:"PartyB"` PhoneNumber string `json:"PhoneNumber"` CallBackURL string `json:"CallBackURL"` AccountReference string `json:"AccountReference"` TransactionDesc string `json:"TransactionDesc"` }
LipaNaMpesaPayload is used to initiate a transaction on Lipa Na M-Pesa Online Payment
type LipaNaMpesaResponse ¶
type LipaNaMpesaResponse struct { // MerchantRequestID is the unique request ID for tracking a transaction. MerchantRequestID string `json:"MerchantRequestID"` // CheckoutRequestID is the unique request ID for the checkout process. CheckoutRequestID string `json:"CheckoutRequestID"` // ResponseCode is the response code for the request. ResponseCode string `json:"ResponseCode"` // ResponseDescription is a description of the response. ResponseDescription string `json:"ResponseDescription"` // CustomerMessage is a message for the customer. CustomerMessage string `json:"CustomerMessage"` }
LipaNaMpesaResponse represents a response from the Lipa Na Mpesa API.
type QRPayload ¶
type QRPayload struct { // MerchantName is the merchant name. MerchantName string `json:"MerchantName"` // RefNo is the reference number. RefNo string `json:"RefNo"` // Amount is the amount to be paid. Amount int `json:"Amount"` // TransactionType is the transaction code. //Transaction Type. The supported types are: // //BG: Pay Merchant (Buy Goods). // //WA: Withdraw Cash at Agent Till. // //PB: Paybill or Business number. // //SM: Send Money(Mobile number). // //SB: Sent to Business. Business number CreditPartyIdentifier in MSISDN format. TransactionType TransactionType `json:"TrxCode"` // CreditPartyIdentifier Credit Party Identifier. Can be a Mobile Number, Business Number, //Agent Till, Paybill or Business number, Merchant Buy Goods. CreditPartyIdentifier string `json:"CPI"` }
QRPayload represents a request payload for a QR code payment.
type QRResponse ¶
type QRResponse struct { // ResponseCode is the response code. ResponseCode string `json:"ResponseCode"` // RequestID is the request ID. RequestID string `json:"RequestID"` // ResponseDescription is the response description. ResponseDescription string `json:"ResponseDescription"` // QRCode is the QR code as a base64-encoded string. QRCode string `json:"QRCode"` }
QRResponse represents a response from a QR code payment request.
type RegisterURLConfig ¶
type ReversalPayload ¶
type ReversalPayload struct { Initiator string `json:"Initiator"` PassKey string `json:"SecurityCredential"` CommandID string `json:"CommandID"` TransactionID string `json:"TransactionID"` Amount string `json:"Amount"` ReceiverParty string `json:"ReceiverParty"` RecieverIdentifierType string `json:"RecieverIdentifierType"` ResultURL string `json:"ResultURL"` QueueTimeOutURL string `json:"QueueTimeOutURL"` Remarks string `json:"Remarks"` Occasion string `json:"Occasion"` }
type ReversalResponse ¶
type ReversalResponse struct { // OriginatorConversationID is the unique request ID for tracking a transaction. OriginatorConversationID string `json:"OriginatorConversationID"` // ConversationID is the unique request ID returned by mpesa for each request made. ConversationID string `json:"ConversationID"` // ResponseDescription is the response description message. ResponseDescription string `json:"ResponseDescription"` }
ReversalResponse represents a response from the Mpesa Reversal.
type STKPushStatusPayload ¶
type STKPushStatusPayload struct { BusinessShortCode string `json:"BusinessShortCode"` Password string `json:"Password"` Timestamp string `json:"Timestamp"` CheckoutRequestID string `json:"CheckoutRequestID"` }
STKPushStatusPayload is used to check the status of a transaction on Lipa Na M-Pesa Online Payment
type STKPushStatusResponse ¶
type STKPushStatusResponse struct { // MerchantRequestID This is a global unique Identifier for any submited payment request. String 16813-1590513-1 MerchantRequestID string `json:"MerchantRequestID"` // CheckoutRequestID This is a global unique Identifier for the processed payment request. String ws_CO_DMZ_1234567890 CheckoutRequestID string `json:"CheckoutRequestID"` // ResponseCode This is a Numeric status code that indicates the status of the transaction submission. 0 means successful submission and any other code means an error occured. Numeric 0\ ResponseCode string `json:"ResponseCode"` // ResponseDescription This is a description of the response code. String Success. Request accepted for processing ResponseDescription string `json:"ResponseDescription"` //ResultDesc Result description is a message from the API that gives the status of the request processing, usualy maps to a specific ResultCode value. It can be a Success processing message or an error description message. String E.g: 0: The service request is processed successfully., 1032: Request cancelled by user ResultDesc string `json:"ResultDesc"` // ResultCode This is a numeric status code that indicates the status of the transaction processing. 0 means successful processing and any other code means an error occured or the transaction failed. Numeric 0, 1032 ResultCode string `json:"ResultCode"` }
STKPushStatusResponse represents a response from the Lipa Na Mpesa API.
type TransactionStatus ¶
type TransactionStatus struct { Initiator string `json:"Initiator"` SecurityCredential string `json:"SecurityCredential"` CommandID string `json:"CommandID"` TransactionID string `json:"TransactionID"` PartyA string `json:"PartyA"` IdentifierType string `json:"IdentifierType"` ResultURL string `json:"ResultURL"` QueueTimeOutURL string `json:"QueueTimeOutURL"` Remarks string `json:"Remarks"` Occasion string `json:"Occasion"` }
type TransactionType ¶
type TransactionType string
const ( TransactionTypeBuyGoods TransactionType = "BG" TransactionTypePayBill TransactionType = "PB" TransactionTypeWithdraw TransactionType = "WA" // Withdraw Cash at Agent Till. TransactionTypeSendMoney TransactionType = "SM" // Send Money to a Phone Number. TransactionTypeSendtoBusiness TransactionType = "SB" // Send Money to a Business. )