Integrate Your Own License Generator With Paddle
15.12.2016 by Jens in python
Paddle is a payment processor for selling things online and specially targeting software developer and their needs. They even support trial version and license activating through their SDK. Unfortunately, last year as I started to use them for my eBook Writing Software Moopato the Windows SDK was dotnet only and not suitable for a python application (They now offer a non dotnet version too). But there was another solution I am going to show you now.
Paddle allows a Webhook to be called when someone buys your software and whatever response the Webhook provides will be added to mail sent to your customer. This was all I needed to run my own licensing generator.
On my side of the webhook, I build a simple LicenseGenerator with Flask. But let's start with the Paddle part.
Go to the Edit your Project screen, and you will see an entry called Set Webhook URL. Click on change and the following button and your ready to set up the hook.
You will need your url, and in the next step, we will build our app responding there. You can define the request method too and add custom values to the request, like customer name, customer email address or even static ones of your own like application name and version. All predefined field are described in the docs - Fulfilment Webhooks.
And the most important part in setting this up the first time the "Test Webhook" Button. Clicking on it will make Paddle send you a webhook request with a dummy customer.
When using an HTTP Post for the Webhhok, Paddle will send a form-encoded request. They also build a hash code of all field values and your secret API key (Displayed on the Webhook site) and send this hash value with the message in the field msg_id. You should build that hash value too and check if the request is a valid one from Paddle.
The code is pretty straight forward. We create a simple Flask app with only one endpoint. We extract the form values we get, validate the message and if it seems to be from Paddle , we build our license key. How to implement that is up to you and your need. My version just generates the key and returns it, but you could also add it to a database or add the user to a mailing list. Your imagination is the limit.
from flask import Flask, request, abort from moopato.editor.license import build_license_key, format_license import hashlib import urllib api_secret= 'your API secret' app = Flask(__name__) application = app @app.route("/register_license", methods=['POST','GET']) def get_license(): data = request.form customer_name = data['customer_name'] customer_email = data['customer_email'] # private fields omitted msg_id = data['msg_id'] # Build msg hash m = hashlib.md5() m.update(urllib.parse.quote(customer_email).encode('utf-8')) m.update(api_secret.encode('utf-8')) #Check if codes are equal if m.hexdigest() == msg_id: return "your license key generation here" abort(403) if __name__ == "__main__": app.run()
comments powered by Disqus