fix
This commit is contained in:
@@ -42,16 +42,17 @@ const (
|
||||
)
|
||||
|
||||
type Payment struct {
|
||||
PaymentId string `json:"paymentId"`
|
||||
Status PaymentStatus `json:"status"`
|
||||
Amount int64 `json:"amount"`
|
||||
Currency string `json:"currency"`
|
||||
Provider string `json:"provider,omitempty"`
|
||||
Method *string `json:"method,omitempty"`
|
||||
Events []*PaymentEvent `json:"events,omitempty"`
|
||||
ProcessorReference *string `json:"processorReference,omitempty"`
|
||||
StartedAt *time.Time `json:"startedAt,omitempty"`
|
||||
CompletedAt *time.Time `json:"completedAt,omitempty"`
|
||||
PaymentId string `json:"paymentId"`
|
||||
Status PaymentStatus `json:"status"`
|
||||
Amount int64 `json:"amount"`
|
||||
Currency string `json:"currency"`
|
||||
Provider string `json:"provider,omitempty"`
|
||||
Method *string `json:"method,omitempty"`
|
||||
SessionData *json.RawMessage `json:"sessionData,omitempty"`
|
||||
Events []PaymentEvent `json:"events,omitempty"`
|
||||
ProcessorReference *string `json:"processorReference,omitempty"`
|
||||
StartedAt *time.Time `json:"startedAt,omitempty"`
|
||||
CompletedAt *time.Time `json:"completedAt,omitempty"`
|
||||
}
|
||||
|
||||
type PaymentEvent struct {
|
||||
|
||||
@@ -11,7 +11,7 @@ func HandlePaymentEvent(g *CheckoutGrain, m *messages.PaymentEvent) error {
|
||||
return ErrPaymentNotFound
|
||||
}
|
||||
metaBytes := m.Data.Value
|
||||
payment.Events = append(payment.Events, &PaymentEvent{
|
||||
payment.Events = append(payment.Events, PaymentEvent{
|
||||
Name: m.Name,
|
||||
Success: m.Success,
|
||||
Data: metaBytes,
|
||||
|
||||
124
pkg/checkout/mutation_payment_event_test.go
Normal file
124
pkg/checkout/mutation_payment_event_test.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package checkout
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
checkout_messages "git.k6n.net/go-cart-actor/proto/checkout"
|
||||
"google.golang.org/protobuf/types/known/anypb"
|
||||
)
|
||||
|
||||
func TestHandlePaymentEvent(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
initialPayments []*Payment
|
||||
event *checkout_messages.PaymentEvent
|
||||
expectedEvents []PaymentEvent
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
name: "add event to existing payment",
|
||||
initialPayments: []*Payment{
|
||||
{
|
||||
PaymentId: "pay123",
|
||||
Events: []PaymentEvent{
|
||||
{Name: "init", Success: true, Data: json.RawMessage(`{"key":"value"}`)},
|
||||
},
|
||||
},
|
||||
},
|
||||
event: &checkout_messages.PaymentEvent{
|
||||
PaymentId: "pay123",
|
||||
Name: "capture",
|
||||
Success: true,
|
||||
Data: &anypb.Any{Value: []byte(`{"amount":100}`)},
|
||||
},
|
||||
expectedEvents: []PaymentEvent{
|
||||
{Name: "init", Success: true, Data: json.RawMessage(`{"key":"value"}`)},
|
||||
{Name: "capture", Success: true, Data: json.RawMessage(`{"amount":100}`)},
|
||||
},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "add event to payment with no initial events",
|
||||
initialPayments: []*Payment{
|
||||
{
|
||||
PaymentId: "pay456",
|
||||
Events: []PaymentEvent{},
|
||||
},
|
||||
},
|
||||
event: &checkout_messages.PaymentEvent{
|
||||
PaymentId: "pay456",
|
||||
Name: "refund",
|
||||
Success: false,
|
||||
Data: &anypb.Any{Value: []byte(`{"reason":"failed"}`)},
|
||||
},
|
||||
expectedEvents: []PaymentEvent{
|
||||
{Name: "refund", Success: false, Data: json.RawMessage(`{"reason":"failed"}`)},
|
||||
},
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "payment not found",
|
||||
initialPayments: []*Payment{
|
||||
{
|
||||
PaymentId: "pay123",
|
||||
},
|
||||
},
|
||||
event: &checkout_messages.PaymentEvent{
|
||||
PaymentId: "nonexistent",
|
||||
Name: "test",
|
||||
Success: true,
|
||||
},
|
||||
expectedEvents: nil,
|
||||
expectError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
grain := &CheckoutGrain{
|
||||
Payments: tt.initialPayments,
|
||||
}
|
||||
|
||||
err := HandlePaymentEvent(grain, tt.event)
|
||||
|
||||
if tt.expectError {
|
||||
if err == nil {
|
||||
t.Errorf("expected error, got nil")
|
||||
} else if err != ErrPaymentNotFound {
|
||||
t.Errorf("expected ErrPaymentNotFound, got %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
payment, found := grain.FindPayment(tt.event.PaymentId)
|
||||
if !found {
|
||||
t.Errorf("payment not found after handling event")
|
||||
return
|
||||
}
|
||||
|
||||
if len(payment.Events) != len(tt.expectedEvents) {
|
||||
t.Errorf("expected %d events, got %d", len(tt.expectedEvents), len(payment.Events))
|
||||
return
|
||||
}
|
||||
|
||||
for i, expected := range tt.expectedEvents {
|
||||
actual := payment.Events[i]
|
||||
if actual.Name != expected.Name {
|
||||
t.Errorf("event %d name mismatch: got %s, expected %s", i, actual.Name, expected.Name)
|
||||
}
|
||||
if actual.Success != expected.Success {
|
||||
t.Errorf("event %d success mismatch: got %t, expected %t", i, actual.Success, expected.Success)
|
||||
}
|
||||
if string(actual.Data) != string(expected.Data) {
|
||||
t.Errorf("event %d data mismatch: got %s, expected %s", i, string(actual.Data), string(expected.Data))
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package checkout
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -52,19 +53,23 @@ func HandlePaymentStarted(g *CheckoutGrain, m *messages.PaymentStarted) error {
|
||||
if method != nil {
|
||||
payment.Method = method
|
||||
}
|
||||
if m.SessionData != nil {
|
||||
payment.SessionData = (*json.RawMessage)(&m.SessionData.Value)
|
||||
}
|
||||
payment.StartedAt = &startedAt
|
||||
payment.CompletedAt = nil
|
||||
payment.ProcessorReference = nil
|
||||
} else {
|
||||
g.PaymentInProgress++
|
||||
g.Payments = append(g.Payments, &Payment{
|
||||
PaymentId: paymentID,
|
||||
Status: "pending",
|
||||
Amount: m.Amount,
|
||||
Currency: currency,
|
||||
Provider: provider,
|
||||
Method: method,
|
||||
StartedAt: &startedAt,
|
||||
PaymentId: paymentID,
|
||||
Status: "pending",
|
||||
Amount: m.Amount,
|
||||
SessionData: (*json.RawMessage)(&m.SessionData.Value),
|
||||
Currency: currency,
|
||||
Provider: provider,
|
||||
Method: method,
|
||||
StartedAt: &startedAt,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
package checkout
|
||||
|
||||
// This file is now empty as mutations have been moved to separate files.
|
||||
Reference in New Issue
Block a user