year normalization
This commit is contained in:
@@ -37,6 +37,7 @@ func (c *Client) sendCardPayout(ctx context.Context, req CardPayoutRequest) (*Ca
|
||||
zap.String("payout_id", req.General.PaymentID),
|
||||
zap.Bool("accepted", r.Accepted),
|
||||
zap.Int("status_code", r.StatusCode),
|
||||
zap.String("provider_status", r.ProviderStatus),
|
||||
zap.String("provider_request_id", r.ProviderRequestID),
|
||||
zap.String("error_code", r.ErrorCode),
|
||||
zap.String("error_message", r.ErrorMessage),
|
||||
@@ -60,6 +61,7 @@ func (c *Client) sendCardTokenPayout(ctx context.Context, req CardTokenPayoutReq
|
||||
zap.String("payout_id", req.General.PaymentID),
|
||||
zap.Bool("accepted", r.Accepted),
|
||||
zap.Int("status_code", r.StatusCode),
|
||||
zap.String("provider_status", r.ProviderStatus),
|
||||
zap.String("provider_request_id", r.ProviderRequestID),
|
||||
zap.String("error_code", r.ErrorCode),
|
||||
zap.String("error_message", r.ErrorMessage),
|
||||
@@ -81,6 +83,7 @@ func (c *Client) sendTokenization(ctx context.Context, req CardTokenizeRequest)
|
||||
if strings.TrimSpace(c.cfg.BaseURL) == "" {
|
||||
return nil, merrors.Internal("monetix base url not configured")
|
||||
}
|
||||
normalizeRequestExpiryYear(&req)
|
||||
|
||||
req.General.Signature = ""
|
||||
signature, err := signPayload(req, c.cfg.SecretKey)
|
||||
@@ -168,6 +171,10 @@ func (c *Client) sendTokenization(ctx context.Context, req CardTokenizeRequest)
|
||||
} else if apiResp.RequestID != "" {
|
||||
result.ProviderRequestID = apiResp.RequestID
|
||||
}
|
||||
result.ProviderStatus = strings.TrimSpace(apiResp.Status)
|
||||
if result.ProviderStatus == "" {
|
||||
result.ProviderStatus = strings.TrimSpace(apiResp.Operation.Status)
|
||||
}
|
||||
|
||||
if !result.Accepted {
|
||||
result.ErrorCode = apiResp.Code
|
||||
@@ -184,6 +191,7 @@ func (c *Client) sendTokenization(ctx context.Context, req CardTokenizeRequest)
|
||||
zap.String("request_id", req.General.PaymentID),
|
||||
zap.Bool("accepted", result.Accepted),
|
||||
zap.Int("status_code", resp.StatusCode),
|
||||
zap.String("provider_status", result.ProviderStatus),
|
||||
zap.String("provider_request_id", result.ProviderRequestID),
|
||||
zap.String("error_code", result.ErrorCode),
|
||||
zap.String("error_message", result.ErrorMessage),
|
||||
@@ -205,6 +213,7 @@ func (c *Client) send(ctx context.Context, req any, path string, dispatchLog fun
|
||||
if strings.TrimSpace(c.cfg.BaseURL) == "" {
|
||||
return nil, merrors.Internal("monetix base url not configured")
|
||||
}
|
||||
normalizeRequestExpiryYear(req)
|
||||
|
||||
setSignature, err := clearSignature(req)
|
||||
if err != nil {
|
||||
@@ -254,12 +263,16 @@ func (c *Client) send(ctx context.Context, req any, path string, dispatchLog fun
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
observeRequest(outcomeNetworkError, duration)
|
||||
c.logger.Warn("Failed to read Monetix response", zap.Int("status_code", resp.StatusCode), zap.Error(err))
|
||||
return nil, merrors.Internal("failed to read monetix response: " + err.Error())
|
||||
}
|
||||
outcome := outcomeAccepted
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
outcome = outcomeHTTPError
|
||||
}
|
||||
observeRequest(outcome, duration)
|
||||
|
||||
result := &CardPayoutSendResult{
|
||||
Accepted: resp.StatusCode >= 200 && resp.StatusCode < 300,
|
||||
@@ -269,7 +282,9 @@ func (c *Client) send(ctx context.Context, req any, path string, dispatchLog fun
|
||||
var apiResp APIResponse
|
||||
if len(body) > 0 {
|
||||
if err := json.Unmarshal(body, &apiResp); err != nil {
|
||||
observeRequest(outcomeHTTPError, duration)
|
||||
c.logger.Warn("Failed to decode Monetix response", zap.Int("status_code", resp.StatusCode), zap.Error(err))
|
||||
return nil, merrors.Internal("failed to decode monetix response: " + err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -278,6 +293,10 @@ func (c *Client) send(ctx context.Context, req any, path string, dispatchLog fun
|
||||
} else if apiResp.RequestID != "" {
|
||||
result.ProviderRequestID = apiResp.RequestID
|
||||
}
|
||||
result.ProviderStatus = strings.TrimSpace(apiResp.Status)
|
||||
if result.ProviderStatus == "" {
|
||||
result.ProviderStatus = strings.TrimSpace(apiResp.Operation.Status)
|
||||
}
|
||||
|
||||
if !result.Accepted {
|
||||
result.ErrorCode = apiResp.Code
|
||||
@@ -293,10 +312,27 @@ func (c *Client) send(ctx context.Context, req any, path string, dispatchLog fun
|
||||
if responseLog != nil {
|
||||
responseLog(result)
|
||||
}
|
||||
observeRequest(outcome, duration)
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func normalizeExpiryYear(year int) int {
|
||||
if year > 0 && year < 100 {
|
||||
return year + 2000
|
||||
}
|
||||
return year
|
||||
}
|
||||
|
||||
func normalizeRequestExpiryYear(req any) {
|
||||
switch r := req.(type) {
|
||||
case *CardPayoutRequest:
|
||||
r.Card.Year = normalizeExpiryYear(r.Card.Year)
|
||||
case *CardTokenizeRequest:
|
||||
r.Card.Year = normalizeExpiryYear(r.Card.Year)
|
||||
}
|
||||
}
|
||||
|
||||
func clearSignature(req any) (func(string), error) {
|
||||
switch r := req.(type) {
|
||||
case *CardPayoutRequest:
|
||||
@@ -323,7 +359,7 @@ func logRequestDeadline(logger mlogger.Logger, ctx context.Context, url string)
|
||||
logger.Info("Monetix request context has no deadline", zap.String("url", url))
|
||||
return
|
||||
}
|
||||
logger.Info("Monetix request context deadline",
|
||||
logger.Debug("Monetix request context deadline",
|
||||
zap.String("url", url),
|
||||
zap.Time("deadline", deadline),
|
||||
zap.Duration("time_until_deadline", time.Until(deadline)),
|
||||
|
||||
Reference in New Issue
Block a user