sberbank_acquiring_go

package module
v0.0.0-...-b5784f9 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 13 Imported by: 0

README

sberbank-acquiring-go

API для работы с эквайринг Сбербанка

Documentation

Index

Constants

View Source
const (
	APIURI        string = "https://securepayments.sberbank.ru"
	APISandboxURI string = "https://3dsec.sberbank.ru"
)

URLS for API endpoints

Variables

View Source
var NewRequest = func(c *Client, ctx context.Context, method, urlPath string, data interface{}) (*http.Request, error) {
	if strings.Contains(urlPath, "rest") {
		return nil, fmt.Errorf("path contains rest request, use NewRestRequest instead")
	}

	if err := c.Config.validate(); err != nil {
		return nil, err
	}

	uri := APIURI + urlPath

	if c.Config.SandboxMode {
		uri = APISandboxURI + urlPath
	}

	if c.Config.endpoint != "" {
		uri = c.Config.endpoint + urlPath
	}

	reqBodyData, _ := json.Marshal(data)

	req, err := http.NewRequest(method, uri, bytes.NewReader(reqBodyData))

	if err != nil {
		return nil, err
	}

	req.Header.Set("Cache-Control", "no-cache")
	req.Header.Set("Content-Type", "application/json")

	req = req.WithContext(ctx)

	return req, nil
}
View Source
var NewRestRequest = func(c *Client, ctx context.Context, method, urlPath string, data map[string]string, jsonParams map[string]string) (*http.Request, error) {
	uri := APIURI + urlPath

	if c.Config.SandboxMode {
		uri = APISandboxURI + urlPath
	}

	if c.Config.endpoint != "" {
		uri = c.Config.endpoint + urlPath
	}

	jsonParamsEncoded, _ := json.Marshal(jsonParams)

	body := url.Values{}
	body.Add("userName", c.Config.UserName)
	body.Add("password", c.Config.Password)
	body.Add("currency", strconv.Itoa(c.Config.Currency))
	if string(jsonParamsEncoded[:]) != "null" {
		body.Add("jsonParams", string(jsonParamsEncoded[:]))
		body.Add("sessionTimeoutSecs", strconv.Itoa(c.Config.SessionTimeoutSecs))
	}

	for key, value := range data {
		if value != "" {
			body.Add(key, value)
		}
	}
	reqData := body.Encode()

	if method == "GET" {
		fmt.Println(reqData)
		uri += "?" + reqData

		reqData = ""
	}

	req, err := http.NewRequest(method, uri, strings.NewReader(reqData))

	if err != nil {
		return nil, err
	}

	req.Header.Set("Cache-Control", "no-cache")
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	req = req.WithContext(ctx)
	return req, nil
}

Functions

func SetConfig

func SetConfig(config ClientConfig)

func WithEndpoint

func WithEndpoint(endpoint string)

WithEndpoint configures a Client to use the specified API endpoint.

func WithToken

func WithToken(token string)

WithToken configures a Client to use the specified token for authentication.

Types

type API

type API interface {
	NewRestRequest(ctx context.Context, method, urlPath string, data map[string]string, jsonParams map[string]string) (*http.Request, error)
	NewRequest(ctx context.Context, method, urlPath string, data interface{}) (*http.Request, error)
	Do(r *http.Request, v interface{}) (*http.Response, error)
}

func GetAPI

func GetAPI(options ...ClientOption) API

type APIs

type APIs struct {
	Api API
	// contains filtered or unexported fields
}

APIs are the currently supported endpoints.

type Body

type Body struct {
	UserName           *string `json:"userName"`
	Token              *string `json:"token"`
	Password           *string `json:"password"`
	Language           *string `json:"language"`
	Currency           *string `json:"currency"`
	SessionTimeoutSecs *int    `json:"sessionTimeoutSecs"`
	JsonParams         []byte  `json:"jsonParams"`
}

Body struct

type Client

type Client struct {
	Config *ClientConfig
	// contains filtered or unexported fields
}

Client is a client to SberBank API

func (Client) Do

func (c Client) Do(r *http.Request, v interface{}) (*http.Response, error)

Do perform an HTTP request against the API.

func (*Client) NewRequest

func (c *Client) NewRequest(ctx context.Context, method, urlPath string, data interface{}) (*http.Request, error)

NewRequest creates an HTTP request against the API (mobile payments). The returned request is assigned with ctx and has all necessary headers set (auth, user agent, etc.). NewRestRequest creates an HTTP request against the API with 'rest' in path. The returned request is assigned with ctx and has all necessary headers set (auth, user agent, etc.).

func (*Client) NewRestRequest

func (c *Client) NewRestRequest(ctx context.Context, method, urlPath string, data map[string]string, jsonParams map[string]string) (*http.Request, error)

NewRestRequest creates an HTTP request against the API with 'rest' in path. The returned request is assigned with ctx and has all necessary headers set (auth, user agent, etc.).

type ClientConfig

type ClientConfig struct {
	UserName           string
	Password           string
	Currency           int
	Language           string
	SessionTimeoutSecs int

	SandboxMode bool
	// contains filtered or unexported fields
}

ClientConfig is used to set client configuration

type ClientOption

type ClientOption func(*Client)

ClientOption is used to configure a Client.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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