refactor/checkout (#8)
Co-authored-by: matst80 <mats.tornberg@gmail.com> Reviewed-on: #8 Co-authored-by: Mats Törnberg <mats@tornberg.me> Co-committed-by: Mats Törnberg <mats@tornberg.me>
This commit was merged in pull request #8.
This commit is contained in:
84
pkg/checkout/mutation_payment_started.go
Normal file
84
pkg/checkout/mutation_payment_started.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package checkout
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
messages "git.k6n.net/go-cart-actor/proto/checkout"
|
||||
)
|
||||
|
||||
// PaymentStarted registers the beginning of a payment attempt for a checkout.
|
||||
// It either upserts the payment entry (based on paymentId) or creates a new one,
|
||||
// marks the checkout as having a payment in progress.
|
||||
func HandlePaymentStarted(g *CheckoutGrain, m *messages.PaymentStarted) error {
|
||||
if m == nil {
|
||||
return fmt.Errorf("PaymentStarted: nil payload")
|
||||
}
|
||||
paymentID := strings.TrimSpace(m.PaymentId)
|
||||
if paymentID == "" {
|
||||
return fmt.Errorf("PaymentStarted: missing paymentId")
|
||||
}
|
||||
if m.Amount < 0 {
|
||||
return fmt.Errorf("PaymentStarted: amount cannot be negative")
|
||||
}
|
||||
|
||||
currency := strings.TrimSpace(m.Currency)
|
||||
provider := strings.TrimSpace(m.Provider)
|
||||
method := copyOptionalString(m.Method)
|
||||
|
||||
startedAt := time.Now().UTC()
|
||||
if m.StartedAt != nil {
|
||||
startedAt = m.StartedAt.AsTime()
|
||||
}
|
||||
|
||||
payment, found := g.FindPayment(paymentID)
|
||||
|
||||
if found {
|
||||
if payment.Status != "pending" {
|
||||
return fmt.Errorf("PaymentStarted: payment already started")
|
||||
}
|
||||
if payment.PaymentId != paymentID {
|
||||
payment.PaymentId = paymentID
|
||||
}
|
||||
payment.Status = "pending"
|
||||
payment.Amount = m.Amount
|
||||
if currency != "" {
|
||||
payment.Currency = currency
|
||||
}
|
||||
if provider != "" {
|
||||
payment.Provider = provider
|
||||
}
|
||||
if method != nil {
|
||||
payment.Method = method
|
||||
}
|
||||
payment.StartedAt = &startedAt
|
||||
payment.CompletedAt = nil
|
||||
payment.ProcessorReference = nil
|
||||
} else {
|
||||
g.PaymentInProgress++
|
||||
g.Payments = append(g.Payments, &Payment{
|
||||
PaymentId: paymentID,
|
||||
Status: "pending",
|
||||
Amount: m.Amount,
|
||||
Currency: currency,
|
||||
Provider: provider,
|
||||
Method: method,
|
||||
StartedAt: &startedAt,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func copyOptionalString(src *string) *string {
|
||||
if src == nil {
|
||||
return nil
|
||||
}
|
||||
trimmed := strings.TrimSpace(*src)
|
||||
if trimmed == "" {
|
||||
return nil
|
||||
}
|
||||
dst := trimmed
|
||||
return &dst
|
||||
}
|
||||
Reference in New Issue
Block a user