canopusgo

package module
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2021 License: MIT Imports: 9 Imported by: 1

README

Canopus-GO

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

Overview

Canopus-GO is implementation of canopus service using Golang.

Repository overview

Provide an overview of the directory structure and files, for example:

├── README.md
├── LICENSE
├── client.go
├── endpoint.go
├── request.go
├── response.go
├── go.mod
├── go.sum
├── config
│   ├── mocks
│   │   └── HTTPClient.go
│   ├── canopus.go
│   └── http.go
├── helper
│   └── signature.go
├── mocks
│   └── ClientMethod.go
└── example
    ├── create_cart.go
    └── get_available_method.go

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)
}
canopusClient := canopus.NewAPICLient(&canopus.ConfigOptions{
		MerchantKey: key,
		MerchantPem: pem,
		MerchantID:  "M-0001",
		Secret:      "yoursupersecret",
		Timeout:	 10
	})

Then you can user this service to create payment

GetAvailableMethod(amount float64) ([]PaymentMethod, error)
GenerateCart(payload CartPayload, paymentMethod PaymentMethod) (CartResponse, error)

Check some examples canopusgo

Additional Config
// canopus.go
Environment        = Staging
DefaultAPIType     = SNAP
DefaultHttpTimeout = time.Second * time.Duration(10)
DefaultLogLevel    = 2
DefaultLogFormat   = "{\"log_type\":\"%s\",\"timestamp\":\"%s\",\"event\":\"%s\",\"detail\":\"%s\"}"

//example to change the default value
config.Environment = config.Production

Documentation

Index

Constants

View Source
const (
	PostTokenEndpoint    string = "%v/api/v1/merchants/%v/token"
	GetPaymentMethod     string = "%v/api/v1/merchants/%v/method"
	PostSnapCartGenerate string = "%v/api/v1/merchants/%v/snap/cart"
	PostAPICartGenerate  string = "%v/api/v1/merchants/%v/cart"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

type Callback struct {
	Request struct {
		Data 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"`
		} `json:"data"`
		Result Result `json:"result"`
	} `json:"request"`
	Signature string `json:"signature"`
}

Callback canopus body when payment settlement, expired and canopus notification

type CartPayload

type CartPayload struct {
	CartDetails struct {
		ID      string `json:"id" validate:"required"`
		Payment struct {
			Key  string `json:"key,omitempty"`
			Type string `json:"type,omitempty"`
		} `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,omitempty"`
		Email          string `json:"email" validate:"required"`
		Phone          string `json:"phone" validate:"required"`
		BillingAddress struct {
			FirstName  string `json:"firstName,omitempty"`
			LastName   string `json:"lastName,omitempty"`
			Phone      string `json:"phone,omitempty"`
			Address    string `json:"address,omitempty"`
			City       string `json:"city,omitempty"`
			PostalCode string `json:"postalCode,omitempty"`
		} `json:"billingAddress,omitempty"`
		ShippingAddress struct {
			FirstName  string `json:"firstName,omitempty"`
			LastName   string `json:"lastName,omitempty"`
			Phone      string `json:"phone,omitempty"`
			Address    string `json:"address,omitempty"`
			City       string `json:"city,omitempty"`
			PostalCode string `json:"postalCode,omitempty"`
		} `json:"shippingAddress,omitempty"`
	} `json:"customerDetails" validate:"required"`
	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,omitempty"`
	} `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 Client added in v1.1.0

type Client struct {
	Canopus    *ConfigOptions
	HTTPClient config.HTTPClient
	Token      string
	Validator  *validator.Validate
	// contains filtered or unexported fields
}

func (*Client) GenerateCart added in v1.1.0

func (c *Client) GenerateCart(payload CartPayload, paymentMethod PaymentMethod) (CartResponse, error)

func (*Client) GetAvailableMethod added in v1.1.0

func (c *Client) GetAvailableMethod(amount float64) ([]PaymentMethod, error)

GetAvailableMethod Get all available method by merhantID

type ClientMethod added in v1.1.0

type ClientMethod interface {
	GetAvailableMethod(amount float64) ([]PaymentMethod, error)
	GenerateCart(payload CartPayload, paymentMethod PaymentMethod) (CartResponse, error)
}

func NewAPICLient added in v1.1.0

func NewAPICLient(cano *ConfigOptions) ClientMethod

type CommonMessage

type CommonMessage struct {
	Response struct {
		Result Result                 `json:"result"`
		Data   map[string]interface{} `json:"data"`
	} `json:"response,omitempty"`
	Request struct {
		Result Result                 `json:"result"`
		Data   map[string]interface{} `json:"data"`
	} `json:"request,omitempty"`
	Signature string `json:"signature"`
}

CommonMessage canopus request/response

func ValidateResponse

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

type ConfigOptions added in v1.1.0

type ConfigOptions struct {
	MerchantKey []byte `validate:"required"`
	MerchantPem []byte `validate:"required"`
	MerchantID  string `validate:"required"`
	Secret      string `validate:"required"`
	Timeout     int
}

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