Skip to content

Credit Note Model

The CreditNote model implements a simple ledger for store credit. Credit notes (refund, adjustment, promotion) increase a customer's balance; debit notes decrease it. The balance is always sum(credits) - sum(debits).

Credit notes are provided by the Responsiv.Pay plugin. For the merchant-facing documentation, see Store Credit.

Properties

PropertyTypeDescription
idintPrimary key
user_idintCustomer account ID
invoice_idint|nullLinked invoice ID (for refunds and debits applied at checkout)
amountintAmount in base currency units (cents). Always positive
currency_codestringThree-letter currency code
reasonstringHuman-readable description
typestringOne of: refund, adjustment, debit, promotion
issued_byint|nullBackend user ID who issued this note (for admin adjustments)
issued_atCarbonWhen the note was issued
created_atCarbonCreation date
updated_atCarbonLast update date

Type Constants

ConstantValueEffect on Balance
TYPE_REFUNDrefundIncreases balance
TYPE_ADJUSTMENTadjustmentIncreases balance
TYPE_PROMOTIONpromotionIncreases balance
TYPE_DEBITdebitDecreases balance

Relationships

PropertyTypeDescription
userUserCustomer account
invoiceInvoice|nullLinked invoice (refund source or checkout invoice)
issued_by_userBackendUser|nullAdmin who issued the note

Static Methods

getBalanceForUser

Returns the credit balance for a user in a given currency. If no currency is specified, the active currency is used.

php
$balance = CreditNote::getBalanceForUser($user, 'USD');
ParameterTypeDescription
$userUserCustomer account
$currencyCodestring|nullCurrency code (defaults to active currency)

Returns: int — Balance in base currency units (cents). Always >= 0.

getHistoryForUser

Returns all credit notes for a user, ordered by most recent first. If a currency is specified, only notes in that currency are returned.

php
$notes = CreditNote::getHistoryForUser($user, 'USD');
ParameterTypeDescription
$userUserCustomer account
$currencyCodestring|nullCurrency code (null for all currencies)

Returns: Collection<CreditNote>

issueRefund

Creates a credit note linked to an invoice refund. The currency is inherited from the invoice.

php
$note = CreditNote::issueRefund($user, $invoice, 2000, 'Damaged item');
ParameterTypeDescription
$userUserCustomer account
$invoiceInvoiceSource invoice
$amountintRefund amount in cents
$reasonstring|nullReason for refund

Returns: CreditNote

issueAdjustment

Creates a credit note for a manual admin adjustment. Currency must be specified explicitly.

php
$note = CreditNote::issueAdjustment($user, 1000, 'USD', 'Compensation for late delivery', $adminUser);
ParameterTypeDescription
$userUserCustomer account
$amountintCredit amount in cents
$currencyCodestringCurrency code
$reasonstringReason for adjustment
$adminUserBackendUser|nullAdmin who issued the adjustment

Returns: CreditNote

issueDebit

Creates a debit note that subtracts from a user's credit balance. An optional invoice can be linked.

php
$note = CreditNote::issueDebit($user, 1500, 'USD', 'Applied to invoice', null, $invoice);
ParameterTypeDescription
$userUserCustomer account
$amountintDebit amount in cents
$currencyCodestringCurrency code
$reasonstringReason for debit
$adminUserBackendUser|nullAdmin who issued the debit
$invoiceInvoice|nullLinked invoice

Returns: CreditNote

applyToInvoice

Applies store credit to an invoice at checkout. Creates a debit note linked to the invoice and updates the invoice's credit_applied field. Uses database-level locking to prevent double-spending in concurrent checkout scenarios.

php
$note = CreditNote::applyToInvoice($user, $invoice, $requestedAmount);
ParameterTypeDescription
$userUserCustomer account
$invoiceInvoiceTarget invoice
$requestedAmountintAmount to apply in cents

Returns: CreditNote — The debit note created.

Throws: ApplicationException if the balance is insufficient or the amount is invalid.

The amount applied is capped at the invoice total. This method runs inside a database transaction with row-level locking to serialize concurrent access.

removeFromInvoice

Reverses credit applied to an invoice by issuing an adjustment note. Resets the invoice's credit_applied to zero.

php
$note = CreditNote::removeFromInvoice($user, $invoice);
ParameterTypeDescription
$userUserCustomer account
$invoiceInvoiceTarget invoice

Returns: CreditNote|null — The adjustment note, or null if no credit was applied.

Query Scopes

ScopeDescription
applyUser($user)Filter by customer
applyType($type)Filter by type (e.g., CreditNote::TYPE_DEBIT)
applyCurrency($code)Filter by currency code

Events

EventParametersDescription
responsiv.pay.creditNote.issuedCreditNote $noteFired when any credit note is created
responsiv.pay.creditNote.appliedUser $user, Invoice $invoice, CreditNote $noteFired when credit is applied to an invoice
responsiv.pay.invoice.creditAppliedInvoice $invoice, int $amountFired when an invoice's credit amount changes

Settings

Store credit must be enabled in payment settings before the UI is available. Check programmatically with:

php
use Responsiv\Pay\Models\Setting;

if (Setting::isCreditEnabled()) {
    // Show credit UI
}