Skip to content

Commit

Permalink
Merge pull request #4 from leulabay1/add-remaining-endpoints
Browse files Browse the repository at this point in the history
Add list all transactions, list all banks and bulk transfer endpoints to the sdk with their respective tests and update the doc
  • Loading branch information
isrugeek authored Oct 23, 2024
2 parents 58fbdd2 + 6d18bfe commit f4f1ac0
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 116 deletions.
46 changes: 43 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
# Chapa-Go
<h1 align="center">
<div align="center">
<a href="https://chapa.co/" target="_blank">
<img src="./docs/logo.png" width="320" alt="Nest Logo"/>
</a>
<p align="center">Go SDK for chapa</p>
</div>
</h1>

![build-workflow](https://github.com/Yinebeb-01/chapa-go/actions/workflows/test.yml/badge.svg)

Unofficial Golang SDK for Chapa ET API
Expand All @@ -11,7 +19,7 @@ Unofficial Golang SDK for Chapa ET API
### Usage
##### 1. Installation
```
go get github.com/Yinebeb-01/chapa-go
go get github.com/Chapa-Et/chapa-go
```

###### API_KEY
Expand All @@ -24,7 +32,7 @@ If you want to run the githb action on your forked repository, you have to creat
package main

import (
chapa "github.com/Yinebeb-01/chapa-go"
chapa "github.com/Chapa-Et/chapa-go"
)

func main(){
Expand Down Expand Up @@ -85,6 +93,38 @@ If you want to run the githb action on your forked repository, you have to creat
fmt.Printf("transfer response: %+v\n", response)
```

##### 6. Get transactions
```go
response, err := chapaAPI.getTransactions()
fmt.Printf("transactions response: %+v\n", response)
```

##### 7. Get banks
```go
response, err := chapaAPI.getBanks()
fmt.Printf("banks response: %+v\n", response)
```

##### 8. Bulk transfer
```go
bulkData := BulkData{
AccountName: "Leul Abay Ejigu",
AccountNumber: "1000212482106",
Amount: 10,
Reference: "3241342142sfdd",
BankCode: "946",
}

request := &BulkTransferRequest{
Title: "Transfer to leul",
Currency: "ETB",
BulkData: []BulkData{bulkData},
}

response, err := chapaAPI.bulkTransfer(request)
fmt.Printf("bulk transfer response: %+v\n", response)
```

### Resources
- https://developer.chapa.co/docs/overview/

Expand Down
134 changes: 125 additions & 9 deletions chapa.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,47 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/spf13/viper"
"io"
"io/ioutil"
"log"
"net/http"
"time"

"github.com/spf13/viper"
)

const (
acceptPaymentV1APIURL = "https://api.chapa.co/v1/transaction/initialize"
verifyPaymentV1APIURL = "https://api.chapa.co/v1/transaction/verify/%v"
transferToBankV1APIURL = "https://api.chapa.co/v1/transfers"
transactionsV1APIURL = "https://api.chapa.co/v1/transactions"
banksV1APIURL = "https://api.chapa.co/v1/banks"
bulkTransferAPIURL = "https://api.chapa.co/v1/bulk-transfers"
)

type API interface {
PaymentRequest(request *PaymentRequest) (*PaymentResponse, error)
Verify(txnRef string) (*VerifyResponse, error)
TransferToBank(request *BankTransfer) (*BankTransferResponse, error)
getTransactions() (*TransactionsResponse, error)
getBanks() (*BanksResponse, error)
bulkTransfer(*BulkTransferRequest) (*BulkTransferResponse, error)
}

type chap struct {
type chapa struct {
apiKey string
client *http.Client
}

func New() API {
return &chap{
return &chapa{
apiKey: viper.GetString("API_KEY"),
client: &http.Client{
Timeout: 1 * time.Minute,
},
}
}

func (c *chap) PaymentRequest(request *PaymentRequest) (*PaymentResponse, error) {
func (c *chapa) PaymentRequest(request *PaymentRequest) (*PaymentResponse, error) {
var err error
if err = request.Validate(); err != nil {
err := fmt.Errorf("invalid input %v", err)
Expand Down Expand Up @@ -81,7 +87,7 @@ func (c *chap) PaymentRequest(request *PaymentRequest) (*PaymentResponse, error)
return &paymentResponse, nil
}

func (c *chap) Verify(txnRef string) (*VerifyResponse, error) {
func (c *chapa) Verify(txnRef string) (*VerifyResponse, error) {

req, err := http.NewRequest(http.MethodGet, fmt.Sprintf(verifyPaymentV1APIURL, txnRef), nil)
if err != nil {
Expand All @@ -99,7 +105,7 @@ func (c *chap) Verify(txnRef string) (*VerifyResponse, error) {

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
Expand All @@ -114,7 +120,7 @@ func (c *chap) Verify(txnRef string) (*VerifyResponse, error) {
return &verifyResponse, nil
}

func (c *chap) TransferToBank(request *BankTransfer) (*BankTransferResponse, error) {
func (c *chapa) TransferToBank(request *BankTransfer) (*BankTransferResponse, error) {
var err error
if err = request.Validate(); err != nil {
err := fmt.Errorf("invalid input %v", err)
Expand Down Expand Up @@ -151,7 +157,117 @@ func (c *chap) TransferToBank(request *BankTransfer) (*BankTransferResponse, err
return nil, err
}

response := BankTransferResponse{}
var response BankTransferResponse
err = json.Unmarshal(body, &response)
if err != nil {
log.Printf("error while unmarshaling response %v", err)
return nil, err
}
return &response, nil
}

func (c *chapa) getTransactions() (*TransactionsResponse, error) {
req, err := http.NewRequest(http.MethodGet, transactionsV1APIURL, nil)
if err != nil {
log.Printf("error %v", err)
return nil, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+c.apiKey)

resp, err := c.client.Do(req)
if err != nil {
log.Printf("error %v", err)
return nil, err
}

body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("error while reading response body %v", err)
return nil, err
}

var response TransactionsResponse
err = json.Unmarshal(body, &response)
if err != nil {
log.Printf("error while unmarshaling response %v", err)
return nil, err
}

return &response, nil
}

func (c *chapa) getBanks() (*BanksResponse, error) {
req, err := http.NewRequest(http.MethodGet, banksV1APIURL, nil)
if err != nil {
log.Printf("error %v", err)
return nil, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+c.apiKey)

resp, err := c.client.Do(req)
if err != nil {
log.Printf("error %v", err)
return nil, err
}

body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("error while reading response body %v", err)
return nil, err
}

var response BanksResponse
err = json.Unmarshal(body, &response)
if err != nil {
log.Printf("error while unmarshaling response %v", err)
return nil, err
}

return &response, nil
}

func (c *chapa) bulkTransfer(request *BulkTransferRequest) (*BulkTransferResponse, error) {
var err error
if err = request.Validate(); err != nil {
err := fmt.Errorf("invalid input %v", err)
log.Printf("error %v input %v", err, request)
return nil, err
}

data, err := json.Marshal(request)
if err != nil {
return nil, err
}

req, err := http.NewRequest(http.MethodPost, bulkTransferAPIURL, bytes.NewBuffer(data))
if err != nil {
log.Printf("error %v", err)
return nil, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+c.apiKey)
req.Close = true

resp, err := c.client.Do(req)
if err != nil {
log.Printf("error %v", err)
return nil, err
}

defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
log.Printf("error while reading resposne body %v", err)
return nil, err
}

response := BulkTransferResponse{}
err = json.Unmarshal(body, &response)
if err != nil {
log.Printf("error while unmarshaling response %v", err)
Expand Down
Loading

0 comments on commit f4f1ac0

Please sign in to comment.