For developers

Loyalty

The Loyalty module keeps points per customer (PLAN › Loyalty).

  • loyalty_entries (Models\LoyaltyEntry): customer_id, signed points, kind (Data\LoyaltyKind: earned, redeemed, refunded, reversed, expired, adjusted), order_id, available_at, note, user_id. The balance is the sum of entries with available_at now or earlier; later ones are pending. Every change for one customer runs under pg_advisory_xact_lock (DatabaseLoyalty::lock()).
  • Settings\LoyaltySettings (loyalty.*): enabled, points_per_leu (decimal string), point_value (bani), min_redeem, max_share (percent), hold_days, expire_months.
  • Contracts\Loyalty (Ledger\DatabaseLoyalty): programme(), balance(), history(), redeem(customerId, points, orderId) (in the order's transaction; Data\LoyaltyRefused when the balance no longer covers it) and adjust() (never below zero).
  • Totals: Totals::$loyaltyPoints comes from carts.loyalty_points (Carts::useLoyalty(), cleared with the cart). Totals\ApplyLoyalty in the loyalty slot caps the points at the balance, at max_share of the goods after earlier discounts and at whole points' worth, refuses fewer than min_redeem (a notice), works only for a signed-in shopper, in RON, outside company accounts, and adds an Adjustment without lines (VAT spread over the rates) whose reference is the number of points. DefaultCheckout reads it back and calls redeem() next to vouchers and gift cards; CheckoutRefused('loyalty') sends the shopper to the cart.
  • Ledger\SettleOrderPoints through Listeners\FollowOrder (OrderStatusChanged): delivered → earn() (points = OrderRevenue::netProducts() × points_per_leu, rounded down; not for company or marketplace orders, guests or when off; once per order), cancelled → refund() (the points spent come back now) and reverse(), returned → reverse() (a still-pending earning is matched by a reversal on the same date, so it never becomes spendable).
  • loyalty:expire (02:20 daily): customers whose newest entry is older than expire_months lose their balance.
  • Storefront: Storefront\Http\Controllers\LoyaltyController (cart.loyalty, cart.loyalty.remove, account.loyalty); the cart shows the offer for a signed-in shopper outside a company with at least min_redeem. Admin: Http\Controllers\LoyaltyController (admin.settings.loyalty.*, admin.loyalty.adjust, loyalty.manage); the customer page card reads the contract.
  • Privacy\LoyaltyPersonalData exports and erases the entries (the account's deletion cascades too).

Cards: loyalty_cards (Models\LoyaltyCard: number unique upper-case, customer_id, expires_on, is_active, linked_at, code_hash: HMAC of the optional activation code), Contracts\LoyaltyCards (Ledger\DatabaseLoyaltyCards: forCustomer(), usableFor(), link(customerId, number, code) → linked | unknown (no card, off, or a wrong or missing code) | taken | one (the account already has a usable card); numbers and codes normalised without spaces or dashes). Settings card_discount (percent string) and card_free_shipping_from (bani). Totals\ApplyLoyaltyCard, registered in the loyalty slot before the points, takes the percent off what is left of each line (not exempt gift card lines) as an Adjustment naming its lines (reference card:···1234, so the checkout's points reader skips it) and sets withFreeShipping() from the threshold (RON). Admin: Http\Controllers\LoyaltyCardController (admin.loyalty.cards.*: list, add, CSV import up to 5,000 rows, switch); storefront: account.loyalty.card (confirmed accounts only, 10 per 10 minutes per address and 5 failures a day per account, counted before the try through RateLimiter::hit() so simultaneous requests cannot slip under it, the same answer for unknown and taken numbers). Holders' names and e-mails, and linking by e-mail in the admin, need customers.view. The privacy export lists the cards; erasure unlinks them.

Tests: tests/Feature/Loyalty/LoyaltyTest.php.