67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package monetix
|
|
|
|
import (
|
|
"context"
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/tech/sendico/pkg/merrors"
|
|
"github.com/tech/sendico/pkg/mlogger"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type Client struct {
|
|
cfg Config
|
|
client *http.Client
|
|
logger mlogger.Logger
|
|
}
|
|
|
|
func NewClient(cfg Config, httpClient *http.Client, logger mlogger.Logger) *Client {
|
|
client := httpClient
|
|
if client == nil {
|
|
client = &http.Client{Timeout: cfg.timeout()}
|
|
}
|
|
cl := logger
|
|
if cl == nil {
|
|
cl = zap.NewNop()
|
|
}
|
|
return &Client{
|
|
cfg: cfg,
|
|
client: client,
|
|
logger: cl.Named("monetix_client"),
|
|
}
|
|
}
|
|
|
|
func (c *Client) CreateCardPayout(ctx context.Context, req CardPayoutRequest) (*CardPayoutSendResult, error) {
|
|
return c.sendCardPayout(ctx, req)
|
|
}
|
|
|
|
func (c *Client) CreateCardTokenPayout(ctx context.Context, req CardTokenPayoutRequest) (*CardPayoutSendResult, error) {
|
|
return c.sendCardTokenPayout(ctx, req)
|
|
}
|
|
|
|
func (c *Client) CreateCardTokenization(ctx context.Context, req CardTokenizeRequest) (*TokenizationResult, error) {
|
|
return c.sendTokenization(ctx, req)
|
|
}
|
|
|
|
func signPayload(payload any, secret string) (string, error) {
|
|
data, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
h := hmac.New(sha256.New, []byte(secret))
|
|
if _, err := h.Write(data); err != nil {
|
|
return "", err
|
|
}
|
|
return hex.EncodeToString(h.Sum(nil)), nil
|
|
}
|
|
|
|
// SignPayload exposes signature calculation for callback verification.
|
|
func SignPayload(payload any, secret string) (string, error) {
|
|
return signPayload(payload, secret)
|
|
}
|