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 28, 8:49 PM +1000 credits credit_pack_purchase –
Jun 28, 8:49 PM +10 credits signup_bonus signup –
Jun 28, 8:49 PM +5100 credits credit_pack_purchase –
Jun 28, 8:49 PM +10 credits subscription_credits first_cycle expires in 29 days
Jun 28, 8:49 PM +10 credits credit_pack_purchase –
Jun 28, 8:49 PM +20 credits referral_bonus referral –
Jun 28, 8:49 PM +10 credits bonus expiring_bonus_credits –
Jun 29, 3:34 AM +10 credits signup_bonus signup –
Jun 29, 8:25 AM +5100 credits credit_pack_purchase –

Payment records (pay gem)

The user has paid $447.99 total

Pay::Charge (4)
# {
                        :id => 2891,
               :customer_id => 4153,
           :subscription_id => nil,
              :processor_id => "VcwemBhYK8xUopKLC83MF",
                    :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-28 20:49:54.947728000 UTC +00:00,
                :updated_at => 2026-06-28 20:49:54.947728000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
# {
                        :id => 2890,
               :customer_id => 4153,
           :subscription_id => nil,
              :processor_id => "nOlEDeBSizXQgQ4f9-8HM",
                    :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" => 6,
                   "exp_year" => 2026
    },
            :stripe_account => nil,
                :created_at => 2026-06-28 20:49:54.923141000 UTC +00:00,
                :updated_at => 2026-06-28 20:49:54.923141000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
# {
                        :id => 2893,
               :customer_id => 4153,
           :subscription_id => nil,
              :processor_id => "orJ9pbNyFBNBGHRiQ0pu0",
                    :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-29 08:25:43.216081000 UTC +00:00,
                :updated_at => 2026-06-29 08:25:43.216081000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
# {
                        :id => 2892,
               :customer_id => 4153,
           :subscription_id => nil,
              :processor_id => "wHc2r0YzqKvkfpBuhIgpW",
                    :amount => 99,
                  :currency => nil,
    :application_fee_amount => nil,
           :amount_refunded => nil,
                  :metadata => {
         "purchase_type" => "credit_pack",
             "pack_name" => "tiny",
               "credits" => 10,
         "bonus_credits" => 0,
           "price_cents" => 99,
        "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-28 20:49:55.193723000 UTC +00:00,
                :updated_at => 2026-06-28 20:49:55.193723000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
Pay::Subscription (1)
# {
                         "id" => 1448,
                "customer_id" => 4153,
                       "name" => "default",
               "processor_id" => "lTuJ_HtBTAb9ziamXz2CK",
             "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-28 20:49:55.171592000 UTC +00:00,
                 "updated_at" => 2026-06-28 20:49:55.171592000 UTC +00:00,
                       "type" => "Pay::FakeProcessor::Subscription",
                    "prorate" => true
}

Model records

UsageCredits::Fulfillment (5)
# {
                          :id => 4337,
                   :wallet_id => 4160,
                 :source_type => "Pay::Charge",
                   :source_id => 2890,
    :credits_last_fulfillment => 1000,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2026-06-28 20:49:54.927976000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 2890,
              "purchased_at" => "2026-06-28T20:49:54.923Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "starter",
                   "credits" => 1000,
             "bonus_credits" => 0,
               "price_cents" => 4900,
            "price_currency" => "USD"
    },
                  :created_at => 2026-06-28 20:49:54.928451000 UTC +00:00,
                  :updated_at => 2026-06-28 20:49:54.928451000 UTC +00:00
}
# {
                          :id => 4338,
                   :wallet_id => 4160,
                 :source_type => "Pay::Charge",
                   :source_id => 2891,
    :credits_last_fulfillment => 5100,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2026-06-28 20:49:54.958386000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 2891,
              "purchased_at" => "2026-06-28T20:49:54.947Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
                  :created_at => 2026-06-28 20:49:54.959018000 UTC +00:00,
                  :updated_at => 2026-06-28 20:49:54.959018000 UTC +00:00
}
# {
                          :id => 4339,
                   :wallet_id => 4160,
                 :source_type => "Pay::Subscription",
                   :source_id => 1448,
    :credits_last_fulfillment => 10,
            :fulfillment_type => "subscription",
           :last_fulfilled_at => 2026-06-28 20:49:55.183400000 UTC +00:00,
         :next_fulfillment_at => 2026-07-28 20:49:55.171592000 UTC +00:00,
          :fulfillment_period => "1 month",
                    :stops_at => nil,
                    :metadata => {
        "subscription_id" => 1448,
                   "plan" => "abcdef123456"
    },
                  :created_at => 2026-06-28 20:49:55.184273000 UTC +00:00,
                  :updated_at => 2026-06-28 20:49:55.184273000 UTC +00:00
}
# {
                          :id => 4340,
                   :wallet_id => 4160,
                 :source_type => "Pay::Charge",
                   :source_id => 2892,
    :credits_last_fulfillment => 10,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2026-06-28 20:49:55.204744000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 2892,
              "purchased_at" => "2026-06-28T20:49:55.193Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "tiny",
                   "credits" => 10,
             "bonus_credits" => 0,
               "price_cents" => 99,
            "price_currency" => "USD"
    },
                  :created_at => 2026-06-28 20:49:55.205716000 UTC +00:00,
                  :updated_at => 2026-06-28 20:49:55.205716000 UTC +00:00
}
# {
                          :id => 4341,
                   :wallet_id => 4160,
                 :source_type => "Pay::Charge",
                   :source_id => 2893,
    :credits_last_fulfillment => 5100,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2026-06-29 08:25:43.240915000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 2893,
              "purchased_at" => "2026-06-29T08:25:43.216Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
                  :created_at => 2026-06-29 08:25:43.251220000 UTC +00:00,
                  :updated_at => 2026-06-29 08:25:43.251220000 UTC +00:00
}
UsageCredits::Transaction (9)
# {
                :id => 7171,
         :wallet_id => 4160,
            :amount => 1000,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 2890,
              "purchased_at" => "2026-06-28T20:49:54.923Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2026-06-28T20:49:54.925Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "starter",
                   "credits" => 1000,
             "bonus_credits" => 0,
               "price_cents" => 4900,
            "price_currency" => "USD"
    },
        :created_at => 2026-06-28 20:49:54.926538000 UTC +00:00,
        :updated_at => 2026-06-28 20:49:54.926538000 UTC +00:00
}
# {
                :id => 7172,
         :wallet_id => 4160,
            :amount => 10,
          :category => "signup_bonus",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "signup"
    },
        :created_at => 2026-06-28 20:49:54.941183000 UTC +00:00,
        :updated_at => 2026-06-28 20:49:54.941183000 UTC +00:00
}
# {
                :id => 7173,
         :wallet_id => 4160,
            :amount => 5100,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 2891,
              "purchased_at" => "2026-06-28T20:49:54.947Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2026-06-28T20:49:54.950Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
        :created_at => 2026-06-28 20:49:54.951686000 UTC +00:00,
        :updated_at => 2026-06-28 20:49:54.951686000 UTC +00:00
}
# {
                :id => 7174,
         :wallet_id => 4160,
            :amount => 10,
          :category => "subscription_credits",
        :expires_at => 2026-07-28 20:54:55.171592000 UTC +00:00,
    :fulfillment_id => 4339,
          :metadata => {
        "subscription_id" => 1448,
                 "reason" => "first_cycle",
                   "plan" => "abcdef123456",
           "fulfilled_at" => "2026-06-28T20:49:55.179Z"
    },
        :created_at => 2026-06-28 20:49:55.180710000 UTC +00:00,
        :updated_at => 2026-06-28 20:49:55.180710000 UTC +00:00
}
# {
                :id => 7175,
         :wallet_id => 4160,
            :amount => 10,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 2892,
              "purchased_at" => "2026-06-28T20:49:55.193Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2026-06-28T20:49:55.197Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "tiny",
                   "credits" => 10,
             "bonus_credits" => 0,
               "price_cents" => 99,
            "price_currency" => "USD"
    },
        :created_at => 2026-06-28 20:49:55.201187000 UTC +00:00,
        :updated_at => 2026-06-28 20:49:55.201187000 UTC +00:00
}
# {
                :id => 7176,
         :wallet_id => 4160,
            :amount => 20,
          :category => "referral_bonus",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "referral"
    },
        :created_at => 2026-06-28 20:49:55.215325000 UTC +00:00,
        :updated_at => 2026-06-28 20:49:55.215325000 UTC +00:00
}
# {
                :id => 7177,
         :wallet_id => 4160,
            :amount => 10,
          :category => "bonus",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "expiring_bonus_credits"
    },
        :created_at => 2026-06-28 20:49:55.266396000 UTC +00:00,
        :updated_at => 2026-06-28 20:49:55.266396000 UTC +00:00
}
# {
                :id => 7178,
         :wallet_id => 4160,
            :amount => 10,
          :category => "signup_bonus",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "signup"
    },
        :created_at => 2026-06-29 03:34:15.834079000 UTC +00:00,
        :updated_at => 2026-06-29 03:34:15.834079000 UTC +00:00
}
# {
                :id => 7180,
         :wallet_id => 4160,
            :amount => 5100,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 2893,
              "purchased_at" => "2026-06-29T08:25:43.216Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2026-06-29T08:25:43.232Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
        :created_at => 2026-06-29 08:25:43.235822000 UTC +00:00,
        :updated_at => 2026-06-29 08:25:43.235822000 UTC +00:00
}
UsageCredits::Allocation (0) none

Audit trail

Audit trail
[
    [0] [
        [0] "credit_pack_purchase",
        [1] 1000,
        [2] {
            "purchase_charge_id" => 2890,
                  "purchased_at" => "2026-06-28T20:49:54.923Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2026-06-28T20:49:54.925Z",
                 "purchase_type" => "credit_pack",
                     "pack_name" => "starter",
                       "credits" => 1000,
                 "bonus_credits" => 0,
                   "price_cents" => 4900,
                "price_currency" => "USD"
        }
    ],
    [1] [
        [0] "signup_bonus",
        [1] 10,
        [2] {
            "reason" => "signup"
        }
    ],
    [2] [
        [0] "credit_pack_purchase",
        [1] 5100,
        [2] {
            "purchase_charge_id" => 2891,
                  "purchased_at" => "2026-06-28T20:49:54.947Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2026-06-28T20:49:54.950Z",
                 "purchase_type" => "credit_pack",
                     "pack_name" => "pro",
                       "credits" => 5000,
                 "bonus_credits" => 100,
                   "price_cents" => 19900,
                "price_currency" => "USD"
        }
    ],
    [3] [
        [0] "subscription_credits",
        [1] 10,
        [2] {
            "subscription_id" => 1448,
                     "reason" => "first_cycle",
                       "plan" => "abcdef123456",
               "fulfilled_at" => "2026-06-28T20:49:55.179Z"
        }
    ],
    [4] [
        [0] "credit_pack_purchase",
        [1] 10,
        [2] {
            "purchase_charge_id" => 2892,
                  "purchased_at" => "2026-06-28T20:49:55.193Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2026-06-28T20:49:55.197Z",
                 "purchase_type" => "credit_pack",
                     "pack_name" => "tiny",
                       "credits" => 10,
                 "bonus_credits" => 0,
                   "price_cents" => 99,
                "price_currency" => "USD"
        }
    ],
    [5] [
        [0] "referral_bonus",
        [1] 20,
        [2] {
            "reason" => "referral"
        }
    ],
    [6] [
        [0] "bonus",
        [1] 10,
        [2] {
            "reason" => "expiring_bonus_credits"
        }
    ],
    [7] [
        [0] "signup_bonus",
        [1] 10,
        [2] {
            "reason" => "signup"
        }
    ],
    [8] [
        [0] "credit_pack_purchase",
        [1] 5100,
        [2] {
            "purchase_charge_id" => 2893,
                  "purchased_at" => "2026-06-29T08:25:43.216Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2026-06-29T08:25:43.232Z",
                 "purchase_type" => "credit_pack",
                     "pack_name" => "pro",
                       "credits" => 5000,
                 "bonus_credits" => 100,
                   "price_cents" => 19900,
                "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
}