Almost seven years ago, in 2007, Radiohead decided to give away their new album, “In Rainbows”, for free – well, sort of. It wasn’t free per se, but they did give the fans the choice to pay what they wanted, and that’s how the “Pay What You Want” pricing strategy came to prominence. But why am I mentioning this?

If you are a developer of any kind, or even an artist, it has probably occurred to you that you could sell something online. Unfortunately, this is easier said than done, since there usually exist only two major options:

  1. Register at a web site that specialises in selling such products (e.g. ThemeForest, if you are creating web templates). The problem with this is that there is a high quality bar that needs to be met if you want to actually sell anything, and even in that case the fees are hefty, so you end up getting only a percentage of the price.
  2. Create your own web site and use an e-commerce system to sell the product. Such systems aren’t cheap, and you still end up having to promote your content in order to attract visitors. Writing your own code for integration with PayPal is also a challenge, because of all the security that you would need to put in place.

Obviously, for someone who would just like to earn a bit on the side, neither of these are a viable option. However, if you are ready to compromise and give your stuff away for free, you can actually earn money by using the “Pay What You Want” system. The logic is simple – if you are willing to put your product up for download, why not give the users the option to pay for it? Surprisingly enough, unlike a “Donate Now” button which nobody clicks, this approach leads to a lot of people paying something, which in the end amounts to a considerable profit.

This was the exact situation my roommate, Momčilo, ended up in. Without going into too much details, I’ll just say that he had previously created a script for visualising songs, which was quite popular, so he decided to start selling it. We looked into PayPal’s documentation, searched the intrawebs, but every approach we found would have taken too long to implement. Finally, we found this easy solution:

<html>
<head>
    ...
    <!-- We need to include jQuery -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    ...
</head>
<body>
    ...
    <form method="post" id="paypal_form" action="https://www.paypal.com/cgi-bin/webscr">
        <input type="hidden" name="rm" value="2" />
        <input type="hidden" name="cmd" value="_xclick" />
        <input type="hidden" name="business" value="YOUR_PAYPAL_EMAIL" />
        <input type="hidden" name="item_name" value="ITEM_NAME" />
        <input type="hidden" name="return" value="DOWNLOAD_LINK" />
        <input type="hidden" name="cancel_return" value="CANCEL_LINK" />
        <input type="hidden" name="amount" id="ppamount" value="10" />
        <input type="hidden" name="currency_code" value="USD" />
        <input name="pwyw" id="amount" value="$10.00" />
        <button type="submit" id="submit" name="submit">Download</button>
    </form>
    ...
</body>
</html>
    function getValue() {
        var str = $("#amount").val();
        str = str.replace(/[^0-9.,]/g, "");
        var val = parseFloat(str.replace(",", "."));
        return (isNaN(val) || val < 0) ? 0 : val;
    }

    $("#amount").on('change blur keyup', function() {
        $("#ppamount").val(getValue().toFixed(2));
    });

    $("#paypal_form").submit(function(event) {
        var val = getValue();
        var msg = 'Due to PayPal fees, the minimum amount is $0.32. ' +
            'If you wish to download the file, press OK and you will not be charged. ' +
            'If you want to change the amount, press Cancel.';
        if (val < 0.32) {
            event.preventDefault();
            if (val == 0 || confirm(msg)) {
                window.location = "DOWNLOAD_LINK";
            }
        }
    });

That’s it. The form will submit the entered amount (with the help of some JavaScript) to the PayPal web site, which will then redirect the user back to DOWNLOAD_LINK once they complete the payment, or to CANCEL_LINK if they bail out. Of course, there are several things you need to be careful about, and that’s where the JavaScript steps in.

First of all, PayPal expects a properly formatted amount, which is why we need to parse the user’s input. In order to do that, we first remove everything that’s not a digit or a decimal point, and then convert it to a float. If it’s not a valid non-negative number, we default it to 0, and update the value in the text field. We need to do this every time the entered value changes, hence the event listener.

Finally, before submitting the form, we need to check the value once again, but there’s also a catch – PayPal has some fees which will be deducted from the entered amount, so you might end up with $0. You can view those fees here, and by using simple maths calculate what the minimal amount of money somebody needs to pay for you to have a profit is. For Standard accounts, this is $0.32.

The best thing about this approach is that it doesn’t have to be bulletproof, since you’re already prepared to give away the product for free. There are also other options you can add to the payment form, and you can see those here.