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
Feb 20, 10:23 PM +5100 credits credit_pack_purchase –
Feb 20, 10:23 PM +20 credits referral_bonus referral –
Feb 20, 10:23 PM +1000 credits credit_pack_purchase –

Payment records (pay gem)

The user has paid $248.0 total

Pay::Charge (2)
# {
                        :id => 315,
               :customer_id => 124,
           :subscription_id => nil,
              :processor_id => "h512Xog7NjwkWW_hGWkSm",
                    :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" => 2,
                   "exp_year" => 2025
    },
            :stripe_account => nil,
                :created_at => 2025-02-20 22:23:52.556235000 UTC +00:00,
                :updated_at => 2025-02-20 22:23:52.556235000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
# {
                        :id => 314,
               :customer_id => 124,
           :subscription_id => nil,
              :processor_id => "zgBcjMW9A3d53my1yhD6K",
                    :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" => 2,
                   "exp_year" => 2025
    },
            :stripe_account => nil,
                :created_at => 2025-02-20 22:23:41.103910000 UTC +00:00,
                :updated_at => 2025-02-20 22:23:41.103910000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
Pay::Subscription (0) none

Model records

UsageCredits::Fulfillment (2)
# {
                          :id => 383,
                   :wallet_id => 124,
                 :source_type => "Pay::Charge",
                   :source_id => 314,
    :credits_last_fulfillment => 5100,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2025-02-20 22:23:41.110498000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 314,
              "purchased_at" => "2025-02-20T22:23:41.103Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
                  :created_at => 2025-02-20 22:23:41.111800000 UTC +00:00,
                  :updated_at => 2025-02-20 22:23:41.111800000 UTC +00:00
}
# {
                          :id => 384,
                   :wallet_id => 124,
                 :source_type => "Pay::Charge",
                   :source_id => 315,
    :credits_last_fulfillment => 1000,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2025-02-20 22:23:52.563792000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 315,
              "purchased_at" => "2025-02-20T22:23:52.556Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "starter",
                   "credits" => 1000,
             "bonus_credits" => 0,
               "price_cents" => 4900,
            "price_currency" => "USD"
    },
                  :created_at => 2025-02-20 22:23:52.564583000 UTC +00:00,
                  :updated_at => 2025-02-20 22:23:52.564583000 UTC +00:00
}
UsageCredits::Transaction (3)
# {
                :id => 527,
         :wallet_id => 124,
            :amount => 5100,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 314,
              "purchased_at" => "2025-02-20T22:23:41.103Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2025-02-20T22:23:41.106Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
        :created_at => 2025-02-20 22:23:41.107875000 UTC +00:00,
        :updated_at => 2025-02-20 22:23:41.107875000 UTC +00:00
}
# {
                :id => 528,
         :wallet_id => 124,
            :amount => 20,
          :category => "referral_bonus",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "referral"
    },
        :created_at => 2025-02-20 22:23:41.129507000 UTC +00:00,
        :updated_at => 2025-02-20 22:23:41.129507000 UTC +00:00
}
# {
                :id => 529,
         :wallet_id => 124,
            :amount => 1000,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 315,
              "purchased_at" => "2025-02-20T22:23:52.556Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2025-02-20T22:23:52.559Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "starter",
                   "credits" => 1000,
             "bonus_credits" => 0,
               "price_cents" => 4900,
            "price_currency" => "USD"
    },
        :created_at => 2025-02-20 22:23:52.561372000 UTC +00:00,
        :updated_at => 2025-02-20 22:23:52.561372000 UTC +00:00
}
UsageCredits::Allocation (0) none

Audit trail

Audit trail
[
    [0] [
        [0] "credit_pack_purchase",
        [1] 5100,
        [2] {
            "purchase_charge_id" => 314,
                  "purchased_at" => "2025-02-20T22:23:41.103Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2025-02-20T22:23:41.106Z",
                 "purchase_type" => "credit_pack",
                     "pack_name" => "pro",
                       "credits" => 5000,
                 "bonus_credits" => 100,
                   "price_cents" => 19900,
                "price_currency" => "USD"
        }
    ],
    [1] [
        [0] "referral_bonus",
        [1] 20,
        [2] {
            "reason" => "referral"
        }
    ],
    [2] [
        [0] "credit_pack_purchase",
        [1] 1000,
        [2] {
            "purchase_charge_id" => 315,
                  "purchased_at" => "2025-02-20T22:23:52.556Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2025-02-20T22:23:52.559Z",
                 "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
}