# go-mpesa
A Go library for the M-Pesa Mozambique API.
Ported from mpesa-mz-nodejs-lib.
## Installation
```bash
go get
github.com
```
## Usage
### Configuration
```go
import "
github.com"
config := mpesa.Config{
PublicKey: " ", // Note: The key must NOT be in PEM format
APIHost: "api.sandbox.vm.co.mz",
APIKey: " ",
Origin: " ",
ServiceProviderCode: " ",
InitiatorIdentifier: " ",
SecurityCredential: " ",
}
client, err := mpesa.NewClient(config)
if err != nil {
panic(err)
}
```
### Customer to Business (C2B)
```go
response, err := client.C2B(mpesa.C2BRequest{
Amount: 100.0,
MSISDN: "841234567",
Reference: "REF123",
ThirdPartyReference: "3RD123",
})
if err != nil {
log.Fatal(err)
}
fmt.Println(response)
```
### Query Status
```go
response, err := client.Query(mpesa.QueryRequest{
QueryReference: "TRANS_ID_OR_CONV_ID",
ThirdPartyReference: "3RD123",
})
```
### Reversal
```go
response, err := client.Reverse(mpesa.ReversalRequest{
Amount: 100.0,
TransactionID: "TRANS_ID",
ThirdPartyReference: "3RD123",
})
```
### Business to Customer (B2C)
```go
response, err := client.B2C(mpesa.B2CRequest{
Amount: 100.0,
MSISDN: "841234567",
Reference: "REF123",
ThirdPartyReference: "3RD123",
})
```
## Features
- RSA Encryption for Authentication (Bearer Token)
- MSISDN Validation (Mozambique format)
- Parameter handling for standard M-Pesa operations
## Example
You can find a complete runnable example in `test/test.go`.
### Prerequisites
Create a `.env` file in your project root with your credentials:
```bash
MPESA_API_KEY=your_api_key_here
MPESA_PUBLIC_KEY=your_public_key_here
MPESA_API_HOST=api.sandbox.vm.co.mz
MPESA_ORIGIN=developer.mpesa.vm.co.mz
MPESA_SERVICE_PROVIDER_CODE=171717
MPESA_INITIATOR_IDENTIFIER=your_initiator_identifier
MPESA_SECURITY_CREDENTIAL=your_security_cr …