Files
go-cart-actor/pkg/checkout/mutation_payment_completed.go
T
mats aa8b2bdedc
Build and Publish / BuildAndDeployAmd64 (push) Failing after 5s
Build and Publish / BuildAndDeployArm64 (push) Failing after 6s
all the refactor
2026-06-28 17:51:52 +02:00

48 lines
1.0 KiB
Go

package checkout
import (
"fmt"
"time"
messages "git.k6n.net/mats/go-cart-actor/proto/checkout"
)
// PaymentCompleted registers the completion of a payment for a checkout.
func HandlePaymentCompleted(g *CheckoutGrain, m *messages.PaymentCompleted) error {
if m == nil {
return fmt.Errorf("PaymentCompleted: nil payload")
}
paymentId := m.PaymentId
payment, found := g.FindPayment(paymentId)
if !found {
return fmt.Errorf("PaymentCompleted: payment not found")
}
payment.ProcessorReference = m.ProcessorReference
payment.Status = PaymentStatusSuccess
if m.Amount > 0 {
payment.Amount = m.Amount
}
if m.Currency != "" {
payment.Currency = m.Currency
}
if m.CompletedAt != nil {
payment.CompletedAt = asPointer(m.CompletedAt.AsTime())
} else {
payment.CompletedAt = asPointer(time.Now())
}
// Update checkout status
g.PaymentInProgress--
sum := g.CartState.TotalPrice.IncVat.Int64()
for _, payment := range g.SettledPayments() {
sum -= payment.Amount
}
g.AmountInCentsRemaining = sum
return nil
}