48 lines
1.0 KiB
Go
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
|
|
}
|