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
Jun 12, 3:11 PM +10 credits subscription_credits first_cycle expires in 30 days
Jun 12, 3:21 PM +5100 credits credit_pack_purchase
Jun 12, 3:24 PM +10 credits signup_bonus signup
Jun 12, 3:27 PM +5100 credits credit_pack_purchase
Jun 12, 3:29 PM +10 credits bonus expiring_bonus_credits 🗑️ expired
Jun 12, 3:39 PM +5100 credits credit_pack_purchase
Jun 12, 3:48 PM +10 credits signup_bonus signup
Jun 12, 3:49 PM +10 credits bonus expiring_bonus_credits 🗑️ expired
Jun 12, 4:28 PM +10 credits bonus expiring_bonus_credits 🗑️ expired

Payment records (pay gem)

The user has paid $597.0 total

Pay::Charge (3)
# {
                        :id => 2796,
               :customer_id => 4015,
           :subscription_id => nil,
              :processor_id => "2pbAk3YDdV7DIRP1XbMEm",
                    :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" => 6,
                   "exp_year" => 2026
    },
            :stripe_account => nil,
                :created_at => 2026-06-12 15:21:29.798869000 UTC +00:00,
                :updated_at => 2026-06-12 15:21:29.798869000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
# {
                        :id => 2797,
               :customer_id => 4015,
           :subscription_id => nil,
              :processor_id => "Q0pUBeIHuUvRAnZu9hEmk",
                    :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" => 6,
                   "exp_year" => 2026
    },
            :stripe_account => nil,
                :created_at => 2026-06-12 15:27:37.002415000 UTC +00:00,
                :updated_at => 2026-06-12 15:27:37.002415000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
# {
                        :id => 2798,
               :customer_id => 4015,
           :subscription_id => nil,
              :processor_id => "wOjCdOZmSP3HxhAyco59f",
                    :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" => 6,
                   "exp_year" => 2026
    },
            :stripe_account => nil,
                :created_at => 2026-06-12 15:39:49.664403000 UTC +00:00,
                :updated_at => 2026-06-12 15:39:49.664403000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
Pay::Subscription (1)
# {
                         "id" => 1415,
                "customer_id" => 4015,
                       "name" => "default",
               "processor_id" => "49MeBiDTYB0C3WceSrGl2",
             "processor_plan" => "abcdef123456",
                   "quantity" => 1,
                     "status" => "active",
       "current_period_start" => nil,
         "current_period_end" => nil,
              "trial_ends_at" => nil,
                    "ends_at" => nil,
                    "metered" => nil,
             "pause_behavior" => nil,
            "pause_starts_at" => nil,
           "pause_resumes_at" => nil,
    "application_fee_percent" => nil,
                   "metadata" => {
                   "purchase_type" => "credit_subscription",
               "subscription_name" => "test_plan",
              "fulfillment_period" => "1 month",
              "credits_per_period" => 10,
            "signup_bonus_credits" => 0,
                   "trial_credits" => 0,
                "rollover_enabled" => false,
        "expire_credits_on_cancel" => false,
        "credit_expiration_period" => nil,
                        "metadata" => {}
    },
                       "data" => nil,
             "stripe_account" => nil,
          "payment_method_id" => nil,
                 "created_at" => 2026-06-12 15:11:02.578423000 UTC +00:00,
                 "updated_at" => 2026-06-12 15:11:02.578423000 UTC +00:00,
                       "type" => "Pay::FakeProcessor::Subscription",
                    "prorate" => true
}

Model records

UsageCredits::Fulfillment (4)
# {
                          :id => 4210,
                   :wallet_id => 4022,
                 :source_type => "Pay::Subscription",
                   :source_id => 1415,
    :credits_last_fulfillment => 10,
            :fulfillment_type => "subscription",
           :last_fulfilled_at => 2026-06-12 15:11:02.581819000 UTC +00:00,
         :next_fulfillment_at => 2026-07-12 15:11:02.578423000 UTC +00:00,
          :fulfillment_period => "1 month",
                    :stops_at => nil,
                    :metadata => {
        "subscription_id" => 1415,
                   "plan" => "abcdef123456"
    },
                  :created_at => 2026-06-12 15:11:02.582365000 UTC +00:00,
                  :updated_at => 2026-06-12 15:11:02.582365000 UTC +00:00
}
# {
                          :id => 4211,
                   :wallet_id => 4022,
                 :source_type => "Pay::Charge",
                   :source_id => 2796,
    :credits_last_fulfillment => 5100,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2026-06-12 15:21:29.805956000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 2796,
              "purchased_at" => "2026-06-12T15:21:29.798Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
                  :created_at => 2026-06-12 15:21:29.806410000 UTC +00:00,
                  :updated_at => 2026-06-12 15:21:29.806410000 UTC +00:00
}
# {
                          :id => 4212,
                   :wallet_id => 4022,
                 :source_type => "Pay::Charge",
                   :source_id => 2797,
    :credits_last_fulfillment => 5100,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2026-06-12 15:27:37.012218000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 2797,
              "purchased_at" => "2026-06-12T15:27:37.002Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
                  :created_at => 2026-06-12 15:27:37.012994000 UTC +00:00,
                  :updated_at => 2026-06-12 15:27:37.012994000 UTC +00:00
}
# {
                          :id => 4213,
                   :wallet_id => 4022,
                 :source_type => "Pay::Charge",
                   :source_id => 2798,
    :credits_last_fulfillment => 5100,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2026-06-12 15:39:49.670649000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 2798,
              "purchased_at" => "2026-06-12T15:39:49.664Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
                  :created_at => 2026-06-12 15:39:49.671069000 UTC +00:00,
                  :updated_at => 2026-06-12 15:39:49.671069000 UTC +00:00
}
UsageCredits::Transaction (9)
# {
                :id => 6953,
         :wallet_id => 4022,
            :amount => 10,
          :category => "subscription_credits",
        :expires_at => 2026-07-12 15:16:02.578423000 UTC +00:00,
    :fulfillment_id => 4210,
          :metadata => {
        "subscription_id" => 1415,
                 "reason" => "first_cycle",
                   "plan" => "abcdef123456",
           "fulfilled_at" => "2026-06-12T15:11:02.579Z"
    },
        :created_at => 2026-06-12 15:11:02.580496000 UTC +00:00,
        :updated_at => 2026-06-12 15:11:02.580496000 UTC +00:00
}
# {
                :id => 6954,
         :wallet_id => 4022,
            :amount => 5100,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 2796,
              "purchased_at" => "2026-06-12T15:21:29.798Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2026-06-12T15:21:29.803Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
        :created_at => 2026-06-12 15:21:29.804580000 UTC +00:00,
        :updated_at => 2026-06-12 15:21:29.804580000 UTC +00:00
}
# {
                :id => 6955,
         :wallet_id => 4022,
            :amount => 10,
          :category => "signup_bonus",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "signup"
    },
        :created_at => 2026-06-12 15:24:04.362559000 UTC +00:00,
        :updated_at => 2026-06-12 15:24:04.362559000 UTC +00:00
}
# {
                :id => 6956,
         :wallet_id => 4022,
            :amount => 5100,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 2797,
              "purchased_at" => "2026-06-12T15:27:37.002Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2026-06-12T15:27:37.006Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
        :created_at => 2026-06-12 15:27:37.009159000 UTC +00:00,
        :updated_at => 2026-06-12 15:27:37.009159000 UTC +00:00
}
# {
                :id => 6957,
         :wallet_id => 4022,
            :amount => 10,
          :category => "bonus",
        :expires_at => 2026-06-12 15:30:33.788105000 UTC +00:00,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "expiring_bonus_credits"
    },
        :created_at => 2026-06-12 15:29:33.793199000 UTC +00:00,
        :updated_at => 2026-06-12 15:29:33.793199000 UTC +00:00
}
# {
                :id => 6958,
         :wallet_id => 4022,
            :amount => 5100,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 2798,
              "purchased_at" => "2026-06-12T15:39:49.664Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2026-06-12T15:39:49.668Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
        :created_at => 2026-06-12 15:39:49.669181000 UTC +00:00,
        :updated_at => 2026-06-12 15:39:49.669181000 UTC +00:00
}
# {
                :id => 6959,
         :wallet_id => 4022,
            :amount => 10,
          :category => "signup_bonus",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "signup"
    },
        :created_at => 2026-06-12 15:48:25.291676000 UTC +00:00,
        :updated_at => 2026-06-12 15:48:25.291676000 UTC +00:00
}
# {
                :id => 6960,
         :wallet_id => 4022,
            :amount => 10,
          :category => "bonus",
        :expires_at => 2026-06-12 15:50:59.752683000 UTC +00:00,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "expiring_bonus_credits"
    },
        :created_at => 2026-06-12 15:49:59.759129000 UTC +00:00,
        :updated_at => 2026-06-12 15:49:59.759129000 UTC +00:00
}
# {
                :id => 6961,
         :wallet_id => 4022,
            :amount => 10,
          :category => "bonus",
        :expires_at => 2026-06-12 16:29:47.487022000 UTC +00:00,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "expiring_bonus_credits"
    },
        :created_at => 2026-06-12 16:28:47.493521000 UTC +00:00,
        :updated_at => 2026-06-12 16:28:47.493521000 UTC +00:00
}
UsageCredits::Allocation (0) none

Audit trail

Audit trail
[
    [0] [
        [0] "subscription_credits",
        [1] 10,
        [2] {
            "subscription_id" => 1415,
                     "reason" => "first_cycle",
                       "plan" => "abcdef123456",
               "fulfilled_at" => "2026-06-12T15:11:02.579Z"
        }
    ],
    [1] [
        [0] "credit_pack_purchase",
        [1] 5100,
        [2] {
            "purchase_charge_id" => 2796,
                  "purchased_at" => "2026-06-12T15:21:29.798Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2026-06-12T15:21:29.803Z",
                 "purchase_type" => "credit_pack",
                     "pack_name" => "pro",
                       "credits" => 5000,
                 "bonus_credits" => 100,
                   "price_cents" => 19900,
                "price_currency" => "USD"
        }
    ],
    [2] [
        [0] "signup_bonus",
        [1] 10,
        [2] {
            "reason" => "signup"
        }
    ],
    [3] [
        [0] "credit_pack_purchase",
        [1] 5100,
        [2] {
            "purchase_charge_id" => 2797,
                  "purchased_at" => "2026-06-12T15:27:37.002Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2026-06-12T15:27:37.006Z",
                 "purchase_type" => "credit_pack",
                     "pack_name" => "pro",
                       "credits" => 5000,
                 "bonus_credits" => 100,
                   "price_cents" => 19900,
                "price_currency" => "USD"
        }
    ],
    [4] [
        [0] "bonus",
        [1] 10,
        [2] {
            "reason" => "expiring_bonus_credits"
        }
    ],
    [5] [
        [0] "credit_pack_purchase",
        [1] 5100,
        [2] {
            "purchase_charge_id" => 2798,
                  "purchased_at" => "2026-06-12T15:39:49.664Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2026-06-12T15:39:49.668Z",
                 "purchase_type" => "credit_pack",
                     "pack_name" => "pro",
                       "credits" => 5000,
                 "bonus_credits" => 100,
                   "price_cents" => 19900,
                "price_currency" => "USD"
        }
    ],
    [6] [
        [0] "signup_bonus",
        [1] 10,
        [2] {
            "reason" => "signup"
        }
    ],
    [7] [
        [0] "bonus",
        [1] 10,
        [2] {
            "reason" => "expiring_bonus_credits"
        }
    ],
    [8] [
        [0] "bonus",
        [1] 10,
        [2] {
            "reason" => "expiring_bonus_credits"
        }
    ]
]

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
}