Files
go-cart-actor/pkg/checkout/mutation_payment_completed.go
matst80 7964e8b849
Some checks failed
Build and Publish / BuildAndDeployAmd64 (push) Successful in 43s
Build and Publish / BuildAndDeployArm64 (push) Has been cancelled
more
2025-12-04 22:59:07 +01:00

48 lines
1009 B
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 = m.CompletedAt.AsTime()
} else {
*payment.CompletedAt = 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
}