mpesa

package
v1.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AuthURL = "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials"

AuthURL is the M-Pesa OAuth endpoint URL (can be modified for testing)

Functions

func GetAccessToken

func GetAccessToken(consumerKey, consumerSecret string) (string, error)

GetAccessToken authenticates with the M-Pesa API using consumer credentials. It sends a request to the M-Pesa OAuth endpoint and returns an access token that can be used for subsequent API calls. The token is valid for the duration specified in the ExpiresIn field of the response.

Parameters:

  • consumerKey: The consumer key from the Daraja portal
  • consumerSecret: The consumer secret from the Daraja portal

Returns:

  • string: The access token for API authentication
  • error: Any error that occurred during authentication

func GetCredentials

func GetCredentials() (string, string, error)

GetCredentials retrieves the M-Pesa consumer key and secret from the system keychain. The credentials must have been previously stored using SetCredentials.

Returns:

  • string: The consumer key
  • string: The consumer secret
  • error: Any error that occurred during retrieval, including if credentials are not found

func QueryTransaction

func QueryTransaction(accessToken, transactionID string) (*transactionStatusResponse, error)

QueryTransaction sends a request to the M-Pesa transaction status API. It queries the status of a specific M-Pesa transaction using the transaction ID. Returns the transaction status response or an error if the query fails.

func QueryTransactionWithConfig

func QueryTransactionWithConfig(accessToken, transactionID string, config *Config) (*transactionStatusResponse, error)

QueryTransactionWithConfig sends a request to the M-Pesa transaction status API using the provided config. If config is nil, it will load the config from file/environment or use defaults.

func SaveConfigTemplate

func SaveConfigTemplate(filepath string) error

SaveConfigTemplate creates a sample configuration file

func SetCredentials

func SetCredentials(consumerKey, consumerSecret string) error

SetCredentials securely stores the M-Pesa consumer key and secret in the system keychain. The credentials are encrypted and stored using the operating system's native keychain/credential manager (Keychain on macOS, Credential Manager on Windows, Secret Service on Linux).

Parameters:

  • consumerKey: The consumer key to store
  • consumerSecret: The consumer secret to store

Returns:

  • error: Any error that occurred during storage

Types

type Config

type Config struct {
	// BusinessShortcode is your organization's shortcode (Paybill or Buygoods - A 5 to 7 digit account number)
	BusinessShortcode string `mapstructure:"business_shortcode"`

	// SecurityCredential is the encrypted credential of the user getting transaction details
	SecurityCredential string `mapstructure:"security_credential"`

	// Environment specifies which M-Pesa environment to use: "sandbox" or "production"
	Environment string `mapstructure:"environment"`

	// Initiator is the name of Initiator to initiating the request
	Initiator string `mapstructure:"initiator"`

	// ResultURL is the path that stores information of transaction
	ResultURL string `mapstructure:"result_url"`

	// QueueTimeOutURL is the path that stores information of time out transaction
	QueueTimeOutURL string `mapstructure:"queue_timeout_url"`
}

Config holds configuration for M-Pesa API operations

func GetConfig

func GetConfig() (*Config, error)

GetConfig returns the current configuration, loading from file and environment variables

func GetDefaultConfig

func GetDefaultConfig() *Config

GetDefaultConfig returns a default configuration for sandbox testing

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL