Initial commit

Signed-off-by: GitHub <noreply@github.com>
This commit is contained in:
b1ackd0t
2022-07-09 17:31:31 +00:00
committed by GitHub
parent 268de546f1
commit 0d92d846c6
13 changed files with 630 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
ci:
strategy:
matrix:
go-version: [1.17.x, 1.18.x]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Verify dependencies
run: go mod verify
- name: Run go vet
run: go vet ./...
- name: Build
run: go build -v ./...
- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest
- name: Run staticcheck
run: staticcheck ./...
- name: Install golint
run: go install golang.org/x/lint/golint@latest
- name: Run golint
run: golint ./...
- name: Run tests
run: go test -mod=vendor -v -race -vet=off -covermode=count -coverprofile cover.out ./...
with:
go-version: ${{ matrix.go-version }}
# - name: Convert coverage.out to coverage.lcov
# uses: jandelgado/gcov2lcov-action@v1.0.6
# - name: Coveralls
# uses: coverallsapp/github-action@v1.1.2
# with:
# github-token: ${{ secrets.github_token }}
# path-to-lcov: coverage.lcov
+3
View File
@@ -0,0 +1,3 @@
module github.com/0x6f736f646f/kopokopo-go
go 1.18
+102
View File
@@ -0,0 +1,102 @@
package kopokopo
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
)
var tokenEndpoint = "oauth"
func (sdk kSDK) GetToken() (string, error) {
q := url.Values{}
q.Add("client_id", sdk.credentials.AppID)
q.Add("client_secret", sdk.credentials.Secret)
q.Add("grant_type", "client_credentials")
endpoint := fmt.Sprintf("%s/%s/token?%s", sdk.baseURL, tokenEndpoint, q.Encode())
req, err := http.NewRequest(http.MethodPost, endpoint, nil)
if err != nil {
return "", err
}
resp, err := sdk.makeRequest(req, "")
if err != nil {
return "", err
}
var tr tokenResp
if err := json.Unmarshal(resp, &tr); err != nil {
return "", err
}
return tr.AccessToken, nil
}
func (sdk kSDK) RevokeToken(token string) error {
if token == "" {
return errors.New("empty token")
}
q := url.Values{}
q.Add("client_id", sdk.credentials.AppID)
q.Add("client_secret", sdk.credentials.Secret)
q.Add("token", token)
endpoint := fmt.Sprintf("%s/%s/revoke?%s", sdk.baseURL, tokenEndpoint, q.Encode())
req, err := http.NewRequest(http.MethodPost, endpoint, nil)
if err != nil {
return err
}
_, err = sdk.makeRequest(req, "")
if err != nil {
return err
}
return nil
}
func (sdk kSDK) TokenIntrospection(token string) (tokenIntrospectionResp, error) {
if token == "" {
return tokenIntrospectionResp{}, errors.New("empty token")
}
q := url.Values{}
q.Add("client_id", sdk.credentials.AppID)
q.Add("client_secret", sdk.credentials.Secret)
q.Add("token", token)
endpoint := fmt.Sprintf("%s/%s/token/info?%s", sdk.baseURL, tokenEndpoint, q.Encode())
req, err := http.NewRequest(http.MethodPost, endpoint, nil)
if err != nil {
return tokenIntrospectionResp{}, err
}
resp, err := sdk.makeRequest(req, "")
if err != nil {
return tokenIntrospectionResp{}, err
}
var tir tokenIntrospectionResp
if err := json.Unmarshal(resp, &tir); err != nil {
return tir, err
}
return tir, nil
}
func (sdk kSDK) TokenInformation(token string) (tokenInfo, error) {
if token == "" {
return tokenInfo{}, errors.New("empty token")
}
endpoint := fmt.Sprintf("%s/%s/token/info", sdk.baseURL, tokenEndpoint)
req, err := http.NewRequest(http.MethodGet, endpoint, nil)
if err != nil {
return tokenInfo{}, err
}
resp, err := sdk.makeRequest(req, token)
if err != nil {
return tokenInfo{}, err
}
var ti tokenInfo
if err := json.Unmarshal(resp, &ti); err != nil {
return ti, err
}
return ti, nil
}
+8
View File
@@ -0,0 +1,8 @@
// This documentation gives you the specifications for connecting your systems to the Kopo Kopo Application.
// Primarily you can connect to the Kopo Kopo system to perform the following:
// Receive webhook notifications
// Receive payments from your users/customers
// Initiate payments to third parties
// Initiate transfers to your settlement accounts
package kopokopo
+86
View File
@@ -0,0 +1,86 @@
package kopokopo
import (
"io/ioutil"
"net/http"
"time"
)
// ContentType represents all possible content types.
type ContentType string
var _ SDK = (*kSDK)(nil)
// SDK contains Kopokopo API.
type SDK interface {
// The client credentials flow is the simplest OAuth 2 grant,
// with a server-to-server exchange of your applications client_id, client_secret
// for an OAuth application access token. In order to execute this flow,
// you will need to make an HTTP request from your application server,
// to the Kopo Kopo authorization server.
GetToken() (string, error)
// The request is used to revoke a particular token at a time.
RevokeToken(token string) error
// It can be used to check the validity of your access tokens, and find out other
// information such as which user and which scopes are associated with the token.
// The client secret will not be displayed as that is to remain confidential with the application owner.
TokenIntrospection(token string) (tokenIntrospectionResp, error)
// Shows details about the token used for authentication.
TokenInformation(token string) (tokenInfo, error)
}
// Credentials contains the credentials
type Credentials struct {
AppID string `json:"app_id"` // Application key.
Secret string `json:"secret"` // Application secret. Only revealed to the user when creating an application or during regeneration of client credentials.
APIKey string `json:"api_key"`
}
type kSDK struct {
baseURL string
credentials Credentials
client *http.Client
}
// Config contains sdk configuration parameters.
type Config struct {
BaseURL string
Credentials Credentials
MaxIdleConns int
IdleConnTimeout time.Duration
}
// NewSDK returns new mainflux SDK instance.
func NewSDK(conf Config) SDK {
return &kSDK{
baseURL: conf.BaseURL,
credentials: conf.Credentials,
client: &http.Client{
Transport: &http.Transport{
MaxIdleConns: conf.MaxIdleConns,
IdleConnTimeout: conf.IdleConnTimeout,
},
},
}
}
func (sdk kSDK) makeRequest(req *http.Request, token string) ([]byte, error) {
if token != "" {
req.Header.Add("Authorization", "Basic "+token)
}
req.Header.Add("Content-Type", "application/json")
resp, err := sdk.client.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return body, nil
}
+14
View File
@@ -0,0 +1,14 @@
package kopokopo
// Receive payments from M-PESA users via STK Push.
// func (sdk kSDK) ReceiveMpesaPayment(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) ProcessIncommingMpesaPayment(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) QueryIncommingMpesaPayment(grantType string) (string, error) {
// panic("Not implemented")
// }
+33
View File
@@ -0,0 +1,33 @@
package kopokopo
// func (sdk kSDK) AddPayRecipients(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) PayMobileRecipient(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) PayBankRecipient(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) PayTillRecipient(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) PayPaybillRecipient(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) CreatePayment(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) ProcessPayment(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) QueryPayment(grantType string) (string, error) {
// panic("Not implemented")
// }
+11
View File
@@ -0,0 +1,11 @@
package kopokopo
// func (sdk kSDK) PollingAPI(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) ProcessPollingAPI(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) QueryPollingAPI(grantType string) (string, error) {
// panic("Not implemented")
// }
+209
View File
@@ -0,0 +1,209 @@
package kopokopo
// type CreateWebhookReq struct {
// EventType string `json:"event_type,omitempty"` //The type of event you are subscribing to
// URL string `json:"url,omitempty"` // The http end point to send the webhook.
// Scope string `json:"scope,omitempty"` // The scope of the webhook subscription.
// ScopeRef string `json:"scope_reference,omitempty"`
// }
// func (cwr CreateWebhookReq) Validate() error {
// if cwr.EventType != "buygoods_transaction_received" &&
// cwr.EventType != "buygoods_transaction_reversed" &&
// cwr.EventType != "b2b_transaction_received" &&
// cwr.EventType != "m2m_transaction_received" &&
// cwr.EventType != "settlement_transfer_completed" &&
// cwr.EventType != "customer_created" {
// return errors.New("invalid event type")
// }
// if !strings.HasPrefix(cwr.URL, "https") {
// return errors.New("URL is not secured with TLS")
// }
// if cwr.Scope != "company" && cwr.Scope != "till" {
// return errors.New("invalid scope")
// }
// if cwr.Scope == "till" && cwr.ScopeRef == "" {
// return errors.New("scope reference is required")
// }
// return nil
// }
// type Destination struct {
// Type string `json:"type,omitempty"`
// Resource DestinationResource `json:"resource,omitempty"`
// }
// type DestinationResource struct {
// Reference string `json:"reference,omitempty"` // The destination reference
// AccountName string `json:"account_name,omitempty"` // The name as indicated on the bank account
// AccountNumber string `json:"account_number,omitempty"` // The bank account number
// BankBranchReference string `json:"bank_branch_ref,omitempty"` // An identifier identifying the destination bank branch
// SettlementMethod string `json:"settlement_method,omitempty"` // EFT or RTS
// FirstName string `json:"first_name,omitempty"` // String First name of the recipient
// LastName string `json:"last_name,omitempty"` // Last name of recipient
// Email string `json:"email,omitempty"` // Email of recipient
// PhoneNumber string `json:"phone_number,omitempty"` // Phone number
// Network string `json:"network,omitempty"` // The mobile network to which the phone number belongs
// }
// type Disbursements struct {
// Status string `json:"status,omitempty"` // The status of the disbursement
// Amount string `json:"amount,omitempty"` // The amount of the disbursement
// OriginationTime string `json:"origination_time,omitempty"` // The Timestamp of when the transaction took place
// TransactionalReference string `json:"transactional_reference,omitempty"` // The reference from the transaction. i.e mpesa reference It is null for eft transactions
// }
// type Resource struct {
// ID string `json:"id,omitempty"` // The api reference of the transaction
// Amount float64 `json:"amount,omitempty"` // The amount of the transaction
// Status string `json:"status,omitempty"` // The status of the transaction
// System string `json:"system,omitempty"` // The mobile money system
// Currency string `json:"currency,omitempty"` // Currency
// Reference string `json:"reference,omitempty"` // The mpesa reference
// TransactionalReference string `json:"transactional_reference,omitempty"`
// TillNumber string `json:"till_number,omitempty"` // The till number to which the payment was made
// SendingTill string `json:"sending_till,omitempty"` // The till number of the sender
// AccountName string `json:"account_name,omitempty"`
// AccountNumber string `json:"account_number,omitempty"`
// BankBranchReference string `json:"bank_branch_ref,omitempty"`
// SettlementMethod string `json:"settlement_method,omitempty"`
// SendingMerchant string `json:"sending_merchant,omitempty"` // Name of merchant
// SenderPhoneNumber string `json:"sender_phone_number,omitempty"` // The phone number that sent the payment
// OriginationTime string `json:"origination_time,omitempty"` // The transaction timestamp
// SenderLastName string `json:"sender_last_name,omitempty"` // Last name of payer
// SenderFirstName string `json:"sender_first_name,omitempty"` // First name of payer
// SenderMiddleName string `json:"sender_middle_name,omitempty"` // Middle name of payer
// Destination Destination `json:"destination,omitempty"` // The destination of the settlement transfer
// Disbursements []Disbursements `json:"disbursements,omitempty"` // These are the disbursements in that particular transfer batch
// }
// type Event struct {
// Type string `json:"type,omitempty"` // The type of transaction
// Resource Resource `json:"resource,omitempty"` // The resource corresponding to the event.
// }
// type Links struct {
// Self string `json:"self,omitempty"`
// Resource string `json:"resource,omitempty"`
// Callback string `json:"callback_url,omitempty"`
// }
// type BuyGoodsTrans struct {
// Topic string `json:"topic,omitempty"` // The ID of the Webhook Event
// ID string `json:"id,omitempty"` // The topic of the webhook.
// CreatedAt string `json:"created_at,omitempty"` // The timestamp of when the webhook event was created.
// Event Event `json:"event,omitempty"`
// Links Links `json:"_links,omitempty"` // A JSON object containing links to the Webhook Event and the corresponding Buygoods Transaction resource
// }
// func (bgt BuyGoodsTrans) Validate() error {
// return nil
// }
// type CustomerResource struct {
// LastName string `json:"last_name,omitempty"` // Last name of payer
// FirstName string `json:"first_name,omitempty"` // First name of payer
// MiddleName string `json:"middle_name,omitempty"` // Middle name of payer
// PhoneNumber string `json:"phone_number,omitempty"` // The phone number that sent the payment
// }
// type CustomerEvent struct {
// Type string `json:"type,omitempty"` // The type of record (Mobile Money User)
// Resource CustomerResource `json:"resource,omitempty"` // The resource corresponding to the event.
// }
// type CustomerReq struct {
// Topic string `json:"topic,omitempty"` // The ID of the Webhook Event
// ID string `json:"id,omitempty"` // The topic of the webhook.
// CreatedAt string `json:"created_at,omitempty"` // The timestamp of when the webhook event was created.
// Event CustomerEvent `json:"event,omitempty"`
// Links Links `json:"_links,omitempty"` // A JSON object containing links to the Webhook Event and the corresponding Buygoods Transaction resource
// }
// type Subscriber struct {
// LastName string `json:"last_name,omitempty"` // Last name of the subscriber
// FirstName string `json:"first_name,omitempty"` // First name of the subscriber
// MiddleName string `json:"middle_name,omitempty"` // Middle name of the subscriber
// PhoneNumber string `json:"phone_number,omitempty"` // The phone number of the subscriber from which the payment will be made
// Email string `json:"email,omitempty"` // E-mail address of the subscriber - optional
// }
// type Amount struct {
// Value string `json:"value,omitempty"` // The amount of the transaction
// Currency string `json:"currency,omitempty"` // Currency
// }
// type ReceiveMpesaReq struct {
// PaymentChannel string `json:"payment_channel,omitempty"` // The payment channel to be used eg. M-PESA
// TillNumber string `json:"till_number,omitempty"` // The online payments till number from the Kopo Kopo dashboard to which the payment will be made
// Subscriber Subscriber `json:"subscriber,omitempty"` // A Subscriber JSON object see below
// Amount Amount `json:"amount,omitempty"` // An Amount JSON object containing currency and amount
// Metadata map[string]interface{} `json:"metadata,omitempty"` // An optional JSON object containing a maximum of 5 key value pairs
// Links Links `json:"_links,omitempty"` // A JOSN object containing the call back URL where the result of the Incoming Payment will be posted
// }
// type IncomingPaymentEvent struct {
// Type string `json:"type,omitempty"` // The type of record (Mobile Money User)
// Resource Resource `json:"resource,omitempty"` // The resource corresponding to the event.
// Errors string `json:"errors,omitempty"` // A string containing information on the error than occured
// }
// type ProcessIncommingPaymentReq struct {
// Topic string `json:"topic,omitempty"` // The topic of the request.
// ID string `json:"id,omitempty"` // The ID of the Incoming Payment
// InitiationTime string `json:"initiation_time,omitempty"` // The timestamp of when the webhook event was created.
// Status string `json:"status,omitempty"` // A status string denoting the status of the Incoming Payment
// Event IncomingPaymentEvent `json:"event,omitempty"` // A JSON Object encapsulating the event of the request
// Metadata map[string]interface{} `json:"metadata,omitempty"` // An optional JSON object containing a maximum of 5 key value pairs
// Links Links `json:"_links,omitempty"` // A JSON object containing links to the Webhook Event and the corresponding Buygoods Transaction resource
// }
// type PaymentRecipient struct {
// LastName string `json:"last_name,omitempty"` // Last name of the recipient
// FirstName string `json:"first_name,omitempty"` // First name of the recipient
// MiddleName string `json:"middle_name,omitempty"` // Middle name of the recipient
// PhoneNumber string `json:"phone_number,omitempty"` // The phone number of the recipient from which the payment will be made
// Email string `json:"email,omitempty"` // E-mail address of the recipient - optional
// AccountName string `json:"account_name,omitempty"` //The name as indicated on the bank account name
// BankBranchReference string `json:"bank_branch_ref,omitempty"` // An identifier identifying the destination bank branch.
// AccountNumber string `json:"account_number,omitempty"` // The bank account number
// SettlementMethod string `json:"settlement_method,omitempty"` // RTS
// TillName string `json:"till_name,omitempty"` // The name as indicated on the till
// TillNumber string `json:"till_number,omitempty"` // The till number
// PayBillName string `json:"paybill_name,omitempty"` // The name referring to the paybill
// PayBillNumber string `json:"paybill_number,omitempty"` // The paybill business number
// PayBillAccountNumber string `json:"paybill_account_number,omitempty"` // The paybill account number
// }
// type AddPAYRecipient struct {
// Type string `json:"type,omitempty"` // The type of the recipient eg. mobile wallet or bank account
// PaymentRecipient PaymentRecipient `json:"payment_recipient,omitempty"` // A JSON object containing details of the recipeint
// }
// type CreatePaymentReq struct {
// DestinationType string `json:"destination_type,omitempty"` // Pay recipient type (bank_account, mobile_wallet, till or paybill
// DestinationReference string `json:"destination_reference,omitempty"` // Reference for the destination.
// Amount Amount `json:"amount,omitempty"` // A JSON object containing the currency and the amount to be transferred
// Description string `json:"description,omitempty"` // A reason for the payment
// Category string `json:"category,omitempty"` // Categorize the transaction
// Tags string `json:"tags,omitempty"` // Define your own tag to label the transaction with
// Metadata map[string]interface{} `json:"metadata,omitempty"` // A JSON containing upto a maximum of 5 key-value pairs for your own use
// Links Links `json:"_links,omitempty"` // A JSON containing a call back URL where the results of the Payment will be posted. MUST be a secure HTTPS (TLS) endpoint
// }
// type MerchantBankAccountReq struct {
// }
// type MerchantMobileAccountReq struct {
// }
// type BlinkTransferReq struct {
// }
// type TargetedTransferReq struct {
// }
// type SendSMSReq struct {
// WebhookID string `json:"webhook_event_reference,omitempty"` // This is the id of the webhook payload you got
// Message string `json:"message,omitempty"` // A string containing the message you want to send to the customer
// Links Links `json:"_links,omitempty"` // A JSON object containing the callback_url where the result of the Transaction Notification will be posted
// }
+32
View File
@@ -0,0 +1,32 @@
package kopokopo
// type ErrorResp struct {
// Code int `json:"error_code"`
// Message string `json:"error_message"`
// }
type tokenResp struct {
AccessToken string `json:"access_token,omitempty"`
Type string `json:"token_type,omitempty"`
Expiry uint64 `json:"expires_in,omitempty"`
Creation uint64 `json:"created_at,omitempty"`
}
type tokenIntrospectionResp struct {
Active bool `json:"active,omitempty"`
Scope string `json:"scope,omitempty"`
ClientID string `json:"client_id,omitempty"`
Type string `json:"token_type,omitempty"`
Expiry uint64 `json:"exp,omitempty"`
Creation uint64 `json:"iat,omitempty"`
}
type application struct {
UID string `json:"uid,omitempty"`
}
type tokenInfo struct {
OwnerID string `json:"resource_owner_id,omitempty"`
Scope []string `json:"scope,omitempty"`
Expiry uint64 `json:"expires_in,omitempty"`
Application application `json:"application,omitempty"`
Creation uint64 `json:"created_at,omitempty"`
}
+11
View File
@@ -0,0 +1,11 @@
package kopokopo
// func (sdk kSDK) SendSMSNotification(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) ProcessTransactionNotification(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) QueryTransactionNotification(grantType string) (string, error) {
// panic("Not implemented")
// }
+23
View File
@@ -0,0 +1,23 @@
package kopokopo
// func (sdk kSDK) CreateMerchantBankAccount(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) CreateMerchantMobileWallet(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) CreateBlindTransfer(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) CreateTargetedTransfer(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) ProcessSettlemntTranser(grantType string) (string, error) {
// panic("Not implemented")
// }
// func (sdk kSDK) QueryTransferStatus(grantType string) (string, error) {
// panic("Not implemented")
// }
+40
View File
@@ -0,0 +1,40 @@
package kopokopo
// func (sdk kSDK) CreateWebhook(webookReq CreateWebhookReq) (string, error) {
// panic("Not implemented")
// }
// // Before processing webhook events, make sure that they originated from Kopo Kopo
// func (sdk kSDK) ValidateWebhook() error {
// panic("Not implemented")
// }
// // Notifies your application when a Buygoods Transaction has been received.
// func (sdk kSDK) C2BSubscription(buyGoodsReq BuyGoodsTrans) (string, error) {
// panic("Not implemented")
// }
// // Notifies your application when a B2b (External Till to Till transaction) has been received.
// // These are payments recieved from other tills and not subscribers.
// func (sdk kSDK) B2BSubscription(b2bReq BuyGoodsTrans) (string, error) {
// panic("Not implemented")
// }
// // Notifies your application when another Kopo Kopo merchant transfers funds to your Kopo Kopo merchant account (Merchant to Merchant)
// func (sdk kSDK) M2MSubscription(m2mReq BuyGoodsTrans) (string, error) {
// panic("Not implemented")
// }
// // Notifies your application when a Buygoods Transaction has been reversed
// func (sdk kSDK) C2BReversalSubscription(c2bReq BuyGoodsTrans) error {
// panic("Not implemented")
// }
// // Parameters contained in a settlement_transfer_completed webhook
// func (sdk kSDK) SettlementSub(setReq BuyGoodsTrans) error {
// panic("Not implemented")
// }
// func (sdk kSDK) CusomerCreationSub(cReq CustomerReq) error {
// panic("Not implemented")
// }