Skip to content
liqpayPublic

About

Liqpay SDK for golang

Resources

Stars

7 stars

Watchers

3 watching

Forks

Latest commit

 

History

18 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sdk-go

Go Reference Go Version

Liqpay SDK for Go — a thin, idiomatic client for the Liqpay payment API.

Features

  • Simple request/response API over the Liqpay HTTP endpoint
  • Automatic request signing (base64(sha1(privateKey + data + privateKey)))
  • Typed API errors via *APIError (works with errors.As)
  • context.Context support for cancellation and deadlines
  • Configurable base URL for sandbox and testing environments
  • Checkout form rendering

Requirements

  • Go 1.18 or newer

Installation

go get github.com/liqpay/sdk-go

Usage

Sending a request

Example: retrieve received payments.

package main

import (
	"context"
	"fmt"

	liqpay "github.com/liqpay/sdk-go"
)

func main() {
	c := liqpay.New("your_pub_key", "your_priv_key", nil)

	r := liqpay.Request{
		"action":    "reports",
		"version":   3,
		"date_from": 1443161386000,
		"date_to":   1443164386000,
	}

	resp, err := c.SendContext(context.Background(), r)
	if err != nil {
		fmt.Printf("error: %v\n", err)
		return
	}
	fmt.Printf("response: %#v\n", resp)
}

SendContext targets the standard request endpoint. The older Send(apiURL, req) method is deprecated and kept only for backward compatibility.

Handling API errors

When the Liqpay API reports a failed operation (status/result of error or failure), SendContext returns an *APIError. Use errors.As to inspect it:

resp, err := c.SendContext(context.Background(), r)
if err != nil {
	var apiErr *liqpay.APIError
	if errors.As(err, &apiErr) {
		fmt.Printf("liqpay rejected the request: code=%s description=%s\n",
			apiErr.Code, apiErr.Description)
		return
	}
	// transport/encoding/decoding error
	fmt.Printf("error: %v\n", err)
	return
}

Using a context

SendContext lets you pass a context.Context for cancellation and deadlines:

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

resp, err := c.SendContext(ctx, r)

Options

New accepts optional configuration via functional options:

// Point the client at a different base URL (e.g. for testing or sandboxes).
c := liqpay.New("pub", "priv", nil, liqpay.WithBaseURL("https://sandbox.example/api"))

// Provide a custom *http.Client via the third argument of New.
c = liqpay.New("pub", "priv", &http.Client{Timeout: 10 * time.Second})

The base URL defaults to liqpay.DefaultBaseURL.

Rendering a checkout form

html, err := c.RenderForm(liqpay.Request{
	"action":   "pay",
	"version":  3,
	"amount":   1,
	"currency": "UAH",
})

Documentation

About

Liqpay SDK for golang

Resources

Stars

7 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages