mpower

package module
v0.0.0-...-a48dbcc Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2015 License: MIT Imports: 8 Imported by: 0

README

MPOWERGO

Build Status

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/mpowergo

Documentation

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

newSetup := mpower.NewStore(map[string]string{
    "name":          "Awesome Store",
    "tagline":       "Easy shopping",
    "phoneNumber":   "0272271893",
    "postalAddress": "P.0. Box MP555, Accra",
    "logoURL":       "http://www.awesomestore.com.gh/logo.png",
})

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

newSetup := mpower.NewSetup(map[string]string{
    "masterKey":  YOUR MASTER KEY,
    "privateKey": YOUR PRIVATE KEY,
    "publicKey":  YOUR PUBLIC KEY,
    "token":      YOUR TOKEN,
    "mode":       MODE,
})
Checkout and Onsite Invoice

To use the checkout invoice, you need your store and setup info above

checkout := mpower.NewCheckoutInvoice(newSetup, newStore)
onsite := mpower.NewOnsiteInvoice(newSetup, newStore)

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)

To create an invoice on mpower, call the Create method on the checkout

This is for the checkout invoice

if ok, err := checkout.Create(); ok {
  //do something with the response info on the checkout instance
  fmt.Printf("%s %s %s %s\n\n", checkout.ResponseCode, checkout.ResponseText, checkout.Description, checkout.Token)
} else {
  //there was an error
}

For onsite invoice

if ok, err := checkout.Create("me"); ok {
  //do something with the response info on the checkout instance
  fmt.Printf("%s %s %s %s\n\n", checkout.ResponseCode, checkout.ResponseText, checkout.Description, checkout.Token)
} else {
  //there was an error
}

Get the invoice url of the recently created invoice with GetInvoiceUrl

str := checkout.GetInvoiceUrl()

For the onsite invoice, you have to charge the customer using the confirm token from Create and token from the user

if str, err := checkout.Confirm(token); err != nil {
    // handle error
} else if str == "completed" {
    // do something
}

Documentation

Index

Constants

View Source
const (
	BASE_URL_LIVE = "https://app.mpowerpayments.com/api/v1"
	BASE_URL_TEST = "https://app.mpowerpayments.com/sandbox-api/v1"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CheckoutInvoice

type CheckoutInvoice struct {
	Invoice

	ResponseCode string `json:"-"`
	ResponseText string `json:"-"`
	Description  string `json:"-"`
	Token        string `json:"-"`
	Status       string `json:"-"`
	// 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(setup *Setup, store *Store) *CheckoutInvoice

NewCheckoutInvoice - create a new checkout instance

Example.

checkout := mpower.NewCheckoutInvoice(newSetup, newStore)

func (*CheckoutInvoice) Confirm

func (c *CheckoutInvoice) Confirm(token string) (string, error)

func (*CheckoutInvoice) Create

func (c *CheckoutInvoice) Create() (bool, error)

Create - creates a new invoice on mpower Returns `boolean` and `error` The `boolean` is used to determine if an error was encountered while making the request

Example.

if ok, err := checkout.Create(); ok {
  //do something with the response info on the checkout instance
  fmt.Printf("%s %s %s %s\n\n", checkout.ResponseCode, checkout.ResponseText, checkout.Description, checkout.Token)
} else {
  //there was an error
}

func (*CheckoutInvoice) GetInvoiceUrl

func (c *CheckoutInvoice) GetInvoiceUrl() string

GetInvoiceUrl - get the invoice's url from the response

Example.

str := checkout.GetInvoiceUrl()

type DirectPay

type DirectPay struct {
	Setup         *Setup
	Status        string
	ResponseCode  string
	ResponseText  string
	Description   string
	TransactionId string
	// contains filtered or unexported fields
}

DirectPay - the direct pay object as defined by mpower

func NewDirectPay

func NewDirectPay(setup *Setup) *DirectPay

NewDirectPay - creates a DirectPay instance

func (*DirectPay) CreditAccount

func (d *DirectPay) CreditAccount(account string, amount int) (bool, error)

CreditAccount - credits the account of an mpower customer

Example.

if ok, err := directPayInStance.CreditAccount("me", 500); ok {
everything was ok
} else {
 There's trouble in hell
}

type Invoice

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

The 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

AddItem 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()

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{})

Sets 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)

Sets the description for the invoice

Example.

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

func (*Invoice) SetTotalAmount

func (i *Invoice) SetTotalAmount(amt float32)

Sets the total amount on the invoice

Example.

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

type OnsiteInvoice

type OnsiteInvoice struct {
	Invoice `json:"invoice_data"`
	OPRData struct {
		Alias string `json:"account_alias"`
	} `json:"opr_data"`

	ReceiptUrl   string `json:"-"`
	ResponseCode string `json:"-"`
	ResponseText string `json:"-"`
	Description  string `json:"-"`
	Token        string `json:"-"`
	InvoiceToken string `json:"-"`
	Status       string `json:"-"`
	Customer     struct {
		Name  string `json:"-"`
		Phone string `json:"-"`
		Email string `json:"-"`
	} `json:"-"`
	// contains filtered or unexported fields
}

The onsite definition as defined by mpower documentation This struct holds all the data with respect to onsite request

func NewOnsiteInvoice

func NewOnsiteInvoice(setup *Setup, store *Store) *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(oprToken, confirmToken string) (bool, 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 Returns a `boolean` and `error` The boolean signifies whether the customer was chargeed or not The response json object can be retrieved on the onsite invoice object

Example.

if ok, err := onsite.Charge(onsite.Token, "4346"); ok {
  //doSomething
} else {

}

func (*OnsiteInvoice) Create

func (on *OnsiteInvoice) Create(name string) (bool, error)

Create - creates a bew invoice on mpowers server Returns a `boolean` and `error` The boolean signifies whether the inovoice was created on not The response json object can be retrieved on the onsite invoice object created by the NewOnsiteInvoice

Example.

ok, err := onsite.Create("hello")
   if ok {
      fmt.Printf("%s %s %s %s", onsite.ResponseCode, onsite.ResponseText, onsite.Description, onsite.Token)
   } else {
       fmt.Printf("%v", err)
   }

type Setup

type Setup struct {
	MasterKey   string
	PrivateKey  string
	PublicKey   string
	Token       string
	ContentType string
	BASE_URL    string
}

The Setup as defined by mpower docs with the exception of the BASE_URL

func NewSetup

func NewSetup(setupInfo map[string]string) *Setup

NewSetup - returns a new setup object

Example.

newSetup := mpower.NewSetup(map[string]string{
    "masterKey":  YOUR MASTER KEY,
    "privateKey": YOUR PRIVATE KEY,
    "publicKey":  "YOUR PUBLIC KEY,
    "token":      YOUR TOKEN,
    "mode":       MODE,
})

func (*Setup) Get

func (setup *Setup) Get(fieldName string) string

Get - gets a value from the struct by using its field name

Example.

key := newSetup.Get("MasterKey")

func (*Setup) GetHeaders

func (setup *Setup) GetHeaders() map[string]string

GetHeaders - gets the respective headers to set on a request for an mpower transaction

type Store

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

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

func NewStore

func NewStore(storeInfo interface{}) (error, *Store)

NewStore - returns a new store object

Example.

_, newStore := mpower.NewStore(map[string]string{
    "name":          "Awesome Store",
    "tagline":       "Easy shopping",
    "phoneNumber":   "0272271893",
    "postalAddress": "P.0. Box MP555, Accra",
    "logoURL":       "http://www.awesomestore.com.gh/logo.png",
})

func (*Store) Get

func (store *Store) Get(fieldName string) string

Get - gets a value from the struct by using its field name

Example.

key := newStore.Get("Name")

Directories

Path Synopsis
Godeps
_workspace/src/github.com/parnurzeal/gorequest
Package gorequest inspired by Nodejs SuperAgent provides easy-way to write http client
Package gorequest inspired by Nodejs SuperAgent provides easy-way to write http client
_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.
_workspace/src/golang.org/x/net/publicsuffix
Package publicsuffix provides a public suffix list based on data from http://publicsuffix.org/.
Package publicsuffix provides a public suffix list based on data from http://publicsuffix.org/.

Jump to

Keyboard shortcuts

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