added support for Dojo terminal-unavailable responses
This commit is contained in:
+35
-2
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
@@ -35,6 +36,20 @@ type Client struct {
|
||||
httpClient *http.Client
|
||||
}
|
||||
|
||||
const (
|
||||
dojoTerminalUnavailableStatus = "TERMINAL_UNAVAILABLE"
|
||||
dojoTerminalUnavailableMessage = "Payment terminal is unavailable"
|
||||
)
|
||||
|
||||
type httpResponseError struct {
|
||||
StatusCode int
|
||||
Body string
|
||||
}
|
||||
|
||||
func (e *httpResponseError) Error() string {
|
||||
return fmt.Sprintf("Dojo returned HTTP %d: %s", e.StatusCode, e.Body)
|
||||
}
|
||||
|
||||
type terminalSessionWithUpdates struct {
|
||||
terminalSessionResponse
|
||||
|
||||
@@ -103,6 +118,21 @@ func (c *Client) Sale(
|
||||
|
||||
session, err := c.createTerminalSession(ctx, intent.ID)
|
||||
if err != nil {
|
||||
var responseErr *httpResponseError
|
||||
if errors.As(err, &responseErr) && responseErr.StatusCode == http.StatusConflict {
|
||||
log.WithFields(log.Fields{
|
||||
"provider": "Dojo",
|
||||
"operation": "createTerminalSession",
|
||||
"status": responseErr.StatusCode,
|
||||
"terminal_unavailable": true,
|
||||
}).Warn(dojoTerminalUnavailableMessage)
|
||||
|
||||
sendPaymentStatus(onStatus, paymentstatus.TerminalUnavailable)
|
||||
result := c.baseResult(req, nil)
|
||||
result.Status = dojoTerminalUnavailableStatus
|
||||
result.ErrorMessage = dojoTerminalUnavailableMessage
|
||||
return result, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -569,10 +599,13 @@ func (c *Client) doJSON(ctx context.Context, method, path string, payload any, t
|
||||
if err != nil {
|
||||
return fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
|
||||
// log.Println("Dojo payment result raw:", string(responseBody))
|
||||
if resp.StatusCode < http.StatusOK ||
|
||||
resp.StatusCode >= http.StatusMultipleChoices {
|
||||
return fmt.Errorf("Dojo returned HTTP %d: %s", resp.StatusCode, strings.TrimSpace(string(responseBody)))
|
||||
return &httpResponseError{
|
||||
StatusCode: resp.StatusCode,
|
||||
Body: strings.TrimSpace(string(responseBody)),
|
||||
}
|
||||
}
|
||||
|
||||
if target == nil || len(responseBody) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user