fixed fee direction
This commit is contained in:
@@ -0,0 +1,321 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/internal/service/provider"
|
||||
"github.com/tech/sendico/gateway/aurora/storage/model"
|
||||
mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func TestAuroraCardPayoutScenarios(t *testing.T) {
|
||||
cfg := provider.Config{
|
||||
ProjectID: 1001,
|
||||
AllowedCurrencies: []string{"RUB"},
|
||||
}
|
||||
now := time.Date(2026, 3, 5, 10, 0, 0, 0, time.UTC)
|
||||
repo := newMockRepository()
|
||||
processor := newCardPayoutProcessor(zap.NewNop(), cfg, staticClock{now: now}, repo, nil, nil)
|
||||
processor.dispatchThrottleInterval = 0
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
pan string
|
||||
wantAccepted bool
|
||||
wantStatus mntxv1.PayoutStatus
|
||||
wantErrorCode string
|
||||
wantProviderCode string
|
||||
wantProviderMatch string
|
||||
}{
|
||||
{
|
||||
name: "approved_instant",
|
||||
pan: "2200001111111111",
|
||||
wantAccepted: true,
|
||||
wantStatus: mntxv1.PayoutStatus_PAYOUT_STATUS_SUCCESS,
|
||||
wantErrorCode: "00",
|
||||
wantProviderCode: "00",
|
||||
wantProviderMatch: "Approved",
|
||||
},
|
||||
{
|
||||
name: "pending_issuer_review",
|
||||
pan: "2200002222222222",
|
||||
wantAccepted: true,
|
||||
wantStatus: mntxv1.PayoutStatus_PAYOUT_STATUS_WAITING,
|
||||
wantErrorCode: "P01",
|
||||
wantProviderCode: "P01",
|
||||
wantProviderMatch: "Pending issuer review",
|
||||
},
|
||||
{
|
||||
name: "insufficient_funds",
|
||||
pan: "2200003333333333",
|
||||
wantAccepted: false,
|
||||
wantStatus: mntxv1.PayoutStatus_PAYOUT_STATUS_FAILED,
|
||||
wantErrorCode: "51",
|
||||
wantProviderCode: "51",
|
||||
wantProviderMatch: "Insufficient funds",
|
||||
},
|
||||
{
|
||||
name: "unknown_card_default_queue",
|
||||
pan: "2200009999999999",
|
||||
wantAccepted: true,
|
||||
wantStatus: mntxv1.PayoutStatus_PAYOUT_STATUS_WAITING,
|
||||
wantErrorCode: "P00",
|
||||
wantProviderCode: "P00",
|
||||
wantProviderMatch: "Queued for provider processing",
|
||||
},
|
||||
{
|
||||
name: "provider_maintenance",
|
||||
pan: "2200009999999997",
|
||||
wantAccepted: false,
|
||||
wantStatus: mntxv1.PayoutStatus_PAYOUT_STATUS_FAILED,
|
||||
wantErrorCode: "91",
|
||||
wantProviderCode: "91",
|
||||
wantProviderMatch: "inoperative",
|
||||
},
|
||||
{
|
||||
name: "provider_system_malfunction",
|
||||
pan: "2200009999999996",
|
||||
wantAccepted: false,
|
||||
wantStatus: mntxv1.PayoutStatus_PAYOUT_STATUS_FAILED,
|
||||
wantErrorCode: "96",
|
||||
wantProviderCode: "96",
|
||||
wantProviderMatch: "System malfunction",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
req := validCardPayoutRequest()
|
||||
req.PayoutId = ""
|
||||
req.OperationRef = "op-" + tc.name
|
||||
req.ParentPaymentRef = "parent-" + tc.name
|
||||
req.IdempotencyKey = "idem-" + tc.name
|
||||
req.CardPan = tc.pan
|
||||
|
||||
resp, err := processor.Submit(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatalf("submit failed: %v", err)
|
||||
}
|
||||
if got, want := resp.GetAccepted(), tc.wantAccepted; got != want {
|
||||
t.Fatalf("accepted mismatch: got=%v want=%v", got, want)
|
||||
}
|
||||
if got, want := resp.GetPayout().GetStatus(), tc.wantStatus; got != want {
|
||||
t.Fatalf("status mismatch: got=%v want=%v", got, want)
|
||||
}
|
||||
if got, want := strings.TrimSpace(resp.GetErrorCode()), tc.wantErrorCode; got != want {
|
||||
t.Fatalf("error_code mismatch: got=%q want=%q", got, want)
|
||||
}
|
||||
|
||||
state, ok := repo.payouts.Get(req.GetOperationRef())
|
||||
if !ok || state == nil {
|
||||
t.Fatalf("expected persisted payout state")
|
||||
}
|
||||
if got, want := strings.TrimSpace(state.ProviderCode), tc.wantProviderCode; got != want {
|
||||
t.Fatalf("provider_code mismatch: got=%q want=%q", got, want)
|
||||
}
|
||||
if tc.wantProviderMatch != "" && !strings.Contains(state.ProviderMessage, tc.wantProviderMatch) {
|
||||
t.Fatalf("provider_message mismatch: got=%q expected to contain %q", state.ProviderMessage, tc.wantProviderMatch)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuroraTransportFailureScenarioEventuallyFails(t *testing.T) {
|
||||
cfg := provider.Config{
|
||||
ProjectID: 1001,
|
||||
AllowedCurrencies: []string{"RUB"},
|
||||
}
|
||||
repo := newMockRepository()
|
||||
processor := newCardPayoutProcessor(
|
||||
zap.NewNop(),
|
||||
cfg,
|
||||
staticClock{now: time.Date(2026, 3, 5, 10, 0, 0, 0, time.UTC)},
|
||||
repo,
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
defer processor.stopRetries()
|
||||
processor.dispatchThrottleInterval = 0
|
||||
processor.dispatchMaxAttempts = 2
|
||||
processor.retryDelayFn = func(uint32) time.Duration { return time.Millisecond }
|
||||
|
||||
req := validCardPayoutRequest()
|
||||
req.PayoutId = ""
|
||||
req.OperationRef = "op-transport-timeout"
|
||||
req.ParentPaymentRef = "parent-transport-timeout"
|
||||
req.IdempotencyKey = "idem-transport-timeout"
|
||||
req.CardPan = "2200008888888888"
|
||||
|
||||
resp, err := processor.Submit(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatalf("submit failed: %v", err)
|
||||
}
|
||||
if !resp.GetAccepted() {
|
||||
t.Fatalf("expected accepted response while transport retry is scheduled")
|
||||
}
|
||||
if got, want := resp.GetPayout().GetStatus(), mntxv1.PayoutStatus_PAYOUT_STATUS_WAITING; got != want {
|
||||
t.Fatalf("initial status mismatch: got=%v want=%v", got, want)
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for {
|
||||
state, ok := repo.payouts.Get(req.GetOperationRef())
|
||||
if ok && state != nil && state.Status == model.PayoutStatusFailed {
|
||||
if !strings.Contains(strings.ToLower(state.FailureReason), "transport error") {
|
||||
t.Fatalf("expected transport failure reason, got=%q", state.FailureReason)
|
||||
}
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("timeout waiting for transport failure terminal state")
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuroraRetryableScenarioEventuallyFails(t *testing.T) {
|
||||
cfg := provider.Config{
|
||||
ProjectID: 1001,
|
||||
AllowedCurrencies: []string{"RUB"},
|
||||
}
|
||||
repo := newMockRepository()
|
||||
processor := newCardPayoutProcessor(
|
||||
zap.NewNop(),
|
||||
cfg,
|
||||
staticClock{now: time.Date(2026, 3, 5, 10, 0, 0, 0, time.UTC)},
|
||||
repo,
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
defer processor.stopRetries()
|
||||
processor.dispatchThrottleInterval = 0
|
||||
processor.dispatchMaxAttempts = 2
|
||||
processor.retryDelayFn = func(uint32) time.Duration { return time.Millisecond }
|
||||
|
||||
req := validCardPayoutRequest()
|
||||
req.PayoutId = ""
|
||||
req.OperationRef = "op-retryable-issuer-unavailable"
|
||||
req.ParentPaymentRef = "parent-retryable-issuer-unavailable"
|
||||
req.IdempotencyKey = "idem-retryable-issuer-unavailable"
|
||||
req.CardPan = "2200004444444444"
|
||||
|
||||
resp, err := processor.Submit(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatalf("submit failed: %v", err)
|
||||
}
|
||||
if !resp.GetAccepted() {
|
||||
t.Fatalf("expected accepted response while retry is scheduled")
|
||||
}
|
||||
if got, want := resp.GetPayout().GetStatus(), mntxv1.PayoutStatus_PAYOUT_STATUS_WAITING; got != want {
|
||||
t.Fatalf("initial status mismatch: got=%v want=%v", got, want)
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for {
|
||||
state, ok := repo.payouts.Get(req.GetOperationRef())
|
||||
if ok && state != nil && state.Status == model.PayoutStatusFailed {
|
||||
if !strings.Contains(state.FailureReason, "10101") {
|
||||
t.Fatalf("expected retryable provider code in failure_reason, got=%q", state.FailureReason)
|
||||
}
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("timeout waiting for retryable scenario terminal failure")
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuroraTokenPayoutUsesTokenizedPANScenario(t *testing.T) {
|
||||
cfg := provider.Config{
|
||||
ProjectID: 1001,
|
||||
AllowedCurrencies: []string{"RUB", "USD"},
|
||||
}
|
||||
repo := newMockRepository()
|
||||
processor := newCardPayoutProcessor(
|
||||
zap.NewNop(),
|
||||
cfg,
|
||||
staticClock{now: time.Date(2026, 3, 5, 10, 0, 0, 0, time.UTC)},
|
||||
repo,
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
processor.dispatchThrottleInterval = 0
|
||||
|
||||
tokenizeReq := validCardTokenizeRequest()
|
||||
tokenizeReq.RequestId = "tok-req-insufficient"
|
||||
tokenizeReq.CardPan = "2200003333333333"
|
||||
|
||||
tokenizeResp, err := processor.Tokenize(context.Background(), tokenizeReq)
|
||||
if err != nil {
|
||||
t.Fatalf("tokenize failed: %v", err)
|
||||
}
|
||||
if tokenizeResp.GetToken() == "" {
|
||||
t.Fatalf("expected non-empty token")
|
||||
}
|
||||
|
||||
payoutReq := validCardTokenPayoutRequest()
|
||||
payoutReq.PayoutId = ""
|
||||
payoutReq.OperationRef = "op-token-insufficient"
|
||||
payoutReq.ParentPaymentRef = "parent-token-insufficient"
|
||||
payoutReq.IdempotencyKey = "idem-token-insufficient"
|
||||
payoutReq.CardToken = tokenizeResp.GetToken()
|
||||
payoutReq.MaskedPan = tokenizeResp.GetMaskedPan()
|
||||
|
||||
resp, err := processor.SubmitToken(context.Background(), payoutReq)
|
||||
if err != nil {
|
||||
t.Fatalf("submit token payout failed: %v", err)
|
||||
}
|
||||
if resp.GetAccepted() {
|
||||
t.Fatalf("expected declined payout for insufficient funds token scenario")
|
||||
}
|
||||
if got, want := resp.GetErrorCode(), "51"; got != want {
|
||||
t.Fatalf("error_code mismatch: got=%q want=%q", got, want)
|
||||
}
|
||||
if got, want := resp.GetPayout().GetStatus(), mntxv1.PayoutStatus_PAYOUT_STATUS_FAILED; got != want {
|
||||
t.Fatalf("status mismatch: got=%v want=%v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuroraTokenPayoutFallsBackToMaskedPANScenario(t *testing.T) {
|
||||
cfg := provider.Config{
|
||||
ProjectID: 1001,
|
||||
AllowedCurrencies: []string{"RUB", "USD"},
|
||||
}
|
||||
repo := newMockRepository()
|
||||
processor := newCardPayoutProcessor(
|
||||
zap.NewNop(),
|
||||
cfg,
|
||||
staticClock{now: time.Date(2026, 3, 5, 10, 0, 0, 0, time.UTC)},
|
||||
repo,
|
||||
nil,
|
||||
nil,
|
||||
)
|
||||
processor.dispatchThrottleInterval = 0
|
||||
|
||||
req := validCardTokenPayoutRequest()
|
||||
req.PayoutId = ""
|
||||
req.OperationRef = "op-token-masked-fallback"
|
||||
req.ParentPaymentRef = "parent-token-masked-fallback"
|
||||
req.IdempotencyKey = "idem-token-masked-fallback"
|
||||
req.CardToken = "unknown-token"
|
||||
req.MaskedPan = "220000******6666"
|
||||
|
||||
resp, err := processor.SubmitToken(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatalf("submit token payout failed: %v", err)
|
||||
}
|
||||
if resp.GetAccepted() {
|
||||
t.Fatalf("expected declined payout for masked-pan fallback scenario")
|
||||
}
|
||||
if got, want := resp.GetErrorCode(), "05"; got != want {
|
||||
t.Fatalf("error_code mismatch: got=%q want=%q", got, want)
|
||||
}
|
||||
if got, want := resp.GetPayout().GetStatus(), mntxv1.PayoutStatus_PAYOUT_STATUS_FAILED; got != want {
|
||||
t.Fatalf("status mismatch: got=%v want=%v", got, want)
|
||||
}
|
||||
}
|
||||
177
api/gateway/aurora/internal/service/gateway/callback.go
Normal file
177
api/gateway/aurora/internal/service/gateway/callback.go
Normal file
@@ -0,0 +1,177 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/internal/service/provider"
|
||||
clockpkg "github.com/tech/sendico/pkg/clock"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
|
||||
"go.uber.org/zap"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
type callbackPayment struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Status string `json:"status"`
|
||||
Date string `json:"date"`
|
||||
Method string `json:"method"`
|
||||
Description string `json:"description"`
|
||||
Sum struct {
|
||||
Amount int64 `json:"amount"`
|
||||
Currency string `json:"currency"`
|
||||
} `json:"sum"`
|
||||
}
|
||||
|
||||
type callbackOperation struct {
|
||||
ID int64 `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Status string `json:"status"`
|
||||
Date string `json:"date"`
|
||||
CreatedDate string `json:"created_date"`
|
||||
RequestID string `json:"request_id"`
|
||||
SumInitial struct {
|
||||
Amount int64 `json:"amount"`
|
||||
Currency string `json:"currency"`
|
||||
} `json:"sum_initial"`
|
||||
SumConverted struct {
|
||||
Amount int64 `json:"amount"`
|
||||
Currency string `json:"currency"`
|
||||
} `json:"sum_converted"`
|
||||
Provider struct {
|
||||
ID int64 `json:"id"`
|
||||
PaymentID string `json:"payment_id"`
|
||||
AuthCode string `json:"auth_code"`
|
||||
EndpointID int64 `json:"endpoint_id"`
|
||||
Date string `json:"date"`
|
||||
} `json:"provider"`
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type providerCallback struct {
|
||||
ProjectID int64 `json:"project_id"`
|
||||
Payment callbackPayment `json:"payment"`
|
||||
Account struct {
|
||||
Number string `json:"number"`
|
||||
Type string `json:"type"`
|
||||
CardHolder string `json:"card_holder"`
|
||||
ExpiryMonth string `json:"expiry_month"`
|
||||
ExpiryYear string `json:"expiry_year"`
|
||||
} `json:"account"`
|
||||
Customer struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"customer"`
|
||||
Operation callbackOperation `json:"operation"`
|
||||
Signature string `json:"signature"`
|
||||
}
|
||||
|
||||
// ProcessProviderCallback ingests provider callbacks and updates payout state.
|
||||
func (s *Service) ProcessProviderCallback(ctx context.Context, payload []byte) (int, error) {
|
||||
log := s.logger.Named("callback")
|
||||
if s.card == nil {
|
||||
log.Warn("Card payout processor not initialised")
|
||||
return http.StatusInternalServerError, merrors.Internal("card payout processor not initialised")
|
||||
}
|
||||
log.Debug("Callback processing requested", zap.Int("payload_bytes", len(payload)))
|
||||
return s.card.ProcessCallback(ctx, payload)
|
||||
}
|
||||
|
||||
func mapCallbackToState(clock clockpkg.Clock, cfg provider.Config, cb providerCallback) (*mntxv1.CardPayoutState, string) {
|
||||
status := strings.ToLower(strings.TrimSpace(cb.Payment.Status))
|
||||
opStatus := strings.ToLower(strings.TrimSpace(cb.Operation.Status))
|
||||
code := strings.TrimSpace(cb.Operation.Code)
|
||||
|
||||
outcome := provider.OutcomeDecline
|
||||
internalStatus := mntxv1.PayoutStatus_PAYOUT_STATUS_FAILED
|
||||
|
||||
if status == cfg.SuccessStatus() && opStatus == cfg.SuccessStatus() && (code == "" || code == "0") {
|
||||
internalStatus = mntxv1.PayoutStatus_PAYOUT_STATUS_SUCCESS
|
||||
outcome = provider.OutcomeSuccess
|
||||
} else if status == cfg.ProcessingStatus() || opStatus == cfg.ProcessingStatus() {
|
||||
internalStatus = mntxv1.PayoutStatus_PAYOUT_STATUS_WAITING
|
||||
outcome = provider.OutcomeProcessing
|
||||
}
|
||||
|
||||
now := timestamppb.New(clock.Now())
|
||||
state := &mntxv1.CardPayoutState{
|
||||
PayoutId: cb.Payment.ID,
|
||||
ProjectId: cb.ProjectID,
|
||||
CustomerId: cb.Customer.ID,
|
||||
AmountMinor: cb.Payment.Sum.Amount,
|
||||
Currency: strings.ToUpper(strings.TrimSpace(cb.Payment.Sum.Currency)),
|
||||
Status: internalStatus,
|
||||
ProviderCode: cb.Operation.Code,
|
||||
ProviderMessage: cb.Operation.Message,
|
||||
ProviderPaymentId: fallbackProviderPaymentID(cb),
|
||||
OperationRef: strings.TrimSpace(cb.Payment.ID),
|
||||
UpdatedAt: now,
|
||||
CreatedAt: now,
|
||||
}
|
||||
|
||||
return state, outcome
|
||||
}
|
||||
|
||||
func fallbackProviderPaymentID(cb providerCallback) string {
|
||||
if cb.Operation.Provider.PaymentID != "" {
|
||||
return cb.Operation.Provider.PaymentID
|
||||
}
|
||||
if cb.Operation.RequestID != "" {
|
||||
return cb.Operation.RequestID
|
||||
}
|
||||
return cb.Payment.ID
|
||||
}
|
||||
|
||||
func verifyCallbackSignature(payload []byte, secret string) (string, error) {
|
||||
root, err := decodeCallbackPayload(payload)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
signature, ok := signatureFromPayload(root)
|
||||
if !ok || strings.TrimSpace(signature) == "" {
|
||||
return "", merrors.InvalidArgument("signature is missing")
|
||||
}
|
||||
calculated, err := provider.SignPayload(root, secret)
|
||||
if err != nil {
|
||||
return signature, err
|
||||
}
|
||||
if subtleConstantTimeCompare(signature, calculated) {
|
||||
return signature, nil
|
||||
}
|
||||
return signature, merrors.DataConflict("signature mismatch")
|
||||
}
|
||||
|
||||
func decodeCallbackPayload(payload []byte) (any, error) {
|
||||
var root any
|
||||
decoder := json.NewDecoder(bytes.NewReader(payload))
|
||||
decoder.UseNumber()
|
||||
if err := decoder.Decode(&root); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return root, nil
|
||||
}
|
||||
|
||||
func signatureFromPayload(root any) (string, bool) {
|
||||
payload, ok := root.(map[string]any)
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
for key, value := range payload {
|
||||
if !strings.EqualFold(key, "signature") {
|
||||
continue
|
||||
}
|
||||
signature, ok := value.(string)
|
||||
return signature, ok
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
|
||||
func subtleConstantTimeCompare(a, b string) bool {
|
||||
return hmac.Equal([]byte(strings.TrimSpace(a)), []byte(strings.TrimSpace(b)))
|
||||
}
|
||||
139
api/gateway/aurora/internal/service/gateway/callback_test.go
Normal file
139
api/gateway/aurora/internal/service/gateway/callback_test.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/internal/service/provider"
|
||||
mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
|
||||
)
|
||||
|
||||
type fixedClock struct {
|
||||
now time.Time
|
||||
}
|
||||
|
||||
func (f fixedClock) Now() time.Time {
|
||||
return f.now
|
||||
}
|
||||
|
||||
func baseCallback() providerCallback {
|
||||
cb := providerCallback{
|
||||
ProjectID: 42,
|
||||
}
|
||||
cb.Payment.ID = "payout-1"
|
||||
cb.Payment.Status = "success"
|
||||
cb.Payment.Sum.Amount = 5000
|
||||
cb.Payment.Sum.Currency = "usd"
|
||||
cb.Customer.ID = "cust-1"
|
||||
cb.Operation.Status = "success"
|
||||
cb.Operation.Code = ""
|
||||
cb.Operation.Message = "ok"
|
||||
cb.Operation.RequestID = "req-1"
|
||||
cb.Operation.Provider.PaymentID = "prov-1"
|
||||
return cb
|
||||
}
|
||||
|
||||
func TestMapCallbackToState_StatusMapping(t *testing.T) {
|
||||
now := time.Date(2024, 1, 2, 3, 4, 5, 0, time.UTC)
|
||||
cfg := provider.DefaultConfig()
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
paymentStatus string
|
||||
operationStatus string
|
||||
code string
|
||||
expectedStatus mntxv1.PayoutStatus
|
||||
expectedOutcome string
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
paymentStatus: "success",
|
||||
operationStatus: "success",
|
||||
code: "0",
|
||||
expectedStatus: mntxv1.PayoutStatus_PAYOUT_STATUS_SUCCESS,
|
||||
expectedOutcome: provider.OutcomeSuccess,
|
||||
},
|
||||
{
|
||||
name: "processing",
|
||||
paymentStatus: "processing",
|
||||
operationStatus: "success",
|
||||
code: "",
|
||||
expectedStatus: mntxv1.PayoutStatus_PAYOUT_STATUS_WAITING,
|
||||
expectedOutcome: provider.OutcomeProcessing,
|
||||
},
|
||||
{
|
||||
name: "decline",
|
||||
paymentStatus: "failed",
|
||||
operationStatus: "failed",
|
||||
code: "1",
|
||||
expectedStatus: mntxv1.PayoutStatus_PAYOUT_STATUS_FAILED,
|
||||
expectedOutcome: provider.OutcomeDecline,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
cb := baseCallback()
|
||||
cb.Payment.Status = tc.paymentStatus
|
||||
cb.Operation.Status = tc.operationStatus
|
||||
cb.Operation.Code = tc.code
|
||||
|
||||
state, outcome := mapCallbackToState(fixedClock{now: now}, cfg, cb)
|
||||
if state.Status != tc.expectedStatus {
|
||||
t.Fatalf("expected status %v, got %v", tc.expectedStatus, state.Status)
|
||||
}
|
||||
if outcome != tc.expectedOutcome {
|
||||
t.Fatalf("expected outcome %q, got %q", tc.expectedOutcome, outcome)
|
||||
}
|
||||
if state.Currency != "USD" {
|
||||
t.Fatalf("expected currency USD, got %q", state.Currency)
|
||||
}
|
||||
if !state.UpdatedAt.AsTime().Equal(now) {
|
||||
t.Fatalf("expected updated_at %v, got %v", now, state.UpdatedAt.AsTime())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFallbackProviderPaymentID(t *testing.T) {
|
||||
cb := baseCallback()
|
||||
if got := fallbackProviderPaymentID(cb); got != "prov-1" {
|
||||
t.Fatalf("expected provider payment id, got %q", got)
|
||||
}
|
||||
cb.Operation.Provider.PaymentID = ""
|
||||
if got := fallbackProviderPaymentID(cb); got != "req-1" {
|
||||
t.Fatalf("expected request id fallback, got %q", got)
|
||||
}
|
||||
cb.Operation.RequestID = ""
|
||||
if got := fallbackProviderPaymentID(cb); got != "payout-1" {
|
||||
t.Fatalf("expected payment id fallback, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyCallbackSignature(t *testing.T) {
|
||||
secret := "secret"
|
||||
cb := baseCallback()
|
||||
|
||||
sig, err := provider.SignPayload(cb, secret)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to sign payload: %v", err)
|
||||
}
|
||||
cb.Signature = sig
|
||||
payload, err := json.Marshal(cb)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal callback: %v", err)
|
||||
}
|
||||
if _, err := verifyCallbackSignature(payload, secret); err != nil {
|
||||
t.Fatalf("expected valid signature, got %v", err)
|
||||
}
|
||||
|
||||
cb.Signature = "invalid"
|
||||
payload, err = json.Marshal(cb)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal callback: %v", err)
|
||||
}
|
||||
if _, err := verifyCallbackSignature(payload, secret); err == nil {
|
||||
t.Fatalf("expected signature mismatch error")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,207 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/tech/sendico/pkg/api/routers/gsresponse"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
"github.com/tech/sendico/pkg/mservice"
|
||||
mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
|
||||
"go.uber.org/zap"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
func (s *Service) CreateCardPayout(ctx context.Context, req *mntxv1.CardPayoutRequest) (*mntxv1.CardPayoutResponse, error) {
|
||||
return executeUnary(ctx, s, "CreateCardPayout", s.handleCreateCardPayout, req)
|
||||
}
|
||||
|
||||
func (s *Service) handleCreateCardPayout(ctx context.Context, req *mntxv1.CardPayoutRequest) gsresponse.Responder[mntxv1.CardPayoutResponse] {
|
||||
log := s.logger.Named("card_payout")
|
||||
log.Info("Create card payout request received",
|
||||
zap.String("payout_id", strings.TrimSpace(req.GetPayoutId())),
|
||||
zap.String("operation_ref", strings.TrimSpace(req.GetOperationRef())),
|
||||
zap.String("parent_payment_ref", strings.TrimSpace(req.GetParentPaymentRef())),
|
||||
zap.String("customer_id", strings.TrimSpace(req.GetCustomerId())),
|
||||
zap.Int64("amount_minor", req.GetAmountMinor()),
|
||||
zap.String("currency", strings.ToUpper(strings.TrimSpace(req.GetCurrency()))),
|
||||
)
|
||||
if s.card == nil {
|
||||
log.Warn("Card payout processor not initialised")
|
||||
return gsresponse.Internal[mntxv1.CardPayoutResponse](s.logger, mservice.MntxGateway, merrors.Internal("card payout processor not initialised"))
|
||||
}
|
||||
|
||||
resp, err := s.card.Submit(ctx, req)
|
||||
if err != nil {
|
||||
log.Warn("Card payout submission failed", zap.Error(err))
|
||||
return gsresponse.Auto[mntxv1.CardPayoutResponse](s.logger, mservice.MntxGateway, err)
|
||||
}
|
||||
log.Info("Card payout submission completed", zap.String("payout_id", resp.GetPayout().GetPayoutId()), zap.Bool("accepted", resp.GetAccepted()))
|
||||
return gsresponse.Success(resp)
|
||||
}
|
||||
|
||||
func (s *Service) CreateCardTokenPayout(ctx context.Context, req *mntxv1.CardTokenPayoutRequest) (*mntxv1.CardTokenPayoutResponse, error) {
|
||||
return executeUnary(ctx, s, "CreateCardTokenPayout", s.handleCreateCardTokenPayout, req)
|
||||
}
|
||||
|
||||
func (s *Service) handleCreateCardTokenPayout(ctx context.Context, req *mntxv1.CardTokenPayoutRequest) gsresponse.Responder[mntxv1.CardTokenPayoutResponse] {
|
||||
log := s.logger.Named("card_token_payout")
|
||||
log.Info("Create card token payout request received",
|
||||
zap.String("payout_id", strings.TrimSpace(req.GetPayoutId())),
|
||||
zap.String("operation_ref", strings.TrimSpace(req.GetOperationRef())),
|
||||
zap.String("parent_payment_ref", strings.TrimSpace(req.GetParentPaymentRef())),
|
||||
zap.String("customer_id", strings.TrimSpace(req.GetCustomerId())),
|
||||
zap.Int64("amount_minor", req.GetAmountMinor()),
|
||||
zap.String("currency", strings.ToUpper(strings.TrimSpace(req.GetCurrency()))),
|
||||
)
|
||||
if s.card == nil {
|
||||
log.Warn("Card payout processor not initialised")
|
||||
return gsresponse.Internal[mntxv1.CardTokenPayoutResponse](s.logger, mservice.MntxGateway, merrors.Internal("card payout processor not initialised"))
|
||||
}
|
||||
|
||||
resp, err := s.card.SubmitToken(ctx, req)
|
||||
if err != nil {
|
||||
log.Warn("Card token payout submission failed", zap.Error(err))
|
||||
return gsresponse.Auto[mntxv1.CardTokenPayoutResponse](s.logger, mservice.MntxGateway, err)
|
||||
}
|
||||
log.Info("Card token payout submission completed", zap.String("payout_id", resp.GetPayout().GetPayoutId()), zap.Bool("accepted", resp.GetAccepted()))
|
||||
return gsresponse.Success(resp)
|
||||
}
|
||||
|
||||
func (s *Service) CreateCardToken(ctx context.Context, req *mntxv1.CardTokenizeRequest) (*mntxv1.CardTokenizeResponse, error) {
|
||||
return executeUnary(ctx, s, "CreateCardToken", s.handleCreateCardToken, req)
|
||||
}
|
||||
|
||||
func (s *Service) handleCreateCardToken(ctx context.Context, req *mntxv1.CardTokenizeRequest) gsresponse.Responder[mntxv1.CardTokenizeResponse] {
|
||||
log := s.logger.Named("card_tokenize")
|
||||
log.Info("Create card token request received",
|
||||
zap.String("request_id", strings.TrimSpace(req.GetRequestId())),
|
||||
zap.String("customer_id", strings.TrimSpace(req.GetCustomerId())),
|
||||
)
|
||||
if s.card == nil {
|
||||
log.Warn("Card payout processor not initialised")
|
||||
return gsresponse.Internal[mntxv1.CardTokenizeResponse](s.logger, mservice.MntxGateway, merrors.Internal("card payout processor not initialised"))
|
||||
}
|
||||
|
||||
resp, err := s.card.Tokenize(ctx, req)
|
||||
if err != nil {
|
||||
log.Warn("Card tokenization failed", zap.Error(err))
|
||||
return gsresponse.Auto[mntxv1.CardTokenizeResponse](s.logger, mservice.MntxGateway, err)
|
||||
}
|
||||
log.Info("Card tokenization completed", zap.String("request_id", resp.GetRequestId()), zap.Bool("success", resp.GetSuccess()))
|
||||
return gsresponse.Success(resp)
|
||||
}
|
||||
|
||||
func (s *Service) GetCardPayoutStatus(ctx context.Context, req *mntxv1.GetCardPayoutStatusRequest) (*mntxv1.GetCardPayoutStatusResponse, error) {
|
||||
return executeUnary(ctx, s, "GetCardPayoutStatus", s.handleGetCardPayoutStatus, req)
|
||||
}
|
||||
|
||||
func (s *Service) handleGetCardPayoutStatus(_ context.Context, req *mntxv1.GetCardPayoutStatusRequest) gsresponse.Responder[mntxv1.GetCardPayoutStatusResponse] {
|
||||
log := s.logger.Named("card_payout_status")
|
||||
log.Info("Get card payout status request received", zap.String("payout_id", strings.TrimSpace(req.GetPayoutId())))
|
||||
if s.card == nil {
|
||||
log.Warn("Card payout processor not initialised")
|
||||
return gsresponse.Internal[mntxv1.GetCardPayoutStatusResponse](s.logger, mservice.MntxGateway, merrors.Internal("card payout processor not initialised"))
|
||||
}
|
||||
|
||||
state, err := s.card.Status(context.Background(), req.GetPayoutId())
|
||||
if err != nil {
|
||||
log.Warn("Card payout status lookup failed", zap.Error(err))
|
||||
return gsresponse.Auto[mntxv1.GetCardPayoutStatusResponse](s.logger, mservice.MntxGateway, err)
|
||||
}
|
||||
log.Info("Card payout status retrieved", zap.String("payout_id", state.GetPayoutId()), zap.String("status", state.GetStatus().String()))
|
||||
return gsresponse.Success(&mntxv1.GetCardPayoutStatusResponse{Payout: state})
|
||||
}
|
||||
|
||||
func sanitizeCardPayoutRequest(req *mntxv1.CardPayoutRequest) *mntxv1.CardPayoutRequest {
|
||||
if req == nil {
|
||||
return nil
|
||||
}
|
||||
clean := proto.Clone(req)
|
||||
r, ok := clean.(*mntxv1.CardPayoutRequest)
|
||||
if !ok {
|
||||
return req
|
||||
}
|
||||
r.PayoutId = strings.TrimSpace(r.GetPayoutId())
|
||||
r.ParentPaymentRef = strings.TrimSpace(r.GetParentPaymentRef())
|
||||
r.CustomerId = strings.TrimSpace(r.GetCustomerId())
|
||||
r.CustomerFirstName = strings.TrimSpace(r.GetCustomerFirstName())
|
||||
r.CustomerMiddleName = strings.TrimSpace(r.GetCustomerMiddleName())
|
||||
r.CustomerLastName = strings.TrimSpace(r.GetCustomerLastName())
|
||||
r.CustomerIp = strings.TrimSpace(r.GetCustomerIp())
|
||||
r.CustomerZip = strings.TrimSpace(r.GetCustomerZip())
|
||||
r.CustomerCountry = strings.TrimSpace(r.GetCustomerCountry())
|
||||
r.CustomerState = strings.TrimSpace(r.GetCustomerState())
|
||||
r.CustomerCity = strings.TrimSpace(r.GetCustomerCity())
|
||||
r.CustomerAddress = strings.TrimSpace(r.GetCustomerAddress())
|
||||
r.Currency = strings.ToUpper(strings.TrimSpace(r.GetCurrency()))
|
||||
r.CardPan = strings.TrimSpace(r.GetCardPan())
|
||||
r.CardHolder = strings.TrimSpace(r.GetCardHolder())
|
||||
r.OperationRef = strings.TrimSpace(r.GetOperationRef())
|
||||
r.IdempotencyKey = strings.TrimSpace(r.GetIdempotencyKey())
|
||||
r.IntentRef = strings.TrimSpace(r.GetIntentRef())
|
||||
return r
|
||||
}
|
||||
|
||||
func sanitizeCardTokenPayoutRequest(req *mntxv1.CardTokenPayoutRequest) *mntxv1.CardTokenPayoutRequest {
|
||||
if req == nil {
|
||||
return nil
|
||||
}
|
||||
clean := proto.Clone(req)
|
||||
r, ok := clean.(*mntxv1.CardTokenPayoutRequest)
|
||||
if !ok {
|
||||
return req
|
||||
}
|
||||
r.PayoutId = strings.TrimSpace(r.GetPayoutId())
|
||||
r.ParentPaymentRef = strings.TrimSpace(r.GetParentPaymentRef())
|
||||
r.CustomerId = strings.TrimSpace(r.GetCustomerId())
|
||||
r.CustomerFirstName = strings.TrimSpace(r.GetCustomerFirstName())
|
||||
r.CustomerMiddleName = strings.TrimSpace(r.GetCustomerMiddleName())
|
||||
r.CustomerLastName = strings.TrimSpace(r.GetCustomerLastName())
|
||||
r.CustomerIp = strings.TrimSpace(r.GetCustomerIp())
|
||||
r.CustomerZip = strings.TrimSpace(r.GetCustomerZip())
|
||||
r.CustomerCountry = strings.TrimSpace(r.GetCustomerCountry())
|
||||
r.CustomerState = strings.TrimSpace(r.GetCustomerState())
|
||||
r.CustomerCity = strings.TrimSpace(r.GetCustomerCity())
|
||||
r.CustomerAddress = strings.TrimSpace(r.GetCustomerAddress())
|
||||
r.Currency = strings.ToUpper(strings.TrimSpace(r.GetCurrency()))
|
||||
r.CardToken = strings.TrimSpace(r.GetCardToken())
|
||||
r.CardHolder = strings.TrimSpace(r.GetCardHolder())
|
||||
r.MaskedPan = strings.TrimSpace(r.GetMaskedPan())
|
||||
r.OperationRef = strings.TrimSpace(r.GetOperationRef())
|
||||
r.IdempotencyKey = strings.TrimSpace(r.GetIdempotencyKey())
|
||||
r.IntentRef = strings.TrimSpace(r.GetIntentRef())
|
||||
return r
|
||||
}
|
||||
|
||||
func sanitizeCardTokenizeRequest(req *mntxv1.CardTokenizeRequest) *mntxv1.CardTokenizeRequest {
|
||||
if req == nil {
|
||||
return nil
|
||||
}
|
||||
clean := proto.Clone(req)
|
||||
r, ok := clean.(*mntxv1.CardTokenizeRequest)
|
||||
if !ok {
|
||||
return req
|
||||
}
|
||||
r.RequestId = strings.TrimSpace(r.GetRequestId())
|
||||
r.CustomerId = strings.TrimSpace(r.GetCustomerId())
|
||||
r.CustomerFirstName = strings.TrimSpace(r.GetCustomerFirstName())
|
||||
r.CustomerMiddleName = strings.TrimSpace(r.GetCustomerMiddleName())
|
||||
r.CustomerLastName = strings.TrimSpace(r.GetCustomerLastName())
|
||||
r.CustomerIp = strings.TrimSpace(r.GetCustomerIp())
|
||||
r.CustomerZip = strings.TrimSpace(r.GetCustomerZip())
|
||||
r.CustomerCountry = strings.TrimSpace(r.GetCustomerCountry())
|
||||
r.CustomerState = strings.TrimSpace(r.GetCustomerState())
|
||||
r.CustomerCity = strings.TrimSpace(r.GetCustomerCity())
|
||||
r.CustomerAddress = strings.TrimSpace(r.GetCustomerAddress())
|
||||
r.CardPan = strings.TrimSpace(r.GetCardPan())
|
||||
r.CardHolder = strings.TrimSpace(r.GetCardHolder())
|
||||
r.CardCvv = strings.TrimSpace(r.GetCardCvv())
|
||||
if card := r.GetCard(); card != nil {
|
||||
card.Pan = strings.TrimSpace(card.GetPan())
|
||||
card.CardHolder = strings.TrimSpace(card.GetCardHolder())
|
||||
card.Cvv = strings.TrimSpace(card.GetCvv())
|
||||
r.Card = card
|
||||
}
|
||||
return r
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/storage"
|
||||
"github.com/tech/sendico/gateway/aurora/storage/model"
|
||||
)
|
||||
|
||||
// mockRepository implements storage.Repository for tests.
|
||||
type mockRepository struct {
|
||||
payouts *cardPayoutStore
|
||||
}
|
||||
|
||||
func newMockRepository() *mockRepository {
|
||||
return &mockRepository{
|
||||
payouts: newCardPayoutStore(),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *mockRepository) Payouts() storage.PayoutsStore {
|
||||
return r.payouts
|
||||
}
|
||||
|
||||
// cardPayoutStore implements storage.PayoutsStore for tests.
|
||||
type cardPayoutStore struct {
|
||||
mu sync.RWMutex
|
||||
data map[string]*model.CardPayout
|
||||
}
|
||||
|
||||
func payoutStoreKey(state *model.CardPayout) string {
|
||||
if state == nil {
|
||||
return ""
|
||||
}
|
||||
if ref := state.OperationRef; ref != "" {
|
||||
return ref
|
||||
}
|
||||
return state.PaymentRef
|
||||
}
|
||||
|
||||
func newCardPayoutStore() *cardPayoutStore {
|
||||
return &cardPayoutStore{
|
||||
data: make(map[string]*model.CardPayout),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *cardPayoutStore) FindByIdempotencyKey(_ context.Context, key string) (*model.CardPayout, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
for _, v := range s.data {
|
||||
if v.IdempotencyKey == key {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *cardPayoutStore) FindByOperationRef(_ context.Context, ref string) (*model.CardPayout, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
for _, v := range s.data {
|
||||
if v.OperationRef == ref {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *cardPayoutStore) FindByPaymentID(_ context.Context, id string) (*model.CardPayout, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
for _, v := range s.data {
|
||||
if v.PaymentRef == id {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *cardPayoutStore) Upsert(_ context.Context, record *model.CardPayout) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.data[payoutStoreKey(record)] = record
|
||||
return nil
|
||||
}
|
||||
|
||||
// Save is a helper for tests to pre-populate data.
|
||||
func (s *cardPayoutStore) Save(state *model.CardPayout) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.data[payoutStoreKey(state)] = state
|
||||
}
|
||||
|
||||
// Get is a helper for tests to retrieve data.
|
||||
func (s *cardPayoutStore) Get(id string) (*model.CardPayout, bool) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
if v, ok := s.data[id]; ok {
|
||||
return v, true
|
||||
}
|
||||
for _, v := range s.data {
|
||||
if v.PaymentRef == id || v.OperationRef == id {
|
||||
return v, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/internal/service/provider"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
|
||||
)
|
||||
|
||||
func validateCardPayoutRequest(req *mntxv1.CardPayoutRequest, cfg provider.Config) error {
|
||||
if req == nil {
|
||||
return newPayoutError("invalid_request", merrors.InvalidArgument("request cannot be empty"))
|
||||
}
|
||||
|
||||
if err := validateOperationIdentity(strings.TrimSpace(req.GetPayoutId()), strings.TrimSpace(req.GetOperationRef())); err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.TrimSpace(req.GetParentPaymentRef()) == "" {
|
||||
return newPayoutError("missing_parent_payment_ref", merrors.InvalidArgument("parent_payment_ref is required", "parent_payment_ref"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerId()) == "" {
|
||||
return newPayoutError("missing_customer_id", merrors.InvalidArgument("customer_id is required", "customer_id"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerFirstName()) == "" {
|
||||
return newPayoutError("missing_customer_first_name", merrors.InvalidArgument("customer_first_name is required", "customer_first_name"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerLastName()) == "" {
|
||||
return newPayoutError("missing_customer_last_name", merrors.InvalidArgument("customer_last_name is required", "customer_last_name"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerIp()) == "" {
|
||||
return newPayoutError("missing_customer_ip", merrors.InvalidArgument("customer_ip is required", "customer_ip"))
|
||||
}
|
||||
|
||||
if req.GetAmountMinor() <= 0 {
|
||||
return newPayoutError("invalid_amount", merrors.InvalidArgument("amount_minor must be positive", "amount_minor"))
|
||||
}
|
||||
|
||||
currency := strings.ToUpper(strings.TrimSpace(req.GetCurrency()))
|
||||
if currency == "" {
|
||||
return newPayoutError("missing_currency", merrors.InvalidArgument("currency is required", "currency"))
|
||||
}
|
||||
if !cfg.CurrencyAllowed(currency) {
|
||||
return newPayoutError("unsupported_currency", merrors.InvalidArgument("currency is not allowed for this project", "currency"))
|
||||
}
|
||||
|
||||
pan := strings.TrimSpace(req.GetCardPan())
|
||||
if pan == "" {
|
||||
return newPayoutError("missing_card_pan", merrors.InvalidArgument("card_pan is required", "card_pan"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCardHolder()) == "" {
|
||||
return newPayoutError("missing_card_holder", merrors.InvalidArgument("card_holder is required", "card_holder"))
|
||||
}
|
||||
if err := validateCardExpiryFields(req.GetCardExpMonth(), req.GetCardExpYear()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if cfg.RequireCustomerAddress {
|
||||
if strings.TrimSpace(req.GetCustomerCountry()) == "" {
|
||||
return newPayoutError("missing_customer_country", merrors.InvalidArgument("customer_country is required", "customer_country"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerCity()) == "" {
|
||||
return newPayoutError("missing_customer_city", merrors.InvalidArgument("customer_city is required", "customer_city"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerAddress()) == "" {
|
||||
return newPayoutError("missing_customer_address", merrors.InvalidArgument("customer_address is required", "customer_address"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerZip()) == "" {
|
||||
return newPayoutError("missing_customer_zip", merrors.InvalidArgument("customer_zip is required", "customer_zip"))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateCardExpiryFields(month uint32, year uint32) error {
|
||||
if month == 0 || month > 12 {
|
||||
return newPayoutError("invalid_expiry_month", merrors.InvalidArgument("card_exp_month must be between 1 and 12", "card_exp_month"))
|
||||
}
|
||||
yearStr := strconv.Itoa(int(year))
|
||||
if len(yearStr) < 2 || year == 0 {
|
||||
return newPayoutError("invalid_expiry_year", merrors.InvalidArgument("card_exp_year must be provided", "card_exp_year"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateOperationIdentity(payoutID, operationRef string) error {
|
||||
payoutID = strings.TrimSpace(payoutID)
|
||||
operationRef = strings.TrimSpace(operationRef)
|
||||
switch {
|
||||
case payoutID == "" && operationRef == "":
|
||||
return newPayoutError("missing_operation_ref", merrors.InvalidArgument("operation_ref or payout_id is required", "operation_ref"))
|
||||
case payoutID != "" && operationRef != "":
|
||||
return newPayoutError("ambiguous_operation_ref", merrors.InvalidArgument("provide either operation_ref or payout_id, not both"))
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/internal/service/provider"
|
||||
mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
|
||||
)
|
||||
|
||||
func TestValidateCardPayoutRequest_Valid(t *testing.T) {
|
||||
cfg := testProviderConfig()
|
||||
req := validCardPayoutRequest()
|
||||
if err := validateCardPayoutRequest(req, cfg); err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateCardPayoutRequest_Errors(t *testing.T) {
|
||||
baseCfg := testProviderConfig()
|
||||
cases := []struct {
|
||||
name string
|
||||
mutate func(*mntxv1.CardPayoutRequest)
|
||||
config func(provider.Config) provider.Config
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "missing_operation_identity",
|
||||
mutate: func(r *mntxv1.CardPayoutRequest) { r.PayoutId = "" },
|
||||
expected: "missing_operation_ref",
|
||||
},
|
||||
{
|
||||
name: "missing_parent_payment_ref",
|
||||
mutate: func(r *mntxv1.CardPayoutRequest) { r.ParentPaymentRef = "" },
|
||||
expected: "missing_parent_payment_ref",
|
||||
},
|
||||
{
|
||||
name: "both_operation_and_payout_identity",
|
||||
mutate: func(r *mntxv1.CardPayoutRequest) {
|
||||
r.PayoutId = "parent-1"
|
||||
r.OperationRef = "parent-1:hop_1_card_payout_send"
|
||||
},
|
||||
expected: "ambiguous_operation_ref",
|
||||
},
|
||||
{
|
||||
name: "missing_customer_id",
|
||||
mutate: func(r *mntxv1.CardPayoutRequest) { r.CustomerId = "" },
|
||||
expected: "missing_customer_id",
|
||||
},
|
||||
{
|
||||
name: "missing_customer_ip",
|
||||
mutate: func(r *mntxv1.CardPayoutRequest) { r.CustomerIp = "" },
|
||||
expected: "missing_customer_ip",
|
||||
},
|
||||
{
|
||||
name: "invalid_amount",
|
||||
mutate: func(r *mntxv1.CardPayoutRequest) { r.AmountMinor = 0 },
|
||||
expected: "invalid_amount",
|
||||
},
|
||||
{
|
||||
name: "missing_currency",
|
||||
mutate: func(r *mntxv1.CardPayoutRequest) { r.Currency = "" },
|
||||
expected: "missing_currency",
|
||||
},
|
||||
{
|
||||
name: "unsupported_currency",
|
||||
mutate: func(r *mntxv1.CardPayoutRequest) { r.Currency = "EUR" },
|
||||
config: func(cfg provider.Config) provider.Config {
|
||||
cfg.AllowedCurrencies = []string{"USD"}
|
||||
return cfg
|
||||
},
|
||||
expected: "unsupported_currency",
|
||||
},
|
||||
{
|
||||
name: "missing_card_pan",
|
||||
mutate: func(r *mntxv1.CardPayoutRequest) { r.CardPan = "" },
|
||||
expected: "missing_card_pan",
|
||||
},
|
||||
{
|
||||
name: "missing_card_holder",
|
||||
mutate: func(r *mntxv1.CardPayoutRequest) { r.CardHolder = "" },
|
||||
expected: "missing_card_holder",
|
||||
},
|
||||
{
|
||||
name: "invalid_expiry_month",
|
||||
mutate: func(r *mntxv1.CardPayoutRequest) { r.CardExpMonth = 13 },
|
||||
expected: "invalid_expiry_month",
|
||||
},
|
||||
{
|
||||
name: "invalid_expiry_year",
|
||||
mutate: func(r *mntxv1.CardPayoutRequest) { r.CardExpYear = 0 },
|
||||
expected: "invalid_expiry_year",
|
||||
},
|
||||
{
|
||||
name: "missing_customer_country_when_required",
|
||||
mutate: func(r *mntxv1.CardPayoutRequest) { r.CustomerCountry = "" },
|
||||
config: func(cfg provider.Config) provider.Config {
|
||||
cfg.RequireCustomerAddress = true
|
||||
return cfg
|
||||
},
|
||||
expected: "missing_customer_country",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
req := validCardPayoutRequest()
|
||||
tc.mutate(req)
|
||||
cfg := baseCfg
|
||||
if tc.config != nil {
|
||||
cfg = tc.config(cfg)
|
||||
}
|
||||
err := validateCardPayoutRequest(req, cfg)
|
||||
requireReason(t, err, tc.expected)
|
||||
})
|
||||
}
|
||||
}
|
||||
1580
api/gateway/aurora/internal/service/gateway/card_processor.go
Normal file
1580
api/gateway/aurora/internal/service/gateway/card_processor.go
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,763 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/internal/service/provider"
|
||||
"github.com/tech/sendico/gateway/aurora/storage/model"
|
||||
clockpkg "github.com/tech/sendico/pkg/clock"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
gatewayv1 "github.com/tech/sendico/pkg/proto/common/gateway/v1"
|
||||
mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type roundTripperFunc func(*http.Request) (*http.Response, error)
|
||||
|
||||
func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
return f(r)
|
||||
}
|
||||
|
||||
type staticClock struct {
|
||||
now time.Time
|
||||
}
|
||||
|
||||
func (s staticClock) Now() time.Time {
|
||||
return s.now
|
||||
}
|
||||
|
||||
type apiResponse struct {
|
||||
RequestID string `json:"request_id"`
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message"`
|
||||
Code string `json:"code"`
|
||||
Operation struct {
|
||||
RequestID string `json:"request_id"`
|
||||
Status string `json:"status"`
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
} `json:"operation"`
|
||||
}
|
||||
|
||||
func TestCardPayoutProcessor_Submit_Success(t *testing.T) {
|
||||
cfg := provider.Config{
|
||||
BaseURL: "https://provider.test",
|
||||
SecretKey: "secret",
|
||||
ProjectID: 99,
|
||||
AllowedCurrencies: []string{"RUB"},
|
||||
}
|
||||
|
||||
existingCreated := time.Date(2020, 2, 3, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
repo := newMockRepository()
|
||||
repo.payouts.Save(&model.CardPayout{
|
||||
PaymentRef: "payment-parent-1",
|
||||
OperationRef: "payout-1",
|
||||
CreatedAt: existingCreated,
|
||||
})
|
||||
|
||||
httpClient := &http.Client{
|
||||
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
||||
resp := apiResponse{}
|
||||
resp.Operation.RequestID = "req-123"
|
||||
body, _ := json.Marshal(resp)
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: io.NopCloser(bytes.NewReader(body)),
|
||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
now := time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)
|
||||
processor := newCardPayoutProcessor(zap.NewNop(), cfg, staticClock{now: now}, repo, httpClient, nil)
|
||||
|
||||
req := validCardPayoutRequest()
|
||||
req.ProjectId = 0
|
||||
|
||||
resp, err := processor.Submit(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
if !resp.GetAccepted() {
|
||||
t.Fatalf("expected accepted payout response")
|
||||
}
|
||||
if resp.GetPayout().GetProjectId() != cfg.ProjectID {
|
||||
t.Fatalf("expected project id %d, got %d", cfg.ProjectID, resp.GetPayout().GetProjectId())
|
||||
}
|
||||
if resp.GetPayout().GetStatus() != mntxv1.PayoutStatus_PAYOUT_STATUS_WAITING {
|
||||
t.Fatalf("expected waiting status, got %v", resp.GetPayout().GetStatus())
|
||||
}
|
||||
if !resp.GetPayout().GetCreatedAt().AsTime().Equal(existingCreated) {
|
||||
t.Fatalf("expected created_at preserved, got %v", resp.GetPayout().GetCreatedAt().AsTime())
|
||||
}
|
||||
|
||||
stored, ok := repo.payouts.Get(req.GetPayoutId())
|
||||
if !ok || stored == nil {
|
||||
t.Fatalf("expected payout state stored")
|
||||
}
|
||||
if stored.ProviderPaymentID == "" {
|
||||
t.Fatalf("expected provider payment id")
|
||||
}
|
||||
if !stored.CreatedAt.Equal(existingCreated) {
|
||||
t.Fatalf("expected created_at preserved in model, got %v", stored.CreatedAt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardPayoutProcessor_Submit_MissingConfig(t *testing.T) {
|
||||
cfg := provider.Config{
|
||||
AllowedCurrencies: []string{"RUB"},
|
||||
}
|
||||
|
||||
processor := newCardPayoutProcessor(
|
||||
zap.NewNop(),
|
||||
cfg,
|
||||
clockpkg.NewSystem(),
|
||||
newMockRepository(),
|
||||
&http.Client{},
|
||||
nil,
|
||||
)
|
||||
|
||||
_, err := processor.Submit(context.Background(), validCardPayoutRequest())
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
if !errors.Is(err, merrors.ErrInternal) {
|
||||
t.Fatalf("expected internal error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardPayoutProcessor_Submit_RejectsAmountBelowConfiguredMinimum(t *testing.T) {
|
||||
cfg := provider.Config{
|
||||
BaseURL: "https://provider.test",
|
||||
SecretKey: "secret",
|
||||
AllowedCurrencies: []string{"RUB"},
|
||||
}
|
||||
|
||||
repo := newMockRepository()
|
||||
processor := newCardPayoutProcessor(
|
||||
zap.NewNop(),
|
||||
cfg,
|
||||
staticClock{now: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)},
|
||||
repo,
|
||||
&http.Client{},
|
||||
nil,
|
||||
)
|
||||
processor.applyGatewayDescriptor(&gatewayv1.GatewayInstanceDescriptor{
|
||||
Limits: &gatewayv1.Limits{
|
||||
PerTxMinAmount: "20.00",
|
||||
},
|
||||
})
|
||||
|
||||
req := validCardPayoutRequest() // 15.00 RUB
|
||||
_, err := processor.Submit(context.Background(), req)
|
||||
requireReason(t, err, "amount_below_minimum")
|
||||
}
|
||||
|
||||
func TestCardPayoutProcessor_SubmitToken_RejectsAmountBelowCurrencyMinimum(t *testing.T) {
|
||||
cfg := provider.Config{
|
||||
BaseURL: "https://provider.test",
|
||||
SecretKey: "secret",
|
||||
AllowedCurrencies: []string{"USD"},
|
||||
}
|
||||
|
||||
repo := newMockRepository()
|
||||
processor := newCardPayoutProcessor(
|
||||
zap.NewNop(),
|
||||
cfg,
|
||||
staticClock{now: time.Date(2024, 1, 1, 12, 0, 0, 0, time.UTC)},
|
||||
repo,
|
||||
&http.Client{},
|
||||
nil,
|
||||
)
|
||||
processor.applyGatewayDescriptor(&gatewayv1.GatewayInstanceDescriptor{
|
||||
Limits: &gatewayv1.Limits{
|
||||
PerTxMinAmount: "20.00",
|
||||
CurrencyLimits: map[string]*gatewayv1.LimitsOverride{
|
||||
"USD": {MinAmount: "30.00"},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
req := validCardTokenPayoutRequest() // 25.00 USD
|
||||
_, err := processor.SubmitToken(context.Background(), req)
|
||||
requireReason(t, err, "amount_below_minimum")
|
||||
}
|
||||
|
||||
func TestCardPayoutProcessor_ProcessCallback(t *testing.T) {
|
||||
cfg := provider.Config{
|
||||
SecretKey: "secret",
|
||||
StatusSuccess: "success",
|
||||
StatusProcessing: "processing",
|
||||
AllowedCurrencies: []string{"RUB"},
|
||||
}
|
||||
|
||||
repo := newMockRepository()
|
||||
|
||||
processor := newCardPayoutProcessor(
|
||||
zap.NewNop(),
|
||||
cfg,
|
||||
staticClock{now: time.Date(2024, 1, 2, 3, 4, 5, 0, time.UTC)},
|
||||
repo,
|
||||
&http.Client{},
|
||||
nil,
|
||||
)
|
||||
|
||||
cb := baseCallback()
|
||||
cb.Payment.Sum.Currency = "RUB"
|
||||
|
||||
sig, err := provider.SignPayload(cb, cfg.SecretKey)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to sign callback: %v", err)
|
||||
}
|
||||
cb.Signature = sig
|
||||
|
||||
payload, err := json.Marshal(cb)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal callback: %v", err)
|
||||
}
|
||||
|
||||
status, err := processor.ProcessCallback(context.Background(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
if status != http.StatusOK {
|
||||
t.Fatalf("expected status ok, got %d", status)
|
||||
}
|
||||
|
||||
state, ok := repo.payouts.Get(cb.Payment.ID)
|
||||
if !ok || state == nil {
|
||||
t.Fatalf("expected payout state stored")
|
||||
}
|
||||
|
||||
if state.Status != model.PayoutStatusSuccess {
|
||||
t.Fatalf("expected success status in model, got %v", state.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardPayoutProcessor_Submit_SameParentDifferentOperationsStoredSeparately(t *testing.T) {
|
||||
cfg := provider.Config{
|
||||
BaseURL: "https://provider.test",
|
||||
SecretKey: "secret",
|
||||
ProjectID: 99,
|
||||
AllowedCurrencies: []string{"RUB"},
|
||||
}
|
||||
|
||||
repo := newMockRepository()
|
||||
var callN int
|
||||
httpClient := &http.Client{
|
||||
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
||||
callN++
|
||||
resp := apiResponse{}
|
||||
resp.Operation.RequestID = fmt.Sprintf("req-%d", callN)
|
||||
body, _ := json.Marshal(resp)
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: io.NopCloser(bytes.NewReader(body)),
|
||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
processor := newCardPayoutProcessor(
|
||||
zap.NewNop(),
|
||||
cfg,
|
||||
staticClock{now: time.Date(2026, 3, 4, 1, 2, 3, 0, time.UTC)},
|
||||
repo,
|
||||
httpClient,
|
||||
nil,
|
||||
)
|
||||
|
||||
parentPaymentRef := "payment-parent-1"
|
||||
op1 := parentPaymentRef + ":hop_4_card_payout_send"
|
||||
op2 := parentPaymentRef + ":hop_4_card_payout_send_2"
|
||||
|
||||
req1 := validCardPayoutRequest()
|
||||
req1.PayoutId = ""
|
||||
req1.OperationRef = op1
|
||||
req1.IdempotencyKey = "idem-1"
|
||||
req1.ParentPaymentRef = parentPaymentRef
|
||||
req1.CardPan = "2204310000002456"
|
||||
|
||||
req2 := validCardPayoutRequest()
|
||||
req2.PayoutId = ""
|
||||
req2.OperationRef = op2
|
||||
req2.IdempotencyKey = "idem-2"
|
||||
req2.ParentPaymentRef = parentPaymentRef
|
||||
req2.CardPan = "2204320000009754"
|
||||
|
||||
if _, err := processor.Submit(context.Background(), req1); err != nil {
|
||||
t.Fatalf("first submit failed: %v", err)
|
||||
}
|
||||
if _, err := processor.Submit(context.Background(), req2); err != nil {
|
||||
t.Fatalf("second submit failed: %v", err)
|
||||
}
|
||||
|
||||
first, err := repo.payouts.FindByOperationRef(context.Background(), op1)
|
||||
if err != nil || first == nil {
|
||||
t.Fatalf("expected first operation stored, err=%v", err)
|
||||
}
|
||||
second, err := repo.payouts.FindByOperationRef(context.Background(), op2)
|
||||
if err != nil || second == nil {
|
||||
t.Fatalf("expected second operation stored, err=%v", err)
|
||||
}
|
||||
|
||||
if got, want := first.PaymentRef, parentPaymentRef; got != want {
|
||||
t.Fatalf("first parent payment ref mismatch: got=%q want=%q", got, want)
|
||||
}
|
||||
if got, want := second.PaymentRef, parentPaymentRef; got != want {
|
||||
t.Fatalf("second parent payment ref mismatch: got=%q want=%q", got, want)
|
||||
}
|
||||
if got, want := first.OperationRef, op1; got != want {
|
||||
t.Fatalf("first operation ref mismatch: got=%q want=%q", got, want)
|
||||
}
|
||||
if got, want := second.OperationRef, op2; got != want {
|
||||
t.Fatalf("second operation ref mismatch: got=%q want=%q", got, want)
|
||||
}
|
||||
if first.ProviderPaymentID == "" || second.ProviderPaymentID == "" {
|
||||
t.Fatalf("expected provider payment ids for both operations")
|
||||
}
|
||||
if first.ProviderPaymentID == second.ProviderPaymentID {
|
||||
t.Fatalf("expected different provider payment ids, got=%q", first.ProviderPaymentID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardPayoutProcessor_StrictMode_BlocksSecondOperationUntilFirstFinalCallback(t *testing.T) {
|
||||
t.Skip("aurora simulator has no external provider transport call counting")
|
||||
|
||||
cfg := provider.Config{
|
||||
BaseURL: "https://provider.test",
|
||||
SecretKey: "secret",
|
||||
ProjectID: 99,
|
||||
StatusSuccess: "success",
|
||||
StatusProcessing: "processing",
|
||||
AllowedCurrencies: []string{"RUB"},
|
||||
}
|
||||
|
||||
repo := newMockRepository()
|
||||
var callN atomic.Int32
|
||||
httpClient := &http.Client{
|
||||
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
||||
n := callN.Add(1)
|
||||
resp := apiResponse{}
|
||||
resp.Operation.RequestID = fmt.Sprintf("req-%d", n)
|
||||
body, _ := json.Marshal(resp)
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: io.NopCloser(bytes.NewReader(body)),
|
||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
processor := newCardPayoutProcessor(
|
||||
zap.NewNop(),
|
||||
cfg,
|
||||
staticClock{now: time.Date(2026, 3, 4, 2, 3, 4, 0, time.UTC)},
|
||||
repo,
|
||||
httpClient,
|
||||
nil,
|
||||
)
|
||||
defer processor.stopRetries()
|
||||
processor.dispatchThrottleInterval = 0
|
||||
processor.setExecutionMode(newStrictIsolatedPayoutExecutionMode())
|
||||
|
||||
req1 := validCardPayoutRequest()
|
||||
req1.PayoutId = ""
|
||||
req1.OperationRef = "op-strict-1"
|
||||
req1.ParentPaymentRef = "payment-strict-1"
|
||||
req1.IdempotencyKey = "idem-strict-1"
|
||||
req1.CardPan = "2204310000002456"
|
||||
|
||||
req2 := validCardPayoutRequest()
|
||||
req2.PayoutId = ""
|
||||
req2.OperationRef = "op-strict-2"
|
||||
req2.ParentPaymentRef = "payment-strict-2"
|
||||
req2.IdempotencyKey = "idem-strict-2"
|
||||
req2.CardPan = "2204320000009754"
|
||||
|
||||
if _, err := processor.Submit(context.Background(), req1); err != nil {
|
||||
t.Fatalf("first submit failed: %v", err)
|
||||
}
|
||||
|
||||
secondDone := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := processor.Submit(context.Background(), req2)
|
||||
secondDone <- err
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-secondDone:
|
||||
t.Fatalf("second submit should block before first operation is final, err=%v", err)
|
||||
case <-time.After(120 * time.Millisecond):
|
||||
}
|
||||
|
||||
cb := baseCallback()
|
||||
cb.Payment.ID = req1.GetOperationRef()
|
||||
cb.Payment.Status = "success"
|
||||
cb.Operation.Status = "success"
|
||||
cb.Operation.Code = "0"
|
||||
cb.Operation.Message = "Success"
|
||||
cb.Payment.Sum.Currency = "RUB"
|
||||
|
||||
sig, err := provider.SignPayload(cb, cfg.SecretKey)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to sign callback: %v", err)
|
||||
}
|
||||
cb.Signature = sig
|
||||
payload, err := json.Marshal(cb)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal callback: %v", err)
|
||||
}
|
||||
|
||||
status, err := processor.ProcessCallback(context.Background(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("callback failed: %v", err)
|
||||
}
|
||||
if status != http.StatusOK {
|
||||
t.Fatalf("unexpected callback status: %d", status)
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-secondDone:
|
||||
if err != nil {
|
||||
t.Fatalf("second submit returned error: %v", err)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatalf("timeout waiting for second submit to unblock")
|
||||
}
|
||||
|
||||
if got, want := callN.Load(), int32(2); got != want {
|
||||
t.Fatalf("unexpected provider call count: got=%d want=%d", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardPayoutProcessor_ProcessCallback_UpdatesMatchingOperationWithinSameParent(t *testing.T) {
|
||||
cfg := provider.Config{
|
||||
SecretKey: "secret",
|
||||
StatusSuccess: "success",
|
||||
StatusProcessing: "processing",
|
||||
AllowedCurrencies: []string{"RUB"},
|
||||
}
|
||||
|
||||
parentPaymentRef := "payment-parent-1"
|
||||
op1 := parentPaymentRef + ":hop_4_card_payout_send"
|
||||
op2 := parentPaymentRef + ":hop_4_card_payout_send_2"
|
||||
now := time.Date(2026, 3, 4, 2, 3, 4, 0, time.UTC)
|
||||
|
||||
repo := newMockRepository()
|
||||
repo.payouts.Save(&model.CardPayout{
|
||||
PaymentRef: parentPaymentRef,
|
||||
OperationRef: op1,
|
||||
Status: model.PayoutStatusWaiting,
|
||||
CreatedAt: now.Add(-time.Minute),
|
||||
UpdatedAt: now.Add(-time.Minute),
|
||||
})
|
||||
repo.payouts.Save(&model.CardPayout{
|
||||
PaymentRef: parentPaymentRef,
|
||||
OperationRef: op2,
|
||||
Status: model.PayoutStatusWaiting,
|
||||
CreatedAt: now.Add(-time.Minute),
|
||||
UpdatedAt: now.Add(-time.Minute),
|
||||
})
|
||||
|
||||
processor := newCardPayoutProcessor(
|
||||
zap.NewNop(),
|
||||
cfg,
|
||||
staticClock{now: now},
|
||||
repo,
|
||||
&http.Client{},
|
||||
nil,
|
||||
)
|
||||
|
||||
cb := baseCallback()
|
||||
cb.Payment.ID = op2
|
||||
cb.Payment.Status = "success"
|
||||
cb.Operation.Status = "success"
|
||||
cb.Operation.Code = "0"
|
||||
cb.Operation.Provider.PaymentID = "provider-op-2"
|
||||
cb.Payment.Sum.Currency = "RUB"
|
||||
|
||||
sig, err := provider.SignPayload(cb, cfg.SecretKey)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to sign callback: %v", err)
|
||||
}
|
||||
cb.Signature = sig
|
||||
payload, err := json.Marshal(cb)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal callback: %v", err)
|
||||
}
|
||||
|
||||
status, err := processor.ProcessCallback(context.Background(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
if status != http.StatusOK {
|
||||
t.Fatalf("expected status ok, got %d", status)
|
||||
}
|
||||
|
||||
first, err := repo.payouts.FindByOperationRef(context.Background(), op1)
|
||||
if err != nil || first == nil {
|
||||
t.Fatalf("expected first operation present, err=%v", err)
|
||||
}
|
||||
second, err := repo.payouts.FindByOperationRef(context.Background(), op2)
|
||||
if err != nil || second == nil {
|
||||
t.Fatalf("expected second operation present, err=%v", err)
|
||||
}
|
||||
|
||||
if got, want := first.Status, model.PayoutStatusWaiting; got != want {
|
||||
t.Fatalf("first operation status mismatch: got=%v want=%v", got, want)
|
||||
}
|
||||
if got, want := second.Status, model.PayoutStatusSuccess; got != want {
|
||||
t.Fatalf("second operation status mismatch: got=%v want=%v", got, want)
|
||||
}
|
||||
if got, want := second.PaymentRef, parentPaymentRef; got != want {
|
||||
t.Fatalf("second parent payment ref mismatch: got=%q want=%q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardPayoutProcessor_Submit_RetriesProviderLimitDeclineUntilSuccess(t *testing.T) {
|
||||
t.Skip("aurora simulator uses deterministic scenario dispatch instead of mocked provider HTTP")
|
||||
|
||||
cfg := provider.Config{
|
||||
BaseURL: "https://provider.test",
|
||||
SecretKey: "secret",
|
||||
ProjectID: 99,
|
||||
AllowedCurrencies: []string{"RUB"},
|
||||
}
|
||||
|
||||
repo := newMockRepository()
|
||||
var calls atomic.Int32
|
||||
httpClient := &http.Client{
|
||||
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
||||
n := calls.Add(1)
|
||||
resp := apiResponse{}
|
||||
if n == 1 {
|
||||
resp.Code = providerCodeDeclineAmountOrFrequencyLimit
|
||||
resp.Message = "Decline due to amount or frequency limit"
|
||||
body, _ := json.Marshal(resp)
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusTooManyRequests,
|
||||
Body: io.NopCloser(bytes.NewReader(body)),
|
||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||
}, nil
|
||||
}
|
||||
resp.Operation.RequestID = "req-retry-success"
|
||||
body, _ := json.Marshal(resp)
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: io.NopCloser(bytes.NewReader(body)),
|
||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
processor := newCardPayoutProcessor(
|
||||
zap.NewNop(),
|
||||
cfg,
|
||||
staticClock{now: time.Date(2026, 3, 4, 1, 2, 3, 0, time.UTC)},
|
||||
repo,
|
||||
httpClient,
|
||||
nil,
|
||||
)
|
||||
defer processor.stopRetries()
|
||||
processor.dispatchThrottleInterval = 0
|
||||
processor.retryDelayFn = func(uint32) time.Duration { return 10 * time.Millisecond }
|
||||
|
||||
req := validCardPayoutRequest()
|
||||
resp, err := processor.Submit(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatalf("submit returned error: %v", err)
|
||||
}
|
||||
if !resp.GetAccepted() {
|
||||
t.Fatalf("expected accepted response when retry is scheduled")
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for {
|
||||
state, ok := repo.payouts.Get(req.GetPayoutId())
|
||||
if ok && state != nil && state.Status == model.PayoutStatusWaiting && state.ProviderPaymentID == "req-retry-success" {
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("timeout waiting for successful retry result")
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
if got, want := calls.Load(), int32(2); got != want {
|
||||
t.Fatalf("unexpected provider call count: got=%d want=%d", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardPayoutProcessor_Submit_RetriesProviderLimitDeclineThenFails(t *testing.T) {
|
||||
t.Skip("aurora simulator uses deterministic scenario dispatch instead of mocked provider HTTP")
|
||||
|
||||
cfg := provider.Config{
|
||||
BaseURL: "https://provider.test",
|
||||
SecretKey: "secret",
|
||||
ProjectID: 99,
|
||||
AllowedCurrencies: []string{"RUB"},
|
||||
}
|
||||
|
||||
repo := newMockRepository()
|
||||
var calls atomic.Int32
|
||||
httpClient := &http.Client{
|
||||
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
||||
_ = calls.Add(1)
|
||||
resp := apiResponse{
|
||||
Code: providerCodeDeclineAmountOrFrequencyLimit,
|
||||
Message: "Decline due to amount or frequency limit",
|
||||
}
|
||||
body, _ := json.Marshal(resp)
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusTooManyRequests,
|
||||
Body: io.NopCloser(bytes.NewReader(body)),
|
||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
processor := newCardPayoutProcessor(
|
||||
zap.NewNop(),
|
||||
cfg,
|
||||
staticClock{now: time.Date(2026, 3, 4, 1, 2, 3, 0, time.UTC)},
|
||||
repo,
|
||||
httpClient,
|
||||
nil,
|
||||
)
|
||||
defer processor.stopRetries()
|
||||
processor.dispatchThrottleInterval = 0
|
||||
processor.retryDelayFn = func(uint32) time.Duration { return time.Millisecond }
|
||||
|
||||
req := validCardPayoutRequest()
|
||||
resp, err := processor.Submit(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatalf("submit returned error: %v", err)
|
||||
}
|
||||
if !resp.GetAccepted() {
|
||||
t.Fatalf("expected accepted response when retry is scheduled")
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for {
|
||||
state, ok := repo.payouts.Get(req.GetPayoutId())
|
||||
if ok && state != nil && state.Status == model.PayoutStatusFailed {
|
||||
if !strings.Contains(state.FailureReason, providerCodeDeclineAmountOrFrequencyLimit) {
|
||||
t.Fatalf("expected failure reason to include provider code, got=%q", state.FailureReason)
|
||||
}
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("timeout waiting for terminal failed status")
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
if got, want := calls.Load(), int32(defaultMaxDispatchAttempts); got != want {
|
||||
t.Fatalf("unexpected provider call count: got=%d want=%d", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardPayoutProcessor_ProcessCallback_RetryableDeclineSchedulesRetry(t *testing.T) {
|
||||
t.Skip("aurora simulator does not run provider HTTP retry flow used by legacy transport tests")
|
||||
|
||||
cfg := provider.Config{
|
||||
BaseURL: "https://provider.test",
|
||||
SecretKey: "secret",
|
||||
ProjectID: 99,
|
||||
StatusSuccess: "success",
|
||||
StatusProcessing: "processing",
|
||||
AllowedCurrencies: []string{"RUB"},
|
||||
}
|
||||
|
||||
repo := newMockRepository()
|
||||
var calls atomic.Int32
|
||||
httpClient := &http.Client{
|
||||
Transport: roundTripperFunc(func(r *http.Request) (*http.Response, error) {
|
||||
n := calls.Add(1)
|
||||
resp := apiResponse{}
|
||||
if n == 1 {
|
||||
resp.Operation.RequestID = "req-initial"
|
||||
} else {
|
||||
resp.Operation.RequestID = "req-after-callback-retry"
|
||||
}
|
||||
body, _ := json.Marshal(resp)
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: io.NopCloser(bytes.NewReader(body)),
|
||||
Header: http.Header{"Content-Type": []string{"application/json"}},
|
||||
}, nil
|
||||
}),
|
||||
}
|
||||
|
||||
processor := newCardPayoutProcessor(
|
||||
zap.NewNop(),
|
||||
cfg,
|
||||
staticClock{now: time.Date(2026, 3, 4, 2, 0, 0, 0, time.UTC)},
|
||||
repo,
|
||||
httpClient,
|
||||
nil,
|
||||
)
|
||||
defer processor.stopRetries()
|
||||
processor.dispatchThrottleInterval = 0
|
||||
processor.retryDelayFn = func(uint32) time.Duration { return 5 * time.Millisecond }
|
||||
|
||||
req := validCardPayoutRequest()
|
||||
resp, err := processor.Submit(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatalf("submit returned error: %v", err)
|
||||
}
|
||||
if !resp.GetAccepted() {
|
||||
t.Fatalf("expected accepted submit response")
|
||||
}
|
||||
|
||||
cb := baseCallback()
|
||||
cb.Payment.ID = req.GetPayoutId()
|
||||
cb.Payment.Status = "failed"
|
||||
cb.Operation.Status = "failed"
|
||||
cb.Operation.Code = providerCodeDeclineAmountOrFrequencyLimit
|
||||
cb.Operation.Message = "Decline due to amount or frequency limit"
|
||||
cb.Payment.Sum.Currency = "RUB"
|
||||
|
||||
sig, err := provider.SignPayload(cb, cfg.SecretKey)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to sign callback: %v", err)
|
||||
}
|
||||
cb.Signature = sig
|
||||
payload, err := json.Marshal(cb)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to marshal callback: %v", err)
|
||||
}
|
||||
|
||||
status, err := processor.ProcessCallback(context.Background(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("process callback returned error: %v", err)
|
||||
}
|
||||
if status != http.StatusOK {
|
||||
t.Fatalf("unexpected callback status: %d", status)
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(2 * time.Second)
|
||||
for {
|
||||
state, ok := repo.payouts.Get(req.GetPayoutId())
|
||||
if ok && state != nil && state.Status == model.PayoutStatusWaiting && state.ProviderPaymentID == "req-after-callback-retry" {
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("timeout waiting for callback-scheduled retry result")
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
|
||||
if got, want := calls.Load(), int32(2); got != want {
|
||||
t.Fatalf("unexpected provider call count: got=%d want=%d", got, want)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/internal/service/provider"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
|
||||
)
|
||||
|
||||
func validateCardTokenPayoutRequest(req *mntxv1.CardTokenPayoutRequest, cfg provider.Config) error {
|
||||
if req == nil {
|
||||
return newPayoutError("invalid_request", merrors.InvalidArgument("request cannot be empty"))
|
||||
}
|
||||
|
||||
if err := validateOperationIdentity(strings.TrimSpace(req.GetPayoutId()), strings.TrimSpace(req.GetOperationRef())); err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.TrimSpace(req.GetParentPaymentRef()) == "" {
|
||||
return newPayoutError("missing_parent_payment_ref", merrors.InvalidArgument("parent_payment_ref is required", "parent_payment_ref"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerId()) == "" {
|
||||
return newPayoutError("missing_customer_id", merrors.InvalidArgument("customer_id is required", "customer_id"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerFirstName()) == "" {
|
||||
return newPayoutError("missing_customer_first_name", merrors.InvalidArgument("customer_first_name is required", "customer_first_name"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerLastName()) == "" {
|
||||
return newPayoutError("missing_customer_last_name", merrors.InvalidArgument("customer_last_name is required", "customer_last_name"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerIp()) == "" {
|
||||
return newPayoutError("missing_customer_ip", merrors.InvalidArgument("customer_ip is required", "customer_ip"))
|
||||
}
|
||||
if req.GetAmountMinor() <= 0 {
|
||||
return newPayoutError("invalid_amount", merrors.InvalidArgument("amount_minor must be positive", "amount_minor"))
|
||||
}
|
||||
|
||||
currency := strings.ToUpper(strings.TrimSpace(req.GetCurrency()))
|
||||
if currency == "" {
|
||||
return newPayoutError("missing_currency", merrors.InvalidArgument("currency is required", "currency"))
|
||||
}
|
||||
if !cfg.CurrencyAllowed(currency) {
|
||||
return newPayoutError("unsupported_currency", merrors.InvalidArgument("currency is not allowed for this project", "currency"))
|
||||
}
|
||||
|
||||
if strings.TrimSpace(req.GetCardToken()) == "" {
|
||||
return newPayoutError("missing_card_token", merrors.InvalidArgument("card_token is required", "card_token"))
|
||||
}
|
||||
|
||||
if cfg.RequireCustomerAddress {
|
||||
if strings.TrimSpace(req.GetCustomerCountry()) == "" {
|
||||
return newPayoutError("missing_customer_country", merrors.InvalidArgument("customer_country is required", "customer_country"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerCity()) == "" {
|
||||
return newPayoutError("missing_customer_city", merrors.InvalidArgument("customer_city is required", "customer_city"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerAddress()) == "" {
|
||||
return newPayoutError("missing_customer_address", merrors.InvalidArgument("customer_address is required", "customer_address"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerZip()) == "" {
|
||||
return newPayoutError("missing_customer_zip", merrors.InvalidArgument("customer_zip is required", "customer_zip"))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/internal/service/provider"
|
||||
mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
|
||||
)
|
||||
|
||||
func TestValidateCardTokenPayoutRequest_Valid(t *testing.T) {
|
||||
cfg := testProviderConfig()
|
||||
req := validCardTokenPayoutRequest()
|
||||
if err := validateCardTokenPayoutRequest(req, cfg); err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateCardTokenPayoutRequest_Errors(t *testing.T) {
|
||||
baseCfg := testProviderConfig()
|
||||
cases := []struct {
|
||||
name string
|
||||
mutate func(*mntxv1.CardTokenPayoutRequest)
|
||||
config func(provider.Config) provider.Config
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "missing_operation_identity",
|
||||
mutate: func(r *mntxv1.CardTokenPayoutRequest) { r.PayoutId = "" },
|
||||
expected: "missing_operation_ref",
|
||||
},
|
||||
{
|
||||
name: "missing_parent_payment_ref",
|
||||
mutate: func(r *mntxv1.CardTokenPayoutRequest) { r.ParentPaymentRef = "" },
|
||||
expected: "missing_parent_payment_ref",
|
||||
},
|
||||
{
|
||||
name: "both_operation_and_payout_identity",
|
||||
mutate: func(r *mntxv1.CardTokenPayoutRequest) {
|
||||
r.PayoutId = "parent-1"
|
||||
r.OperationRef = "parent-1:hop_1_card_payout_send"
|
||||
},
|
||||
expected: "ambiguous_operation_ref",
|
||||
},
|
||||
{
|
||||
name: "missing_customer_id",
|
||||
mutate: func(r *mntxv1.CardTokenPayoutRequest) { r.CustomerId = "" },
|
||||
expected: "missing_customer_id",
|
||||
},
|
||||
{
|
||||
name: "missing_customer_ip",
|
||||
mutate: func(r *mntxv1.CardTokenPayoutRequest) { r.CustomerIp = "" },
|
||||
expected: "missing_customer_ip",
|
||||
},
|
||||
{
|
||||
name: "invalid_amount",
|
||||
mutate: func(r *mntxv1.CardTokenPayoutRequest) { r.AmountMinor = 0 },
|
||||
expected: "invalid_amount",
|
||||
},
|
||||
{
|
||||
name: "missing_currency",
|
||||
mutate: func(r *mntxv1.CardTokenPayoutRequest) { r.Currency = "" },
|
||||
expected: "missing_currency",
|
||||
},
|
||||
{
|
||||
name: "unsupported_currency",
|
||||
mutate: func(r *mntxv1.CardTokenPayoutRequest) { r.Currency = "EUR" },
|
||||
config: func(cfg provider.Config) provider.Config {
|
||||
cfg.AllowedCurrencies = []string{"USD"}
|
||||
return cfg
|
||||
},
|
||||
expected: "unsupported_currency",
|
||||
},
|
||||
{
|
||||
name: "missing_card_token",
|
||||
mutate: func(r *mntxv1.CardTokenPayoutRequest) { r.CardToken = "" },
|
||||
expected: "missing_card_token",
|
||||
},
|
||||
{
|
||||
name: "missing_customer_city_when_required",
|
||||
mutate: func(r *mntxv1.CardTokenPayoutRequest) {
|
||||
r.CustomerCountry = "US"
|
||||
r.CustomerCity = ""
|
||||
r.CustomerAddress = "Main St"
|
||||
r.CustomerZip = "12345"
|
||||
},
|
||||
config: func(cfg provider.Config) provider.Config {
|
||||
cfg.RequireCustomerAddress = true
|
||||
return cfg
|
||||
},
|
||||
expected: "missing_customer_city",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
req := validCardTokenPayoutRequest()
|
||||
tc.mutate(req)
|
||||
cfg := baseCfg
|
||||
if tc.config != nil {
|
||||
cfg = tc.config(cfg)
|
||||
}
|
||||
err := validateCardTokenPayoutRequest(req, cfg)
|
||||
requireReason(t, err, tc.expected)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/internal/service/provider"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
|
||||
)
|
||||
|
||||
type tokenizeCardInput struct {
|
||||
pan string
|
||||
month uint32
|
||||
year uint32
|
||||
holder string
|
||||
cvv string
|
||||
}
|
||||
|
||||
func validateCardTokenizeRequest(req *mntxv1.CardTokenizeRequest, cfg provider.Config) (*tokenizeCardInput, error) {
|
||||
if req == nil {
|
||||
return nil, newPayoutError("invalid_request", merrors.InvalidArgument("request cannot be empty"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetRequestId()) == "" {
|
||||
return nil, newPayoutError("missing_request_id", merrors.InvalidArgument("request_id is required", "request_id"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerId()) == "" {
|
||||
return nil, newPayoutError("missing_customer_id", merrors.InvalidArgument("customer_id is required", "customer_id"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerFirstName()) == "" {
|
||||
return nil, newPayoutError("missing_customer_first_name", merrors.InvalidArgument("customer_first_name is required", "customer_first_name"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerLastName()) == "" {
|
||||
return nil, newPayoutError("missing_customer_last_name", merrors.InvalidArgument("customer_last_name is required", "customer_last_name"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerIp()) == "" {
|
||||
return nil, newPayoutError("missing_customer_ip", merrors.InvalidArgument("customer_ip is required", "customer_ip"))
|
||||
}
|
||||
|
||||
card := extractTokenizeCard(req)
|
||||
if card.pan == "" {
|
||||
return nil, newPayoutError("missing_card_pan", merrors.InvalidArgument("card_pan is required", "card.pan"))
|
||||
}
|
||||
if card.holder == "" {
|
||||
return nil, newPayoutError("missing_card_holder", merrors.InvalidArgument("card_holder is required", "card.holder"))
|
||||
}
|
||||
if card.month == 0 || card.month > 12 {
|
||||
return nil, newPayoutError("invalid_expiry_month", merrors.InvalidArgument("card_exp_month must be between 1 and 12", "card.exp_month"))
|
||||
}
|
||||
if card.year == 0 {
|
||||
return nil, newPayoutError("invalid_expiry_year", merrors.InvalidArgument("card_exp_year must be provided", "card.exp_year"))
|
||||
}
|
||||
if card.cvv == "" {
|
||||
return nil, newPayoutError("missing_cvv", merrors.InvalidArgument("card_cvv is required", "card.cvv"))
|
||||
}
|
||||
if expired(card.month, card.year) {
|
||||
return nil, newPayoutError("expired_card", merrors.InvalidArgument("card expiry is in the past", "card.expiry"))
|
||||
}
|
||||
|
||||
if cfg.RequireCustomerAddress {
|
||||
if strings.TrimSpace(req.GetCustomerCountry()) == "" {
|
||||
return nil, newPayoutError("missing_customer_country", merrors.InvalidArgument("customer_country is required", "customer_country"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerCity()) == "" {
|
||||
return nil, newPayoutError("missing_customer_city", merrors.InvalidArgument("customer_city is required", "customer_city"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerAddress()) == "" {
|
||||
return nil, newPayoutError("missing_customer_address", merrors.InvalidArgument("customer_address is required", "customer_address"))
|
||||
}
|
||||
if strings.TrimSpace(req.GetCustomerZip()) == "" {
|
||||
return nil, newPayoutError("missing_customer_zip", merrors.InvalidArgument("customer_zip is required", "customer_zip"))
|
||||
}
|
||||
}
|
||||
|
||||
return card, nil
|
||||
}
|
||||
|
||||
func extractTokenizeCard(req *mntxv1.CardTokenizeRequest) *tokenizeCardInput {
|
||||
card := req.GetCard()
|
||||
if card != nil {
|
||||
return &tokenizeCardInput{
|
||||
pan: strings.TrimSpace(card.GetPan()),
|
||||
month: card.GetExpMonth(),
|
||||
year: card.GetExpYear(),
|
||||
holder: strings.TrimSpace(card.GetCardHolder()),
|
||||
cvv: strings.TrimSpace(card.GetCvv()),
|
||||
}
|
||||
}
|
||||
return &tokenizeCardInput{
|
||||
pan: strings.TrimSpace(req.GetCardPan()),
|
||||
month: req.GetCardExpMonth(),
|
||||
year: req.GetCardExpYear(),
|
||||
holder: strings.TrimSpace(req.GetCardHolder()),
|
||||
cvv: strings.TrimSpace(req.GetCardCvv()),
|
||||
}
|
||||
}
|
||||
|
||||
func expired(month uint32, year uint32) bool {
|
||||
now := time.Now()
|
||||
y := int(year)
|
||||
m := time.Month(month)
|
||||
// Normalize 2-digit years: assume 2000-2099.
|
||||
if y < 100 {
|
||||
y += 2000
|
||||
}
|
||||
expiry := time.Date(y, m, 1, 0, 0, 0, 0, time.UTC).AddDate(0, 1, -1)
|
||||
return now.After(expiry)
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
|
||||
)
|
||||
|
||||
func TestValidateCardTokenizeRequest_ValidTopLevel(t *testing.T) {
|
||||
cfg := testProviderConfig()
|
||||
req := validCardTokenizeRequest()
|
||||
if _, err := validateCardTokenizeRequest(req, cfg); err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateCardTokenizeRequest_ValidNestedCard(t *testing.T) {
|
||||
cfg := testProviderConfig()
|
||||
req := validCardTokenizeRequest()
|
||||
req.Card = &mntxv1.CardDetails{
|
||||
Pan: "4111111111111111",
|
||||
ExpMonth: req.CardExpMonth,
|
||||
ExpYear: req.CardExpYear,
|
||||
CardHolder: req.CardHolder,
|
||||
Cvv: req.CardCvv,
|
||||
}
|
||||
req.CardPan = ""
|
||||
req.CardExpMonth = 0
|
||||
req.CardExpYear = 0
|
||||
req.CardHolder = ""
|
||||
req.CardCvv = ""
|
||||
|
||||
if _, err := validateCardTokenizeRequest(req, cfg); err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateCardTokenizeRequest_Expired(t *testing.T) {
|
||||
cfg := testProviderConfig()
|
||||
req := validCardTokenizeRequest()
|
||||
now := time.Now().UTC()
|
||||
req.CardExpMonth = uint32(now.Month())
|
||||
req.CardExpYear = uint32(now.Year() - 1)
|
||||
|
||||
_, err := validateCardTokenizeRequest(req, cfg)
|
||||
requireReason(t, err, "expired_card")
|
||||
}
|
||||
|
||||
func TestValidateCardTokenizeRequest_MissingCvv(t *testing.T) {
|
||||
cfg := testProviderConfig()
|
||||
req := validCardTokenizeRequest()
|
||||
req.CardCvv = ""
|
||||
|
||||
_, err := validateCardTokenizeRequest(req, cfg)
|
||||
requireReason(t, err, "missing_cvv")
|
||||
}
|
||||
|
||||
func TestValidateCardTokenizeRequest_MissingCardPan(t *testing.T) {
|
||||
cfg := testProviderConfig()
|
||||
req := validCardTokenizeRequest()
|
||||
req.CardPan = ""
|
||||
|
||||
_, err := validateCardTokenizeRequest(req, cfg)
|
||||
requireReason(t, err, "missing_card_pan")
|
||||
}
|
||||
|
||||
func TestValidateCardTokenizeRequest_AddressRequired(t *testing.T) {
|
||||
cfg := testProviderConfig()
|
||||
cfg.RequireCustomerAddress = true
|
||||
req := validCardTokenizeRequest()
|
||||
req.CustomerCountry = ""
|
||||
|
||||
_, err := validateCardTokenizeRequest(req, cfg)
|
||||
requireReason(t, err, "missing_customer_country")
|
||||
}
|
||||
388
api/gateway/aurora/internal/service/gateway/connector.go
Normal file
388
api/gateway/aurora/internal/service/gateway/connector.go
Normal file
@@ -0,0 +1,388 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/tech/sendico/gateway/aurora/internal/appversion"
|
||||
"github.com/tech/sendico/pkg/connector/params"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
moneyv1 "github.com/tech/sendico/pkg/proto/common/money/v1"
|
||||
connectorv1 "github.com/tech/sendico/pkg/proto/connector/v1"
|
||||
mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
)
|
||||
|
||||
const connectorTypeID = "mntx"
|
||||
|
||||
func (s *Service) GetCapabilities(_ context.Context, _ *connectorv1.GetCapabilitiesRequest) (*connectorv1.GetCapabilitiesResponse, error) {
|
||||
return &connectorv1.GetCapabilitiesResponse{
|
||||
Capabilities: &connectorv1.ConnectorCapabilities{
|
||||
ConnectorType: connectorTypeID,
|
||||
Version: appversion.Create().Short(),
|
||||
SupportedAccountKinds: nil,
|
||||
SupportedOperationTypes: []connectorv1.OperationType{connectorv1.OperationType_PAYOUT},
|
||||
OperationParams: connectorOperationParams(),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) OpenAccount(_ context.Context, _ *connectorv1.OpenAccountRequest) (*connectorv1.OpenAccountResponse, error) {
|
||||
return &connectorv1.OpenAccountResponse{Error: connectorError(connectorv1.ErrorCode_UNSUPPORTED_ACCOUNT_KIND, "open_account: unsupported", nil, "")}, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetAccount(_ context.Context, _ *connectorv1.GetAccountRequest) (*connectorv1.GetAccountResponse, error) {
|
||||
return nil, merrors.NotImplemented("get_account: unsupported")
|
||||
}
|
||||
|
||||
func (s *Service) ListAccounts(_ context.Context, _ *connectorv1.ListAccountsRequest) (*connectorv1.ListAccountsResponse, error) {
|
||||
return nil, merrors.NotImplemented("list_accounts: unsupported")
|
||||
}
|
||||
|
||||
func (s *Service) GetBalance(_ context.Context, _ *connectorv1.GetBalanceRequest) (*connectorv1.GetBalanceResponse, error) {
|
||||
return nil, merrors.NotImplemented("get_balance: unsupported")
|
||||
}
|
||||
|
||||
func (s *Service) SubmitOperation(ctx context.Context, req *connectorv1.SubmitOperationRequest) (*connectorv1.SubmitOperationResponse, error) {
|
||||
if req == nil || req.GetOperation() == nil {
|
||||
return &connectorv1.SubmitOperationResponse{Receipt: &connectorv1.OperationReceipt{Error: connectorError(connectorv1.ErrorCode_INVALID_PARAMS, "submit_operation: operation is required", nil, "")}}, nil
|
||||
}
|
||||
op := req.GetOperation()
|
||||
idempotencyKey := strings.TrimSpace(op.GetIdempotencyKey())
|
||||
if idempotencyKey == "" {
|
||||
return &connectorv1.SubmitOperationResponse{Receipt: &connectorv1.OperationReceipt{Error: connectorError(connectorv1.ErrorCode_INVALID_PARAMS, "submit_operation: idempotency_key is required", op, "")}}, nil
|
||||
}
|
||||
operationRef := strings.TrimSpace(op.GetOperationRef())
|
||||
if operationRef == "" {
|
||||
return &connectorv1.SubmitOperationResponse{Receipt: &connectorv1.OperationReceipt{Error: connectorError(connectorv1.ErrorCode_INVALID_PARAMS, "submit_operation: operation_ref is required", op, "")}}, nil
|
||||
}
|
||||
intentRef := strings.TrimSpace(op.GetIntentRef())
|
||||
if intentRef == "" {
|
||||
return &connectorv1.SubmitOperationResponse{Receipt: &connectorv1.OperationReceipt{Error: connectorError(connectorv1.ErrorCode_INVALID_PARAMS, "submit_operation: intent_ref is required", op, "")}}, nil
|
||||
}
|
||||
if op.GetType() != connectorv1.OperationType_PAYOUT {
|
||||
return &connectorv1.SubmitOperationResponse{Receipt: &connectorv1.OperationReceipt{Error: connectorError(connectorv1.ErrorCode_UNSUPPORTED_OPERATION, "submit_operation: unsupported operation type", op, "")}}, nil
|
||||
}
|
||||
reader := params.New(op.GetParams())
|
||||
amountMinor, currency, err := payoutAmount(op, reader)
|
||||
if err != nil {
|
||||
return &connectorv1.SubmitOperationResponse{Receipt: &connectorv1.OperationReceipt{Error: connectorError(connectorv1.ErrorCode_INVALID_PARAMS, err.Error(), op, "")}}, nil
|
||||
}
|
||||
parentPaymentRef := strings.TrimSpace(reader.String("parent_payment_ref"))
|
||||
|
||||
payoutID := operationIDForRequest(operationRef)
|
||||
|
||||
if strings.TrimSpace(reader.String("card_token")) != "" {
|
||||
resp, err := s.CreateCardTokenPayout(ctx, buildCardTokenPayoutRequestFromParams(reader, payoutID, parentPaymentRef, idempotencyKey, operationRef, intentRef, amountMinor, currency))
|
||||
if err != nil {
|
||||
return &connectorv1.SubmitOperationResponse{Receipt: &connectorv1.OperationReceipt{Error: connectorError(mapErrorCode(err), err.Error(), op, "")}}, nil
|
||||
}
|
||||
return &connectorv1.SubmitOperationResponse{Receipt: payoutReceipt(resp.GetPayout())}, nil
|
||||
}
|
||||
cr := buildCardPayoutRequestFromParams(reader, payoutID, parentPaymentRef, idempotencyKey, operationRef, intentRef, amountMinor, currency)
|
||||
resp, err := s.CreateCardPayout(ctx, cr)
|
||||
if err != nil {
|
||||
return &connectorv1.SubmitOperationResponse{Receipt: &connectorv1.OperationReceipt{Error: connectorError(mapErrorCode(err), err.Error(), op, "")}}, nil
|
||||
}
|
||||
return &connectorv1.SubmitOperationResponse{Receipt: payoutReceipt(resp.GetPayout())}, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetOperation(ctx context.Context, req *connectorv1.GetOperationRequest) (*connectorv1.GetOperationResponse, error) {
|
||||
if req == nil || strings.TrimSpace(req.GetOperationId()) == "" {
|
||||
return nil, merrors.InvalidArgument("get_operation: operation_id is required")
|
||||
}
|
||||
|
||||
operationRef := strings.TrimSpace(req.GetOperationId())
|
||||
if s.storage == nil || s.storage.Payouts() == nil {
|
||||
return nil, merrors.Internal("get_operation: storage is not configured")
|
||||
}
|
||||
|
||||
payout, err := s.storage.Payouts().FindByOperationRef(ctx, operationRef)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if payout == nil {
|
||||
return nil, merrors.NoData("payout not found")
|
||||
}
|
||||
|
||||
return &connectorv1.GetOperationResponse{Operation: payoutToOperation(StateToProto(payout))}, nil
|
||||
}
|
||||
|
||||
func (s *Service) ListOperations(_ context.Context, _ *connectorv1.ListOperationsRequest) (*connectorv1.ListOperationsResponse, error) {
|
||||
return nil, merrors.NotImplemented("list_operations: unsupported")
|
||||
}
|
||||
|
||||
func connectorOperationParams() []*connectorv1.OperationParamSpec {
|
||||
return []*connectorv1.OperationParamSpec{
|
||||
{OperationType: connectorv1.OperationType_PAYOUT, Params: []*connectorv1.ParamSpec{
|
||||
{Key: "customer_id", Type: connectorv1.ParamType_STRING, Required: true},
|
||||
{Key: "customer_first_name", Type: connectorv1.ParamType_STRING, Required: true},
|
||||
{Key: "customer_last_name", Type: connectorv1.ParamType_STRING, Required: true},
|
||||
{Key: "customer_ip", Type: connectorv1.ParamType_STRING, Required: true},
|
||||
{Key: "card_token", Type: connectorv1.ParamType_STRING, Required: false},
|
||||
{Key: "card_pan", Type: connectorv1.ParamType_STRING, Required: false},
|
||||
{Key: "card_exp_year", Type: connectorv1.ParamType_INT, Required: false},
|
||||
{Key: "card_exp_month", Type: connectorv1.ParamType_INT, Required: false},
|
||||
{Key: "card_holder", Type: connectorv1.ParamType_STRING, Required: false},
|
||||
{Key: "amount_minor", Type: connectorv1.ParamType_INT, Required: false},
|
||||
{Key: "project_id", Type: connectorv1.ParamType_INT, Required: false},
|
||||
{Key: "parent_payment_ref", Type: connectorv1.ParamType_STRING, Required: true},
|
||||
{Key: "customer_middle_name", Type: connectorv1.ParamType_STRING, Required: false},
|
||||
{Key: "customer_country", Type: connectorv1.ParamType_STRING, Required: false},
|
||||
{Key: "customer_state", Type: connectorv1.ParamType_STRING, Required: false},
|
||||
{Key: "customer_city", Type: connectorv1.ParamType_STRING, Required: false},
|
||||
{Key: "customer_address", Type: connectorv1.ParamType_STRING, Required: false},
|
||||
{Key: "customer_zip", Type: connectorv1.ParamType_STRING, Required: false},
|
||||
{Key: "masked_pan", Type: connectorv1.ParamType_STRING, Required: false},
|
||||
{Key: "metadata", Type: connectorv1.ParamType_JSON, Required: false},
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
func payoutAmount(op *connectorv1.Operation, reader params.Reader) (int64, string, error) {
|
||||
if op == nil {
|
||||
return 0, "", merrors.InvalidArgument("payout: operation is required")
|
||||
}
|
||||
currency := currencyFromOperation(op)
|
||||
if currency == "" {
|
||||
return 0, "", merrors.InvalidArgument("payout: currency is required")
|
||||
}
|
||||
if minor, ok := reader.Int64("amount_minor"); ok && minor > 0 {
|
||||
return minor, currency, nil
|
||||
}
|
||||
money := op.GetMoney()
|
||||
if money == nil {
|
||||
return 0, "", merrors.InvalidArgument("payout: money is required")
|
||||
}
|
||||
amount := strings.TrimSpace(money.GetAmount())
|
||||
if amount == "" {
|
||||
return 0, "", merrors.InvalidArgument("payout: amount is required")
|
||||
}
|
||||
dec, err := decimal.NewFromString(amount)
|
||||
if err != nil {
|
||||
return 0, "", merrors.InvalidArgument("payout: invalid amount")
|
||||
}
|
||||
minor := dec.Mul(decimal.NewFromInt(100)).IntPart()
|
||||
return minor, currency, nil
|
||||
}
|
||||
|
||||
func currencyFromOperation(op *connectorv1.Operation) string {
|
||||
if op == nil || op.GetMoney() == nil {
|
||||
return ""
|
||||
}
|
||||
currency := strings.TrimSpace(op.GetMoney().GetCurrency())
|
||||
if idx := strings.Index(currency, "-"); idx > 0 {
|
||||
currency = currency[:idx]
|
||||
}
|
||||
return strings.ToUpper(currency)
|
||||
}
|
||||
|
||||
func operationIDForRequest(operationRef string) string {
|
||||
return strings.TrimSpace(operationRef)
|
||||
}
|
||||
|
||||
func metadataFromReader(reader params.Reader) map[string]string {
|
||||
metadata := reader.StringMap("metadata")
|
||||
if len(metadata) == 0 {
|
||||
return nil
|
||||
}
|
||||
return metadata
|
||||
}
|
||||
|
||||
func buildCardTokenPayoutRequestFromParams(reader params.Reader,
|
||||
payoutID, parentPaymentRef, idempotencyKey, operationRef, intentRef string,
|
||||
amountMinor int64, currency string) *mntxv1.CardTokenPayoutRequest {
|
||||
operationRef = strings.TrimSpace(operationRef)
|
||||
payoutID = strings.TrimSpace(payoutID)
|
||||
if operationRef != "" {
|
||||
payoutID = ""
|
||||
}
|
||||
req := &mntxv1.CardTokenPayoutRequest{
|
||||
PayoutId: payoutID,
|
||||
ParentPaymentRef: strings.TrimSpace(parentPaymentRef),
|
||||
ProjectId: readerInt64(reader, "project_id"),
|
||||
CustomerId: strings.TrimSpace(reader.String("customer_id")),
|
||||
CustomerFirstName: strings.TrimSpace(reader.String("customer_first_name")),
|
||||
CustomerMiddleName: strings.TrimSpace(reader.String("customer_middle_name")),
|
||||
CustomerLastName: strings.TrimSpace(reader.String("customer_last_name")),
|
||||
CustomerIp: strings.TrimSpace(reader.String("customer_ip")),
|
||||
CustomerZip: strings.TrimSpace(reader.String("customer_zip")),
|
||||
CustomerCountry: strings.TrimSpace(reader.String("customer_country")),
|
||||
CustomerState: strings.TrimSpace(reader.String("customer_state")),
|
||||
CustomerCity: strings.TrimSpace(reader.String("customer_city")),
|
||||
CustomerAddress: strings.TrimSpace(reader.String("customer_address")),
|
||||
AmountMinor: amountMinor,
|
||||
Currency: currency,
|
||||
CardToken: strings.TrimSpace(reader.String("card_token")),
|
||||
CardHolder: strings.TrimSpace(reader.String("card_holder")),
|
||||
MaskedPan: strings.TrimSpace(reader.String("masked_pan")),
|
||||
Metadata: metadataFromReader(reader),
|
||||
OperationRef: operationRef,
|
||||
IdempotencyKey: strings.TrimSpace(idempotencyKey),
|
||||
IntentRef: strings.TrimSpace(intentRef),
|
||||
}
|
||||
return req
|
||||
}
|
||||
|
||||
func buildCardPayoutRequestFromParams(reader params.Reader,
|
||||
payoutID, parentPaymentRef, idempotencyKey, operationRef, intentRef string,
|
||||
amountMinor int64, currency string) *mntxv1.CardPayoutRequest {
|
||||
operationRef = strings.TrimSpace(operationRef)
|
||||
payoutID = strings.TrimSpace(payoutID)
|
||||
if operationRef != "" {
|
||||
payoutID = ""
|
||||
}
|
||||
return &mntxv1.CardPayoutRequest{
|
||||
PayoutId: payoutID,
|
||||
ParentPaymentRef: strings.TrimSpace(parentPaymentRef),
|
||||
ProjectId: readerInt64(reader, "project_id"),
|
||||
CustomerId: strings.TrimSpace(reader.String("customer_id")),
|
||||
CustomerFirstName: strings.TrimSpace(reader.String("customer_first_name")),
|
||||
CustomerMiddleName: strings.TrimSpace(reader.String("customer_middle_name")),
|
||||
CustomerLastName: strings.TrimSpace(reader.String("customer_last_name")),
|
||||
CustomerIp: strings.TrimSpace(reader.String("customer_ip")),
|
||||
CustomerZip: strings.TrimSpace(reader.String("customer_zip")),
|
||||
CustomerCountry: strings.TrimSpace(reader.String("customer_country")),
|
||||
CustomerState: strings.TrimSpace(reader.String("customer_state")),
|
||||
CustomerCity: strings.TrimSpace(reader.String("customer_city")),
|
||||
CustomerAddress: strings.TrimSpace(reader.String("customer_address")),
|
||||
AmountMinor: amountMinor,
|
||||
Currency: currency,
|
||||
CardPan: strings.TrimSpace(reader.String("card_pan")),
|
||||
CardExpYear: uint32(readerInt64(reader, "card_exp_year")),
|
||||
CardExpMonth: uint32(readerInt64(reader, "card_exp_month")),
|
||||
CardHolder: strings.TrimSpace(reader.String("card_holder")),
|
||||
Metadata: metadataFromReader(reader),
|
||||
OperationRef: operationRef,
|
||||
IdempotencyKey: strings.TrimSpace(idempotencyKey),
|
||||
IntentRef: strings.TrimSpace(intentRef),
|
||||
}
|
||||
}
|
||||
|
||||
func readerInt64(reader params.Reader, key string) int64 {
|
||||
if v, ok := reader.Int64(key); ok {
|
||||
return v
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func payoutReceipt(state *mntxv1.CardPayoutState) *connectorv1.OperationReceipt {
|
||||
if state == nil {
|
||||
return &connectorv1.OperationReceipt{
|
||||
Status: connectorv1.OperationStatus_OPERATION_PROCESSING,
|
||||
}
|
||||
}
|
||||
return &connectorv1.OperationReceipt{
|
||||
OperationId: firstNonEmpty(strings.TrimSpace(state.GetOperationRef()), strings.TrimSpace(state.GetPayoutId())),
|
||||
Status: payoutStatusToOperation(state.GetStatus()),
|
||||
ProviderRef: strings.TrimSpace(state.GetProviderPaymentId()),
|
||||
}
|
||||
}
|
||||
|
||||
func payoutToOperation(state *mntxv1.CardPayoutState) *connectorv1.Operation {
|
||||
if state == nil {
|
||||
return nil
|
||||
}
|
||||
op := &connectorv1.Operation{
|
||||
OperationId: firstNonEmpty(strings.TrimSpace(state.GetOperationRef()), strings.TrimSpace(state.GetPayoutId())),
|
||||
Type: connectorv1.OperationType_PAYOUT,
|
||||
Status: payoutStatusToOperation(state.GetStatus()),
|
||||
Money: &moneyv1.Money{
|
||||
Amount: minorToDecimal(state.GetAmountMinor()),
|
||||
Currency: strings.ToUpper(strings.TrimSpace(state.GetCurrency())),
|
||||
},
|
||||
ProviderRef: strings.TrimSpace(state.GetProviderPaymentId()),
|
||||
IntentRef: strings.TrimSpace(state.GetIntentRef()),
|
||||
OperationRef: strings.TrimSpace(state.GetOperationRef()),
|
||||
CreatedAt: state.GetCreatedAt(),
|
||||
UpdatedAt: state.GetUpdatedAt(),
|
||||
}
|
||||
|
||||
params := map[string]interface{}{}
|
||||
if paymentRef := strings.TrimSpace(state.GetParentPaymentRef()); paymentRef != "" {
|
||||
params["payment_ref"] = paymentRef
|
||||
params["parent_payment_ref"] = paymentRef
|
||||
}
|
||||
if providerCode := strings.TrimSpace(state.GetProviderCode()); providerCode != "" {
|
||||
params["provider_code"] = providerCode
|
||||
}
|
||||
if providerMessage := strings.TrimSpace(state.GetProviderMessage()); providerMessage != "" {
|
||||
params["provider_message"] = providerMessage
|
||||
params["failure_reason"] = providerMessage
|
||||
}
|
||||
if len(params) > 0 {
|
||||
op.Params = structFromMap(params)
|
||||
}
|
||||
|
||||
return op
|
||||
}
|
||||
|
||||
func minorToDecimal(amount int64) string {
|
||||
dec := decimal.NewFromInt(amount).Div(decimal.NewFromInt(100))
|
||||
return dec.StringFixed(2)
|
||||
}
|
||||
|
||||
func payoutStatusToOperation(status mntxv1.PayoutStatus) connectorv1.OperationStatus {
|
||||
switch status {
|
||||
|
||||
case mntxv1.PayoutStatus_PAYOUT_STATUS_CREATED:
|
||||
return connectorv1.OperationStatus_OPERATION_CREATED
|
||||
|
||||
case mntxv1.PayoutStatus_PAYOUT_STATUS_WAITING:
|
||||
return connectorv1.OperationStatus_OPERATION_WAITING
|
||||
|
||||
case mntxv1.PayoutStatus_PAYOUT_STATUS_SUCCESS:
|
||||
return connectorv1.OperationStatus_OPERATION_SUCCESS
|
||||
|
||||
case mntxv1.PayoutStatus_PAYOUT_STATUS_FAILED:
|
||||
return connectorv1.OperationStatus_OPERATION_FAILED
|
||||
|
||||
case mntxv1.PayoutStatus_PAYOUT_STATUS_CANCELLED:
|
||||
return connectorv1.OperationStatus_OPERATION_CANCELLED
|
||||
|
||||
default:
|
||||
return connectorv1.OperationStatus_OPERATION_STATUS_UNSPECIFIED
|
||||
}
|
||||
}
|
||||
|
||||
func structFromMap(values map[string]interface{}) *structpb.Struct {
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
result, err := structpb.NewStruct(values)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func connectorError(code connectorv1.ErrorCode, message string, op *connectorv1.Operation, accountID string) *connectorv1.ConnectorError {
|
||||
err := &connectorv1.ConnectorError{
|
||||
Code: code,
|
||||
Message: strings.TrimSpace(message),
|
||||
AccountId: strings.TrimSpace(accountID),
|
||||
}
|
||||
if op != nil {
|
||||
err.CorrelationId = strings.TrimSpace(op.GetCorrelationId())
|
||||
err.ParentIntentId = strings.TrimSpace(op.GetParentIntentId())
|
||||
err.OperationId = strings.TrimSpace(op.GetOperationId())
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func mapErrorCode(err error) connectorv1.ErrorCode {
|
||||
switch {
|
||||
case errors.Is(err, merrors.ErrInvalidArg):
|
||||
return connectorv1.ErrorCode_INVALID_PARAMS
|
||||
case errors.Is(err, merrors.ErrNoData):
|
||||
return connectorv1.ErrorCode_NOT_FOUND
|
||||
case errors.Is(err, merrors.ErrNotImplemented):
|
||||
return connectorv1.ErrorCode_UNSUPPORTED_OPERATION
|
||||
case errors.Is(err, merrors.ErrInternal):
|
||||
return connectorv1.ErrorCode_TEMPORARY_UNAVAILABLE
|
||||
default:
|
||||
return connectorv1.ErrorCode_PROVIDER_ERROR
|
||||
}
|
||||
}
|
||||
99
api/gateway/aurora/internal/service/gateway/helpers.go
Normal file
99
api/gateway/aurora/internal/service/gateway/helpers.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/storage/model"
|
||||
clockpkg "github.com/tech/sendico/pkg/clock"
|
||||
mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
func tsOrNow(clock clockpkg.Clock, ts *timestamppb.Timestamp) time.Time {
|
||||
if ts == nil {
|
||||
return clock.Now()
|
||||
}
|
||||
return ts.AsTime()
|
||||
}
|
||||
|
||||
func CardPayoutStateFromProto(clock clockpkg.Clock, p *mntxv1.CardPayoutState) *model.CardPayout {
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &model.CardPayout{
|
||||
PaymentRef: strings.TrimSpace(p.GetParentPaymentRef()),
|
||||
OperationRef: p.GetOperationRef(),
|
||||
IntentRef: p.GetIntentRef(),
|
||||
IdempotencyKey: p.GetIdempotencyKey(),
|
||||
ProjectID: p.ProjectId,
|
||||
CustomerID: p.CustomerId,
|
||||
AmountMinor: p.AmountMinor,
|
||||
Currency: p.Currency,
|
||||
Status: payoutStatusFromProto(p.Status),
|
||||
ProviderCode: p.ProviderCode,
|
||||
ProviderMessage: p.ProviderMessage,
|
||||
ProviderPaymentID: p.ProviderPaymentId,
|
||||
CreatedAt: tsOrNow(clock, p.CreatedAt),
|
||||
UpdatedAt: tsOrNow(clock, p.UpdatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func StateToProto(m *model.CardPayout) *mntxv1.CardPayoutState {
|
||||
return &mntxv1.CardPayoutState{
|
||||
PayoutId: firstNonEmpty(m.OperationRef, m.PaymentRef),
|
||||
ParentPaymentRef: m.PaymentRef,
|
||||
ProjectId: m.ProjectID,
|
||||
CustomerId: m.CustomerID,
|
||||
AmountMinor: m.AmountMinor,
|
||||
Currency: m.Currency,
|
||||
Status: payoutStatusToProto(m.Status),
|
||||
ProviderCode: m.ProviderCode,
|
||||
ProviderMessage: m.ProviderMessage,
|
||||
ProviderPaymentId: m.ProviderPaymentID,
|
||||
CreatedAt: timestamppb.New(m.CreatedAt),
|
||||
UpdatedAt: timestamppb.New(m.UpdatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func payoutStatusToProto(s model.PayoutStatus) mntxv1.PayoutStatus {
|
||||
switch s {
|
||||
case model.PayoutStatusCreated:
|
||||
return mntxv1.PayoutStatus_PAYOUT_STATUS_CREATED
|
||||
case model.PayoutStatusProcessing:
|
||||
return mntxv1.PayoutStatus_PAYOUT_STATUS_WAITING
|
||||
case model.PayoutStatusWaiting:
|
||||
return mntxv1.PayoutStatus_PAYOUT_STATUS_WAITING
|
||||
case model.PayoutStatusSuccess:
|
||||
return mntxv1.PayoutStatus_PAYOUT_STATUS_SUCCESS
|
||||
case model.PayoutStatusFailed:
|
||||
return mntxv1.PayoutStatus_PAYOUT_STATUS_FAILED
|
||||
case model.PayoutStatusCancelled:
|
||||
return mntxv1.PayoutStatus_PAYOUT_STATUS_CANCELLED
|
||||
default:
|
||||
return mntxv1.PayoutStatus_PAYOUT_STATUS_CREATED
|
||||
}
|
||||
}
|
||||
|
||||
func payoutStatusFromProto(s mntxv1.PayoutStatus) model.PayoutStatus {
|
||||
switch s {
|
||||
case mntxv1.PayoutStatus_PAYOUT_STATUS_CREATED:
|
||||
return model.PayoutStatusCreated
|
||||
|
||||
case mntxv1.PayoutStatus_PAYOUT_STATUS_WAITING:
|
||||
return model.PayoutStatusWaiting
|
||||
|
||||
case mntxv1.PayoutStatus_PAYOUT_STATUS_SUCCESS:
|
||||
return model.PayoutStatusSuccess
|
||||
|
||||
case mntxv1.PayoutStatus_PAYOUT_STATUS_FAILED:
|
||||
return model.PayoutStatusFailed
|
||||
|
||||
case mntxv1.PayoutStatus_PAYOUT_STATUS_CANCELLED:
|
||||
return model.PayoutStatusCancelled
|
||||
|
||||
default:
|
||||
return model.PayoutStatusCreated
|
||||
}
|
||||
}
|
||||
62
api/gateway/aurora/internal/service/gateway/instances.go
Normal file
62
api/gateway/aurora/internal/service/gateway/instances.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/tech/sendico/pkg/api/routers/gsresponse"
|
||||
gatewayv1 "github.com/tech/sendico/pkg/proto/common/gateway/v1"
|
||||
mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
// ListGatewayInstances exposes the Aurora gateway instance descriptors.
|
||||
func (s *Service) ListGatewayInstances(ctx context.Context, req *mntxv1.ListGatewayInstancesRequest) (*mntxv1.ListGatewayInstancesResponse, error) {
|
||||
return executeUnary(ctx, s, "ListGatewayInstances", s.handleListGatewayInstances, req)
|
||||
}
|
||||
|
||||
func (s *Service) handleListGatewayInstances(_ context.Context, _ *mntxv1.ListGatewayInstancesRequest) gsresponse.Responder[mntxv1.ListGatewayInstancesResponse] {
|
||||
items := make([]*gatewayv1.GatewayInstanceDescriptor, 0, 1)
|
||||
if s.gatewayDescriptor != nil {
|
||||
items = append(items, cloneGatewayDescriptor(s.gatewayDescriptor))
|
||||
}
|
||||
return gsresponse.Success(&mntxv1.ListGatewayInstancesResponse{Items: items})
|
||||
}
|
||||
|
||||
func cloneGatewayDescriptor(src *gatewayv1.GatewayInstanceDescriptor) *gatewayv1.GatewayInstanceDescriptor {
|
||||
if src == nil {
|
||||
return nil
|
||||
}
|
||||
cp := proto.Clone(src).(*gatewayv1.GatewayInstanceDescriptor)
|
||||
if src.Currencies != nil {
|
||||
cp.Currencies = append([]string(nil), src.Currencies...)
|
||||
}
|
||||
if src.Capabilities != nil {
|
||||
cp.Capabilities = proto.Clone(src.Capabilities).(*gatewayv1.RailCapabilities)
|
||||
}
|
||||
if src.Limits != nil {
|
||||
limits := &gatewayv1.Limits{}
|
||||
if src.Limits.VolumeLimit != nil {
|
||||
limits.VolumeLimit = map[string]string{}
|
||||
for key, value := range src.Limits.VolumeLimit {
|
||||
limits.VolumeLimit[key] = value
|
||||
}
|
||||
}
|
||||
if src.Limits.VelocityLimit != nil {
|
||||
limits.VelocityLimit = map[string]int32{}
|
||||
for key, value := range src.Limits.VelocityLimit {
|
||||
limits.VelocityLimit[key] = value
|
||||
}
|
||||
}
|
||||
if src.Limits.CurrencyLimits != nil {
|
||||
limits.CurrencyLimits = map[string]*gatewayv1.LimitsOverride{}
|
||||
for key, value := range src.Limits.CurrencyLimits {
|
||||
if value == nil {
|
||||
continue
|
||||
}
|
||||
limits.CurrencyLimits[key] = proto.Clone(value).(*gatewayv1.LimitsOverride)
|
||||
}
|
||||
}
|
||||
cp.Limits = limits
|
||||
}
|
||||
return cp
|
||||
}
|
||||
66
api/gateway/aurora/internal/service/gateway/metrics.go
Normal file
66
api/gateway/aurora/internal/service/gateway/metrics.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
)
|
||||
|
||||
var (
|
||||
metricsOnce sync.Once
|
||||
|
||||
rpcLatency *prometheus.HistogramVec
|
||||
rpcStatus *prometheus.CounterVec
|
||||
)
|
||||
|
||||
func initMetrics() {
|
||||
metricsOnce.Do(func() {
|
||||
rpcLatency = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Namespace: "sendico",
|
||||
Subsystem: "aurora_gateway",
|
||||
Name: "rpc_latency_seconds",
|
||||
Help: "Latency distribution for Aurora gateway RPC handlers.",
|
||||
Buckets: prometheus.DefBuckets,
|
||||
}, []string{"method"})
|
||||
|
||||
rpcStatus = promauto.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: "sendico",
|
||||
Subsystem: "aurora_gateway",
|
||||
Name: "rpc_requests_total",
|
||||
Help: "Total number of RPC invocations grouped by method and status.",
|
||||
}, []string{"method", "status"})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func observeRPC(method string, err error, duration time.Duration) {
|
||||
if rpcLatency != nil {
|
||||
rpcLatency.WithLabelValues(method).Observe(duration.Seconds())
|
||||
}
|
||||
if rpcStatus != nil {
|
||||
rpcStatus.WithLabelValues(method, statusLabel(err)).Inc()
|
||||
}
|
||||
}
|
||||
|
||||
func statusLabel(err error) string {
|
||||
switch {
|
||||
case err == nil:
|
||||
return "ok"
|
||||
case errors.Is(err, merrors.ErrInvalidArg):
|
||||
return "invalid_argument"
|
||||
case errors.Is(err, merrors.ErrNoData):
|
||||
return "not_found"
|
||||
case errors.Is(err, merrors.ErrDataConflict):
|
||||
return "conflict"
|
||||
case errors.Is(err, merrors.ErrAccessDenied):
|
||||
return "denied"
|
||||
case errors.Is(err, merrors.ErrInternal):
|
||||
return "internal"
|
||||
default:
|
||||
return "error"
|
||||
}
|
||||
}
|
||||
86
api/gateway/aurora/internal/service/gateway/options.go
Normal file
86
api/gateway/aurora/internal/service/gateway/options.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/internal/service/provider"
|
||||
"github.com/tech/sendico/gateway/aurora/storage"
|
||||
"github.com/tech/sendico/pkg/clock"
|
||||
msg "github.com/tech/sendico/pkg/messaging"
|
||||
pmodel "github.com/tech/sendico/pkg/model"
|
||||
gatewayv1 "github.com/tech/sendico/pkg/proto/common/gateway/v1"
|
||||
)
|
||||
|
||||
// Option configures optional service dependencies.
|
||||
type Option func(*Service)
|
||||
|
||||
// WithClock injects a custom clock (useful for tests).
|
||||
func WithClock(c clock.Clock) Option {
|
||||
return func(s *Service) {
|
||||
if c != nil {
|
||||
s.clock = c
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithProducer attaches a messaging producer to the service.
|
||||
func WithProducer(p msg.Producer) Option {
|
||||
return func(s *Service) {
|
||||
s.producer = p
|
||||
}
|
||||
}
|
||||
|
||||
func WithStorage(storage storage.Repository) Option {
|
||||
return func(s *Service) {
|
||||
s.storage = storage
|
||||
}
|
||||
}
|
||||
|
||||
// WithHTTPClient injects a custom HTTP client (useful for tests).
|
||||
func WithHTTPClient(client *http.Client) Option {
|
||||
return func(s *Service) {
|
||||
if client != nil {
|
||||
s.httpClient = client
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithProviderConfig sets provider integration options.
|
||||
func WithProviderConfig(cfg provider.Config) Option {
|
||||
return func(s *Service) {
|
||||
s.config = cfg
|
||||
}
|
||||
}
|
||||
|
||||
// WithGatewayDescriptor sets the self-declared gateway instance descriptor.
|
||||
func WithGatewayDescriptor(descriptor *gatewayv1.GatewayInstanceDescriptor) Option {
|
||||
return func(s *Service) {
|
||||
if descriptor != nil {
|
||||
s.gatewayDescriptor = descriptor
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithDiscoveryInvokeURI sets the invoke URI used when announcing the gateway.
|
||||
func WithDiscoveryInvokeURI(invokeURI string) Option {
|
||||
return func(s *Service) {
|
||||
s.invokeURI = strings.TrimSpace(invokeURI)
|
||||
}
|
||||
}
|
||||
|
||||
// WithMessagingSettings applies messaging driver settings.
|
||||
func WithMessagingSettings(settings pmodel.SettingsT) Option {
|
||||
return func(s *Service) {
|
||||
if settings != nil {
|
||||
s.msgCfg = settings
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WithStrictOperationIsolation serialises payout processing to one unresolved operation at a time.
|
||||
func WithStrictOperationIsolation(enabled bool) Option {
|
||||
return func(s *Service) {
|
||||
s.strictIsolation = enabled
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
gatewayoutbox "github.com/tech/sendico/gateway/common/outbox"
|
||||
"github.com/tech/sendico/pkg/db/transaction"
|
||||
me "github.com/tech/sendico/pkg/messaging/envelope"
|
||||
)
|
||||
|
||||
type outboxProvider interface {
|
||||
Outbox() gatewayoutbox.Store
|
||||
}
|
||||
|
||||
type transactionProvider interface {
|
||||
TransactionFactory() transaction.Factory
|
||||
}
|
||||
|
||||
func (p *cardPayoutProcessor) outboxStore() gatewayoutbox.Store {
|
||||
provider, ok := p.store.(outboxProvider)
|
||||
if !ok || provider == nil {
|
||||
return nil
|
||||
}
|
||||
return provider.Outbox()
|
||||
}
|
||||
|
||||
func (p *cardPayoutProcessor) startOutboxReliableProducer() error {
|
||||
if p == nil || p.outbox == nil {
|
||||
return nil
|
||||
}
|
||||
return p.outbox.Start(p.logger, p.producer, p.outboxStore(), p.msgCfg)
|
||||
}
|
||||
|
||||
func (p *cardPayoutProcessor) sendWithOutbox(ctx context.Context, env me.Envelope) error {
|
||||
if err := p.startOutboxReliableProducer(); err != nil {
|
||||
return err
|
||||
}
|
||||
if p.outbox == nil {
|
||||
return nil
|
||||
}
|
||||
return p.outbox.Send(ctx, env)
|
||||
}
|
||||
|
||||
func (p *cardPayoutProcessor) executeTransaction(ctx context.Context, cb transaction.Callback) (any, error) {
|
||||
provider, ok := p.store.(transactionProvider)
|
||||
if !ok || provider == nil || provider.TransactionFactory() == nil {
|
||||
return cb(ctx)
|
||||
}
|
||||
return provider.TransactionFactory().CreateTransaction().Execute(ctx, cb)
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/storage/model"
|
||||
)
|
||||
|
||||
const (
|
||||
payoutExecutionModeDefaultName = "default"
|
||||
payoutExecutionModeStrictIsolatedName = "strict_isolated"
|
||||
)
|
||||
|
||||
var errPayoutExecutionModeStopped = errors.New("payout execution mode stopped")
|
||||
|
||||
type payoutExecutionMode interface {
|
||||
Name() string
|
||||
BeforeDispatch(ctx context.Context, operationRef string) error
|
||||
OnPersistedState(operationRef string, status model.PayoutStatus)
|
||||
Shutdown()
|
||||
}
|
||||
|
||||
type defaultPayoutExecutionMode struct{}
|
||||
|
||||
func newDefaultPayoutExecutionMode() payoutExecutionMode {
|
||||
return &defaultPayoutExecutionMode{}
|
||||
}
|
||||
|
||||
func (m *defaultPayoutExecutionMode) Name() string {
|
||||
return payoutExecutionModeDefaultName
|
||||
}
|
||||
|
||||
func (m *defaultPayoutExecutionMode) BeforeDispatch(_ context.Context, _ string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *defaultPayoutExecutionMode) OnPersistedState(_ string, _ model.PayoutStatus) {}
|
||||
|
||||
func (m *defaultPayoutExecutionMode) Shutdown() {}
|
||||
|
||||
type strictIsolatedPayoutExecutionMode struct {
|
||||
mu sync.Mutex
|
||||
activeOperation string
|
||||
waitCh chan struct{}
|
||||
stopped bool
|
||||
}
|
||||
|
||||
func newStrictIsolatedPayoutExecutionMode() payoutExecutionMode {
|
||||
return &strictIsolatedPayoutExecutionMode{
|
||||
waitCh: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *strictIsolatedPayoutExecutionMode) Name() string {
|
||||
return payoutExecutionModeStrictIsolatedName
|
||||
}
|
||||
|
||||
func (m *strictIsolatedPayoutExecutionMode) BeforeDispatch(ctx context.Context, operationRef string) error {
|
||||
opRef := strings.TrimSpace(operationRef)
|
||||
if opRef == "" {
|
||||
return nil
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
|
||||
for {
|
||||
waitCh, allowed, err := m.tryAcquire(opRef)
|
||||
if allowed {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
case <-waitCh:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *strictIsolatedPayoutExecutionMode) OnPersistedState(operationRef string, status model.PayoutStatus) {
|
||||
opRef := strings.TrimSpace(operationRef)
|
||||
if opRef == "" {
|
||||
return
|
||||
}
|
||||
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if m.stopped {
|
||||
return
|
||||
}
|
||||
|
||||
if isFinalPayoutStatus(status) {
|
||||
if m.activeOperation == opRef {
|
||||
m.activeOperation = ""
|
||||
m.signalLocked()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if m.activeOperation == "" {
|
||||
m.activeOperation = opRef
|
||||
m.signalLocked()
|
||||
}
|
||||
}
|
||||
|
||||
func (m *strictIsolatedPayoutExecutionMode) Shutdown() {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if m.stopped {
|
||||
return
|
||||
}
|
||||
m.stopped = true
|
||||
m.activeOperation = ""
|
||||
m.signalLocked()
|
||||
}
|
||||
|
||||
func (m *strictIsolatedPayoutExecutionMode) tryAcquire(operationRef string) (<-chan struct{}, bool, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.stopped {
|
||||
return nil, false, errPayoutExecutionModeStopped
|
||||
}
|
||||
|
||||
switch owner := strings.TrimSpace(m.activeOperation); {
|
||||
case owner == "":
|
||||
m.activeOperation = operationRef
|
||||
m.signalLocked()
|
||||
return nil, true, nil
|
||||
case owner == operationRef:
|
||||
return nil, true, nil
|
||||
default:
|
||||
return m.waitCh, false, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m *strictIsolatedPayoutExecutionMode) signalLocked() {
|
||||
if m.waitCh == nil {
|
||||
m.waitCh = make(chan struct{})
|
||||
return
|
||||
}
|
||||
close(m.waitCh)
|
||||
m.waitCh = make(chan struct{})
|
||||
}
|
||||
|
||||
func normalizePayoutExecutionMode(mode payoutExecutionMode) payoutExecutionMode {
|
||||
if mode == nil {
|
||||
return newDefaultPayoutExecutionMode()
|
||||
}
|
||||
return mode
|
||||
}
|
||||
|
||||
func payoutExecutionModeName(mode payoutExecutionMode) string {
|
||||
if mode == nil {
|
||||
return payoutExecutionModeDefaultName
|
||||
}
|
||||
name := strings.TrimSpace(mode.Name())
|
||||
if name == "" {
|
||||
return payoutExecutionModeDefaultName
|
||||
}
|
||||
return name
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/storage/model"
|
||||
)
|
||||
|
||||
func TestStrictIsolatedPayoutExecutionMode_BlocksOtherOperationUntilFinalStatus(t *testing.T) {
|
||||
mode := newStrictIsolatedPayoutExecutionMode()
|
||||
if err := mode.BeforeDispatch(context.Background(), "op-1"); err != nil {
|
||||
t.Fatalf("first acquire failed: %v", err)
|
||||
}
|
||||
|
||||
waitCtx, waitCancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer waitCancel()
|
||||
secondDone := make(chan error, 1)
|
||||
go func() {
|
||||
secondDone <- mode.BeforeDispatch(waitCtx, "op-2")
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-secondDone:
|
||||
t.Fatalf("second operation should be blocked before final status, got err=%v", err)
|
||||
case <-time.After(80 * time.Millisecond):
|
||||
}
|
||||
|
||||
mode.OnPersistedState("op-1", model.PayoutStatusWaiting)
|
||||
|
||||
select {
|
||||
case err := <-secondDone:
|
||||
t.Fatalf("second operation should remain blocked on non-final status, got err=%v", err)
|
||||
case <-time.After(80 * time.Millisecond):
|
||||
}
|
||||
|
||||
mode.OnPersistedState("op-1", model.PayoutStatusSuccess)
|
||||
|
||||
select {
|
||||
case err := <-secondDone:
|
||||
if err != nil {
|
||||
t.Fatalf("second operation should proceed after final status, got err=%v", err)
|
||||
}
|
||||
case <-time.After(time.Second):
|
||||
t.Fatalf("timeout waiting for second operation to proceed")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStrictIsolatedPayoutExecutionMode_AllowsSameOperationReentry(t *testing.T) {
|
||||
mode := newStrictIsolatedPayoutExecutionMode()
|
||||
if err := mode.BeforeDispatch(context.Background(), "op-1"); err != nil {
|
||||
t.Fatalf("first acquire failed: %v", err)
|
||||
}
|
||||
if err := mode.BeforeDispatch(context.Background(), "op-1"); err != nil {
|
||||
t.Fatalf("same operation should be re-entrant, got err=%v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
providerCodeDeclineAmountOrFrequencyLimit = "10101"
|
||||
)
|
||||
|
||||
type payoutFailureAction int
|
||||
|
||||
const (
|
||||
payoutFailureActionFail payoutFailureAction = iota + 1
|
||||
payoutFailureActionRetry
|
||||
)
|
||||
|
||||
type payoutFailureDecision struct {
|
||||
Action payoutFailureAction
|
||||
Reason string
|
||||
}
|
||||
|
||||
type payoutFailurePolicy struct {
|
||||
providerCodeActions map[string]payoutFailureAction
|
||||
}
|
||||
|
||||
func defaultPayoutFailurePolicy() payoutFailurePolicy {
|
||||
return payoutFailurePolicy{
|
||||
providerCodeActions: map[string]payoutFailureAction{
|
||||
providerCodeDeclineAmountOrFrequencyLimit: payoutFailureActionRetry,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (p payoutFailurePolicy) decideProviderFailure(code string) payoutFailureDecision {
|
||||
normalized := strings.TrimSpace(code)
|
||||
if normalized == "" {
|
||||
return payoutFailureDecision{
|
||||
Action: payoutFailureActionFail,
|
||||
Reason: "provider_failure",
|
||||
}
|
||||
}
|
||||
if action, ok := p.providerCodeActions[normalized]; ok {
|
||||
return payoutFailureDecision{
|
||||
Action: action,
|
||||
Reason: "provider_code_" + normalized,
|
||||
}
|
||||
}
|
||||
return payoutFailureDecision{
|
||||
Action: payoutFailureActionFail,
|
||||
Reason: "provider_code_" + normalized,
|
||||
}
|
||||
}
|
||||
|
||||
func (p payoutFailurePolicy) decideTransportFailure() payoutFailureDecision {
|
||||
return payoutFailureDecision{
|
||||
Action: payoutFailureActionRetry,
|
||||
Reason: "transport_failure",
|
||||
}
|
||||
}
|
||||
|
||||
func payoutFailureReason(code, message string) string {
|
||||
cleanCode := strings.TrimSpace(code)
|
||||
cleanMessage := strings.TrimSpace(message)
|
||||
switch {
|
||||
case cleanCode != "" && cleanMessage != "":
|
||||
return cleanCode + ": " + cleanMessage
|
||||
case cleanCode != "":
|
||||
return cleanCode
|
||||
default:
|
||||
return cleanMessage
|
||||
}
|
||||
}
|
||||
|
||||
func retryDelayForAttempt(attempt uint32) int {
|
||||
// Backoff in seconds by attempt number (attempt starts at 1).
|
||||
switch {
|
||||
case attempt <= 1:
|
||||
return 5
|
||||
case attempt == 2:
|
||||
return 15
|
||||
case attempt == 3:
|
||||
return 30
|
||||
default:
|
||||
return 60
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package gateway
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPayoutFailurePolicy_DecideProviderFailure(t *testing.T) {
|
||||
policy := defaultPayoutFailurePolicy()
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
code string
|
||||
action payoutFailureAction
|
||||
}{
|
||||
{
|
||||
name: "retryable provider limit code",
|
||||
code: providerCodeDeclineAmountOrFrequencyLimit,
|
||||
action: payoutFailureActionRetry,
|
||||
},
|
||||
{
|
||||
name: "unknown provider code",
|
||||
code: "99999",
|
||||
action: payoutFailureActionFail,
|
||||
},
|
||||
{
|
||||
name: "empty provider code",
|
||||
code: "",
|
||||
action: payoutFailureActionFail,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
t.Helper()
|
||||
got := policy.decideProviderFailure(tc.code)
|
||||
if got.Action != tc.action {
|
||||
t.Fatalf("action mismatch: got=%v want=%v", got.Action, tc.action)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayoutFailureReason(t *testing.T) {
|
||||
if got, want := payoutFailureReason("10101", "Decline due to amount or frequency limit"), "10101: Decline due to amount or frequency limit"; got != want {
|
||||
t.Fatalf("failure reason mismatch: got=%q want=%q", got, want)
|
||||
}
|
||||
if got, want := payoutFailureReason("", "network error"), "network error"; got != want {
|
||||
t.Fatalf("failure reason mismatch: got=%q want=%q", got, want)
|
||||
}
|
||||
if got, want := payoutFailureReason("10101", ""), "10101"; got != want {
|
||||
t.Fatalf("failure reason mismatch: got=%q want=%q", got, want)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/internal/service/provider"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
)
|
||||
|
||||
type simulatedCardScenario struct {
|
||||
Name string
|
||||
CardNumbers []string
|
||||
CardLast4 []string
|
||||
Accepted bool
|
||||
ProviderStatus string
|
||||
ErrorCode string
|
||||
ErrorMessage string
|
||||
DispatchError string
|
||||
}
|
||||
|
||||
type payoutSimulator struct {
|
||||
scenarios []simulatedCardScenario
|
||||
defaultScenario simulatedCardScenario
|
||||
seq atomic.Uint64
|
||||
}
|
||||
|
||||
func newPayoutSimulator() *payoutSimulator {
|
||||
return &payoutSimulator{
|
||||
scenarios: []simulatedCardScenario{
|
||||
{
|
||||
Name: "approved_instant",
|
||||
CardNumbers: []string{"2200001111111111"},
|
||||
Accepted: true,
|
||||
ProviderStatus: "success",
|
||||
ErrorCode: "00",
|
||||
ErrorMessage: "Approved by issuer",
|
||||
},
|
||||
{
|
||||
Name: "pending_issuer_review",
|
||||
CardNumbers: []string{"2200002222222222"},
|
||||
Accepted: true,
|
||||
ProviderStatus: "processing",
|
||||
ErrorCode: "P01",
|
||||
ErrorMessage: "Pending issuer review",
|
||||
},
|
||||
{
|
||||
Name: "insufficient_funds",
|
||||
CardNumbers: []string{"2200003333333333"},
|
||||
CardLast4: []string{"3333"},
|
||||
Accepted: false,
|
||||
ErrorCode: "51",
|
||||
ErrorMessage: "Insufficient funds",
|
||||
},
|
||||
{
|
||||
Name: "issuer_unavailable_retryable",
|
||||
CardNumbers: []string{"2200004444444444"},
|
||||
CardLast4: []string{"4444"},
|
||||
Accepted: false,
|
||||
ErrorCode: "10101",
|
||||
ErrorMessage: "Issuer temporary unavailable, retry later",
|
||||
},
|
||||
{
|
||||
Name: "stolen_card",
|
||||
CardNumbers: []string{"2200005555555555"},
|
||||
CardLast4: []string{"5555"},
|
||||
Accepted: false,
|
||||
ErrorCode: "43",
|
||||
ErrorMessage: "Stolen card, pickup",
|
||||
},
|
||||
{
|
||||
Name: "do_not_honor",
|
||||
CardNumbers: []string{"2200006666666666"},
|
||||
CardLast4: []string{"6666"},
|
||||
Accepted: false,
|
||||
ErrorCode: "05",
|
||||
ErrorMessage: "Do not honor",
|
||||
},
|
||||
{
|
||||
Name: "expired_card",
|
||||
CardNumbers: []string{"2200007777777777"},
|
||||
CardLast4: []string{"7777"},
|
||||
Accepted: false,
|
||||
ErrorCode: "54",
|
||||
ErrorMessage: "Expired card",
|
||||
},
|
||||
{
|
||||
Name: "provider_timeout_transport",
|
||||
CardNumbers: []string{"2200008888888888"},
|
||||
CardLast4: []string{"8888"},
|
||||
DispatchError: "provider timeout while calling payout endpoint",
|
||||
},
|
||||
{
|
||||
Name: "provider_unreachable_transport",
|
||||
CardNumbers: []string{"2200009999999998"},
|
||||
CardLast4: []string{"9998"},
|
||||
DispatchError: "provider host unreachable",
|
||||
},
|
||||
{
|
||||
Name: "provider_maintenance",
|
||||
CardNumbers: []string{"2200009999999997"},
|
||||
CardLast4: []string{"9997"},
|
||||
Accepted: false,
|
||||
ErrorCode: "91",
|
||||
ErrorMessage: "Issuer or switch is inoperative",
|
||||
},
|
||||
{
|
||||
Name: "provider_system_malfunction",
|
||||
CardNumbers: []string{"2200009999999996"},
|
||||
CardLast4: []string{"9996"},
|
||||
Accepted: false,
|
||||
ErrorCode: "96",
|
||||
ErrorMessage: "System malfunction",
|
||||
},
|
||||
},
|
||||
defaultScenario: simulatedCardScenario{
|
||||
Name: "default_processing",
|
||||
Accepted: true,
|
||||
ProviderStatus: "processing",
|
||||
ErrorCode: "P00",
|
||||
ErrorMessage: "Queued for provider processing",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *payoutSimulator) resolveByPAN(pan string) simulatedCardScenario {
|
||||
return s.resolve(normalizeCardNumber(pan), "")
|
||||
}
|
||||
|
||||
func (s *payoutSimulator) resolveByMaskedPAN(masked string) simulatedCardScenario {
|
||||
digits := normalizeCardNumber(masked)
|
||||
last4 := ""
|
||||
if len(digits) >= 4 {
|
||||
last4 = digits[len(digits)-4:]
|
||||
}
|
||||
return s.resolve("", last4)
|
||||
}
|
||||
|
||||
func (s *payoutSimulator) resolve(pan, last4 string) simulatedCardScenario {
|
||||
if s == nil {
|
||||
return simulatedCardScenario{}
|
||||
}
|
||||
for _, scenario := range s.scenarios {
|
||||
for _, value := range scenario.CardNumbers {
|
||||
if pan != "" && normalizeCardNumber(value) == pan {
|
||||
return scenario
|
||||
}
|
||||
}
|
||||
}
|
||||
if strings.TrimSpace(last4) != "" {
|
||||
for _, scenario := range s.scenarios {
|
||||
if scenarioMatchesLast4(scenario, last4) {
|
||||
return scenario
|
||||
}
|
||||
}
|
||||
}
|
||||
return s.defaultScenario
|
||||
}
|
||||
|
||||
func (s *payoutSimulator) buildPayoutResult(operationRef string, scenario simulatedCardScenario) (*provider.CardPayoutSendResult, error) {
|
||||
if s == nil {
|
||||
return &provider.CardPayoutSendResult{
|
||||
Accepted: true,
|
||||
StatusCode: 200,
|
||||
ErrorCode: "P00",
|
||||
ErrorMessage: "Queued for provider processing",
|
||||
}, nil
|
||||
}
|
||||
if msg := strings.TrimSpace(scenario.DispatchError); msg != "" {
|
||||
return nil, merrors.Internal("aurora simulated transport error: " + msg)
|
||||
}
|
||||
id := s.seq.Add(1)
|
||||
ref := strings.TrimSpace(operationRef)
|
||||
if ref == "" {
|
||||
ref = "card-op"
|
||||
}
|
||||
statusCode := 200
|
||||
if !scenario.Accepted {
|
||||
statusCode = 422
|
||||
}
|
||||
return &provider.CardPayoutSendResult{
|
||||
Accepted: scenario.Accepted,
|
||||
ProviderRequestID: fmt.Sprintf("aurora-%s-%06d", ref, id),
|
||||
ProviderStatus: strings.TrimSpace(scenario.ProviderStatus),
|
||||
StatusCode: statusCode,
|
||||
ErrorCode: strings.TrimSpace(scenario.ErrorCode),
|
||||
ErrorMessage: strings.TrimSpace(scenario.ErrorMessage),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func scenarioMatchesLast4(scenario simulatedCardScenario, last4 string) bool {
|
||||
candidate := strings.TrimSpace(last4)
|
||||
if candidate == "" {
|
||||
return false
|
||||
}
|
||||
for _, value := range scenario.CardLast4 {
|
||||
if normalizeCardNumber(value) == candidate {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for _, value := range scenario.CardNumbers {
|
||||
normalized := normalizeCardNumber(value)
|
||||
if len(normalized) >= 4 && normalized[len(normalized)-4:] == candidate {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func normalizeCardNumber(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return ""
|
||||
}
|
||||
var b strings.Builder
|
||||
b.Grow(len(value))
|
||||
for _, r := range value {
|
||||
if r >= '0' && r <= '9' {
|
||||
b.WriteRune(r)
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func normalizeExpiryYear(year uint32) string {
|
||||
if year == 0 {
|
||||
return ""
|
||||
}
|
||||
v := int(year)
|
||||
if v < 100 {
|
||||
v += 2000
|
||||
}
|
||||
return fmt.Sprintf("%04d", v)
|
||||
}
|
||||
|
||||
func buildSimulatedCardToken(requestID, pan string) string {
|
||||
input := strings.TrimSpace(requestID) + "|" + normalizeCardNumber(pan)
|
||||
sum := sha1.Sum([]byte(input))
|
||||
return "aur_tok_" + hex.EncodeToString(sum[:8])
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/internal/service/provider"
|
||||
"github.com/tech/sendico/gateway/aurora/storage/model"
|
||||
)
|
||||
|
||||
func TestPayoutSimulatorResolveByPAN_KnownCard(t *testing.T) {
|
||||
sim := newPayoutSimulator()
|
||||
|
||||
scenario := sim.resolveByPAN("2200003333333333")
|
||||
if scenario.Name != "insufficient_funds" {
|
||||
t.Fatalf("unexpected scenario: got=%q", scenario.Name)
|
||||
}
|
||||
if scenario.ErrorCode != "51" {
|
||||
t.Fatalf("unexpected error code: got=%q", scenario.ErrorCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayoutSimulatorResolveByPAN_Default(t *testing.T) {
|
||||
sim := newPayoutSimulator()
|
||||
|
||||
scenario := sim.resolveByPAN("2200009999999999")
|
||||
if scenario.Name != "default_processing" {
|
||||
t.Fatalf("unexpected default scenario: got=%q", scenario.Name)
|
||||
}
|
||||
if !scenario.Accepted {
|
||||
t.Fatalf("default scenario should be accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplyCardPayoutSendResult_AcceptedSuccessStatus(t *testing.T) {
|
||||
state := &model.CardPayout{}
|
||||
result := &provider.CardPayoutSendResult{
|
||||
Accepted: true,
|
||||
ProviderStatus: "success",
|
||||
ErrorCode: "00",
|
||||
ErrorMessage: "Approved",
|
||||
}
|
||||
|
||||
applyCardPayoutSendResult(state, result)
|
||||
|
||||
if state.Status != model.PayoutStatusSuccess {
|
||||
t.Fatalf("unexpected status: got=%q", state.Status)
|
||||
}
|
||||
if state.ProviderCode != "00" {
|
||||
t.Fatalf("unexpected provider code: got=%q", state.ProviderCode)
|
||||
}
|
||||
}
|
||||
303
api/gateway/aurora/internal/service/gateway/service.go
Normal file
303
api/gateway/aurora/internal/service/gateway/service.go
Normal file
@@ -0,0 +1,303 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/internal/appversion"
|
||||
"github.com/tech/sendico/gateway/aurora/internal/service/provider"
|
||||
"github.com/tech/sendico/gateway/aurora/storage"
|
||||
gatewayoutbox "github.com/tech/sendico/gateway/common/outbox"
|
||||
"github.com/tech/sendico/pkg/api/routers"
|
||||
"github.com/tech/sendico/pkg/api/routers/gsresponse"
|
||||
clockpkg "github.com/tech/sendico/pkg/clock"
|
||||
"github.com/tech/sendico/pkg/discovery"
|
||||
msg "github.com/tech/sendico/pkg/messaging"
|
||||
"github.com/tech/sendico/pkg/mlogger"
|
||||
pmodel "github.com/tech/sendico/pkg/model"
|
||||
"github.com/tech/sendico/pkg/mservice"
|
||||
gatewayv1 "github.com/tech/sendico/pkg/proto/common/gateway/v1"
|
||||
connectorv1 "github.com/tech/sendico/pkg/proto/connector/v1"
|
||||
"go.uber.org/zap"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
logger mlogger.Logger
|
||||
clock clockpkg.Clock
|
||||
producer msg.Producer
|
||||
msgCfg pmodel.SettingsT
|
||||
storage storage.Repository
|
||||
config provider.Config
|
||||
httpClient *http.Client
|
||||
card *cardPayoutProcessor
|
||||
outbox gatewayoutbox.ReliableRuntime
|
||||
gatewayDescriptor *gatewayv1.GatewayInstanceDescriptor
|
||||
announcer *discovery.Announcer
|
||||
invokeURI string
|
||||
strictIsolation bool
|
||||
|
||||
connectorv1.UnimplementedConnectorServiceServer
|
||||
}
|
||||
|
||||
type payoutFailure interface {
|
||||
error
|
||||
Reason() string
|
||||
}
|
||||
|
||||
type reasonedError struct {
|
||||
reason string
|
||||
err error
|
||||
}
|
||||
|
||||
func (r reasonedError) Error() string {
|
||||
return r.err.Error()
|
||||
}
|
||||
|
||||
func (r reasonedError) Unwrap() error {
|
||||
return r.err
|
||||
}
|
||||
|
||||
func (r reasonedError) Reason() string {
|
||||
return r.reason
|
||||
}
|
||||
|
||||
// NewService constructs the Aurora gateway service skeleton.
|
||||
func NewService(logger mlogger.Logger, opts ...Option) *Service {
|
||||
svc := &Service{
|
||||
logger: logger.Named("service"),
|
||||
clock: clockpkg.NewSystem(),
|
||||
config: provider.DefaultConfig(),
|
||||
msgCfg: map[string]any{},
|
||||
}
|
||||
|
||||
initMetrics()
|
||||
|
||||
for _, opt := range opts {
|
||||
if opt != nil {
|
||||
opt(svc)
|
||||
}
|
||||
}
|
||||
|
||||
if svc.clock == nil {
|
||||
svc.clock = clockpkg.NewSystem()
|
||||
}
|
||||
|
||||
if svc.httpClient == nil {
|
||||
svc.httpClient = &http.Client{Timeout: svc.config.Timeout()}
|
||||
} else if svc.httpClient.Timeout <= 0 {
|
||||
svc.httpClient.Timeout = svc.config.Timeout()
|
||||
}
|
||||
|
||||
svc.card = newCardPayoutProcessor(svc.logger, svc.config, svc.clock, svc.storage, svc.httpClient, svc.producer)
|
||||
if svc.strictIsolation {
|
||||
svc.card.setExecutionMode(newStrictIsolatedPayoutExecutionMode())
|
||||
}
|
||||
svc.card.outbox = &svc.outbox
|
||||
svc.card.msgCfg = svc.msgCfg
|
||||
if err := svc.card.startOutboxReliableProducer(); err != nil {
|
||||
svc.logger.Warn("Failed to initialise outbox reliable producer", zap.Error(err))
|
||||
}
|
||||
svc.card.applyGatewayDescriptor(svc.gatewayDescriptor)
|
||||
svc.startDiscoveryAnnouncer()
|
||||
|
||||
return svc
|
||||
}
|
||||
|
||||
// Register wires the service onto the provided gRPC router.
|
||||
func (s *Service) Register(router routers.GRPC) error {
|
||||
return router.Register(func(reg grpc.ServiceRegistrar) {
|
||||
connectorv1.RegisterConnectorServiceServer(reg, s)
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Service) Shutdown() {
|
||||
if s == nil {
|
||||
return
|
||||
}
|
||||
if s.card != nil {
|
||||
s.card.stopRetries()
|
||||
}
|
||||
s.outbox.Stop()
|
||||
if s.announcer != nil {
|
||||
s.announcer.Stop()
|
||||
}
|
||||
}
|
||||
|
||||
func executeUnary[TReq any, TResp any](ctx context.Context, svc *Service, method string, handler func(context.Context, *TReq) gsresponse.Responder[TResp], req *TReq) (*TResp, error) {
|
||||
log := svc.logger.Named("rpc")
|
||||
log.Info("RPC request started", zap.String("method", method))
|
||||
|
||||
start := svc.clock.Now()
|
||||
resp, err := gsresponse.Unary(svc.logger, mservice.MntxGateway, handler)(ctx, req)
|
||||
duration := svc.clock.Now().Sub(start)
|
||||
observeRPC(method, err, duration)
|
||||
|
||||
if err != nil {
|
||||
log.Warn("RPC request failed", zap.String("method", method), zap.Duration("duration", duration), zap.Error(err))
|
||||
} else {
|
||||
log.Info("RPC request completed", zap.String("method", method), zap.Duration("duration", duration))
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func normalizeReason(reason string) string {
|
||||
return strings.ToLower(strings.TrimSpace(reason))
|
||||
}
|
||||
|
||||
func newPayoutError(reason string, err error) error {
|
||||
return reasonedError{
|
||||
reason: normalizeReason(reason),
|
||||
err: err,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) startDiscoveryAnnouncer() {
|
||||
if s == nil || s.producer == nil {
|
||||
return
|
||||
}
|
||||
announce := discovery.Announcement{
|
||||
Service: mservice.MntxGateway,
|
||||
Rail: discovery.RailCardPayout,
|
||||
Operations: discovery.CardPayoutRailGatewayOperations(),
|
||||
InvokeURI: s.invokeURI,
|
||||
Version: appversion.Create().Short(),
|
||||
InstanceID: discovery.InstanceID(),
|
||||
}
|
||||
if s.gatewayDescriptor != nil {
|
||||
if id := strings.TrimSpace(s.gatewayDescriptor.GetId()); id != "" {
|
||||
announce.ID = id
|
||||
}
|
||||
announce.Currencies = currenciesFromDescriptor(s.gatewayDescriptor)
|
||||
}
|
||||
if strings.TrimSpace(announce.ID) == "" {
|
||||
announce.ID = discovery.StablePaymentGatewayID(discovery.RailCardPayout)
|
||||
}
|
||||
s.announcer = discovery.NewAnnouncer(s.logger, s.producer, string(mservice.MntxGateway), announce)
|
||||
s.announcer.Start()
|
||||
}
|
||||
|
||||
func currenciesFromDescriptor(src *gatewayv1.GatewayInstanceDescriptor) []discovery.CurrencyAnnouncement {
|
||||
if src == nil {
|
||||
return nil
|
||||
}
|
||||
network := strings.TrimSpace(src.GetNetwork())
|
||||
limitsCfg := src.GetLimits()
|
||||
values := src.GetCurrencies()
|
||||
if len(values) == 0 {
|
||||
return nil
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
result := make([]discovery.CurrencyAnnouncement, 0, len(values))
|
||||
for _, value := range values {
|
||||
currency := strings.ToUpper(strings.TrimSpace(value))
|
||||
if currency == "" || seen[currency] {
|
||||
continue
|
||||
}
|
||||
seen[currency] = true
|
||||
result = append(result, discovery.CurrencyAnnouncement{
|
||||
Currency: currency,
|
||||
Network: network,
|
||||
Limits: currencyLimitsFromDescriptor(limitsCfg, currency),
|
||||
})
|
||||
}
|
||||
if len(result) == 0 {
|
||||
return nil
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func currencyLimitsFromDescriptor(src *gatewayv1.Limits, currency string) *discovery.CurrencyLimits {
|
||||
if src == nil {
|
||||
return nil
|
||||
}
|
||||
amountMin := firstNonEmpty(src.GetPerTxMinAmount(), src.GetMinAmount())
|
||||
amountMax := firstNonEmpty(src.GetPerTxMaxAmount(), src.GetMaxAmount())
|
||||
|
||||
limits := &discovery.CurrencyLimits{}
|
||||
if amountMin != "" || amountMax != "" {
|
||||
limits.Amount = &discovery.CurrencyAmount{
|
||||
Min: amountMin,
|
||||
Max: amountMax,
|
||||
}
|
||||
}
|
||||
|
||||
running := &discovery.CurrencyRunningLimits{}
|
||||
for bucket, max := range src.GetVolumeLimit() {
|
||||
bucket = strings.TrimSpace(bucket)
|
||||
max = strings.TrimSpace(max)
|
||||
if bucket == "" || max == "" {
|
||||
continue
|
||||
}
|
||||
running.Volume = append(running.Volume, discovery.VolumeLimit{
|
||||
Window: discovery.Window{
|
||||
Raw: bucket,
|
||||
Named: bucket,
|
||||
},
|
||||
Max: max,
|
||||
})
|
||||
}
|
||||
for bucket, max := range src.GetVelocityLimit() {
|
||||
bucket = strings.TrimSpace(bucket)
|
||||
if bucket == "" || max <= 0 {
|
||||
continue
|
||||
}
|
||||
running.Velocity = append(running.Velocity, discovery.VelocityLimit{
|
||||
Window: discovery.Window{
|
||||
Raw: bucket,
|
||||
Named: bucket,
|
||||
},
|
||||
Max: int(max),
|
||||
})
|
||||
}
|
||||
if override := src.GetCurrencyLimits()[strings.ToUpper(strings.TrimSpace(currency))]; override != nil {
|
||||
if min := strings.TrimSpace(override.GetMinAmount()); min != "" {
|
||||
if limits.Amount == nil {
|
||||
limits.Amount = &discovery.CurrencyAmount{}
|
||||
}
|
||||
limits.Amount.Min = min
|
||||
}
|
||||
if max := strings.TrimSpace(override.GetMaxAmount()); max != "" {
|
||||
if limits.Amount == nil {
|
||||
limits.Amount = &discovery.CurrencyAmount{}
|
||||
}
|
||||
limits.Amount.Max = max
|
||||
}
|
||||
if maxVolume := strings.TrimSpace(override.GetMaxVolume()); maxVolume != "" {
|
||||
running.Volume = append(running.Volume, discovery.VolumeLimit{
|
||||
Window: discovery.Window{
|
||||
Raw: "default",
|
||||
Named: "default",
|
||||
},
|
||||
Max: maxVolume,
|
||||
})
|
||||
}
|
||||
if maxOps := int(override.GetMaxOps()); maxOps > 0 {
|
||||
running.Velocity = append(running.Velocity, discovery.VelocityLimit{
|
||||
Window: discovery.Window{
|
||||
Raw: "default",
|
||||
Named: "default",
|
||||
},
|
||||
Max: maxOps,
|
||||
})
|
||||
}
|
||||
}
|
||||
if len(running.Volume) > 0 || len(running.Velocity) > 0 {
|
||||
limits.Running = running
|
||||
}
|
||||
if limits.Amount == nil && limits.Running == nil {
|
||||
return nil
|
||||
}
|
||||
return limits
|
||||
}
|
||||
|
||||
func firstNonEmpty(values ...string) string {
|
||||
for _, value := range values {
|
||||
clean := strings.TrimSpace(value)
|
||||
if clean != "" {
|
||||
return clean
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
19
api/gateway/aurora/internal/service/gateway/service_test.go
Normal file
19
api/gateway/aurora/internal/service/gateway/service_test.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func TestNewService_StrictOperationIsolationOption(t *testing.T) {
|
||||
svc := NewService(zap.NewNop(), WithStrictOperationIsolation(true))
|
||||
t.Cleanup(svc.Shutdown)
|
||||
|
||||
if svc.card == nil {
|
||||
t.Fatalf("expected card processor to be initialised")
|
||||
}
|
||||
if got, want := payoutExecutionModeName(svc.card.executionMode), payoutExecutionModeStrictIsolatedName; got != want {
|
||||
t.Fatalf("execution mode mismatch: got=%q want=%q", got, want)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/internal/service/provider"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
mntxv1 "github.com/tech/sendico/pkg/proto/gateway/mntx/v1"
|
||||
)
|
||||
|
||||
func requireReason(t *testing.T, err error, reason string) {
|
||||
t.Helper()
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
if !errors.Is(err, merrors.ErrInvalidArg) {
|
||||
t.Fatalf("expected invalid argument error, got %v", err)
|
||||
}
|
||||
reasoned, ok := err.(payoutFailure)
|
||||
if !ok {
|
||||
t.Fatalf("expected payout failure reason, got %T", err)
|
||||
}
|
||||
if reasoned.Reason() != reason {
|
||||
t.Fatalf("expected reason %q, got %q", reason, reasoned.Reason())
|
||||
}
|
||||
}
|
||||
|
||||
func testProviderConfig() provider.Config {
|
||||
return provider.Config{
|
||||
AllowedCurrencies: []string{"RUB", "USD"},
|
||||
}
|
||||
}
|
||||
|
||||
func validCardPayoutRequest() *mntxv1.CardPayoutRequest {
|
||||
return &mntxv1.CardPayoutRequest{
|
||||
PayoutId: "payout-1",
|
||||
ParentPaymentRef: "payment-parent-1",
|
||||
CustomerId: "cust-1",
|
||||
CustomerFirstName: "Jane",
|
||||
CustomerLastName: "Doe",
|
||||
CustomerIp: "203.0.113.10",
|
||||
AmountMinor: 1500,
|
||||
Currency: "RUB",
|
||||
CardPan: "4111111111111111",
|
||||
CardHolder: "JANE DOE",
|
||||
CardExpMonth: 12,
|
||||
CardExpYear: 2035,
|
||||
}
|
||||
}
|
||||
|
||||
func validCardTokenPayoutRequest() *mntxv1.CardTokenPayoutRequest {
|
||||
return &mntxv1.CardTokenPayoutRequest{
|
||||
PayoutId: "payout-1",
|
||||
ParentPaymentRef: "payment-parent-1",
|
||||
CustomerId: "cust-1",
|
||||
CustomerFirstName: "Jane",
|
||||
CustomerLastName: "Doe",
|
||||
CustomerIp: "203.0.113.11",
|
||||
AmountMinor: 2500,
|
||||
Currency: "USD",
|
||||
CardToken: "tok_123",
|
||||
}
|
||||
}
|
||||
|
||||
func validCardTokenizeRequest() *mntxv1.CardTokenizeRequest {
|
||||
month, year := futureExpiry()
|
||||
return &mntxv1.CardTokenizeRequest{
|
||||
RequestId: "req-1",
|
||||
CustomerId: "cust-1",
|
||||
CustomerFirstName: "Jane",
|
||||
CustomerLastName: "Doe",
|
||||
CustomerIp: "203.0.113.12",
|
||||
CardPan: "4111111111111111",
|
||||
CardHolder: "JANE DOE",
|
||||
CardCvv: "123",
|
||||
CardExpMonth: month,
|
||||
CardExpYear: year,
|
||||
}
|
||||
}
|
||||
|
||||
func futureExpiry() (uint32, uint32) {
|
||||
now := time.Now().UTC()
|
||||
return uint32(now.Month()), uint32(now.Year() + 1)
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/tech/sendico/gateway/aurora/storage/model"
|
||||
"github.com/tech/sendico/pkg/merrors"
|
||||
paymentgateway "github.com/tech/sendico/pkg/messaging/notifications/paymentgateway"
|
||||
pmodel "github.com/tech/sendico/pkg/model"
|
||||
"github.com/tech/sendico/pkg/mservice"
|
||||
"github.com/tech/sendico/pkg/mutil/mzap"
|
||||
"github.com/tech/sendico/pkg/payments/rail"
|
||||
paytypes "github.com/tech/sendico/pkg/payments/types"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func isFinalStatus(t *model.CardPayout) bool {
|
||||
if t == nil {
|
||||
return false
|
||||
}
|
||||
return isFinalPayoutStatus(t.Status)
|
||||
}
|
||||
|
||||
func isFinalPayoutStatus(status model.PayoutStatus) bool {
|
||||
switch status {
|
||||
case model.PayoutStatusFailed, model.PayoutStatusSuccess, model.PayoutStatusCancelled:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func toOpStatus(t *model.CardPayout) (rail.OperationResult, error) {
|
||||
switch t.Status {
|
||||
case model.PayoutStatusFailed:
|
||||
return rail.OperationResultFailed, nil
|
||||
case model.PayoutStatusSuccess:
|
||||
return rail.OperationResultSuccess, nil
|
||||
case model.PayoutStatusCancelled:
|
||||
return rail.OperationResultCancelled, nil
|
||||
default:
|
||||
return rail.OperationResultFailed, merrors.InvalidArgument(fmt.Sprintf("unexpected transfer status, %s", t.Status), "t.Status")
|
||||
}
|
||||
}
|
||||
|
||||
func (p *cardPayoutProcessor) updatePayoutStatus(ctx context.Context, state *model.CardPayout) error {
|
||||
if !isFinalStatus(state) {
|
||||
if err := p.store.Payouts().Upsert(ctx, state); err != nil {
|
||||
p.logger.Warn("Failed to update transfer status", zap.Error(err), mzap.ObjRef("payout_ref", state.ID),
|
||||
zap.String("payment_ref", state.PaymentRef), zap.String("status", string(state.Status)),
|
||||
)
|
||||
return err
|
||||
}
|
||||
p.observeExecutionState(state)
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err := p.executeTransaction(ctx, func(txCtx context.Context) (any, error) {
|
||||
if upsertErr := p.store.Payouts().Upsert(txCtx, state); upsertErr != nil {
|
||||
return nil, upsertErr
|
||||
}
|
||||
if isFinalStatus(state) {
|
||||
if emitErr := p.emitTransferStatusEvent(txCtx, state); emitErr != nil {
|
||||
return nil, emitErr
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
})
|
||||
if err != nil {
|
||||
p.logger.Warn("Failed to update transfer status", zap.Error(err), mzap.ObjRef("payout_ref", state.ID),
|
||||
zap.String("payment_ref", state.PaymentRef), zap.String("status", string(state.Status)),
|
||||
)
|
||||
return err
|
||||
}
|
||||
p.observeExecutionState(state)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *cardPayoutProcessor) emitTransferStatusEvent(ctx context.Context, payout *model.CardPayout) error {
|
||||
if p == nil || payout == nil {
|
||||
return nil
|
||||
}
|
||||
if p.producer == nil || p.outboxStore() == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
status, err := toOpStatus(payout)
|
||||
if err != nil {
|
||||
p.logger.Warn("Failed to convert payout status to operation status for transfer status event", zap.Error(err),
|
||||
mzap.ObjRef("payout_ref", payout.ID), zap.String("payment_ref", payout.PaymentRef), zap.String("status", string(payout.Status)))
|
||||
return err
|
||||
}
|
||||
|
||||
exec := pmodel.PaymentGatewayExecution{
|
||||
PaymentIntentID: payout.IntentRef,
|
||||
IdempotencyKey: payout.IdempotencyKey,
|
||||
ExecutedMoney: &paytypes.Money{
|
||||
Amount: fmt.Sprintf("%d", payout.AmountMinor),
|
||||
Currency: payout.Currency,
|
||||
},
|
||||
PaymentRef: payout.PaymentRef,
|
||||
Status: status,
|
||||
OperationRef: payout.OperationRef,
|
||||
Error: payout.FailureReason,
|
||||
TransferRef: payout.GetID().Hex(),
|
||||
}
|
||||
env := paymentgateway.PaymentGatewayExecution(mservice.MntxGateway, &exec)
|
||||
if err := p.sendWithOutbox(ctx, env); err != nil {
|
||||
p.logger.Warn("Failed to publish transfer status event", zap.Error(err), mzap.ObjRef("transfer_ref", payout.ID))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
78
api/gateway/aurora/internal/service/provider/config.go
Normal file
78
api/gateway/aurora/internal/service/provider/config.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultRequestTimeout = 15 * time.Second
|
||||
DefaultStatusSuccess = "success"
|
||||
DefaultStatusProcessing = "processing"
|
||||
|
||||
OutcomeSuccess = "success"
|
||||
OutcomeProcessing = "processing"
|
||||
OutcomeDecline = "decline"
|
||||
)
|
||||
|
||||
// Config holds resolved settings for communicating with Aurora.
|
||||
type Config struct {
|
||||
BaseURL string
|
||||
ProjectID int64
|
||||
SecretKey string
|
||||
AllowedCurrencies []string
|
||||
RequireCustomerAddress bool
|
||||
RequestTimeout time.Duration
|
||||
StatusSuccess string
|
||||
StatusProcessing string
|
||||
}
|
||||
|
||||
func DefaultConfig() Config {
|
||||
return Config{
|
||||
RequestTimeout: DefaultRequestTimeout,
|
||||
StatusSuccess: DefaultStatusSuccess,
|
||||
StatusProcessing: DefaultStatusProcessing,
|
||||
}
|
||||
}
|
||||
|
||||
func (c Config) timeout() time.Duration {
|
||||
if c.RequestTimeout <= 0 {
|
||||
return DefaultRequestTimeout
|
||||
}
|
||||
return c.RequestTimeout
|
||||
}
|
||||
|
||||
// Timeout exposes the configured HTTP timeout for external callers.
|
||||
func (c Config) Timeout() time.Duration {
|
||||
return c.timeout()
|
||||
}
|
||||
|
||||
func (c Config) CurrencyAllowed(code string) bool {
|
||||
code = strings.ToUpper(strings.TrimSpace(code))
|
||||
if code == "" {
|
||||
return false
|
||||
}
|
||||
if len(c.AllowedCurrencies) == 0 {
|
||||
return true
|
||||
}
|
||||
for _, allowed := range c.AllowedCurrencies {
|
||||
if strings.EqualFold(strings.TrimSpace(allowed), code) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c Config) SuccessStatus() string {
|
||||
if strings.TrimSpace(c.StatusSuccess) == "" {
|
||||
return DefaultStatusSuccess
|
||||
}
|
||||
return strings.ToLower(strings.TrimSpace(c.StatusSuccess))
|
||||
}
|
||||
|
||||
func (c Config) ProcessingStatus() string {
|
||||
if strings.TrimSpace(c.StatusProcessing) == "" {
|
||||
return DefaultStatusProcessing
|
||||
}
|
||||
return strings.ToLower(strings.TrimSpace(c.StatusProcessing))
|
||||
}
|
||||
21
api/gateway/aurora/internal/service/provider/mask.go
Normal file
21
api/gateway/aurora/internal/service/provider/mask.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package provider
|
||||
|
||||
import "strings"
|
||||
|
||||
// MaskPAN redacts a primary account number by keeping the first 6 and last 4 digits.
|
||||
func MaskPAN(pan string) string {
|
||||
p := strings.TrimSpace(pan)
|
||||
if len(p) <= 4 {
|
||||
return strings.Repeat("*", len(p))
|
||||
}
|
||||
|
||||
if len(p) <= 10 {
|
||||
return p[:2] + strings.Repeat("*", len(p)-4) + p[len(p)-2:]
|
||||
}
|
||||
|
||||
maskLen := len(p) - 10
|
||||
if maskLen < 0 {
|
||||
maskLen = 0
|
||||
}
|
||||
return p[:6] + strings.Repeat("*", maskLen) + p[len(p)-4:]
|
||||
}
|
||||
23
api/gateway/aurora/internal/service/provider/mask_test.go
Normal file
23
api/gateway/aurora/internal/service/provider/mask_test.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package provider
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestMaskPAN(t *testing.T) {
|
||||
cases := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{input: "1234", expected: "****"},
|
||||
{input: "1234567890", expected: "12******90"},
|
||||
{input: "1234567890123456", expected: "123456******3456"},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.input, func(t *testing.T) {
|
||||
got := MaskPAN(tc.input)
|
||||
if got != tc.expected {
|
||||
t.Fatalf("expected %q, got %q", tc.expected, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
39
api/gateway/aurora/internal/service/provider/metrics.go
Normal file
39
api/gateway/aurora/internal/service/provider/metrics.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
)
|
||||
|
||||
var (
|
||||
metricsOnce sync.Once
|
||||
|
||||
cardPayoutCallbacks *prometheus.CounterVec
|
||||
)
|
||||
|
||||
func initMetrics() {
|
||||
metricsOnce.Do(func() {
|
||||
cardPayoutCallbacks = promauto.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: "sendico",
|
||||
Subsystem: "aurora_gateway",
|
||||
Name: "card_payout_callbacks_total",
|
||||
Help: "Aurora card payout callbacks grouped by provider status.",
|
||||
}, []string{"status"})
|
||||
})
|
||||
}
|
||||
|
||||
// ObserveCallback records callback status for Aurora card payouts.
|
||||
func ObserveCallback(status string) {
|
||||
initMetrics()
|
||||
status = strings.TrimSpace(status)
|
||||
if status == "" {
|
||||
status = "unknown"
|
||||
}
|
||||
status = strings.ToLower(status)
|
||||
if cardPayoutCallbacks != nil {
|
||||
cardPayoutCallbacks.WithLabelValues(status).Inc()
|
||||
}
|
||||
}
|
||||
11
api/gateway/aurora/internal/service/provider/payloads.go
Normal file
11
api/gateway/aurora/internal/service/provider/payloads.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package provider
|
||||
|
||||
// CardPayoutSendResult is the minimal provider result contract used by Aurora simulator.
|
||||
type CardPayoutSendResult struct {
|
||||
Accepted bool
|
||||
ProviderRequestID string
|
||||
ProviderStatus string
|
||||
StatusCode int
|
||||
ErrorCode string
|
||||
ErrorMessage string
|
||||
}
|
||||
112
api/gateway/aurora/internal/service/provider/signature.go
Normal file
112
api/gateway/aurora/internal/service/provider/signature.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/hmac"
|
||||
"crypto/sha512"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func signPayload(payload any, secret string) (string, error) {
|
||||
canonical, err := signaturePayloadString(payload)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
mac := hmac.New(sha512.New, []byte(secret))
|
||||
if _, err := mac.Write([]byte(canonical)); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.StdEncoding.EncodeToString(mac.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// SignPayload exposes signature calculation for callback verification.
|
||||
func SignPayload(payload any, secret string) (string, error) {
|
||||
return signPayload(payload, secret)
|
||||
}
|
||||
|
||||
func signaturePayloadString(payload any) (string, error) {
|
||||
data, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var root any
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
decoder.UseNumber()
|
||||
if err := decoder.Decode(&root); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
lines := make([]string, 0)
|
||||
collectSignatureLines(nil, root, &lines)
|
||||
sort.Strings(lines)
|
||||
|
||||
return strings.Join(lines, ";"), nil
|
||||
}
|
||||
|
||||
func collectSignatureLines(path []string, value any, lines *[]string) {
|
||||
switch v := value.(type) {
|
||||
case map[string]any:
|
||||
for key, child := range v {
|
||||
if strings.EqualFold(key, "signature") {
|
||||
continue
|
||||
}
|
||||
collectSignatureLines(append(path, key), child, lines)
|
||||
}
|
||||
case []any:
|
||||
if len(v) == 0 {
|
||||
return
|
||||
}
|
||||
for idx, child := range v {
|
||||
collectSignatureLines(append(path, strconv.Itoa(idx)), child, lines)
|
||||
}
|
||||
default:
|
||||
line := formatSignatureLine(path, v)
|
||||
if line != "" {
|
||||
*lines = append(*lines, line)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func formatSignatureLine(path []string, value any) string {
|
||||
if len(path) == 0 {
|
||||
return ""
|
||||
}
|
||||
val := signatureValueString(value)
|
||||
segments := append(append([]string{}, path...), val)
|
||||
return strings.Join(segments, ":")
|
||||
}
|
||||
|
||||
func signatureValueString(value any) string {
|
||||
switch v := value.(type) {
|
||||
case nil:
|
||||
return "null"
|
||||
case string:
|
||||
return v
|
||||
case json.Number:
|
||||
return v.String()
|
||||
case bool:
|
||||
if v {
|
||||
return "1"
|
||||
}
|
||||
return "0"
|
||||
case float64:
|
||||
return strconv.FormatFloat(v, 'f', -1, 64)
|
||||
case float32:
|
||||
return strconv.FormatFloat(float64(v), 'f', -1, 32)
|
||||
case int:
|
||||
return strconv.Itoa(v)
|
||||
case int8, int16, int32, int64:
|
||||
return fmt.Sprint(v)
|
||||
case uint, uint8, uint16, uint32, uint64:
|
||||
return fmt.Sprint(v)
|
||||
default:
|
||||
return fmt.Sprint(v)
|
||||
}
|
||||
}
|
||||
211
api/gateway/aurora/internal/service/provider/signature_test.go
Normal file
211
api/gateway/aurora/internal/service/provider/signature_test.go
Normal file
@@ -0,0 +1,211 @@
|
||||
package provider
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSignaturePayloadString_Example(t *testing.T) {
|
||||
payload := map[string]any{
|
||||
"general": map[string]any{
|
||||
"project_id": 3254,
|
||||
"payment_id": "id_38202316",
|
||||
"signature": "<ignored>",
|
||||
},
|
||||
"customer": map[string]any{
|
||||
"id": "585741",
|
||||
"email": "johndoe@example.com",
|
||||
"first_name": "John",
|
||||
"last_name": "Doe",
|
||||
"address": "Downing str., 23",
|
||||
"identify": map[string]any{
|
||||
"doc_number": "54122312544",
|
||||
},
|
||||
"ip_address": "198.51.100.47",
|
||||
},
|
||||
"payment": map[string]any{
|
||||
"amount": 10800,
|
||||
"currency": "USD",
|
||||
"description": "Computer keyboards",
|
||||
},
|
||||
"receipt_data": map[string]any{
|
||||
"positions": []any{
|
||||
map[string]any{
|
||||
"quantity": "10",
|
||||
"amount": "108",
|
||||
"description": "Computer keyboard",
|
||||
},
|
||||
},
|
||||
},
|
||||
"return_url": map[string]any{
|
||||
"success": "https://paymentpage.example.com/complete-redirect?id=success",
|
||||
"decline": "https://paymentpage.example.com/complete-redirect?id=decline",
|
||||
},
|
||||
}
|
||||
|
||||
got, err := signaturePayloadString(payload)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to build signature string: %v", err)
|
||||
}
|
||||
|
||||
expected := "customer:address:Downing str., 23;customer:email:johndoe@example.com;customer:first_name:John;customer:id:585741;customer:identify:doc_number:54122312544;customer:ip_address:198.51.100.47;customer:last_name:Doe;general:payment_id:id_38202316;general:project_id:3254;payment:amount:10800;payment:currency:USD;payment:description:Computer keyboards;receipt_data:positions:0:amount:108;receipt_data:positions:0:description:Computer keyboard;receipt_data:positions:0:quantity:10;return_url:decline:https://paymentpage.example.com/complete-redirect?id=decline;return_url:success:https://paymentpage.example.com/complete-redirect?id=success"
|
||||
if got != expected {
|
||||
t.Fatalf("unexpected signature string\nexpected: %s\ngot: %s", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignPayload_Example(t *testing.T) {
|
||||
payload := map[string]any{
|
||||
"general": map[string]any{
|
||||
"project_id": 3254,
|
||||
"payment_id": "id_38202316",
|
||||
"signature": "<ignored>",
|
||||
},
|
||||
"customer": map[string]any{
|
||||
"id": "585741",
|
||||
"email": "johndoe@example.com",
|
||||
"first_name": "John",
|
||||
"last_name": "Doe",
|
||||
"address": "Downing str., 23",
|
||||
"identify": map[string]any{
|
||||
"doc_number": "54122312544",
|
||||
},
|
||||
"ip_address": "198.51.100.47",
|
||||
},
|
||||
"payment": map[string]any{
|
||||
"amount": 10800,
|
||||
"currency": "USD",
|
||||
"description": "Computer keyboards",
|
||||
},
|
||||
"receipt_data": map[string]any{
|
||||
"positions": []any{
|
||||
map[string]any{
|
||||
"quantity": "10",
|
||||
"amount": "108",
|
||||
"description": "Computer keyboard",
|
||||
},
|
||||
},
|
||||
},
|
||||
"return_url": map[string]any{
|
||||
"success": "https://paymentpage.example.com/complete-redirect?id=success",
|
||||
"decline": "https://paymentpage.example.com/complete-redirect?id=decline",
|
||||
},
|
||||
}
|
||||
|
||||
got, err := SignPayload(payload, "secret")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to sign payload: %v", err)
|
||||
}
|
||||
expected := "lagSnuspAn+F6XkmQISqwtBg0PsiTy62fF9x33TM+278mnufIDZyi1yP0BQALuCxyikkIxIMbodBn2F8hMdRwA=="
|
||||
if got != expected {
|
||||
t.Fatalf("unexpected signature\nexpected: %s\ngot: %s", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignaturePayloadString_BooleansAndArrays(t *testing.T) {
|
||||
payload := map[string]any{
|
||||
"flag": true,
|
||||
"false_flag": false,
|
||||
"empty": "",
|
||||
"zero": 0,
|
||||
"nested": map[string]any{
|
||||
"list": []any{},
|
||||
"items": []any{"alpha", "beta"},
|
||||
},
|
||||
}
|
||||
|
||||
got, err := signaturePayloadString(payload)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to build signature string: %v", err)
|
||||
}
|
||||
|
||||
expected := "empty:;false_flag:0;flag:1;nested:items:0:alpha;nested:items:1:beta;zero:0"
|
||||
if got != expected {
|
||||
t.Fatalf("unexpected signature string\nexpected: %s\ngot: %s", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignPayload_EthEstimateGasExample(t *testing.T) {
|
||||
payload := map[string]any{
|
||||
"jsonrpc": "2.0",
|
||||
"id": 3,
|
||||
"method": "eth_estimateGas",
|
||||
"params": []any{
|
||||
map[string]any{
|
||||
"from": "0xfa89b4d534bdeb2713d4ffd893e79d6535fb58f8",
|
||||
"to": "0x44162e39eefd9296231e772663a92e72958e182f",
|
||||
"gasPrice": "0x64",
|
||||
"data": "0xa9059cbb00000000000000000000000044162e39eefd9296231e772663a92e72958e182f00000000000000000000000000000000000000000000000000000000000f4240",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
got, err := SignPayload(payload, "1")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to sign payload: %v", err)
|
||||
}
|
||||
expected := "C4WbSvXKSMyX8yLamQcUe/Nzr6nSt9m3HYn4jHSyA7yi/FaTiqk0r8BlfIzfxSCoDaRgrSd82ihgZW+DxELhdQ=="
|
||||
if got != expected {
|
||||
t.Fatalf("unexpected signature\nexpected: %s\ngot: %s", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignPayload_AuroraCallbackExample(t *testing.T) {
|
||||
payload := map[string]any{
|
||||
"customer": map[string]any{
|
||||
"id": "694ece88df756c2672dc6ce8",
|
||||
},
|
||||
"account": map[string]any{
|
||||
"number": "220070******0161",
|
||||
"type": "mir",
|
||||
"card_holder": "STEPHAN",
|
||||
"expiry_month": "03",
|
||||
"expiry_year": "2030",
|
||||
},
|
||||
"project_id": 157432,
|
||||
"payment": map[string]any{
|
||||
"id": "6952d0b307d2916aba87d4e8",
|
||||
"type": "payout",
|
||||
"status": "success",
|
||||
"date": "2025-12-29T19:04:24+0000",
|
||||
"method": "card",
|
||||
"sum": map[string]any{
|
||||
"amount": 10849,
|
||||
"currency": "RUB",
|
||||
},
|
||||
"description": "",
|
||||
},
|
||||
"operation": map[string]any{
|
||||
"sum_initial": map[string]any{
|
||||
"amount": 10849,
|
||||
"currency": "RUB",
|
||||
},
|
||||
"sum_converted": map[string]any{
|
||||
"amount": 10849,
|
||||
"currency": "RUB",
|
||||
},
|
||||
"code": "0",
|
||||
"message": "Success",
|
||||
"provider": map[string]any{
|
||||
"id": 26226,
|
||||
"payment_id": "a3761838-eabc-4c65-aa36-c854c47a226b",
|
||||
"auth_code": "",
|
||||
"endpoint_id": 26226,
|
||||
"date": "2025-12-29T19:04:23+0000",
|
||||
},
|
||||
"id": int64(5089807000008124),
|
||||
"type": "payout",
|
||||
"status": "success",
|
||||
"date": "2025-12-29T19:04:24+0000",
|
||||
"created_date": "2025-12-29T19:04:21+0000",
|
||||
"request_id": "7c3032f00629c94ad78e399c87da936f1cdc30de-2559ba11d6958d558a9f8ab8c20474d33061c654-05089808",
|
||||
},
|
||||
"signature": "IBgtwCoxhMUxF15q8DLc7orYOIJomeiaNpWs8JHHsdDYPKJsIKn4T+kYavPnKTO+yibhCLNKeL+hk2oWg9wPCQ==",
|
||||
}
|
||||
|
||||
got, err := SignPayload(payload, "1")
|
||||
if err != nil {
|
||||
t.Fatalf("failed to sign payload: %v", err)
|
||||
}
|
||||
expected := "IBgtwCoxhMUxF15q8DLc7orYOIJomeiaNpWs8JHHsdDYPKJsIKn4T+kYavPnKTO+yibhCLNKeL+hk2oWg9wPCQ=="
|
||||
if got != expected {
|
||||
t.Fatalf("unexpected signature\nexpected: %s\ngot: %s", expected, got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user