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
Dec 14, 3:30 PM +10 credits subscription_credits first_cycle expires in about 1 month
Dec 14, 3:31 PM +10 credits credit_pack_purchase
Dec 14, 3:31 PM +1000 credits credit_pack_purchase
Dec 14, 3:31 PM +5100 credits credit_pack_purchase
Dec 14, 3:31 PM +20 credits referral_bonus referral
Dec 14, 3:31 PM +10 credits signup_bonus signup
Dec 14, 3:31 PM +10 credits bonus expiring_bonus_credits 🗑️ expired

Payment records (pay gem)

The user has paid $248.99 total

Pay::Charge (3)
# {
                        :id => 1773,
               :customer_id => 2765,
           :subscription_id => nil,
              :processor_id => "34vQfQozIGFNXvkxaBFWQ",
                    :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" => 12,
                   "exp_year" => 2025
    },
            :stripe_account => nil,
                :created_at => 2025-12-14 15:31:09.351070000 UTC +00:00,
                :updated_at => 2025-12-14 15:31:09.351070000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
# {
                        :id => 1772,
               :customer_id => 2765,
           :subscription_id => nil,
              :processor_id => "DPoQec7POcci0WnepzPr-",
                    :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" => 12,
                   "exp_year" => 2025
    },
            :stripe_account => nil,
                :created_at => 2025-12-14 15:31:03.139841000 UTC +00:00,
                :updated_at => 2025-12-14 15:31:03.139841000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
# {
                        :id => 1774,
               :customer_id => 2765,
           :subscription_id => nil,
              :processor_id => "uTurNhE0sdEKmYOoQ_wSi",
                    :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" => 12,
                   "exp_year" => 2025
    },
            :stripe_account => nil,
                :created_at => 2025-12-14 15:31:15.211186000 UTC +00:00,
                :updated_at => 2025-12-14 15:31:15.211186000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
Pay::Subscription (1)
# {
                         "id" => 991,
                "customer_id" => 2765,
                       "name" => "default",
               "processor_id" => "hKa_qsVgTttC9BNtJ3Qg3",
             "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" => 2025-12-14 15:30:56.427048000 UTC +00:00,
                 "updated_at" => 2025-12-14 15:30:56.427048000 UTC +00:00,
                       "type" => "Pay::FakeProcessor::Subscription",
                    "prorate" => true
}

Model records

UsageCredits::Fulfillment (4)
# {
                          :id => 2762,
                   :wallet_id => 2770,
                 :source_type => "Pay::Subscription",
                   :source_id => 991,
    :credits_last_fulfillment => 10,
            :fulfillment_type => "subscription",
           :last_fulfilled_at => 2025-12-14 15:30:56.431340000 UTC +00:00,
         :next_fulfillment_at => 2026-01-14 15:30:56.427048000 UTC +00:00,
          :fulfillment_period => "1 month",
                    :stops_at => nil,
                    :metadata => {
        "subscription_id" => 991,
                   "plan" => "abcdef123456"
    },
                  :created_at => 2025-12-14 15:30:56.433292000 UTC +00:00,
                  :updated_at => 2025-12-14 15:30:56.433292000 UTC +00:00
}
# {
                          :id => 2763,
                   :wallet_id => 2770,
                 :source_type => "Pay::Charge",
                   :source_id => 1772,
    :credits_last_fulfillment => 10,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2025-12-14 15:31:03.144355000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 1772,
              "purchased_at" => "2025-12-14T15:31:03.139Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "tiny",
                   "credits" => 10,
             "bonus_credits" => 0,
               "price_cents" => 99,
            "price_currency" => "USD"
    },
                  :created_at => 2025-12-14 15:31:03.144815000 UTC +00:00,
                  :updated_at => 2025-12-14 15:31:03.144815000 UTC +00:00
}
# {
                          :id => 2764,
                   :wallet_id => 2770,
                 :source_type => "Pay::Charge",
                   :source_id => 1773,
    :credits_last_fulfillment => 1000,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2025-12-14 15:31:09.357566000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 1773,
              "purchased_at" => "2025-12-14T15:31:09.351Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "starter",
                   "credits" => 1000,
             "bonus_credits" => 0,
               "price_cents" => 4900,
            "price_currency" => "USD"
    },
                  :created_at => 2025-12-14 15:31:09.358297000 UTC +00:00,
                  :updated_at => 2025-12-14 15:31:09.358297000 UTC +00:00
}
# {
                          :id => 2765,
                   :wallet_id => 2770,
                 :source_type => "Pay::Charge",
                   :source_id => 1774,
    :credits_last_fulfillment => 5100,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2025-12-14 15:31:15.219099000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 1774,
              "purchased_at" => "2025-12-14T15:31:15.211Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
                  :created_at => 2025-12-14 15:31:15.219747000 UTC +00:00,
                  :updated_at => 2025-12-14 15:31:15.219747000 UTC +00:00
}
UsageCredits::Transaction (7)
# {
                :id => 4550,
         :wallet_id => 2770,
            :amount => 10,
          :category => "subscription_credits",
        :expires_at => 2026-01-14 15:35:56.427048000 UTC +00:00,
    :fulfillment_id => 2762,
          :metadata => {
        "subscription_id" => 991,
                 "reason" => "first_cycle",
                   "plan" => "abcdef123456",
           "fulfilled_at" => "2025-12-14T15:30:56.428Z"
    },
        :created_at => 2025-12-14 15:30:56.429726000 UTC +00:00,
        :updated_at => 2025-12-14 15:30:56.429726000 UTC +00:00
}
# {
                :id => 4551,
         :wallet_id => 2770,
            :amount => 10,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 1772,
              "purchased_at" => "2025-12-14T15:31:03.139Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2025-12-14T15:31:03.141Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "tiny",
                   "credits" => 10,
             "bonus_credits" => 0,
               "price_cents" => 99,
            "price_currency" => "USD"
    },
        :created_at => 2025-12-14 15:31:03.142962000 UTC +00:00,
        :updated_at => 2025-12-14 15:31:03.142962000 UTC +00:00
}
# {
                :id => 4552,
         :wallet_id => 2770,
            :amount => 1000,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 1773,
              "purchased_at" => "2025-12-14T15:31:09.351Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2025-12-14T15:31:09.353Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "starter",
                   "credits" => 1000,
             "bonus_credits" => 0,
               "price_cents" => 4900,
            "price_currency" => "USD"
    },
        :created_at => 2025-12-14 15:31:09.355474000 UTC +00:00,
        :updated_at => 2025-12-14 15:31:09.355474000 UTC +00:00
}
# {
                :id => 4553,
         :wallet_id => 2770,
            :amount => 5100,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 1774,
              "purchased_at" => "2025-12-14T15:31:15.211Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2025-12-14T15:31:15.214Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
        :created_at => 2025-12-14 15:31:15.216408000 UTC +00:00,
        :updated_at => 2025-12-14 15:31:15.216408000 UTC +00:00
}
# {
                :id => 4554,
         :wallet_id => 2770,
            :amount => 20,
          :category => "referral_bonus",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "referral"
    },
        :created_at => 2025-12-14 15:31:20.510001000 UTC +00:00,
        :updated_at => 2025-12-14 15:31:20.510001000 UTC +00:00
}
# {
                :id => 4555,
         :wallet_id => 2770,
            :amount => 10,
          :category => "signup_bonus",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "signup"
    },
        :created_at => 2025-12-14 15:31:26.192888000 UTC +00:00,
        :updated_at => 2025-12-14 15:31:26.192888000 UTC +00:00
}
# {
                :id => 4556,
         :wallet_id => 2770,
            :amount => 10,
          :category => "bonus",
        :expires_at => 2025-12-14 15:32:31.517928000 UTC +00:00,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "expiring_bonus_credits"
    },
        :created_at => 2025-12-14 15:31:31.520979000 UTC +00:00,
        :updated_at => 2025-12-14 15:31:31.520979000 UTC +00:00
}
UsageCredits::Allocation (0) none

Audit trail

Audit trail
[
    [0] [
        [0] "subscription_credits",
        [1] 10,
        [2] {
            "subscription_id" => 991,
                     "reason" => "first_cycle",
                       "plan" => "abcdef123456",
               "fulfilled_at" => "2025-12-14T15:30:56.428Z"
        }
    ],
    [1] [
        [0] "credit_pack_purchase",
        [1] 10,
        [2] {
            "purchase_charge_id" => 1772,
                  "purchased_at" => "2025-12-14T15:31:03.139Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2025-12-14T15:31:03.141Z",
                 "purchase_type" => "credit_pack",
                     "pack_name" => "tiny",
                       "credits" => 10,
                 "bonus_credits" => 0,
                   "price_cents" => 99,
                "price_currency" => "USD"
        }
    ],
    [2] [
        [0] "credit_pack_purchase",
        [1] 1000,
        [2] {
            "purchase_charge_id" => 1773,
                  "purchased_at" => "2025-12-14T15:31:09.351Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2025-12-14T15:31:09.353Z",
                 "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" => 1774,
                  "purchased_at" => "2025-12-14T15:31:15.211Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2025-12-14T15:31:15.214Z",
                 "purchase_type" => "credit_pack",
                     "pack_name" => "pro",
                       "credits" => 5000,
                 "bonus_credits" => 100,
                   "price_cents" => 19900,
                "price_currency" => "USD"
        }
    ],
    [4] [
        [0] "referral_bonus",
        [1] 20,
        [2] {
            "reason" => "referral"
        }
    ],
    [5] [
        [0] "signup_bonus",
        [1] 10,
        [2] {
            "reason" => "signup"
        }
    ],
    [6] [
        [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
}