Usage-based (pay per use) software licensing in .NET
Many software vendors nowadays move away from one-time payments to other licensing models. One such example is the usage-based model. By doing so helps lowering the barrier of entry for new customers, as they no longer need to commit to the product long term, which is usually the case with one-time payments. If you already have a subscription model, supporting usage-based payments can help you to monetise a group of users who would otherwise not buy the product.
You can read the entire tutorial here.
Getting started
In Cryptolens, usage-based licensing can be implemented using data objects, aka custom variables. We can use these variables to record how often features are used and keep track of any usage credits that a customer has purchased. There are two ways of billing customers:
- Upfront payment: customers need to purchase usage credits in advance.
- Based on actual usage: customers pay for the actual usage in the end of the billing period.
Charging based on actual usage
If you choose to charge your customers based on actual usage, we can simply use the code below:
var auth = "Access token with AddDataObject, ListDataObject and IncrementIntValue permission. Please also set KeyLock value to '-1'"; var licenseKey = "LZKZU-MPJEW-TARNP-UHDBQ"; var result = Data.ListDataObjects(auth, new ListDataObjectsToKeyModel { Contains = "usagecount", Key = licenseKey, ProductId = 3349 }); var obj = result.DataObjects.Get("usagecount"); if (obj == null) { // make sure to create it in case it does not exist. Data.AddDataObject(auth, new AddDataObjectToKeyModel { Key = licenseKey, ProductId = 3349, Name = "usagecount", IntValue = 1 }); if(res == null || res.Result == ResultType.Error) { Console.WriteLine("Could not create new data object. Terminate." + res.Message); } } else { var res = obj.IncrementIntValue(auth, 1, licenseKey: new LicenseKey { Key = licenseKey, ProductId = 3349 }); if (res == false) { Console.WriteLine("We could not update the data object. Terminate."); } }
Upfront payments
If you instead want to charge your users upfront, we need to create the data objects when creating the license. If you are using payment forms, we can set up two requests, one creating a new license and another creating a new data object (inspired by this tutorial), as the result from key creation will be “piped” into data object creation request. You can then have another payment form that allows users to refill their credits, in which case the custom field can be used.
You can use the code below to verify if the limit was reached inside your application:
var auth = "Access token with AddDataObject, ListDataObject and IncrementIntValue permission. Please also set KeyLock value to '-1'"; var licenseKey = "LZKZU-MPJEW-TARNP-UHDBQ"; var result = Data.ListDataObjects(auth, new ListDataObjectsToKeyModel { Contains = "usagecount", Key = licenseKey, ProductId = 3349 }); var obj = result.DataObjects.Get("usagecount"); var res = obj.DecrementIntValue(auth, decrementValue: 1, enableBound:true, lowerBound: 0, licenseKey: new LicenseKey { Key = licenseKey, ProductId = 3349 }); if (!res) { Console.WriteLine("Could not decrement the data object. The limit was reached."); }