Files
go-cart-actor/pkg/checkout/mutation_payment_completed.go
matst80 593d6cfe44
All checks were successful
Build and Publish / BuildAndDeployAmd64 (push) Successful in 43s
Build and Publish / BuildAndDeployArm64 (push) Successful in 5m3s
s
2025-12-04 23:06:04 +01:00

48 lines
1.0 KiB
Go

package checkout
import (
"fmt"
"time"
messages "git.k6n.net/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
for _, payment := range g.SettledPayments() {
sum -= payment.Amount
}
g.AmountInCentsRemaining = sum
return nil
}