mirror of
https://github.com/hexolan/stocklet.git
synced 2026-03-26 11:41:18 +00:00
chore: initial commit
This commit is contained in:
39
internal/svc/payment/api/gateway.go
Normal file
39
internal/svc/payment/api/gateway.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/payment/v1"
|
||||
"github.com/hexolan/stocklet/internal/pkg/serve"
|
||||
"github.com/hexolan/stocklet/internal/svc/payment"
|
||||
)
|
||||
|
||||
func PrepareGateway(cfg *payment.ServiceConfig) *runtime.ServeMux {
|
||||
mux, clientOpts := serve.NewGatewayServeBase(&cfg.Shared)
|
||||
|
||||
ctx := context.Background()
|
||||
err := pb.RegisterPaymentServiceHandlerFromEndpoint(ctx, mux, serve.GetAddrToGrpc("localhost"), clientOpts)
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("failed to register endpoint for gateway")
|
||||
}
|
||||
|
||||
return mux
|
||||
}
|
||||
30
internal/svc/payment/api/grpc.go
Normal file
30
internal/svc/payment/api/grpc.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package api
|
||||
|
||||
import (
|
||||
"google.golang.org/grpc"
|
||||
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/payment/v1"
|
||||
"github.com/hexolan/stocklet/internal/pkg/serve"
|
||||
"github.com/hexolan/stocklet/internal/svc/payment"
|
||||
)
|
||||
|
||||
func PrepareGrpc(cfg *payment.ServiceConfig, svc *payment.PaymentService) *grpc.Server {
|
||||
svr := serve.NewGrpcServeBase(&cfg.Shared)
|
||||
pb.RegisterPaymentServiceServer(svr, svc)
|
||||
return svr
|
||||
}
|
||||
42
internal/svc/payment/config.go
Normal file
42
internal/svc/payment/config.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package payment
|
||||
|
||||
import (
|
||||
"github.com/hexolan/stocklet/internal/pkg/config"
|
||||
)
|
||||
|
||||
// Order Service Configuration
|
||||
type ServiceConfig struct {
|
||||
// Core Configuration
|
||||
Shared config.SharedConfig
|
||||
|
||||
// Dynamically loaded configuration
|
||||
Postgres config.PostgresConfig
|
||||
Kafka config.KafkaConfig
|
||||
}
|
||||
|
||||
// load the base service configuration
|
||||
func NewServiceConfig() (*ServiceConfig, error) {
|
||||
cfg := ServiceConfig{}
|
||||
|
||||
// Load the core configuration
|
||||
if err := cfg.Shared.Load(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
140
internal/svc/payment/controller/kafka.go
Normal file
140
internal/svc/payment/controller/kafka.go
Normal file
@@ -0,0 +1,140 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/twmb/franz-go/pkg/kgo"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/hexolan/stocklet/internal/pkg/messaging"
|
||||
eventpb "github.com/hexolan/stocklet/internal/pkg/protogen/events/v1"
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/payment/v1"
|
||||
"github.com/hexolan/stocklet/internal/svc/payment"
|
||||
)
|
||||
|
||||
type kafkaController struct {
|
||||
cl *kgo.Client
|
||||
|
||||
svc pb.PaymentServiceServer
|
||||
|
||||
ctx context.Context
|
||||
ctxCancel context.CancelFunc
|
||||
}
|
||||
|
||||
func NewKafkaController(cl *kgo.Client) payment.ConsumerController {
|
||||
// Create a cancellable context for the consumer
|
||||
ctx, ctxCancel := context.WithCancel(context.Background())
|
||||
|
||||
// Ensure the required Kafka topics exist
|
||||
err := messaging.EnsureKafkaTopics(
|
||||
cl,
|
||||
|
||||
messaging.Payment_Balance_Created_Topic,
|
||||
messaging.Payment_Balance_Credited_Topic,
|
||||
messaging.Payment_Balance_Debited_Topic,
|
||||
messaging.Payment_Balance_Closed_Topic,
|
||||
messaging.Payment_Transaction_Created_Topic,
|
||||
messaging.Payment_Transaction_Reversed_Topic,
|
||||
messaging.Payment_Processing_Topic,
|
||||
|
||||
messaging.User_State_Created_Topic,
|
||||
|
||||
messaging.Shipping_Shipment_Allocation_Topic,
|
||||
)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("kafka: raised attempting to ensure svc topics")
|
||||
}
|
||||
|
||||
// Add the consumption topics
|
||||
cl.AddConsumeTopics(
|
||||
messaging.User_State_Created_Topic,
|
||||
messaging.Shipping_Shipment_Allocation_Topic,
|
||||
)
|
||||
|
||||
return &kafkaController{cl: cl, ctx: ctx, ctxCancel: ctxCancel}
|
||||
}
|
||||
|
||||
func (c *kafkaController) Attach(svc pb.PaymentServiceServer) {
|
||||
c.svc = svc
|
||||
}
|
||||
|
||||
func (c *kafkaController) Start() {
|
||||
if c.svc == nil {
|
||||
log.Panic().Msg("consumer: no service interface attached")
|
||||
}
|
||||
|
||||
for {
|
||||
fetches := c.cl.PollFetches(c.ctx)
|
||||
if errs := fetches.Errors(); len(errs) > 0 {
|
||||
log.Panic().Any("kafka-errs", errs).Msg("consumer: unrecoverable kafka errors")
|
||||
}
|
||||
|
||||
fetches.EachTopic(func(ft kgo.FetchTopic) {
|
||||
switch ft.Topic {
|
||||
case messaging.User_State_Created_Topic:
|
||||
c.consumeUserCreatedEventTopic(ft)
|
||||
case messaging.Shipping_Shipment_Allocation_Topic:
|
||||
c.consumeShipmentAllocationEventTopic(ft)
|
||||
default:
|
||||
log.Warn().Str("topic", ft.Topic).Msg("consumer: recieved records from unexpected topic")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (c *kafkaController) Stop() {
|
||||
// Cancel the consumer context
|
||||
c.ctxCancel()
|
||||
}
|
||||
|
||||
func (c *kafkaController) consumeUserCreatedEventTopic(ft kgo.FetchTopic) {
|
||||
log.Info().Str("topic", ft.Topic).Msg("consumer: recieved records from topic")
|
||||
|
||||
// Process each message from the topic
|
||||
ft.EachRecord(func(record *kgo.Record) {
|
||||
// Unmarshal the event
|
||||
var event eventpb.UserCreatedEvent
|
||||
err := proto.Unmarshal(record.Value, &event)
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("consumer: failed to unmarshal event")
|
||||
}
|
||||
|
||||
// Process the event
|
||||
ctx := context.Background()
|
||||
c.svc.ProcessUserCreatedEvent(ctx, &event)
|
||||
})
|
||||
}
|
||||
|
||||
func (c *kafkaController) consumeShipmentAllocationEventTopic(ft kgo.FetchTopic) {
|
||||
log.Info().Str("topic", ft.Topic).Msg("consumer: recieved records from topic")
|
||||
|
||||
// Process each message from the topic
|
||||
ft.EachRecord(func(record *kgo.Record) {
|
||||
// Unmarshal the event
|
||||
var event eventpb.ShipmentAllocationEvent
|
||||
err := proto.Unmarshal(record.Value, &event)
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("consumer: failed to unmarshal event")
|
||||
}
|
||||
|
||||
// Process the event
|
||||
ctx := context.Background()
|
||||
c.svc.ProcessShipmentAllocationEvent(ctx, &event)
|
||||
})
|
||||
}
|
||||
458
internal/svc/payment/controller/postgres.go
Normal file
458
internal/svc/payment/controller/postgres.go
Normal file
@@ -0,0 +1,458 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/hexolan/stocklet/internal/pkg/errors"
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/payment/v1"
|
||||
"github.com/hexolan/stocklet/internal/svc/payment"
|
||||
)
|
||||
|
||||
const (
|
||||
pgTransactionBaseQuery string = "SELECT id, order_id, customer_id, amount, reversed_at, processed_at FROM transactions"
|
||||
pgCustomerBalanceBaseQuery string = "SELECT customer_id, balance FROM customer_balances"
|
||||
)
|
||||
|
||||
type postgresController struct {
|
||||
cl *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewPostgresController(cl *pgxpool.Pool) payment.StorageController {
|
||||
return postgresController{cl: cl}
|
||||
}
|
||||
|
||||
func (c postgresController) GetBalance(ctx context.Context, customerId string) (*pb.CustomerBalance, error) {
|
||||
return c.getBalance(ctx, nil, customerId)
|
||||
}
|
||||
|
||||
func (c postgresController) getBalance(ctx context.Context, tx *pgx.Tx, customerId string) (*pb.CustomerBalance, error) {
|
||||
// Determine if a db transaction is being used
|
||||
var row pgx.Row
|
||||
const query = pgCustomerBalanceBaseQuery + " WHERE customer_id=$1"
|
||||
if tx == nil {
|
||||
row = c.cl.QueryRow(ctx, query, customerId)
|
||||
} else {
|
||||
row = (*tx).QueryRow(ctx, query, customerId)
|
||||
}
|
||||
|
||||
// Scan row to protobuf obj
|
||||
balance, err := scanRowToCustomerBalance(row)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return balance, nil
|
||||
}
|
||||
|
||||
func (c postgresController) GetTransaction(ctx context.Context, transactionId string) (*pb.Transaction, error) {
|
||||
return c.getTransaction(ctx, nil, transactionId)
|
||||
}
|
||||
|
||||
func (c postgresController) getTransaction(ctx context.Context, tx *pgx.Tx, transactionId string) (*pb.Transaction, error) {
|
||||
// Determine if a db transaction is being used
|
||||
var row pgx.Row
|
||||
const query = pgTransactionBaseQuery + " WHERE id=$1"
|
||||
if tx == nil {
|
||||
row = c.cl.QueryRow(ctx, query, transactionId)
|
||||
} else {
|
||||
row = (*tx).QueryRow(ctx, query, transactionId)
|
||||
}
|
||||
|
||||
// Scan row to protobuf obj
|
||||
transaction, err := scanRowToTransaction(row)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return transaction, nil
|
||||
}
|
||||
|
||||
func (c postgresController) CreateBalance(ctx context.Context, customerId string) error {
|
||||
// Begin a DB transaction
|
||||
tx, err := c.cl.Begin(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to begin transaction", err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
_, err = tx.Exec(
|
||||
ctx,
|
||||
"INSERT INTO customer_balances (customer_id, balance) VALUES ($1, $2)",
|
||||
customerId,
|
||||
0.00,
|
||||
)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to create balance", err)
|
||||
}
|
||||
|
||||
// Add the event to the outbox table with the transaction
|
||||
evt, evtTopic, err := payment.PrepareBalanceCreatedEvent(&pb.CustomerBalance{CustomerId: customerId, Balance: 0.00})
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeService, "failed to create event", err)
|
||||
}
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO event_outbox (aggregateid, aggregatetype, payload) VALUES ($1, $2, $3)", customerId, evtTopic, evt)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert event", err)
|
||||
}
|
||||
|
||||
// Commit the transaction
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to commit transaction", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c postgresController) CreditBalance(ctx context.Context, customerId string, amount float32) error {
|
||||
// Begin a DB transaction
|
||||
tx, err := c.cl.Begin(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to begin transaction", err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
// Add to balance
|
||||
_, err = tx.Exec(
|
||||
ctx,
|
||||
"UPDATE customer_balances SET balance = balance + $1 WHERE customer_id=$2",
|
||||
amount,
|
||||
customerId,
|
||||
)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to update balance", err)
|
||||
}
|
||||
|
||||
// Get updated balance
|
||||
balance, err := c.getBalance(ctx, &tx, customerId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Add the event to the outbox table with the transaction
|
||||
evt, evtTopic, err := payment.PrepareBalanceCreditedEvent(
|
||||
balance.CustomerId,
|
||||
amount,
|
||||
balance.Balance,
|
||||
)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeService, "failed to create event", err)
|
||||
}
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO event_outbox (aggregateid, aggregatetype, payload) VALUES ($1, $2, $3)", balance.CustomerId, evtTopic, evt)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert event", err)
|
||||
}
|
||||
|
||||
// Commit the transaction
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to commit transaction", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c postgresController) DebitBalance(ctx context.Context, customerId string, amount float32, orderId *string) (*pb.Transaction, error) {
|
||||
return c.debitBalance(ctx, nil, customerId, amount, orderId)
|
||||
}
|
||||
|
||||
func (c postgresController) debitBalance(ctx context.Context, tx *pgx.Tx, customerId string, amount float32, orderId *string) (*pb.Transaction, error) {
|
||||
// Determine if a transaction has already been provided
|
||||
var (
|
||||
funcTx pgx.Tx
|
||||
err error
|
||||
)
|
||||
if tx != nil {
|
||||
funcTx = *tx
|
||||
} else {
|
||||
funcTx, err = c.cl.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to begin transaction", err)
|
||||
}
|
||||
defer funcTx.Rollback(ctx)
|
||||
}
|
||||
|
||||
// Subtract from balance
|
||||
_, err = funcTx.Exec(
|
||||
ctx,
|
||||
"UPDATE customer_balances SET balance = balance - $1 WHERE customer_id=$2",
|
||||
amount,
|
||||
customerId,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to update balance", err)
|
||||
}
|
||||
|
||||
// Get updated balance
|
||||
balance, err := c.getBalance(ctx, &funcTx, customerId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Ensure that the balance is not negative
|
||||
if balance.Balance < 0.00 {
|
||||
return nil, errors.NewServiceError(errors.ErrCodeInvalidArgument, "insufficient balance")
|
||||
}
|
||||
|
||||
// Add the balance event to the outbox table with the transaction
|
||||
evt, evtTopic, err := payment.PrepareBalanceDebitedEvent(
|
||||
balance.CustomerId,
|
||||
amount,
|
||||
balance.Balance,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeService, "failed to create event", err)
|
||||
}
|
||||
|
||||
_, err = funcTx.Exec(ctx, "INSERT INTO event_outbox (aggregateid, aggregatetype, payload) VALUES ($1, $2, $3)", balance.CustomerId, evtTopic, evt)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert event", err)
|
||||
}
|
||||
|
||||
// Create a payment transaction record
|
||||
transaction, err := c.createTransaction(ctx, &funcTx, orderId, customerId, amount)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Commit the transaction (if created in this func)
|
||||
if tx == nil {
|
||||
err = funcTx.Commit(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to commit transaction", err)
|
||||
}
|
||||
}
|
||||
|
||||
return transaction, nil
|
||||
}
|
||||
|
||||
func (c postgresController) CloseBalance(ctx context.Context, customerId string) error {
|
||||
// Begin a DB transaction
|
||||
tx, err := c.cl.Begin(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to begin transaction", err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
// Get current balance
|
||||
balance, err := c.getBalance(ctx, &tx, customerId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete balance
|
||||
_, err = tx.Exec(ctx, "DELETE FROM customer_balances WHERE customer_id=$1", customerId)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to delete balance", err)
|
||||
}
|
||||
|
||||
// Add the event to the outbox table with the transaction
|
||||
evt, evtTopic, err := payment.PrepareBalanceClosedEvent(balance)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeService, "failed to create event", err)
|
||||
}
|
||||
|
||||
_, err = tx.Exec(ctx, "INSERT INTO event_outbox (aggregateid, aggregatetype, payload) VALUES ($1, $2, $3)", balance.CustomerId, evtTopic, evt)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert event", err)
|
||||
}
|
||||
|
||||
// Commit the transaction
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to commit transaction", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c postgresController) PaymentForOrder(ctx context.Context, orderId string, customerId string, amount float32) error {
|
||||
// Begin a DB transaction
|
||||
tx, err := c.cl.Begin(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to begin transaction", err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
// Attempt to debit balance for the order
|
||||
transaction, err := c.debitBalance(ctx, &tx, customerId, amount, &orderId)
|
||||
if err != nil {
|
||||
// check that error is not a result of insufficient balance
|
||||
// - or the customer not having a balance
|
||||
errText := err.Error()
|
||||
if errText != "insufficient balance" && !strings.HasPrefix(errText, "failed to update balance") {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// Prepare response event
|
||||
var (
|
||||
evt []byte
|
||||
evtTopic string
|
||||
)
|
||||
if transaction != nil {
|
||||
// Succesful
|
||||
evt, evtTopic, err = payment.PreparePaymentProcessedEvent_Success(transaction)
|
||||
} else {
|
||||
// Failure
|
||||
// - result of insufficient/non-existent balance
|
||||
evt, evtTopic, err = payment.PreparePaymentProcessedEvent_Failure(orderId, customerId, amount)
|
||||
}
|
||||
|
||||
// Ensure the event was prepared succesfully
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeService, "failed to create event", err)
|
||||
}
|
||||
|
||||
// Add the event to the outbox table
|
||||
_, err = tx.Exec(ctx, "INSERT INTO event_outbox (aggregateid, aggregatetype, payload) VALUES ($1, $2, $3)", orderId, evtTopic, evt)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert event", err)
|
||||
}
|
||||
|
||||
// Commit the transaction
|
||||
err = tx.Commit(ctx)
|
||||
if err != nil {
|
||||
return errors.WrapServiceError(errors.ErrCodeExtService, "failed to commit transaction", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c postgresController) createTransaction(ctx context.Context, tx *pgx.Tx, orderId *string, customerId string, amount float32) (*pb.Transaction, error) {
|
||||
// Determine if a transaction has already been provided
|
||||
var (
|
||||
funcTx pgx.Tx
|
||||
err error
|
||||
)
|
||||
if tx != nil {
|
||||
funcTx = *tx
|
||||
} else {
|
||||
funcTx, err = c.cl.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to begin transaction", err)
|
||||
}
|
||||
defer funcTx.Rollback(ctx)
|
||||
}
|
||||
|
||||
// Insert the transaction
|
||||
var transactionId string
|
||||
err = funcTx.QueryRow(
|
||||
ctx,
|
||||
"INSERT INTO transactions (order_id, customer_id, amount) VALUES ($1, $2, $3) RETURNING id",
|
||||
orderId,
|
||||
customerId,
|
||||
amount,
|
||||
).Scan(&transactionId)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert transaction", err)
|
||||
}
|
||||
|
||||
// Get the transaction obj
|
||||
transaction, err := c.getTransaction(ctx, &funcTx, transactionId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Add the event to the outbox table with the transaction
|
||||
evt, evtTopic, err := payment.PrepareTransactionLoggedEvent(transaction)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeService, "failed to create event", err)
|
||||
}
|
||||
|
||||
_, err = funcTx.Exec(ctx, "INSERT INTO event_outbox (aggregateid, aggregatetype, payload) VALUES ($1, $2, $3)", transaction.Id, evtTopic, evt)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to insert event", err)
|
||||
}
|
||||
|
||||
// Commit the transaction (if created in this func)
|
||||
if tx == nil {
|
||||
err = funcTx.Commit(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to commit transaction", err)
|
||||
}
|
||||
}
|
||||
|
||||
return transaction, nil
|
||||
}
|
||||
|
||||
// Scan a postgres row to a protobuf object
|
||||
func scanRowToTransaction(row pgx.Row) (*pb.Transaction, error) {
|
||||
var transaction pb.Transaction
|
||||
|
||||
// Temporary variables that require conversion
|
||||
var tmpProcessedAt pgtype.Timestamp
|
||||
var tmpReversedAt pgtype.Timestamp
|
||||
|
||||
err := row.Scan(
|
||||
&transaction.Id,
|
||||
&transaction.OrderId,
|
||||
&transaction.CustomerId,
|
||||
&transaction.Amount,
|
||||
&tmpReversedAt,
|
||||
&tmpProcessedAt,
|
||||
)
|
||||
if err != nil {
|
||||
if err == pgx.ErrNoRows {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeNotFound, "transaction not found", err)
|
||||
} else {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to scan object from database", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Convert the temporary variables
|
||||
// - converting postgres timestamps to unix format
|
||||
if tmpProcessedAt.Valid {
|
||||
transaction.ProcessedAt = tmpProcessedAt.Time.Unix()
|
||||
} else {
|
||||
return nil, errors.NewServiceError(errors.ErrCodeUnknown, "failed to scan object from database (timestamp conversion)")
|
||||
}
|
||||
|
||||
if tmpReversedAt.Valid {
|
||||
unixReversed := tmpReversedAt.Time.Unix()
|
||||
transaction.ReversedAt = &unixReversed
|
||||
}
|
||||
|
||||
return &transaction, nil
|
||||
}
|
||||
|
||||
// Scan a postgres row to a protobuf object
|
||||
func scanRowToCustomerBalance(row pgx.Row) (*pb.CustomerBalance, error) {
|
||||
var balance pb.CustomerBalance
|
||||
|
||||
err := row.Scan(
|
||||
&balance.CustomerId,
|
||||
&balance.Balance,
|
||||
)
|
||||
if err != nil {
|
||||
if err == pgx.ErrNoRows {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeNotFound, "balance not found", err)
|
||||
} else {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "failed to scan object from database", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &balance, nil
|
||||
}
|
||||
129
internal/svc/payment/event.go
Normal file
129
internal/svc/payment/event.go
Normal file
@@ -0,0 +1,129 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package payment
|
||||
|
||||
import (
|
||||
"github.com/hexolan/stocklet/internal/pkg/messaging"
|
||||
eventspb "github.com/hexolan/stocklet/internal/pkg/protogen/events/v1"
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/payment/v1"
|
||||
)
|
||||
|
||||
func PrepareBalanceCreatedEvent(bal *pb.CustomerBalance) ([]byte, string, error) {
|
||||
topic := messaging.Payment_Balance_Created_Topic
|
||||
event := &eventspb.BalanceCreatedEvent{
|
||||
Revision: 1,
|
||||
|
||||
CustomerId: bal.CustomerId,
|
||||
Balance: bal.Balance,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PrepareBalanceCreditedEvent(customerId string, amount float32, newBalance float32) ([]byte, string, error) {
|
||||
topic := messaging.Payment_Balance_Credited_Topic
|
||||
event := &eventspb.BalanceCreditedEvent{
|
||||
Revision: 1,
|
||||
|
||||
CustomerId: customerId,
|
||||
Amount: amount,
|
||||
NewBalance: newBalance,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PrepareBalanceDebitedEvent(customerId string, amount float32, newBalance float32) ([]byte, string, error) {
|
||||
topic := messaging.Payment_Balance_Debited_Topic
|
||||
event := &eventspb.BalanceDebitedEvent{
|
||||
Revision: 1,
|
||||
|
||||
CustomerId: customerId,
|
||||
Amount: amount,
|
||||
NewBalance: newBalance,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PrepareBalanceClosedEvent(bal *pb.CustomerBalance) ([]byte, string, error) {
|
||||
topic := messaging.Payment_Balance_Closed_Topic
|
||||
event := &eventspb.BalanceClosedEvent{
|
||||
Revision: 1,
|
||||
|
||||
CustomerId: bal.CustomerId,
|
||||
Balance: bal.Balance,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PrepareTransactionLoggedEvent(transaction *pb.Transaction) ([]byte, string, error) {
|
||||
topic := messaging.Payment_Transaction_Created_Topic
|
||||
event := &eventspb.TransactionLoggedEvent{
|
||||
Revision: 1,
|
||||
|
||||
TransactionId: transaction.Id,
|
||||
Amount: transaction.Amount,
|
||||
OrderId: transaction.OrderId,
|
||||
CustomerId: transaction.CustomerId,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PrepareTransactionReversedEvent(transaction *pb.Transaction) ([]byte, string, error) {
|
||||
topic := messaging.Payment_Transaction_Reversed_Topic
|
||||
event := &eventspb.TransactionReversedEvent{
|
||||
Revision: 1,
|
||||
|
||||
TransactionId: transaction.Id,
|
||||
Amount: transaction.Amount,
|
||||
OrderId: transaction.OrderId,
|
||||
CustomerId: transaction.CustomerId,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PreparePaymentProcessedEvent_Success(transaction *pb.Transaction) ([]byte, string, error) {
|
||||
topic := messaging.Payment_Processing_Topic
|
||||
event := &eventspb.PaymentProcessedEvent{
|
||||
Revision: 1,
|
||||
|
||||
Type: eventspb.PaymentProcessedEvent_TYPE_SUCCESS,
|
||||
OrderId: transaction.OrderId,
|
||||
CustomerId: transaction.CustomerId,
|
||||
Amount: transaction.Amount,
|
||||
TransactionId: &transaction.Id,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
|
||||
func PreparePaymentProcessedEvent_Failure(orderId string, customerId string, amount float32) ([]byte, string, error) {
|
||||
topic := messaging.Payment_Processing_Topic
|
||||
event := &eventspb.PaymentProcessedEvent{
|
||||
Revision: 1,
|
||||
|
||||
Type: eventspb.PaymentProcessedEvent_TYPE_FAILED,
|
||||
OrderId: orderId,
|
||||
CustomerId: customerId,
|
||||
Amount: amount,
|
||||
}
|
||||
|
||||
return messaging.MarshalEvent(event, topic)
|
||||
}
|
||||
132
internal/svc/payment/payment.go
Normal file
132
internal/svc/payment/payment.go
Normal file
@@ -0,0 +1,132 @@
|
||||
// Copyright (C) 2024 Declan Teevan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package payment
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/bufbuild/protovalidate-go"
|
||||
"github.com/rs/zerolog/log"
|
||||
"google.golang.org/protobuf/types/known/emptypb"
|
||||
|
||||
"github.com/hexolan/stocklet/internal/pkg/errors"
|
||||
"github.com/hexolan/stocklet/internal/pkg/messaging"
|
||||
commonpb "github.com/hexolan/stocklet/internal/pkg/protogen/common/v1"
|
||||
eventpb "github.com/hexolan/stocklet/internal/pkg/protogen/events/v1"
|
||||
pb "github.com/hexolan/stocklet/internal/pkg/protogen/payment/v1"
|
||||
)
|
||||
|
||||
// Interface for the service
|
||||
type PaymentService struct {
|
||||
pb.UnimplementedPaymentServiceServer
|
||||
|
||||
store StorageController
|
||||
pbVal *protovalidate.Validator
|
||||
}
|
||||
|
||||
// Interface for database methods
|
||||
// Flexibility for implementing seperate controllers for different databases (e.g. Postgres, MongoDB, etc)
|
||||
type StorageController interface {
|
||||
GetBalance(ctx context.Context, customerId string) (*pb.CustomerBalance, error)
|
||||
GetTransaction(ctx context.Context, transactionId string) (*pb.Transaction, error)
|
||||
|
||||
CreateBalance(ctx context.Context, customerId string) error
|
||||
CreditBalance(ctx context.Context, customerId string, amount float32) error
|
||||
DebitBalance(ctx context.Context, customerId string, amount float32, orderId *string) (*pb.Transaction, error)
|
||||
CloseBalance(ctx context.Context, customerId string) error
|
||||
|
||||
PaymentForOrder(ctx context.Context, orderId string, customerId string, amount float32) error
|
||||
}
|
||||
|
||||
// Interface for event consumption
|
||||
// Flexibility for seperate controllers for different messaging systems (e.g. Kafka, NATS, etc)
|
||||
type ConsumerController interface {
|
||||
messaging.ConsumerController
|
||||
|
||||
Attach(svc pb.PaymentServiceServer)
|
||||
}
|
||||
|
||||
// Create the payment service
|
||||
func NewPaymentService(cfg *ServiceConfig, store StorageController) *PaymentService {
|
||||
// Initialise the protobuf validator
|
||||
pbVal, err := protovalidate.New()
|
||||
if err != nil {
|
||||
log.Panic().Err(err).Msg("failed to initialise protobuf validator")
|
||||
}
|
||||
|
||||
// Initialise the service
|
||||
return &PaymentService{
|
||||
store: store,
|
||||
pbVal: pbVal,
|
||||
}
|
||||
}
|
||||
|
||||
func (svc PaymentService) ServiceInfo(ctx context.Context, req *commonpb.ServiceInfoRequest) (*commonpb.ServiceInfoResponse, error) {
|
||||
return &commonpb.ServiceInfoResponse{
|
||||
Name: "payment",
|
||||
Source: "https://github.com/hexolan/stocklet",
|
||||
SourceLicense: "AGPL-3.0",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (svc PaymentService) ViewTransaction(ctx context.Context, req *pb.ViewTransactionRequest) (*pb.ViewTransactionResponse, error) {
|
||||
// Attempt to get the transaction from the db
|
||||
transaction, err := svc.store.GetTransaction(ctx, req.TransactionId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.ViewTransactionResponse{Transaction: transaction}, nil
|
||||
}
|
||||
|
||||
func (svc PaymentService) ViewBalance(ctx context.Context, req *pb.ViewBalanceRequest) (*pb.ViewBalanceResponse, error) {
|
||||
// todo: permission checking
|
||||
|
||||
// Attempt to get the balance from the db
|
||||
balance, err := svc.store.GetBalance(ctx, req.CustomerId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.ViewBalanceResponse{Balance: balance}, nil
|
||||
}
|
||||
|
||||
func (svc PaymentService) ProcessUserCreatedEvent(ctx context.Context, req *eventpb.UserCreatedEvent) (*emptypb.Empty, error) {
|
||||
err := svc.store.CreateBalance(ctx, req.UserId)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "error processing event", err)
|
||||
}
|
||||
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
func (svc PaymentService) ProcessUserDeletedEvent(ctx context.Context, req *eventpb.UserDeletedEvent) (*emptypb.Empty, error) {
|
||||
err := svc.store.CloseBalance(ctx, req.UserId)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "error processing event", err)
|
||||
}
|
||||
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
|
||||
func (svc PaymentService) ProcessShipmentAllocationEvent(ctx context.Context, req *eventpb.ShipmentAllocationEvent) (*emptypb.Empty, error) {
|
||||
err := svc.store.PaymentForOrder(ctx, req.OrderId, req.OrderMetadata.CustomerId, req.OrderMetadata.TotalPrice)
|
||||
if err != nil {
|
||||
return nil, errors.WrapServiceError(errors.ErrCodeExtService, "error processing event", err)
|
||||
}
|
||||
|
||||
return &emptypb.Empty{}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user