canopusgo

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2021 License: MIT Imports: 15 Imported by: 1

README

GitHub GitHub go.mod Go version GitHub code size in bytes GitHub all releases GitHub release (latest SemVer)

Overview

Canopusgo is implementation of canopus service using Golang.

Usage

Create service first:

key, err := ioutil.ReadFile("/your/path/to/M-00001.key")
if err != nil {
  fmt.Println(err)
}
pem, err := ioutil.ReadFile("/your/path/to/M-0001.pem")
if err != nil {
  fmt.Println(err)
}
client := &http.Client{Timeout: time.Second * time.Duration(60)}

cano := canopusgo.CreateService("snap", key, pem, "M-0001", "yoursecret", client)

Then you can user this service to create payment

func (cano *Canopus) GenerateSignature(payload []byte) (string, error)
func (cano *Canopus) GetToken() (string, error)
func (cano *Canopus) GetAvailableMethod(amount float64) ([]PaymentMethod, error)
func (cano *Canopus) GenerateCart(payload CartPayload, paymentMethod PaymentMethod) (CartResponse, error)

Check some examples canopusgo

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	BaseURL         = "https://canopus-auth.sumpahpalapa.com"
	DefaultDuration = time.Second * time.Duration(10)
	DefaultType     = "api"
	Err002          = errors.New("required parameter is missing")
	Err004          = errors.New("parameter has illegal value")
	Err007          = errors.New("signature is invalid")
	Err008          = errors.New("payement method is not available")
)

Functions

func VerifySignature

func VerifySignature(merchantPem []byte, response []byte, signature string) (bool, error)

Types

type Callback

type Callback struct {
	Request struct {
		Data   DataCallback `json:"data"`
		Result Result       `json:"result"`
	} `json:"request"`
	Signature string `json:"signature"`
}

Callback canopus body when payment settlement, expired and canopus notification

type Canopus

type Canopus struct {
	Type        string // snap | api
	MerchantKey []byte
	MerchantPem []byte
	MerchantID  string
	Secret      string
	Client      *http.Client
	Token       string
	Validator   *validator.Validate
}

func CreateService

func CreateService(init InitService) (*Canopus, error)

TODO: create validation in parameter for each function

func (*Canopus) GenerateCart

func (cano *Canopus) GenerateCart(payload CartPayload, paymentMethod PaymentMethod) (CartResponse, error)

func (*Canopus) GenerateSignature

func (cano *Canopus) GenerateSignature(payload []byte) (string, error)

GenerateSignature ...

func (*Canopus) GetAvailableMethod

func (cano *Canopus) GetAvailableMethod(amount float64) ([]PaymentMethod, error)

GetAvailableMethod Get all available method by merhantID

func (*Canopus) GetToken

func (cano *Canopus) GetToken() (string, error)

GetToken ...

type CartPayload

type CartPayload struct {
	CartDetails struct {
		ID      string `json:"id" validate:"required"`
		Payment struct {
			Key  string `json:"key"`
			Type string `json:"type"`
		} `json:"payment"`
		Amount    float64 `json:"amount" validate:"required"`
		Title     string  `json:"title" validate:"required"`
		Currency  string  `json:"currency" validate:"required"`
		ExpiredAt string  `json:"expiredAt" validate:"required"`
	} `json:"cartDetails" validate:"required"`
	ItemDetails     []CartPayloadItemDetail `json:"itemDetails" validate:"required"`
	CustomerDetails struct {
		FirstName      string `json:"firstName" validate:"required"`
		LastName       string `json:"lastName"`
		Email          string `json:"email" validate:"required"`
		Phone          string `json:"phone" validate:"required"`
		BillingAddress struct {
			FirstName  string `json:"firstName"`
			LastName   string `json:"lastName"`
			Phone      string `json:"phone"`
			Address    string `json:"address"`
			City       string `json:"city"`
			PostalCode string `json:"postalCode"`
		} `json:"billingAddress"`
		ShippingAddress struct {
			FirstName  string `json:"firstName"`
			LastName   string `json:"lastName"`
			Phone      string `json:"phone"`
			Address    string `json:"address"`
			City       string `json:"city"`
			PostalCode string `json:"postalCode"`
		} `json:"shippingAddress"`
	} `json:"customerDetails" validate:"required"`
	Environment struct {
		Agent   string `json:"agent"`
		Mode    string `json:"mode"`
		Os      string `json:"os"`
		Version string `json:"version"`
	} `json:"environment"`
	URL struct {
		ReturnURL       string `json:"returnURL" validate:"required"`
		CancelURL       string `json:"cancelURL" validate:"required"`
		NotificationURL string `json:"notificationURL" validate:"required"`
	} `json:"url" validate:"required"`
	ExtendInfo struct {
		AdditionalPrefix string `json:"additionalPrefix"`
	} `json:"extendInfo"`
}

CartPayload payload to create cart

type CartPayloadItemDetail

type CartPayloadItemDetail struct {
	Name           string  `json:"name" validate:"required"`
	Desc           string  `json:"desc"`
	Price          float64 `json:"price" validate:"required"`
	Quantity       int     `json:"quantity" validate:"required"`
	SKU            string  `json:"SKU" validate:"required"`
	AdditionalInfo struct {
		NoHandphone string `json:"No Handphone"`
	} `json:"additionalInfo"`
}

CartPayloadItemDetail item cart detail

type CartResponse

type CartResponse struct {
	CartID string `json:"cartID"`
	PayTo  string `json:"payto"`
	Amount string `json:"amount"`
	Bank   string `json:"bank"`
	Number string `json:"number"`
}

CartResponse response after generate cart

type CommonMessage

type CommonMessage struct {
	Response  Message `json:"response,omitempty"`
	Request   Message `json:"request,omitempty"`
	Signature string  `json:"signature"`
}

CommonMessage canopus request/response

func ValidateResponse

func ValidateResponse(resp []byte) (CommonMessage, error)

type DataCallback

type DataCallback struct {
	Amount          int    `json:"amount"`
	Bank            string `json:"bank"`
	MerchantID      string `json:"merchantID"`
	MerchantOrderID string `json:"merchantOrderId"`
	Number          string `json:"number"`
	OrderID         string `json:"orderID"`
	Status          string `json:"status"`
	Time            struct {
		Created time.Time `json:"created"`
		Updated time.Time `json:"updated"`
	} `json:"time"`
	TransactionID string `json:"transactionID"`
	Type          string `json:"type"`
}

DataCallback data callback from canopus

type InitService added in v1.0.2

type InitService struct {
	Type        string // snap | api
	MerchantKey []byte `validate:"required"`
	MerchantPem []byte `validate:"required"`
	MerchantID  string `validate:"required"`
	Secret      string `validate:"required"`
	TimeOut     time.Duration
}

InitService to create canoput service

type Message

type Message struct {
	Result Result                 `json:"result"`
	Data   map[string]interface{} `json:"data"`
}

Message canopus subparameter

type NotifData

type NotifData struct {
	Amount float64 `json:"amount"`
	Bank   string  `json:"bank"`
	Number string  `json:"number"`
}

NotifData extract data when notification callback

type PaymentMethod

type PaymentMethod struct {
	Key         string      `json:"key" validate:"required"`
	Name        string      `json:"name"`
	Type        string      `json:"type" validate:"required"`
	Instruction interface{} `json:"instruction"`
}

PaymentMethod payment method available

type RawMessage

type RawMessage struct {
	Response  json.RawMessage `json:"response,omitempty"`
	Request   json.RawMessage `json:"request,omitempty"`
	Signature string          `json:"signature"`
}

RawMessage partly get json for validate request/response and signature

type Result

type Result struct {
	Code    string `json:"code"`
	Message string `json:"message"`
	Status  string `json:"status"`
}

Result extract Canopus Response (json key result)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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