There are 2 SharePoint App Models: SharePoint Hosted App and Provider Hosted App.
1. For SP hosted app:
You can make a generate button to create a license key and store it in a custom list. Then when end-users access to your app, check if the value of licensing field existing or not. If yes, let them access your app functions. If no, alert to them for buying license.
2. For Provider hosted app:
Step 1: Create a database to manage licensing
Step 2: In provider hosted app, you implement code to connect to licensing database and check if the license is existed. If yes, display your app functions. If no, alert to end-users for them to ask for buying license.