How to Accept Social Payments on your Website

Web publishers and content creators are experimenting with a “cash-less” payment model where people can use social networks to pay for premium products like ebooks, songs, discount coupons, research reports, etc.

Say you have written a short ebook on a particular topic. Now instead of asking 99¢ for a copy, you can let anyone download your ebook either in exchange of a tweet, a like on Facebook or a +1 on Google Plus. The buyer makes a public recommendation of your product on a social network and you reward them with a free copy.

[rss]Pay with Tweet or Facebook Like](/internet/social-payments/27898/)[/rss][norss]Before getting into the implementation, click here for a demo (screenshot). You can perform a Like, do a +1 or post a tweet and that action will unlock the link to download a PDF file.

Unlock Premium Content with a Tweet, Like or +1

To integrate social payments into your website, copy everything between the <body> tags from the snippet below and add it your website template.  Also modify the line blocks 13 (file URL), 56 (text of the tweet), 61 (URL to +1) & 65 (your Facebook Page).

<html xmlns:fb="http://ogp.me/ns/fb#">
  <head>
    <title>Pay with a Tweet or Facebook Like</title>
    /* Social Payments by Digital Inspiration (2013) */ /* You are free to use, modify or republish this code */
  </head>
  <body>
    <div id="fb-root"></div>
    <script type="text/javascript">
      function labnol() {
        // The file URL that visitors can download after their Like/tweet/+1
        var url = 'http://www.labnol.org/';
        url = "<a href='" + url + "'>Click here</a> to download the file.";
        document.getElementById('restricted').innerHTML = url;
      }

      window.fbAsyncInit = function () {
        FB.init({ status: true, cookie: true, xfbml: true });
        FB.Event.subscribe('edge.create', function (response) {
          labnol();
        });
      };

      (function (d, s, id) {
        var js,
          fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s);
        js.id = id;
        js.src = '//connect.facebook.net/en_US/all.js';
        fjs.parentNode.insertBefore(js, fjs);
      })(document, 'script', 'facebook-jssdk');

      window.twttr = (function (d, s, id) {
        var t,
          js,
          fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s);
        js.id = id;
        js.src = '//platform.twitter.com/widgets.js';
        fjs.parentNode.insertBefore(js, fjs);
        return (
          window.twttr ||
          (t = {
            _e: [],
            ready: function (f) {
              t._e.push(f);
            },
          })
        );
      })(document, 'script', 'twitter-wjs');

      twttr.ready(function (twttr) {
        twttr.events.bind('tweet', function (event) {
          labnol();
        });
      });
    </script>

    <div id="restricted">
      // Add the default text of the tweet and your Twitter handle
      <a
        href="https://twitter.com/share"
        data-text="Digital Inspiration is my favorite how-to blog http://labnol.org"
        data-via="labnol"
        class="twitter-share-button"
        data-lang="en"
      ></a>

      // The full URL of your Facebook Page goes here
      <fb:like
        href="http://www.facebook.com/digital.inspiration"
        send="false"
        layout="button_count"
        width="220"
        show_faces="false"
      ></fb:like>
    </div>
  </body>
</html>

Here’s how the code works:

When the user clicks on any of the social buttons, the event is captured and then, using simple JavaScript, the link to download the file is added in place of the button. If the user open the tweet dialog but decides not to post one, that “cancel” action won’t unlock the secret link.

There are ready made solutions available, Pay with a Tweet is a popular one, but they require too many steps for the user to complete the transaction. Here, it’s as easy as clicking the regular “tweet” or “like” button.

Also, anyone with technical skills can easily discover the download link from your HTML source code but most web users aren’t likely to do that.[/norss]

Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.