/mpowergo

A simple client library interfacing with the MPower HTTP API

Primary LanguageGoMIT LicenseMIT

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
}