117 lines
4.1 KiB
Go
117 lines
4.1 KiB
Go
package paymentapi
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// Execute sends exactly one request. Missing, malformed or interrupted results are
|
|
// uncertain; this client never retries a financial request.
|
|
func Execute(ctx context.Context, client *http.Client, baseURL, operation string, request Request, onStatus func(string)) (Result, error) {
|
|
var path string
|
|
switch operation {
|
|
case Sale:
|
|
path = SalePath
|
|
if request.Amount <= 0 {
|
|
return Result{}, errors.New("sale amount must be positive")
|
|
}
|
|
case Preauth:
|
|
path = PreauthPath
|
|
if request.Amount < 0 {
|
|
return Result{}, errors.New("preauth amount must not be negative")
|
|
}
|
|
default:
|
|
return Result{}, errors.New("invalid terminal operation")
|
|
}
|
|
if client == nil || strings.TrimSpace(baseURL) == "" {
|
|
return Result{}, errors.New("terminal client is not configured")
|
|
}
|
|
body, err := json.Marshal(request)
|
|
if err != nil {
|
|
return Result{}, fmt.Errorf("encode terminal request: %w", err)
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(baseURL, `/\`)+path, bytes.NewReader(body))
|
|
if err != nil {
|
|
return Result{}, errors.New("invalid terminal endpoint")
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Accept", "application/x-ndjson")
|
|
response, err := client.Do(req)
|
|
if err != nil {
|
|
if ctx.Err() != nil {
|
|
return Result{}, fmt.Errorf("terminal request interrupted: %w", ctx.Err())
|
|
}
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
|
return Result{}, fmt.Errorf("terminal request interrupted: %w", context.DeadlineExceeded)
|
|
}
|
|
if errors.Is(err, context.Canceled) {
|
|
return Result{}, fmt.Errorf("terminal request interrupted: %w", context.Canceled)
|
|
}
|
|
// Do not expose a transport error that may contain a URL or raw payload.
|
|
return Result{}, errors.New("terminal request failed; outcome is uncertain")
|
|
}
|
|
defer response.Body.Close()
|
|
if !strings.HasPrefix(response.Header.Get("Content-Type"), "application/x-ndjson") {
|
|
return Result{}, fmt.Errorf("terminal returned an invalid response contract (HTTP %d)", response.StatusCode)
|
|
}
|
|
decoder := json.NewDecoder(response.Body)
|
|
for {
|
|
var frame struct {
|
|
Frame
|
|
Legacy json.RawMessage `json:"response"`
|
|
}
|
|
if err := decoder.Decode(&frame); err != nil {
|
|
if ctx.Err() != nil {
|
|
return Result{}, fmt.Errorf("terminal stream interrupted: %w", ctx.Err())
|
|
}
|
|
if errors.Is(err, io.EOF) {
|
|
return Result{}, errors.New("terminal stream ended before a final result")
|
|
}
|
|
return Result{}, errors.New("terminal stream is invalid; outcome is uncertain")
|
|
}
|
|
if len(frame.Legacy) != 0 {
|
|
return Result{}, errors.New("terminal returned a legacy result contract")
|
|
}
|
|
switch frame.Type {
|
|
case "status":
|
|
if frame.Code == "" || frame.Result != nil {
|
|
return Result{}, errors.New("invalid terminal status frame")
|
|
}
|
|
if onStatus != nil {
|
|
onStatus(frame.Code)
|
|
}
|
|
case "result":
|
|
if frame.Result == nil || frame.Code != "" {
|
|
return Result{}, errors.New("invalid terminal result frame")
|
|
}
|
|
result := *frame.Result
|
|
switch result.Outcome {
|
|
case "approved", "declined", "cancelled", "timeout", "error", "unknown":
|
|
default:
|
|
return Result{}, errors.New("invalid terminal outcome")
|
|
}
|
|
if result.HTTPStatus < 100 || result.HTTPStatus > 599 {
|
|
return Result{}, errors.New("invalid terminal operation status")
|
|
}
|
|
if result.Outcome == "approved" && (result.HTTPStatus < 200 || result.HTTPStatus >= 300 || response.StatusCode < 200 || response.StatusCode >= 300 || result.Retryable) {
|
|
return Result{}, errors.New("contradictory terminal approval")
|
|
}
|
|
if result.Outcome == "unknown" && result.Retryable {
|
|
return Result{}, errors.New("uncertain terminal result cannot permit retry")
|
|
}
|
|
if response.StatusCode < 200 || response.StatusCode >= 300 || result.HTTPStatus < 200 || result.HTTPStatus >= 300 {
|
|
return result, fmt.Errorf("terminal operation failed (HTTP %d, operation %d)", response.StatusCode, result.HTTPStatus)
|
|
}
|
|
return result, nil
|
|
default:
|
|
return Result{}, errors.New("invalid terminal frame type")
|
|
}
|
|
}
|
|
}
|