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
Apr 16, 9:51 AM +5100 credits credit_pack_purchase
Apr 16, 9:51 AM +20 credits referral_bonus referral
Apr 16, 9:58 AM +10 credits credit_pack_purchase
Apr 17, 12:55 PM +10 credits credit_pack_purchase
Apr 17, 2:38 PM +10 credits subscription_credits first_cycle expires in 29 days
Apr 17, 11:05 PM +10 credits bonus expiring_bonus_credits 🗑️ expired
Apr 18, 1:36 AM +10 credits signup_bonus signup
Apr 18, 3:48 AM +20 credits referral_bonus referral
Apr 18, 5:56 AM +5100 credits credit_pack_purchase
Apr 18, 7:48 AM +1000 credits credit_pack_purchase

Payment records (pay gem)

The user has paid $448.98 total

Pay::Charge (5)
# {
                        :id => 621,
               :customer_id => 448,
           :subscription_id => nil,
              :processor_id => "1bsWBg7yodiLJzQ3wNQGS",
                    :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" => 4,
                   "exp_year" => 2025
    },
            :stripe_account => nil,
                :created_at => 2025-04-17 12:55:01.001784000 UTC +00:00,
                :updated_at => 2025-04-17 12:55:01.001784000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
# {
                        :id => 623,
               :customer_id => 448,
           :subscription_id => nil,
              :processor_id => "5aPJdDh67ublPK5G1lLEl",
                    :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" => 4,
                   "exp_year" => 2025
    },
            :stripe_account => nil,
                :created_at => 2025-04-18 07:48:03.926513000 UTC +00:00,
                :updated_at => 2025-04-18 07:48:03.926513000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
# {
                        :id => 622,
               :customer_id => 448,
           :subscription_id => nil,
              :processor_id => "IC0jNnvmzl7L3yrVFRb1b",
                    :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" => 4,
                   "exp_year" => 2025
    },
            :stripe_account => nil,
                :created_at => 2025-04-18 05:56:46.145848000 UTC +00:00,
                :updated_at => 2025-04-18 05:56:46.145848000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
# {
                        :id => 619,
               :customer_id => 448,
           :subscription_id => nil,
              :processor_id => "QEl7Z5MblAALXlKgPRUcL",
                    :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" => 4,
                   "exp_year" => 2025
    },
            :stripe_account => nil,
                :created_at => 2025-04-16 09:58:59.210162000 UTC +00:00,
                :updated_at => 2025-04-16 09:58:59.210162000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
# {
                        :id => 618,
               :customer_id => 448,
           :subscription_id => nil,
              :processor_id => "TBhPzo5WrY02mfyJV3e_K",
                    :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" => 4,
                   "exp_year" => 2025
    },
            :stripe_account => nil,
                :created_at => 2025-04-16 09:51:06.219218000 UTC +00:00,
                :updated_at => 2025-04-16 09:51:06.219218000 UTC +00:00,
                      :type => "Pay::FakeProcessor::Charge"
}
Pay::Subscription (1)
# {
                         "id" => 205,
                "customer_id" => 448,
                       "name" => "default",
               "processor_id" => "SQ2lQNY6yDGm74nyVTR81",
             "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-04-17 14:38:47.278789000 UTC +00:00,
                 "updated_at" => 2025-04-17 14:38:47.278789000 UTC +00:00,
                       "type" => "Pay::FakeProcessor::Subscription",
                    "prorate" => true
}

Model records

UsageCredits::Fulfillment (6)
# {
                          :id => 821,
                   :wallet_id => 448,
                 :source_type => "Pay::Charge",
                   :source_id => 618,
    :credits_last_fulfillment => 5100,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2025-04-16 09:51:06.223245000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 618,
              "purchased_at" => "2025-04-16T09:51:06.219Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
                  :created_at => 2025-04-16 09:51:06.223836000 UTC +00:00,
                  :updated_at => 2025-04-16 09:51:06.223836000 UTC +00:00
}
# {
                          :id => 822,
                   :wallet_id => 448,
                 :source_type => "Pay::Charge",
                   :source_id => 619,
    :credits_last_fulfillment => 10,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2025-04-16 09:58:59.223941000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 619,
              "purchased_at" => "2025-04-16T09:58:59.210Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "tiny",
                   "credits" => 10,
             "bonus_credits" => 0,
               "price_cents" => 99,
            "price_currency" => "USD"
    },
                  :created_at => 2025-04-16 09:58:59.225184000 UTC +00:00,
                  :updated_at => 2025-04-16 09:58:59.225184000 UTC +00:00
}
# {
                          :id => 825,
                   :wallet_id => 448,
                 :source_type => "Pay::Charge",
                   :source_id => 621,
    :credits_last_fulfillment => 10,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2025-04-17 12:55:01.009101000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 621,
              "purchased_at" => "2025-04-17T12:55:01.001Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "tiny",
                   "credits" => 10,
             "bonus_credits" => 0,
               "price_cents" => 99,
            "price_currency" => "USD"
    },
                  :created_at => 2025-04-17 12:55:01.009596000 UTC +00:00,
                  :updated_at => 2025-04-17 12:55:01.009596000 UTC +00:00
}
# {
                          :id => 826,
                   :wallet_id => 448,
                 :source_type => "Pay::Subscription",
                   :source_id => 205,
    :credits_last_fulfillment => 10,
            :fulfillment_type => "subscription",
           :last_fulfilled_at => 2025-04-17 14:38:47.285893000 UTC +00:00,
         :next_fulfillment_at => 2025-05-17 14:38:47.278789000 UTC +00:00,
          :fulfillment_period => "1 month",
                    :stops_at => nil,
                    :metadata => {
        "subscription_id" => 205,
                   "plan" => "abcdef123456"
    },
                  :created_at => 2025-04-17 14:38:47.286620000 UTC +00:00,
                  :updated_at => 2025-04-17 14:38:47.286620000 UTC +00:00
}
# {
                          :id => 827,
                   :wallet_id => 448,
                 :source_type => "Pay::Charge",
                   :source_id => 622,
    :credits_last_fulfillment => 5100,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2025-04-18 05:56:46.154173000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 622,
              "purchased_at" => "2025-04-18T05:56:46.145Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
                  :created_at => 2025-04-18 05:56:46.154730000 UTC +00:00,
                  :updated_at => 2025-04-18 05:56:46.154730000 UTC +00:00
}
# {
                          :id => 828,
                   :wallet_id => 448,
                 :source_type => "Pay::Charge",
                   :source_id => 623,
    :credits_last_fulfillment => 1000,
            :fulfillment_type => "credit_pack",
           :last_fulfilled_at => 2025-04-18 07:48:03.934024000 UTC +00:00,
         :next_fulfillment_at => nil,
          :fulfillment_period => nil,
                    :stops_at => nil,
                    :metadata => {
        "purchase_charge_id" => 623,
              "purchased_at" => "2025-04-18T07:48:03.926Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "starter",
                   "credits" => 1000,
             "bonus_credits" => 0,
               "price_cents" => 4900,
            "price_currency" => "USD"
    },
                  :created_at => 2025-04-18 07:48:03.934595000 UTC +00:00,
                  :updated_at => 2025-04-18 07:48:03.934595000 UTC +00:00
}
UsageCredits::Transaction (10)
# {
                :id => 1212,
         :wallet_id => 448,
            :amount => 5100,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 618,
              "purchased_at" => "2025-04-16T09:51:06.219Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2025-04-16T09:51:06.220Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
        :created_at => 2025-04-16 09:51:06.221854000 UTC +00:00,
        :updated_at => 2025-04-16 09:51:06.221854000 UTC +00:00
}
# {
                :id => 1213,
         :wallet_id => 448,
            :amount => 20,
          :category => "referral_bonus",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "referral"
    },
        :created_at => 2025-04-16 09:51:06.436292000 UTC +00:00,
        :updated_at => 2025-04-16 09:51:06.436292000 UTC +00:00
}
# {
                :id => 1214,
         :wallet_id => 448,
            :amount => 10,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 619,
              "purchased_at" => "2025-04-16T09:58:59.210Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2025-04-16T09:58:59.217Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "tiny",
                   "credits" => 10,
             "bonus_credits" => 0,
               "price_cents" => 99,
            "price_currency" => "USD"
    },
        :created_at => 2025-04-16 09:58:59.220290000 UTC +00:00,
        :updated_at => 2025-04-16 09:58:59.220290000 UTC +00:00
}
# {
                :id => 1217,
         :wallet_id => 448,
            :amount => 10,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 621,
              "purchased_at" => "2025-04-17T12:55:01.001Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2025-04-17T12:55:01.005Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "tiny",
                   "credits" => 10,
             "bonus_credits" => 0,
               "price_cents" => 99,
            "price_currency" => "USD"
    },
        :created_at => 2025-04-17 12:55:01.007060000 UTC +00:00,
        :updated_at => 2025-04-17 12:55:01.007060000 UTC +00:00
}
# {
                :id => 1218,
         :wallet_id => 448,
            :amount => 10,
          :category => "subscription_credits",
        :expires_at => 2025-05-17 14:43:47.278789000 UTC +00:00,
    :fulfillment_id => 826,
          :metadata => {
        "subscription_id" => 205,
                 "reason" => "first_cycle",
                   "plan" => "abcdef123456",
           "fulfilled_at" => "2025-04-17T14:38:47.282Z"
    },
        :created_at => 2025-04-17 14:38:47.283705000 UTC +00:00,
        :updated_at => 2025-04-17 14:38:47.283705000 UTC +00:00
}
# {
                :id => 1219,
         :wallet_id => 448,
            :amount => 10,
          :category => "bonus",
        :expires_at => 2025-04-17 23:06:54.075958000 UTC +00:00,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "expiring_bonus_credits"
    },
        :created_at => 2025-04-17 23:05:54.080941000 UTC +00:00,
        :updated_at => 2025-04-17 23:05:54.080941000 UTC +00:00
}
# {
                :id => 1220,
         :wallet_id => 448,
            :amount => 10,
          :category => "signup_bonus",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "signup"
    },
        :created_at => 2025-04-18 01:36:26.678600000 UTC +00:00,
        :updated_at => 2025-04-18 01:36:26.678600000 UTC +00:00
}
# {
                :id => 1221,
         :wallet_id => 448,
            :amount => 20,
          :category => "referral_bonus",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "reason" => "referral"
    },
        :created_at => 2025-04-18 03:48:46.858990000 UTC +00:00,
        :updated_at => 2025-04-18 03:48:46.858990000 UTC +00:00
}
# {
                :id => 1222,
         :wallet_id => 448,
            :amount => 5100,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 622,
              "purchased_at" => "2025-04-18T05:56:46.145Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2025-04-18T05:56:46.150Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "pro",
                   "credits" => 5000,
             "bonus_credits" => 100,
               "price_cents" => 19900,
            "price_currency" => "USD"
    },
        :created_at => 2025-04-18 05:56:46.152409000 UTC +00:00,
        :updated_at => 2025-04-18 05:56:46.152409000 UTC +00:00
}
# {
                :id => 1223,
         :wallet_id => 448,
            :amount => 1000,
          :category => "credit_pack_purchase",
        :expires_at => nil,
    :fulfillment_id => nil,
          :metadata => {
        "purchase_charge_id" => 623,
              "purchased_at" => "2025-04-18T07:48:03.926Z",
         "credits_fulfilled" => true,
              "fulfilled_at" => "2025-04-18T07:48:03.930Z",
             "purchase_type" => "credit_pack",
                 "pack_name" => "starter",
                   "credits" => 1000,
             "bonus_credits" => 0,
               "price_cents" => 4900,
            "price_currency" => "USD"
    },
        :created_at => 2025-04-18 07:48:03.932477000 UTC +00:00,
        :updated_at => 2025-04-18 07:48:03.932477000 UTC +00:00
}
UsageCredits::Allocation (0) none

Audit trail

Audit trail
[
    [0] [
        [0] "credit_pack_purchase",
        [1] 5100,
        [2] {
            "purchase_charge_id" => 618,
                  "purchased_at" => "2025-04-16T09:51:06.219Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2025-04-16T09:51:06.220Z",
                 "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] 10,
        [2] {
            "purchase_charge_id" => 619,
                  "purchased_at" => "2025-04-16T09:58:59.210Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2025-04-16T09:58:59.217Z",
                 "purchase_type" => "credit_pack",
                     "pack_name" => "tiny",
                       "credits" => 10,
                 "bonus_credits" => 0,
                   "price_cents" => 99,
                "price_currency" => "USD"
        }
    ],
    [3] [
        [0] "credit_pack_purchase",
        [1] 10,
        [2] {
            "purchase_charge_id" => 621,
                  "purchased_at" => "2025-04-17T12:55:01.001Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2025-04-17T12:55:01.005Z",
                 "purchase_type" => "credit_pack",
                     "pack_name" => "tiny",
                       "credits" => 10,
                 "bonus_credits" => 0,
                   "price_cents" => 99,
                "price_currency" => "USD"
        }
    ],
    [4] [
        [0] "subscription_credits",
        [1] 10,
        [2] {
            "subscription_id" => 205,
                     "reason" => "first_cycle",
                       "plan" => "abcdef123456",
               "fulfilled_at" => "2025-04-17T14:38:47.282Z"
        }
    ],
    [5] [
        [0] "bonus",
        [1] 10,
        [2] {
            "reason" => "expiring_bonus_credits"
        }
    ],
    [6] [
        [0] "signup_bonus",
        [1] 10,
        [2] {
            "reason" => "signup"
        }
    ],
    [7] [
        [0] "referral_bonus",
        [1] 20,
        [2] {
            "reason" => "referral"
        }
    ],
    [8] [
        [0] "credit_pack_purchase",
        [1] 5100,
        [2] {
            "purchase_charge_id" => 622,
                  "purchased_at" => "2025-04-18T05:56:46.145Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2025-04-18T05:56:46.150Z",
                 "purchase_type" => "credit_pack",
                     "pack_name" => "pro",
                       "credits" => 5000,
                 "bonus_credits" => 100,
                   "price_cents" => 19900,
                "price_currency" => "USD"
        }
    ],
    [9] [
        [0] "credit_pack_purchase",
        [1] 1000,
        [2] {
            "purchase_charge_id" => 623,
                  "purchased_at" => "2025-04-18T07:48:03.926Z",
             "credits_fulfilled" => true,
                  "fulfilled_at" => "2025-04-18T07:48:03.930Z",
                 "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
}