92 lines
2.4 KiB
Go
92 lines
2.4 KiB
Go
package kyc
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
const (
|
|
StateCreated = "CREATED"
|
|
StateInitiated = "INITIATED"
|
|
StateFailed = "FAILED"
|
|
StateAborted = "ABORTED"
|
|
StateCompleted = "COMPLETED"
|
|
StateRejected = "REJECTED"
|
|
StateApproved = "APPROVED"
|
|
)
|
|
|
|
func doPassportRequest(ctx context.Context, method, path string, body any, target any) error {
|
|
publicKey := viper.GetString("kyc.passport_reader_public_key")
|
|
secret := viper.GetString("kyc.passport_reader_secret")
|
|
baseURL := "https://passportreader.app/api/v1"
|
|
|
|
var bodyReader io.Reader
|
|
if body != nil {
|
|
jsonData, err := json.Marshal(body)
|
|
if err != nil {
|
|
return fmt.Errorf("marshal request failed: %w", err)
|
|
}
|
|
bodyReader = bytes.NewBuffer(jsonData)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, method, baseURL+path, bodyReader)
|
|
if err != nil {
|
|
return fmt.Errorf("create request failed: %w", err)
|
|
}
|
|
|
|
req.SetBasicAuth(publicKey, secret)
|
|
if body != nil {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
|
|
client := &http.Client{Timeout: 15 * time.Second}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("http request failed: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
respBody, _ := io.ReadAll(resp.Body)
|
|
return fmt.Errorf("api error: status %d, body %s", resp.StatusCode, string(respBody))
|
|
}
|
|
|
|
if target != nil {
|
|
if err := json.NewDecoder(resp.Body).Decode(target); err != nil {
|
|
return fmt.Errorf("decode response failed: %w", err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func CreateSession(ctx context.Context) (*PassportReaderSessionResponse, error) {
|
|
var resp PassportReaderSessionResponse
|
|
err := doPassportRequest(ctx, "POST", "/session.create", nil, &resp)
|
|
return &resp, err
|
|
}
|
|
|
|
func GetSessionState(ctx context.Context, sessionID int) (string, error) {
|
|
payload := PassportReaderGetSessionRequest{ID: sessionID}
|
|
var resp PassportReaderStateResponse
|
|
err := doPassportRequest(ctx, "POST", "/session.state", payload, &resp)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return resp.State, nil
|
|
}
|
|
|
|
func GetSessionDetails(ctx context.Context, sessionID int) (*PassportReaderSessionDetailResponse, error) {
|
|
payload := PassportReaderGetSessionRequest{ID: sessionID}
|
|
var resp PassportReaderSessionDetailResponse
|
|
err := doPassportRequest(ctx, "POST", "/session.get", payload, &resp)
|
|
return &resp, err
|
|
}
|