Go SDK

Installation

go get github.com/quarksgroup/paypack-go@latest

Initialization

package main

import (
    "github.com/quarksgroup/paypack-go"
    "github.com/quarksgroup/paypack-go/paypack"
)

type service struct{
    client *paypack.Client
}

func main() {
    cli := api.NewDefault()
    cli.Http = &http.Client{
        Transport: &oauth.Transport{
            Scheme: oauth.SchemeBearer,
            Source: oauth.ContextTokenSource(),
            Base:   http.DefaultTransport,
        },
    }
    return &service{client: cli}
}

cashin

package main

type txReq struct {
    amount float64
    phone  string
  mode   string //development or production
}

func (s *Service) Cashin(ctx context.Context, tx txReq, accessTk string) (*paypack.TransactionResponse, error) {

    ctx = context.WithValue(ctx, paypack.TokenKey{}, &paypack.Token{
        Access: accessTk,
    })

    in :=&paypack.TransactionRequest{
    Amount:tx.amount,
        Phone:tx.phone,
        Mode:tx.mode,
    }

    res, err := s.client.Transaction.Cashin(ctx, in)

    if err != nil {
        log.Printf("Cashin Error:%s", err)
        return nil, err
    }

    return res, err
}

Response

{
  "amount": 1000,
  "created_at": "2005-11-09T21:19:07.459Z",
  "kind": "CASHIN",
  "ref": "d0bb2807-1d52-4795-b373-3feaf63dceb1",
  "status": "pending"
}

cashout

package main

type txReq struct {
    amount float64
    phone string
  mode   string //development or production
}

func (s *Service) Cashout(ctx context.Context, tx txReq, accessTk string) (*paypack.TransactionResponse, error) {

    ctx = context.WithValue(ctx, paypack.TokenKey{}, &paypack.Token{
        Access: accessTk,
    })

    in :=&paypack.TransactionRequest{
    Amount:tx.amount,
        Phone:tx.phone,
        Mode:tx.mode,
    }

    res, err := s.client.Transaction.Cashout(ctx, in)

    if err != nil {
        log.Printf("Cashout Error:%s", err)
        return nil, err
    }

    return res, err
}

Response

{
  "amount": 1000,
  "created_at": "2005-11-09T21:19:07.459Z",
  "kind": "CASHOUT",
  "ref": "d0bb2807-1d52-4795-b373-3feaf63dceb1",
  "status": "pending"
}

Authorization

package main

import (
        "context"
        "error"
    )

type token struct {
    refresh_token string
    access_token string
    expires_at int64
}

//Login call
func (s *Service) Login(ctx context.Context, client_id, client_secret string) (*token, error) {

    tk, err := s.client.Auth.Login(ctx, client_id, client_secret)

    if err != nil {
        log.Printf("Login Error: %s", err)
        return nil, err
    }
    return &token{
        acces_token:tk.Access,
        refresh_token:tk.Refresh,
        expires_at:tk.Expires,
    }, nil
}

//Refresh access token
func (s *Service) Refresh(ctx context.Context, refresh_token string) (*/.Token, error) {

    in := &paypack.Token{
        Refresh: refresh_token,
    }

    tk, err := s.client.Auth.Refresh(ctx, in)

    if err != nil {
        log.Printf("refresh Error: %s", err)
        return nil, err
    }

    return &store.Token{
        Access:  tk.Access,
        Refresh: token.Refresh,
        Expires: token.Expires,
    }, nil
}

Response

{
  "access": "xxxxxxxxxxxxxxxxxxxx",
  "refresh": "xxxxxxxxxxxxxxxxxxxx",
  "expires": "xxxxxxxx"
}

Events

    package main


func (s *Service) Events(ctx context.Context, accessTk string, options ...paypack.Option) (*paypack.EventList, error) {

    ctx = context.WithValue(ctx, paypack.TokenKey{}, &paypack.Token{
        Access: accessTk,
    })

    res, err := s.client.Event.List(ctx, options...)
    if err != nil {
        log.Printf("Events Error:%s", err)
        return nil, err
    }

    return res, err

}

Response

{
  "amount": 1000,
  "client": "078xxxxxxx",
  "event-kind": "transaction:created",
  "kind": "CASHIN",
  "limit": 20,
  "offset": 0,
  "ref": "d0bb2807-1d52-4795-b373-3feaf63dceb1",
  "status": "failed",
  "total": 250,
  "transactions": [
    {
        "event_id": "bf76c3a8-cafe-11ec-9478-dead2ba023b5",
        "event_kind": "transaction:processed",
        "created_at": "2022-05-03T16:33:22.434606Z",
        "data": {
            "ref": "ajsfh44w3j-4h4r-28438-efnef-e9f44a5b4c2d",
            "kind": "CASHIN",
            "fee": 2.3,
            "merchant": "XXXXX",
            "client": "078xxxxxxx",
            "amount": 100,
            "status": "successful",
            "created_at": "2022-05-03T16:27:01.292808134Z",
            "processed_at": "2022-05-03T16:33:22.434351492Z"
        }
    },
  ]
}

Account information

package main

type merchant struct {
    Name string
    InRate float64
    OutRate float64
    Balance float64
}

func (s *Service) Me(ctx context.Context, accessTk string) (*merchant, error) {

    ctx = context.WithValue(ctx, paypack.TokenKey{}, &paypack.Token{
        Access: accessTk,
    })

    res, err := s.client.Merchant.Me(ctx)

    if err != nil {
        log.Printf("Merchant Error:%s", err)
        return nil, err
    }
    out := merchant{
        Name:    res.Name,
        In:      res.InRate,
        Out:     res.OutRate,
        Balance: res.Balance,
    }

    return out, err
}

Response

{
"balance": 10000,
"email": "email@example.com",
"id": "XXXXX",
"in_rate": 0.05,
"name": "Company Name",
"out_rate": 0.05,
}