more functions

This commit is contained in:
matst80
2025-11-20 21:34:39 +01:00
parent 60cd6cfd51
commit 729c67f992
6 changed files with 122 additions and 75 deletions

View File

@@ -78,6 +78,12 @@ type SubscriptionDetails struct {
Meta json.RawMessage `json:"data,omitempty"`
}
type Notice struct {
Timestamp time.Time `json:"timestamp"`
Message string `json:"message"`
Code *string `json:"code,omitempty"`
}
type Marking struct {
Type uint32 `json:"type"`
Text string `json:"text"`
@@ -104,7 +110,7 @@ type CartGrain struct {
Vouchers []*Voucher `json:"vouchers,omitempty"`
Notifications []CartNotification `json:"cartNotification,omitempty"`
SubscriptionDetails map[string]*SubscriptionDetails `json:"subscriptionDetails,omitempty"`
PaymentDeclinedAt time.Time `json:"paymentDeclinedAt,omitempty"`
PaymentDeclinedNotices []Notice `json:"paymentDeclinedNotices,omitempty"`
ConfirmationViewCount int `json:"confirmationViewCount,omitempty"`
ConfirmationLastViewedAt time.Time `json:"confirmationLastViewedAt,omitempty"`
CheckoutOrderId string `json:"checkoutOrderId,omitempty"`

View File

@@ -2,12 +2,17 @@ package cart
import (
"time"
messages "git.tornberg.me/go-cart-actor/pkg/messages"
)
func PaymentDeclined(grain *CartGrain, req *messages.PaymentDeclined) error {
grain.PaymentStatus = "declined"
grain.PaymentDeclinedAt = time.Now()
grain.PaymentDeclinedNotices = append(grain.PaymentDeclinedNotices, Notice{
Timestamp: time.Now(),
Message: req.Message,
Code: req.Code,
})
// Optionally clear checkout order if in progress
if grain.CheckoutOrderId != "" {
grain.CheckoutOrderId = ""

View File

@@ -101,8 +101,8 @@ func msgSubscriptionAdded(itemId uint32, detailsId, orderRef string) *messages.S
return &messages.SubscriptionAdded{ItemId: itemId, DetailsId: detailsId, OrderReference: orderRef}
}
func msgPaymentDeclined() *messages.PaymentDeclined {
return &messages.PaymentDeclined{}
func msgPaymentDeclined(message, code string) *messages.PaymentDeclined {
return &messages.PaymentDeclined{Message: message, Code: &code}
}
func msgConfirmationViewed() *messages.ConfirmationViewed {
@@ -650,12 +650,22 @@ func TestPaymentDeclined(t *testing.T) {
g := newTestGrain()
g.CheckoutOrderId = "test-order"
applyOK(t, reg, g, msgPaymentDeclined())
applyOK(t, reg, g, msgPaymentDeclined("Payment failed due to insufficient funds", "INSUFFICIENT_FUNDS"))
if g.PaymentStatus != "declined" || g.CheckoutOrderId != "" {
t.Fatalf("payment declined not handled: status=%s checkoutId=%s", g.PaymentStatus, g.CheckoutOrderId)
}
if g.PaymentDeclinedAt.IsZero() {
t.Fatalf("PaymentDeclinedAt not set")
if len(g.PaymentDeclinedNotices) != 1 {
t.Fatalf("expected 1 notice, got %d", len(g.PaymentDeclinedNotices))
}
notice := g.PaymentDeclinedNotices[0]
if notice.Message != "Payment failed due to insufficient funds" {
t.Fatalf("notice message not set correctly: %s", notice.Message)
}
if notice.Code == nil || *notice.Code != "INSUFFICIENT_FUNDS" {
t.Fatalf("notice code not set correctly: %v", notice.Code)
}
if notice.Timestamp.IsZero() {
t.Fatalf("notice timestamp not set")
}
}