feat(loyalty): pay orders with EnakPoint at the cashier

Adds paying with the EnakPoint method (docs/prd-point-coin.md F9, K7,
PC-305).

POST /payments with the EnakPoint method now takes points and the
customer's payment code and goes through PointPaymentProcessor instead of
the generic path, which would record a payment without taking any balance.
After checking the order, its customer (not walk-in, active), the outlet
(accepts EnakPoint, minimum) and the method, it redeems the code, then in
one transaction locks the order row and the wallet, recomputes the F9
limits from fresh data, inserts the payment with points_used and the frozen
point_value, writes the PAYMENT ledger row (key payment:{id}, the outlet,
the cashier) and updates the order. The limits are
min(balance, floor(min(remaining, total × max_payment_percent / 100 − paid
with EnakPoint) / point_value)) in cents, so EnakPoint never pays more than
what is left and gives no change.

Unlike the generic CreatePayment, which always marks the order paid, an
EnakPoint payment leaves it partial with the right remaining amount until
it is settled, so the rest can be paid in cash. Settling it triggers
earning, whose basis leaves out the EnakPoint part. Splitting with the
EnakPoint method is refused. Refusals answer 400. The payment response
carries points_used and point_value for the receipt.

GET /orders/:id/point-payment/preview returns eligibility, balance, point
value and the maximum for the use-maximum button.

The payment and order repositories write outside transactions, so this
path uses its own repository that joins it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
efrilm
2026-09-30 11:46:31 +07:00
co-authored by Claude Opus 5.5
parent 0c4dd72583
commit 4b3beaed41
17 changed files with 1096 additions and 21 deletions
@@ -0,0 +1,178 @@
package repository
import (
"context"
"errors"
"fmt"
"github.com/google/uuid"
"gorm.io/gorm"
"apskel-pos-be/internal/constants"
"apskel-pos-be/internal/entities"
)
// ErrPointPaymentOrderNotFound means the order does not exist.
var ErrPointPaymentOrderNotFound = errors.New("point payment: order not found")
// PointPaymentOrder is what paying with EnakPoint needs to know about an order.
type PointPaymentOrder struct {
ID uuid.UUID
OrganizationID uuid.UUID
OutletID uuid.UUID
OrderNumber string
OutletName string
CustomerID *uuid.UUID
TotalAmount float64
PaymentStatus string
IsVoid bool
CustomerIsDefault *bool
CustomerIsActive *bool
}
// PointPaymentRepository reads and writes what paying an order with EnakPoint touches
// (docs/prd-point-coin.md F9). Unlike the order and payment repositories, every method
// joins the caller's transaction, since the payment row, the balance and the order
// must change together.
type PointPaymentRepository interface {
// GetOrder reads the order; with lock it also locks the order row for the rest of
// the transaction, so two payments of the same order queue up.
GetOrder(ctx context.Context, orderID uuid.UUID, lock bool) (*PointPaymentOrder, error)
// GetPaymentMethod returns a method's organization and type.
GetPaymentMethod(ctx context.Context, methodID uuid.UUID) (organizationID uuid.UUID, methodType string, err error)
// TotalPaid sums the order's completed payments, as the rest of the order flow does.
TotalPaid(ctx context.Context, orderID uuid.UUID) (float64, error)
// PaidWithPoints sums the rupiah of the order's completed EnakPoint payments.
PaidWithPoints(ctx context.Context, orderID uuid.UUID) (float64, error)
InsertPayment(ctx context.Context, payment *entities.Payment) error
// UpdateOrderAfterPayment stores what is left to pay and marks the order paid when
// nothing is.
UpdateOrderAfterPayment(ctx context.Context, orderID uuid.UUID, remaining float64, completed bool) error
}
type pointPaymentRepository struct {
db *gorm.DB
}
func NewPointPaymentRepository(db *gorm.DB) PointPaymentRepository {
return &pointPaymentRepository{db: db}
}
func (r *pointPaymentRepository) GetOrder(ctx context.Context, orderID uuid.UUID, lock bool) (*PointPaymentOrder, error) {
lockClause := ""
if lock {
lockClause = "FOR UPDATE OF o"
}
var rows []struct {
ID string
OrganizationID string
OutletID string
OrderNumber string
OutletName string
CustomerID *string
TotalAmount float64
PaymentStatus string
IsVoid bool
CustomerIsDefault *bool
CustomerIsActive *bool
}
err := DBFromContext(ctx, r.db).WithContext(ctx).Raw(`
SELECT o.id::text AS id, o.organization_id::text AS organization_id, o.outlet_id::text AS outlet_id,
o.order_number, COALESCE(ou.name, '') AS outlet_name, o.customer_id::text AS customer_id,
o.total_amount, o.payment_status, COALESCE(o.is_void, false) AS is_void,
c.is_default AS customer_is_default, c.is_active AS customer_is_active
FROM orders o
LEFT JOIN outlets ou ON ou.id = o.outlet_id
LEFT JOIN customers c ON c.id = o.customer_id
WHERE o.id = ?
`+lockClause, orderID).Scan(&rows).Error
if err != nil {
return nil, fmt.Errorf("failed to read order: %w", err)
}
if len(rows) == 0 {
return nil, ErrPointPaymentOrderNotFound
}
row := rows[0]
order := &PointPaymentOrder{
OrderNumber: row.OrderNumber,
OutletName: row.OutletName,
TotalAmount: row.TotalAmount,
PaymentStatus: row.PaymentStatus,
IsVoid: row.IsVoid,
CustomerIsDefault: row.CustomerIsDefault,
CustomerIsActive: row.CustomerIsActive,
}
order.ID, _ = uuid.Parse(row.ID)
order.OrganizationID, _ = uuid.Parse(row.OrganizationID)
order.OutletID, _ = uuid.Parse(row.OutletID)
if row.CustomerID != nil {
if id, err := uuid.Parse(*row.CustomerID); err == nil {
order.CustomerID = &id
}
}
return order, nil
}
func (r *pointPaymentRepository) GetPaymentMethod(ctx context.Context, methodID uuid.UUID) (uuid.UUID, string, error) {
var rows []struct {
OrganizationID string
Type string
}
err := DBFromContext(ctx, r.db).WithContext(ctx).Raw(`
SELECT organization_id::text AS organization_id, type FROM payment_methods WHERE id = ?`, methodID).Scan(&rows).Error
if err != nil {
return uuid.Nil, "", fmt.Errorf("failed to read payment method: %w", err)
}
if len(rows) == 0 {
return uuid.Nil, "", fmt.Errorf("payment method not found")
}
org, _ := uuid.Parse(rows[0].OrganizationID)
return org, rows[0].Type, nil
}
func (r *pointPaymentRepository) TotalPaid(ctx context.Context, orderID uuid.UUID) (float64, error) {
var total float64
err := DBFromContext(ctx, r.db).WithContext(ctx).Raw(`
SELECT COALESCE(SUM(amount), 0) FROM payments WHERE order_id = ? AND status = ?`,
orderID, entities.PaymentTransactionStatusCompleted).Scan(&total).Error
if err != nil {
return 0, fmt.Errorf("failed to sum payments: %w", err)
}
return total, nil
}
func (r *pointPaymentRepository) PaidWithPoints(ctx context.Context, orderID uuid.UUID) (float64, error) {
var total float64
err := DBFromContext(ctx, r.db).WithContext(ctx).Raw(`
SELECT COALESCE(SUM(p.amount), 0) FROM payments p
JOIN payment_methods pm ON pm.id = p.payment_method_id
WHERE p.order_id = ? AND p.status = ? AND pm.type = ?`,
orderID, entities.PaymentTransactionStatusCompleted, constants.PaymentMethodTypePoint).Scan(&total).Error
if err != nil {
return 0, fmt.Errorf("failed to sum EnakPoint payments: %w", err)
}
return total, nil
}
func (r *pointPaymentRepository) InsertPayment(ctx context.Context, payment *entities.Payment) error {
if err := DBFromContext(ctx, r.db).WithContext(ctx).Create(payment).Error; err != nil {
return fmt.Errorf("failed to create payment: %w", err)
}
return nil
}
func (r *pointPaymentRepository) UpdateOrderAfterPayment(ctx context.Context, orderID uuid.UUID, remaining float64, completed bool) error {
paymentStatus := entities.PaymentStatusPartial
if completed {
paymentStatus = entities.PaymentStatusCompleted
}
err := DBFromContext(ctx, r.db).WithContext(ctx).Exec(`
UPDATE orders SET remaining_amount = ?, payment_status = ?,
status = CASE WHEN ? THEN ? ELSE status END, updated_at = NOW()
WHERE id = ?`,
remaining, paymentStatus, completed, entities.OrderStatusCompleted, orderID).Error
if err != nil {
return fmt.Errorf("failed to update order after payment: %w", err)
}
return nil
}