Documentation
¶
Index ¶
- Variables
- func GetAccessToken(consumerKey, consumerSecret string) (string, error)
- func GetCredentials() (string, string, error)
- func QueryTransaction(accessToken, transactionID string) (*transactionStatusResponse, error)
- func QueryTransactionWithConfig(accessToken, transactionID string, config *Config) (*transactionStatusResponse, error)
- func SaveConfigTemplate(filepath string) error
- func SetCredentials(consumerKey, consumerSecret string) error
- type Config
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 ¶
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 ¶
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 ¶
SaveConfigTemplate creates a sample configuration file
func SetCredentials ¶
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 ¶
GetConfig returns the current configuration, loading from file and environment variables
func GetDefaultConfig ¶
func GetDefaultConfig() *Config
GetDefaultConfig returns a default configuration for sandbox testing