checkout

package module
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2022 License: MIT Imports: 14 Imported by: 0

README ¶

Checkout.com Go

Status

The official Checkout Go client library.

🚀 Installation

Make sure your project is using Go Modules (it will have a go.mod file in its root if it already is):

go mod init

Then, reference checkout-sdk-go in a Go program with import:

import (
    "github.com/bos-hieu/checkout-sdk-go"
)

Run any of the normal go commands (build/install/test). The Go toolchain will resolve and fetch the checkout-sdk-go module automatically.

📖 Documentation

You can see the SDK documentation here.

For details on all the functionality in this library, see the GoDoc documentation.

Below are a few simple examples:

API

If you're dealing with multiple keys, it is recommended you use client.API. This allows you to create as many clients as needed, each with their own individual key.

import (
    "github.com/bos-hieu/checkout-sdk-go"
    "github.com/bos-hieu/checkout-sdk-go/client"
)

api := &client.API{}
api.Init(secretKey, &publicKey)
Tokens
import (
    "github.com/bos-hieu/checkout-sdk-go"
    "github.com/bos-hieu/checkout-sdk-go/tokens"
)

config, err := checkout.Create(secretKey, &publicKey)
if err != nil {
    return
}
var client = tokens.NewClient(*config)
var card = &tokens.Card{
    Type:        common.Card,
    Number:      "4242424242424242",
    ExpiryMonth: 2,
    ExpiryYear:  2022,
    Name:        "Customer Name",
    CVV:         "100",
}
var request = &tokens.Request{
    Card: card,
}
response, err := client.Request(request)
Payments
import (
    "github.com/bos-hieu/checkout-sdk-go"
    "github.com/bos-hieu/checkout-sdk-go/payments"
)

idempotencyKey := checkout.NewIdempotencyKey()
params := checkout.Params{
    IdempotencyKey: &idempotencyKey,
}
config, err := checkout.Create(secretKey, &publicKey)

if err != nil {
    return
}
var client = payments.NewClient(*config)
var source = payments.TokenSource{
    Type:  common.Token.String(),
    Token: "tok_",
}
var request = &payments.Request{
    Source:   source,
    Amount:   "100",
    Currency: "USD",
    Reference: "Payment Reference",
    Customer: &payments.Customer{
        Email: "example@email.com",
        Name:  "First Name Last Name",
    },
    Metadata: map[string]string{
        "udf1": "User Define",
    },
}
response, err := client.Request(request, &params)
Payment Detail
import (
    "github.com/bos-hieu/checkout-sdk-go"
    "github.com/bos-hieu/checkout-sdk-go/payments"
)

config, err := checkout.Create(secretKey, &publicKey)

if err != nil {
    return
}
var client = payments.NewClient(*config)
response, err := client.Get("pay_")
Actions
import (
    "github.com/bos-hieu/checkout-sdk-go"
    "github.com/bos-hieu/checkout-sdk-go/payments"
)

config, err := checkout.Create(secretKey, &publicKey)
if err != nil {
    return
}
var client = payments.NewClient(*config)
response, err := client.Actions("pay_")
Captures
import (
    "github.com/bos-hieu/checkout-sdk-go"
    "github.com/bos-hieu/checkout-sdk-go/payments"
)

idempotencyKey := checkout.NewIdempotencyKey()
params := checkout.Params{
    IdempotencyKey: &idempotencyKey,
}
config, err := checkout.Create(secretKey, &publicKey)
if err != nil {
    return
}
var client = payments.NewClient(*config)
request := &payments.CapturesRequest{
    Amount:    100,
    Reference: "Reference",
    Metadata: map[string]string{
        "udf1": "User Define",
    },
}
response, err := client.Captures("pay_", request, &params)
Voids
import (
    "github.com/bos-hieu/checkout-sdk-go"
    "github.com/bos-hieu/checkout-sdk-go/payments"
)

idempotencyKey := checkout.NewIdempotencyKey()
params := checkout.Params{
    IdempotencyKey: &idempotencyKey,
}
config, err := checkout.Create(secretKey, &publicKey)
if err != nil {
    return
}
var client = payments.NewClient(*config)
request := &payments.VoidsRequest{
    Reference: "Reference",
    Metadata: map[string]string{
        "udf1": "User Define",
    },
}
response, err := client.Voids("pay_", request, &params)
Refunds
import (
    "github.com/bos-hieu/checkout-sdk-go"
    "github.com/bos-hieu/checkout-sdk-go/payments"
)

idempotencyKey := checkout.NewIdempotencyKey()
params := checkout.Params{
    IdempotencyKey: &idempotencyKey,
}
config, err := checkout.Create(secretKey, &publicKey)
if err != nil {
    return
}
var client = payments.NewClient(*config)
request := &payments.RefundsRequest{
    Amount:    100,
    Reference: "Reference",
    Metadata: map[string]string{
        "udf1": "User Define",
    },
}
response, err := client.Refunds("pay_", request, &params)

For any requests, bug or comments, please open an issue or submit a pull request.

Documentation ¶

Index ¶

Constants ¶

View Source
const (
	// Sandbox - Sandbox
	Sandbox SupportedEnvironment = "sandbox.checkout.com"
	// Production - Production
	Production SupportedEnvironment = "checkout.com"
	// UnknownPlatform - Production
	UnknownPlatform string = "unknown platform"
)
View Source
const (
	// CKORequestID ...
	CKORequestID = "cko-request-id"
	// CKOVersion ...
	CKOVersion = "cko-version"
)
View Source
const ClientVersion = "0.0.1"

ClientVersion ...

View Source
const (
	// DefaultMaxNetworkRetries is the default maximum number of retries made
	// by a Checkout.com client.
	DefaultMaxNetworkRetries int64 = 2
)

Variables ¶

View Source
var DefaultConfig = Config{
	URI: String(sandboxURI),
}

DefaultConfig ...

Functions ¶

func Bool ¶

func Bool(v bool) *bool

Bool returns a pointer to the bool value passed in.

func BoolSlice ¶

func BoolSlice(v []bool) []*bool

BoolSlice returns a slice of bool pointers given a slice of bools.

func BoolValue ¶

func BoolValue(v *bool) bool

BoolValue returns the value of the bool pointer passed in or false if the pointer is nil.

func Float64 ¶

func Float64(v float64) *float64

Float64 returns a pointer to the float64 value passed in.

func Float64Slice ¶

func Float64Slice(v []float64) []*float64

Float64Slice returns a slice of float64 pointers given a slice of float64s.

func Float64Value ¶

func Float64Value(v *float64) float64

Float64Value returns the value of the float64 pointer passed in or 0 if the pointer is nil.

func Int64 ¶

func Int64(v int64) *int64

Int64 returns a pointer to the int64 value passed in.

func Int64Value ¶

func Int64Value(v *int64) int64

Int64Value returns the value of the int64 pointer passed in or 0 if the pointer is nil.

func NewIdempotencyKey ¶

func NewIdempotencyKey() string

NewIdempotencyKey -

func SetAppInfo ¶

func SetAppInfo(info *AppInfo)

SetAppInfo sets app information. See AppInfo.

func String ¶

func String(v string) *string

String returns a pointer to the string value passed in.

func StringSlice ¶

func StringSlice(v []string) []*string

StringSlice returns a slice of string pointers given a slice of strings.

func StringValue ¶

func StringValue(v *string) string

StringValue returns the value of the string pointer passed in or "" if the pointer is nil.

Types ¶

type AppInfo ¶

type AppInfo struct {
	Name    string `json:"name"`
	URL     string `json:"url"`
	Version string `json:"version"`
}

AppInfo ...

type Config ¶

type Config struct {
	PublicKey         string
	SecretKey         string
	URI               *string
	HTTPClient        *http.Client
	LeveledLogger     LeveledLoggerInterface
	MaxNetworkRetries *int64
}

Config ...

func Create ¶

func Create(secretKey string, publicKey *string) (*Config, error)

Create ...

type HTTPClient ¶

type HTTPClient interface {
	Get(path string) (*StatusResponse, error)
	Post(path string, request interface{}, params *Params) (*StatusResponse, error)
	Put(path string, request interface{}) (*StatusResponse, error)
	Patch(path string, request interface{}) (*StatusResponse, error)
	Delete(path string) (*StatusResponse, error)
	Upload(path, boundary string, body *bytes.Buffer) (*StatusResponse, error)
	Download(path string) (*StatusResponse, error)
}

HTTPClient ...

type Headers ¶

type Headers struct {
	Header       http.Header
	CKORequestID *string `json:"cko-request-id,omitempty"`
	CKOVersion   *string `json:"cko-version,omitempty"`
}

Headers ...

type Level ¶

type Level uint32

Level represents a logging level.

const (
	// LevelNull sets a logger to show no messages at all
	LevelNull Level = 0
	// LevelError sets a logger to show error messages only
	LevelError Level = 1
	// LevelWarn sets a logger to show warning messages or anything more severe.
	LevelWarn Level = 2
	// LevelInfo sets a logger to show informational messages or anything more severe.
	LevelInfo Level = 3
	// LevelDebug sets a logger to show information messages or anything more severe.
	LevelDebug Level = 4
)

type LeveledLogger ¶

type LeveledLogger struct {
	// Level is the minimum logging level that will be emitted by this logger.
	//
	// For example, a Level set to LevelWarn will emit warnings and errors, but not information or debug messages.
	//
	// Always set this with a constant like LevelWarn because the individual values are not guaranteed to be stable.
	Level Level
	// contains filtered or unexported fields
}

LeveledLogger is a leveled logger implementation.

It prints warnings and errors to `os.Stderr` and other messages to `os.Stdout`.

func (*LeveledLogger) Debugf ¶

func (l *LeveledLogger) Debugf(format string, v ...interface{})

Debugf - logs a debug message using Printf conventions.

func (*LeveledLogger) Errorf ¶

func (l *LeveledLogger) Errorf(format string, v ...interface{})

Errorf - logs a warning message using Printf conventions.

func (*LeveledLogger) Infof ¶

func (l *LeveledLogger) Infof(format string, v ...interface{})

Infof - logs an informational message using Printf conventions.

func (*LeveledLogger) Warnf ¶

func (l *LeveledLogger) Warnf(format string, v ...interface{})

Warnf - logs a warning message using Printf conventions.

type LeveledLoggerInterface ¶

type LeveledLoggerInterface interface {
	// Debugf logs a debug message using Printf conventions.
	Debugf(format string, v ...interface{})
	// Errorf logs a warning message using Printf conventions.
	Errorf(format string, v ...interface{})
	// Infof logs an informational message using Printf conventions.
	Infof(format string, v ...interface{})
	// Warnf logs a warning message using Printf conventions.
	Warnf(format string, v ...interface{})
}

LeveledLoggerInterface provides a basic leveled logging interface for printing debug, informational, warning, and error messages.

It's implemented by LeveledLogger and also provides out-of-the-box compatibility with a Logrus Logger, but may require a thin shim for use with other logging libraries that you use less standard conventions like Zap.

var DefaultLeveledLogger LeveledLoggerInterface = &LeveledLogger{
	Level: LevelError,
}

DefaultLeveledLogger -

type Params ¶

type Params struct {
	// Headers may be used to provide extra header lines on the HTTP request.
	Headers        http.Header `form:"-"`
	IdempotencyKey *string     `form:"-"` // Passed as header
}

Params is the structure that contains the common properties of any *Params structure.

func (*Params) GetParams ¶

func (p *Params) GetParams() *Params

GetParams -

func (*Params) SetIdempotencyKey ¶

func (p *Params) SetIdempotencyKey(val string)

SetIdempotencyKey sets a value for the Idempotency-Key header.

type ParamsContainer ¶

type ParamsContainer interface {
	GetParams() *Params
}

ParamsContainer is a general interface for which all parameter structs should comply. They achieve this by embedding a Params struct and inheriting its implementation of this interface.

type StatusResponse ¶

type StatusResponse struct {
	Status       string     `json:"status,omitempty"`
	StatusCode   int        `json:"status_code,omitempty"`
	ResponseBody []byte     `json:"response_body,omitempty"`
	ResponseCSV  [][]string `json:"response_csv,omitempty"`
	Headers      *Headers   `json:"headers,omitempty"`
}

StatusResponse ...

type SupportedAPI ¶

type SupportedAPI string

SupportedAPI is an enumeration of supported Checkout.com endpoints. Currently supported values are "Unified Payment Gateway".

const (
	// UPAPI - Unified Payment API
	UPAPI SupportedAPI = "api"
	// Access - OAuth Authorization
	Access SupportedAPI = "access"
)

type SupportedEnvironment ¶

type SupportedEnvironment string

SupportedEnvironment is an enumeration of supported Checkout.com environment. Currently supported values are "Sandbox" & "Production".

Jump to

Keyboard shortcuts

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