usage_credits

Demo app for the 💳✨ usage_credits gem

The basics: get and spend credits

 

Subscribe to a plan: Buy credit packs: Get arbitrary bonuses: Spend credits:

Ledger status

Date Amount Category Expiration
Jul 08, 5:59 PM +20 credits referral_bonus referral –
Jul 08, 5:59 PM +10 credits signup_bonus signup –
Jul 08, 5:59 PM +1000 credits credit_pack_purchase –
Jul 08, 5:59 PM +5100 credits credit_pack_purchase –
Jul 08, 8:23 PM +1000 credits credit_pack_purchase –

Payment records (pay gem)

The user has paid $297.0 total

Pay::Charge (3)
# {
                        :id => 2925,
               :customer_id => 4201,
           :subscription_id => nil,
              :processor_id => "GBOPxFWNkCpO_LTjmmwSg",
                    :amount => 19900,
                  :currency => nil,
    :application_fee_amount => nil,
           :amount_refunded => nil,
                  :metadata => {
         "purchase_type" => "credit_pack",
             "pack_name" => "pro",
               "credits" => 5000,
         "bonus_credits" => 100,
           "price_cents" => 19900,
        "price_currency" => "USD"
    },
                      :data => {
        "payment_method_type" => "card",
                      "brand" => "Fake",
                      "last4" => 1234,
                  "exp_month" => 7,
                   "exp_year" => 2026
    },
            :stripe_account => nil,
                :created_at => 2026-07-08 17:59:29.253324000 UTC +00:00,
                :updated_at => 2026-07-08 17:59:29.253324000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
# {
                        :id => 2924,
               :customer_id => 4201,
           :subscription_id => nil,
              :processor_id => "e5fmd_9dPAV4UtdQlFr_l",
                    :amount => 4900,
                  :currency => nil,
    :application_fee_amount => nil,
           :amount_refunded => nil,
                  :metadata => {
         "purchase_type" => "credit_pack",
             "pack_name" => "starter",
               "credits" => 1000,
         "bonus_credits" => 0,
           "price_cents" => 4900,
        "price_currency" => "USD"
    },
                      :data => {
        "payment_method_type" => "card",
                      "brand" => "Fake",
                      "last4" => 1234,
                  "exp_month" => 7,
                   "exp_year" => 2026
    },
            :stripe_account => nil,
                :created_at => 2026-07-08 17:59:29.238802000 UTC +00:00,
                :updated_at => 2026-07-08 17:59:29.238802000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
# {
                        :id => 2926,
               :customer_id => 4201,
           :subscription_id => nil,
              :processor_id => "ij7pLOTIBiJntcBjP1dXd",
                    :amount => 4900,
                  :currency => nil,
    :application_fee_amount => nil,
           :amount_refunded => nil,
                  :metadata => {
         "purchase_type" => "credit_pack",
             "pack_name" => "starter",
               "credits" => 1000,
         "bonus_credits" => 0,
           "price_cents" => 4900,
        "price_currency" => "USD"
    },
                      :data => {
        "payment_method_type" => "card",
                      "brand" => "Fake",
                      "last4" => 1234,
                  "exp_month" => 7,
                   "exp_year" => 2026
    },
            :stripe_account => nil,
                :created_at => 2026-07-08 20:23:04.667943000 UTC +00:00,
                :updated_at => 2026-07-08 20:23:04.667943000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
Pay::Subscription (0) none

Model records

UsageCredits::Fulfillment (3)
# {
                          :id => 4380,
                   :wallet_id => 4208,
                 :source_type => "Pay::Charge",
                   :source_id => 2924,
    :credits_last_fulfillment => 1000,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2026-07-08 17:59:29.244301000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 2924,
              "purchased_at" => "2026-07-08T17:59:29.238Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "starter",
                   "credits" => 1000,
             "bonus_credits" => 0,
               "price_cents" => 4900,
            "price_currency" => "USD"
    },
                  :created_at => 2026-07-08 17:59:29.244790000 UTC +00:00,
                  :updated_at => 2026-07-08 17:59:29.244790000 UTC +00:00
}
# {
                          :id => 4381,
                   :wallet_id => 4208,
                 :source_type => "Pay::Charge",
                   :source_id => 2925,
    :credits_last_fulfillment => 5100,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2026-07-08 17:59:29.259030000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 2925,
              "purchased_at" => "2026-07-08T17:59:29.253Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
                  :created_at => 2026-07-08 17:59:29.259689000 UTC +00:00,
                  :updated_at => 2026-07-08 17:59:29.259689000 UTC +00:00
}
# {
                          :id => 4382,
                   :wallet_id => 4208,
                 :source_type => "Pay::Charge",
                   :source_id => 2926,
    :credits_last_fulfillment => 1000,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2026-07-08 20:23:04.679076000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 2926,
              "purchased_at" => "2026-07-08T20:23:04.667Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "starter",
                   "credits" => 1000,
             "bonus_credits" => 0,
               "price_cents" => 4900,
            "price_currency" => "USD"
    },
                  :created_at => 2026-07-08 20:23:04.679882000 UTC +00:00,
                  :updated_at => 2026-07-08 20:23:04.679882000 UTC +00:00
}
UsageCredits::Transaction (5)
# {
                :id => 7244,
         :wallet_id => 4208,
            :amount => 20,
          :category => "referral_bonus",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "referral"
    },
        :created_at => 2026-07-08 17:59:29.208731000 UTC +00:00,
        :updated_at => 2026-07-08 17:59:29.208731000 UTC +00:00
}
# {
                :id => 7245,
         :wallet_id => 4208,
            :amount => 10,
          :category => "signup_bonus",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "signup"
    },
        :created_at => 2026-07-08 17:59:29.214433000 UTC +00:00,
        :updated_at => 2026-07-08 17:59:29.214433000 UTC +00:00
}
# {
                :id => 7246,
         :wallet_id => 4208,
            :amount => 1000,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 2924,
              "purchased_at" => "2026-07-08T17:59:29.238Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2026-07-08T17:59:29.240Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "starter",
                   "credits" => 1000,
             "bonus_credits" => 0,
               "price_cents" => 4900,
            "price_currency" => "USD"
    },
        :created_at => 2026-07-08 17:59:29.242121000 UTC +00:00,
        :updated_at => 2026-07-08 17:59:29.242121000 UTC +00:00
}
# {
                :id => 7247,
         :wallet_id => 4208,
            :amount => 5100,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 2925,
              "purchased_at" => "2026-07-08T17:59:29.253Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2026-07-08T17:59:29.255Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
        :created_at => 2026-07-08 17:59:29.257430000 UTC +00:00,
        :updated_at => 2026-07-08 17:59:29.257430000 UTC +00:00
}
# {
                :id => 7248,
         :wallet_id => 4208,
            :amount => 1000,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 2926,
              "purchased_at" => "2026-07-08T20:23:04.667Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2026-07-08T20:23:04.674Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "starter",
                   "credits" => 1000,
             "bonus_credits" => 0,
               "price_cents" => 4900,
            "price_currency" => "USD"
    },
        :created_at => 2026-07-08 20:23:04.676489000 UTC +00:00,
        :updated_at => 2026-07-08 20:23:04.676489000 UTC +00:00
}
UsageCredits::Allocation (0) none

Audit trail

Audit trail
[
    [0] [
        [0] "referral_bonus",
        [1] 20,
        [2] {
            "reason" => "referral"
        }
    ],
    [1] [
        [0] "signup_bonus",
        [1] 10,
        [2] {
            "reason" => "signup"
        }
    ],
    [2] [
        [0] "credit_pack_purchase",
        [1] 1000,
        [2] {
            "purchase_charge_id" => 2924,
                  "purchased_at" => "2026-07-08T17:59:29.238Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2026-07-08T17:59:29.240Z",
                 "purchase_type" => "credit_pack",
                     "pack_name" => "starter",
                       "credits" => 1000,
                 "bonus_credits" => 0,
                   "price_cents" => 4900,
                "price_currency" => "USD"
        }
    ],
    [3] [
        [0] "credit_pack_purchase",
        [1] 5100,
        [2] {
            "purchase_charge_id" => 2925,
                  "purchased_at" => "2026-07-08T17:59:29.253Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2026-07-08T17:59:29.255Z",
                 "purchase_type" => "credit_pack",
                     "pack_name" => "pro",
                       "credits" => 5000,
                 "bonus_credits" => 100,
                   "price_cents" => 19900,
                "price_currency" => "USD"
        }
    ],
    [4] [
        [0] "credit_pack_purchase",
        [1] 1000,
        [2] {
            "purchase_charge_id" => 2926,
                  "purchased_at" => "2026-07-08T20:23:04.667Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2026-07-08T20:23:04.674Z",
                 "purchase_type" => "credit_pack",
                     "pack_name" => "starter",
                       "credits" => 1000,
                 "bonus_credits" => 0,
                   "price_cents" => 4900,
                "price_currency" => "USD"
        }
    ]
]

Loaded config

Literal, raw config file
      # frozen_string_literal: true

UsageCredits.configure do |config|

  # Default test operations
  operation :small_operation do
    costs 1.credit
  end

  # operation :test_operation do
  #   costs 10.credits + 1.credits_per(:mb)
  #   validate ->(params) { params[:size].to_i <= 100.megabytes }, "File too large (max: 100 MB)"
  # end

  operation :prohibitive_operation do
    costs 1_000_000.credits
  end

  operation :this_operation_will_always_fail do
    costs 42.credits
  end


  # Define test credit packs
  credit_pack :tiny do
    gives 10.credits
    costs 99.cents
  end

  credit_pack :starter do
    gives 1000.credits
    costs 49.dollars
  end

  credit_pack :pro do
    gives 5000.credits
    bonus 100.credits  # Optional: give bonus credits
    costs 199.dollars
    currency :usd
  end


  # Define subscriptions
  subscription_plan :test_plan do
    processor_plan(:fake_processor, "abcdef123456")
    gives 10.credits.every(:month)
    unused_credits :expire
  end

  # Alert when balance drops below 100 credits
  # Set to nil to disable low balance alerts
  # config.low_balance_threshold = 20.credits

  # # Handle low credit balance alerts
  # config.on_low_balance do |user|
  #   # Send notification to user when their balance drops below the threshold
  #   ApplicationMailer.generic_email(to: user.email, body: "Heads up! You're low on credits.", subject: "Low credits alert").deliver_now
  # end

end

    
Credit-consuming operations
Credit packs
Credit subscription plans
Rest of the config
      {
  "default_currency": "usd",
  "rounding_strategy": "ceil",
  "credit_formatter": {},
  "fulfillment_grace_period": 300,
  "allow_negative_balance": false,
  "low_balance_threshold": null,
  "low_balance_callback": null
}