mpower

package module
v0.0.0-...-4fcf146 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2015 License: MIT Imports: 5 Imported by: 0

README

MPOWER - GOLANG LIBRARY FOR MPOWER API

Build Status

GoDoc

This a go implementation library that interfaces with the mpower http api.

Built on the MPower Payments HTTP API (beta).

Installation

$ go get github.com/ngenerio/mpower

Documentation

Create a new store instance to use in the checkout or onsite invoice

mpowerStore := NewStore("Awesome Store")

Create a new setup instance to use in the checkout or onsite invoice

// Get your keys from MPower Integration Setup
mpowerSetup := NewSetup(MASTER_KEY, PRIVATE_KEY , PUBLIC_KEY, TOKEN)
Checkout and Onsite Invoice

To use the checkout invoice, you need create an mpower instance

mpower := mpower.NewMPower(seup, store, "test")
checkout := mpower.NewCheckoutInvoice(mpower)
onsite := mpower.NewOnsiteInvoice(mpower)

Add an item to the invoice

checkout.AddItem("Yam Phone", 1, 50.00, 50.00, "Hello World")

Add tax information to the invoice to be displayed on the cutomer's receipt

checkout.AddTax("VAT", 30.00)

Set custom data on the invoice

checkout.SetCustomData("bonus", yeah)

Set some description on the invoice

checkout.SetDescription("Hello World")

Set the total amount on the invoice

checkout.SetTotalAmount(80.00)
Creating an invoice

This sample code shows how to create an mpower invoice after adding some items to your checkout or onsite invoice

//`response` is of type [`napping.Response`](http://godoc.org/github.com/jmcvetta/napping#Response)
responseBody, response, err := checkout.Create()

if err != nil {
    // handle the error
}
// where `responseBody.Token` is the token of the created invoice
fmt.Println(responseBody.Token)
Confirming the status of an invoice
// `TOKEN` is the token of the invoice created
responseBody, response, err := checkout.Confirm(TOKEN)

if err != nil {
    // handle the error
}

// `response.Status` could either be `pending`, `cancelled` or `completed`
fmt.Println(responseBody.Status)
Charging the mpower customer with onsite payment request
// `TOKEN` is the onsite token of the invoice created and the `CUSTOMER_TOKEN` is from the customer
responseBody, response, err := onsite.Charge(TOKEN, CUSTOMER_TOKEN)
Direct Mobile

Docs coming up soon

Direct Pay

Docs coming up soon

For more docs, read up: Mpower docs

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Actions

type Actions struct {
	CancelURL string `json:"cancel_url,omitempty"`
	ReturnURL string `json:"return_url,omitempty"`
}

Actions represent the action urls of the invoice

type CheckoutInvoice

type CheckoutInvoice struct {
	Invoice
	// contains filtered or unexported fields
}

CheckoutInvoice holds all the data related to checkout invoice Invoice is an embedded struct, so all methods of Invoice can be called on it

func NewCheckoutInvoice

func NewCheckoutInvoice(mp *MPower) *CheckoutInvoice

NewCheckoutInvoice - create a new checkout instance

Example.

checkout := mpower.NewCheckoutInvoice(mpower)

func (*CheckoutInvoice) Confirm

Confirm - This confirms the token status

func (*CheckoutInvoice) Create

Create - creates a new invoice on mpower

type CheckoutInvoiceRequest

type CheckoutInvoiceRequest struct {
	Invoice
}

type CheckoutInvoiceResponse

type CheckoutInvoiceResponse struct {
	Response
	Token string `json:"token,omitempty"`
}

CheckoutInvoiceResponse is the response data as specified by the mpower It retrieves the response json data and stores it on the checkout invoice object

type CheckoutInvoiceStatus

type CheckoutInvoiceStatus struct {
	Response
	Status string `json:"status,omitempty"`
}

CheckoutInvoiceStatus holds all the data related to status of an invoice created on mpower

type DirectMobile

type DirectMobile struct {
	// contains filtered or unexported fields
}

DirectMobile is used to handle api requests to mpower direct mobile payments

func NewDirectMobile

func NewDirectMobile(mp *MPower) *DirectMobile

NewDirectMobile creates a new DirectMobile instance

func (*DirectMobile) Charge

func (d *DirectMobile) Charge(name, email, phone, merchant, wallet, amount string) (*DirectMobileResponse, *napping.Response, error)

Charge charges customers' mobile money money wallets directly on your site or application

Example.

resp, err := directMobileInstance.Charge("Eugene", "ngene84@gmail.com", "0272271893", "Awesome Shopping", "MTN", "20")

func (*DirectMobile) Status

Status checks the status of a direct mobile transaction

type DirectMobileRequest

type DirectMobileRequest struct {
	CustomerName   string `json:"customer_name"`
	CustomerEmail  string `json:"customer_email"`
	CustomerPhone  string `json:"customer_phone"`
	MerchantName   string `json:"merchant_name"`
	WalletProvider string `json:"wallet_provider"`
	Amount         string `json:"amount"`
}

DirectMobileRequest The request json to be sent over during a `charge` api request

type DirectMobileResponse

type DirectMobileResponse struct {
	Response
	Token               string `json:"token"`
	TransactionID       string `json:"transaction_id"`
	MobileInvoiceNumber string `json:"mobile_invoice_no"`
}

DirectMobileResponse is the response from a direct mobile charge request

type DirectMobileStatusResponse

type DirectMobileStatusResponse struct {
	Response
	TXStatus            string `json:"tx_status"`
	TransactionID       string `json:"transaction_id"`
	MobileInvoiceNumber string `json:"mobile_invoice_no"`
	CancelReason        string `json:"cancel_reason"`
}

DirectMobileStatusResponse is the status of a direct mobile transaction

type DirectPay

type DirectPay struct {
	// contains filtered or unexported fields
}

DirectPay - the direct pay object as defined by mpower

func NewDirectPay

func NewDirectPay(mp *MPower) *DirectPay

NewDirectPay - creates a DirectPay instance

func (*DirectPay) CreditAccount

func (d *DirectPay) CreditAccount(account string, amount int) (*DirectPayResponse, *napping.Response, error)

CreditAccount - credits the account of an mpower customer

type DirectPayRequest

type DirectPayRequest struct {
	Alias  string `json:"account_alias,omitempty"`
	Amount int    `json:"amount,omitempty"`
}

DirectPayRequest - `struct` to send the data as json to mpower

type DirectPayResponse

type DirectPayResponse struct {
	Response
	TransactionID string `json:"transaction_id,omitempty"`
}

DirectPayResponse - the response from mpower is serialiazed into this form

type Invoice

type Invoice struct {
	sync.RWMutex
	Setup       *Setup                 `json:"-"`
	Store       Store                  `json:"store"`
	InvoiceData invoice                `json:"invoice"`
	CustomData  map[string]interface{} `json:"custom_data,omitempty"`
	Actions     Actions                `json:"actions,omitempty"`
}

Invoice definition It specifies the required field keys and values we will be sending over to mpower This is supposed to be an embedded struct in the Onsite Invoice and Checkout Invoice

func (*Invoice) AddItem

func (i *Invoice) AddItem(name string, quantity int, unitPrice float32, totalPrice float32, desc string) error

AddItem add an `item - struct` to the items in the invoice

Example.

checkout := mpower.NewCheckoutInvoice(newSetup, newStore)
checkout.AddItem("Yam Phone", 1, 50.00, 50.00, "Hello World")

func (*Invoice) AddTax

func (i *Invoice) AddTax(name string, amount float32) error

AddTax add an `tax - struct` to the taxes in the invoice

Example.

checkout := mpower.NewCheckoutInvoice(newSetup, newStore)
checkout.AddTax("VAT", 30.00)

func (*Invoice) Clear

func (i *Invoice) Clear()

Clear clears all the items in the invoice

Example.

checkout.Clear()

func (*Invoice) ClearAllItems

func (i *Invoice) ClearAllItems()

ClearAllItems clears all the items in the invoice

Example.

checkout.ClearAllItems()

func (*Invoice) ClearAllTaxes

func (i *Invoice) ClearAllTaxes()

ClearAllTaxes clears all the taxes in the invoice

Example.

checkout.ClearAllTaxes()

func (*Invoice) PrepareForRequest

func (i *Invoice) PrepareForRequest()

PrepareForRequest prepares the invoice for request This is called before the request to mpower invoice is made to set the items and taxes into a json format

func (*Invoice) RemoveItem

func (i *Invoice) RemoveItem(name string)

RemoveItem removes the item with name of `name`

Example.

checkout.RemoveItem()

func (*Invoice) RemoveTax

func (i *Invoice) RemoveTax(name string)

RemoveTax removes the tax with name of `name`

Example.

checkout.RemoveTax()

func (*Invoice) SetCustomData

func (i *Invoice) SetCustomData(key string, val interface{})

SetCustomData the total amount on the invoice

Example.

checkout := mpower.NewCheckoutInvoice(newSetup, newStore)
checkout.SetCustomData("bonus", yeah)

func (*Invoice) SetDescription

func (i *Invoice) SetDescription(desc string)

SetDescription the description for the invoice

Example.

checkout := mpower.NewCheckoutInvoice(newSetup, newStore)
checkout.SetDescription("Hello World")

func (*Invoice) SetTotalAmount

func (i *Invoice) SetTotalAmount(amt float32)

SetTotalAmount the total amount on the invoice

Example.

checkout := mpower.NewCheckoutInvoice(newSetup, newStore)
checkout.SetTotalAmount(80.00)

func (*Invoice) SetURLS

func (i *Invoice) SetURLS(cancelURL, returnURL string)

SetURLS sets the cancel url of the invoice

type MPower

type MPower struct {
	Session *napping.Session
	// contains filtered or unexported fields
}

MPower holds the setup and store data It includes all instances of the MPower API

func NewMPower

func NewMPower(setup *Setup, store *Store, mode string) *MPower

NewMPower creates a new MPower

func (*MPower) NewRequest

func (mp *MPower) NewRequest(method, url string, payload, result interface{}, header *http.Header) (resp *napping.Response, err error)

NewRequest is the method provided for all the api services to used

type OnsiteInvoice

type OnsiteInvoice struct {
	Invoice
	// contains filtered or unexported fields
}

OnsiteInvoice allows you to create an onsite invoice as per mpower docs

func NewOnsiteInvoice

func NewOnsiteInvoice(mp *MPower) *OnsiteInvoice

NewOnsiteInvoice create a new onsite invoice object It require a setup and store object

Example.

onsite := mpower.NewOnsiteInvoice(newSetup, newStore)

func (*OnsiteInvoice) Charge

func (on *OnsiteInvoice) Charge(onsitePaymentRequestToken, customerConfirmToken string) (*OnsitePaymentRequestChargeResponse, *napping.Response, error)

Charge - it charges the customer on mpower and returns a response json object which contains the receipt url with other information The `confirmToken` is from the customer

func (*OnsiteInvoice) Create

Create - creates a new invoice on mpowers server

type OnsiteInvoiceRequest

type OnsiteInvoiceRequest struct {
	Invoice `json:"invoice_data"`
	OPRData OnsitePaymentRequestData `json:"opr_data"`
}

OnsiteInvoiceRequest This struct holds all the data with respect to onsite request

type OnsiteInvoiceResponse

type OnsiteInvoiceResponse struct {
	Response
	Token        string `json:"token"`
	InvoiceToken string `json:"invoice_token"`
}

OnsiteInvoiceResponse is the response you get back from creating an onsite invoice

type OnsitePaymentRequestCharge

type OnsitePaymentRequestCharge struct {
	Token        string `json:"token"`
	ConfirmToken string `json:"confirm_token"`
}

OnsitePaymentRequestCharge charges a customer

type OnsitePaymentRequestChargeResponse

type OnsitePaymentRequestChargeResponse struct {
	InvoiceData struct {
		ReceiptURL string `json:"receipt_url"`
		Status     string `json:"status"`
		Invoice    struct {
			TotalAmount float32 `json:"total_amount"`
			Description string  `json:"description"`
		}
		Customer struct {
			Name  string `json:"name"`
			Phone string `json:"phone"`
			Email string `json:"email"`
		} `json:"customer"`
	} `json:"invoice_data"`
}

OnsitePaymentRequestChargeResponse is the response from an onsite charge request

type OnsitePaymentRequestData

type OnsitePaymentRequestData struct {
	Alias string `json:"account_alias"`
}

type Response

type Response struct {
	ResponseText string `json:"response_text,omitempty"`
	ResponseCode string `json:"response_code,omitempty"`
	Description  string `json:"description,omitempty"`
}

Response - almost all mpower JSON responses contain all these fields This struct is embeded in other structs

type Setup

type Setup struct {
	MasterKey  string
	PrivateKey string
	PublicKey  string
	Token      string
	Headers    map[string]string
	BaseURL    string
}

Setup as defined by mpower docs with the exception of the BaseURL

func NewSetup

func NewSetup(masterKey, privateKey, publicKey, token string) *Setup

NewSetup - returns a new setup object

func NewSetupFromEnv

func NewSetupFromEnv() *Setup

NewSetupFromEnv creates a setup from your environment keys

type Store

type Store struct {
	Name          string `json:"name,omitempty"`
	Tagline       string `json:"tagline,omitempty"`
	PhoneNumber   string `json:"phone,omitempty"`
	PostalAddress string `json:"postal_address,omitempty"`
	LogoURL       string `json:"logo_url,omitempty"`
}

The Store holds the store information and ised to define the store data for mpower transaction

func NewStore

func NewStore(name string) *Store

NewStore - returns a new store object

Example.

mpowerStore := NewStore("Awesome Store")

Directories

Path Synopsis
Godeps
_workspace/src/github.com/jmcvetta/napping
Package napping is a client library for interacting with RESTful APIs.
Package napping is a client library for interacting with RESTful APIs.
_workspace/src/github.com/jmcvetta/napping/examples/github_auth_token command
Example demonstrating use of package napping, with HTTP Basic authentictation over HTTPS, to retrieve a Github auth token.
Example demonstrating use of package napping, with HTTP Basic authentictation over HTTPS, to retrieve a Github auth token.
_workspace/src/github.com/stretchr/testify/assert
Package assert provides a set of comprehensive testing tools for use with the normal Go testing system.
Package assert provides a set of comprehensive testing tools for use with the normal Go testing system.
_workspace/src/github.com/stretchr/testify/require
Alternative testing tools which stop test execution if test failed.
Alternative testing tools which stop test execution if test failed.
_workspace/src/github.com/stretchr/testify/suite
The suite package contains logic for creating testing suite structs and running the methods on those structs as tests.
The suite package contains logic for creating testing suite structs and running the methods on those structs as tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL