Selling your Downloads
There are two ways to sell your download. The simplest way is to copy the sell page link from the download configuration in your dashboard and paste in any where on your site. This approach will allow you to sell downloads with zero changes on your website.
A more comprehensive approach that would provide deeper integration would be to follow the steps below.
Integration
Step 1 - Prerequisites
In the HEAD of your HTML page, include the tinypass.js file. For example:
<script type="text/javascript" src="http://code.tinypass.com/tinypass.js"></script>
Step 2 - Configuration
/* Include TinyPass - make sure to use the correct path */ include_once "/path/to/api/TinyPass.php" TinyPass::$AID = "your_sandbox_aid"; TinyPass::$PRIVATE_KEY = "your_sandbox_private_key"; TinyPass::$SANDBOX = true;
TinyPass.SANDBOX = true; TinyPass.AID = "your_sandbox_aid"; TinyPass.PRIVATE_KEY = "your_sandbox_private_key";
TinyPass.TP.SANDBOX = true; TinyPass.TP.AID = "your_sandbox_aid"; TinyPass.TP.PRIVATE_KEY = "your_sandbox_private_key";
require 'tinypass' Tinypass.sandbox = true Tinypass.aid = "your_sandbox_aid" Tinypass.private_key = "your_sandbox_private_key"
Step 3 - Check Access
At this point, we need to check if the user already has access to the requested download. You can retrieve the download resource id by opening the dashboard and clicking on an existing download.
/* Include TinyPass - make sure to use the correct path */ include_once "/path/to/api/TinyPass.php" /* Load the incoming request cookie header into AccessTokenStore, which will parse * out the user access information */ $rid = "unique_resource_id"; $store = new TPAccessTokenStore(); $store->loadTokensFromCookie($_COOKIE); $token = $store->getAccessToken($rid); if($token->isAccessGranted()) { //Generate a secure URL $dlmap = TinyPass::generateDownloadURL(array('rid'=>$rid, 'lifetime'=>10)); //dlmap is a map containing a 'url' element echo "<a href='".$dlmap['url']."' >Download is Ready, click to start.</a>"; } else { //Forward the user to a buy page (or a way to call the buyNow javascript method below) }
/* Load the incoming request cookie header into AccessTokenStore, which will parse * out the user access information */ String rid = "unique_resource_id"; AccessTokenStore store = new AccessTokenStore(); store.loadTokensFromCookie(request.getHeader("cookie")); AccessToken token = store.getAccessToken(rid); if(token.isAccessGranted()) { //Generate a secure URL String url = TinyPass.generateDownloadURL(rid, 10, null); out.println("<a href='" + url + "' >Click here to download</a>"); } else { //Forward the user to a buy page (or a way to call the buyNow javascript method below) }
/* Load the incoming request cookie header into AccessTokenStore, which will parse * out the user access information */ var rid = "unique_resource_id"; var store = new AccessTokenStore(); store.LoadTokensFromCookie(Request.Headers["cookie"]); var token = store.GetAccessToken(rid); if(token.IsAccessGranted()) { //Generate a secure URL //dlmap is a map containing a 'url' element var dlmap = TinyPass.TP.generateDownloadURL(rid,30,null); } else { //Forward the user to a buy page (or a way to call the buyNow javascript method below) }
require 'tinypass' # Load the incoming request cookie header into AccessTokenStore, which will parse * out the user access information rid = "unique_resource_id" store = Tinypass::AccessTokenStore.new store.load_tokens_from_cookie(cookies) if store.get_access_token(rid).access_granted? # Generate a secure URL # dlmap.url will be the path for a unique download URL a user can use dlmap = Tinypass.generate_download_url({rid: rid, lifetime: 30}) else # Forward the user to a buy page (or a way to call the buyNow javascript method below) end
Step 4 - Access denied? Give them a way to buy
In the previous step, we left out the logic for determining what happened when access is granted.
In the event of access being denied, you'll want to direct them to a page with the following snippet of JavaScript.
<script type='text/javascript' src='http://code.tinypass.com/tinypass.js'></script> <div> <a href="#" onclick="buyNow("your_rid", "your_aid")">Click to Purchase this Download</a> </div> <script type='text/javascript'> //Configure the environment for the Tinypass client code //TinyPass.ENV = "prod"; TinyPass.ENV = "sand"; //The default client-side access approach is to refresh once access is granted function buyNow(rid, aid){ var params = { rid: rid, aid: aid, //Callback to catch any errors onError: function(message){ //error handler alert(message); }, //Callback for listening after the popup closes and the user has access onAccessGranted: function(data){ window.location.reload(); } }; //Show the popup, onAccessGranted or onError will be called on completion tinypass.showOffer(params); } </script>
Single-Page PHP Example
<!DOCTYPE html> <html lang="en"> <head> <title>PHP SDK Download Example</title> <script type="text/javascript" src="http://code.tinypass.com/tinypass.js"></script> <script> //Configure the environment for the Tinypass client code //TinyPass.ENV = "prod"; TinyPass.ENV = "sand"; //The default client-side access approach is to refresh once access is granted function buyNow(rid, aid){ var params = { rid: rid, aid: aid, //Callback to catch any errors onError: function(message){ //error handler alert(message); }, //Callback for listening after the popup closes and the user has access onAccessGranted: function(data){ window.location.reload(); } }; //Show the popup, onAccessGranted or onError will be called on completion tinypass.showOffer(params); } </script> </head> <body> <h1>My Download</h1> <?php error_reporting(E_ALL); //Make sure to check your PHP log file ini_set('display_errors', '1'); /* Include TinyPass - make sure to use the correct path */ include_once (dirname(__FILE__) . "/tinypass-php-sdk-2.0.9/Tinypass.php"); // configure Tinypass TinyPass::$SANDBOX = true; TinyPass::$AID = "YOUR_AID"; TinyPass::$PRIVATE_KEY = "YOUR_PRIVATE_KEY"; /* Load the incoming request cookie header into AccessTokenStore, which will parse * out the user access information */ $rid = "copy_rid_from_your_download_dashboard"; $store = new TPAccessTokenStore(); $store->loadTokensFromCookie($_COOKIE); $token = $store->getAccessToken($rid); if($token->isAccessGranted()) { //Generate a secure URL $dlmap = TinyPass::generateDownloadURL(array('rid'=>$rid, 'lifetime'=>10)); //dlmap is a map containing a 'url' element echo "<a href='".$dlmap['url']."' >Download is Ready, click to start.</a>"; } else { #A reliable approach to escape the values ?> <script type="text/javascript"> var buyNowInfo = <?php print json_encode(array( 'aid' => TinyPass::$AID, 'rid' => $rid)); ?>; </script> <a href="#" onclick="buyNow(buyNowInfo.rid, buyNowInfo.aid);">Buy my download</a> <?php } ?> <p>© Tinypass 2013</p> </body> </html>