Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!-- FastSpring demo showing how to enable a buyer to dynamically select a custom price for a product and have the localised version of that price (in the buyer's local currency) displayed back to them.

This demo uses the following features:

- Secure Requests: 
    - https://fastspring.com/docs/passing-sensitive-data-with-secure-requests/
    - https://fastspringexamples.com/method/fastspring-builder-securesecurepayload-securekey/
- Data Callbacks
    - https://fastspringexamples.com/callbacks/
    - https://fastspringexamples.com/callback/data-data-callback/

An example use-case for this feature might be allowing the buyer to select a number of seats for a license.

Please note: this demo may execute slowly because it is running on FastSpring's test server, which is not optimised for end-user experience. Live stores will execute in a more performant fashion.

-->

<html>
	<head>
    <!-- Loading JQuery -->
		<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
		<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css" />
	  <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
		
    <!-- Loading the FastSpring Storebuilder Library -->
    <script 
      id="fsc-api"
      src="https://sbl.onfastspring.com/sbl/1.0.1/fastspring-builder.min.js"
      type="text/javascript"
      data-storefront="aharvey.test.onfastspring.com/popup-aharvey"
      data-access-key="WVD4LEQOT0UV0HKWYTDJSA"
      data-data-callback="dataCallback">
    </script>
	</head>
  
	<body>
    
    
      <!-- Switch country to US on load -->
      <script>
        fastspring.builder.country('US');
      </script>
    
      <!-- Country selector for demo purposes -->
      <div class="column" style="background-color: lightSlateGrey; padding:20px; border-radius: 25px;">  
        <p>
          <button id="country-us" disabled="true" onclick="switchCountry('US');">Switch to US</button>
          <button id="country-fr" onclick="switchCountry('FR');">Switch to France</button>
          <button id="country-jp" onclick="switchCountry('JP');">Switch to Japan</button>
        </p>  
      </div>
      <br />    
			<span style="f
                   ont-weight: bold;" data-fsc-item-path="demo-product-1" data-fsc-item-display></span>
			<br />
			<span data-fsc-item-path="demo-product-1" data-fsc-item-description-summary></span>
			<p>&nbsp;</p>
			<img data-fsc-item-path="demo-product-1" data-fsc-item-image></img>
			<p>&nbsp;</p>
  
			<p>Select your price:</p>
			<div id="slider"></div>
  
			<p>Price: 
				<span id="slider-price"></span>
			</p>
  
    <button class="buyNowButton" id="buyNow" data-fsc-item-path-value="demo-product-1" data-fsc-action="Add,Checkout">
    <span data-fsc-item-path="demo-product-1" data-fsc-item-description-action></span>
    </button>
  
	</body>
              
            
!

CSS

              
                img {
    width: 200px;
    height: 200px;
}

body {
  font-family: Arial, Helvetica, sans-serif;
  margin:20px;
  padding:20px;
}

#slider {
    margin: 10px;
}
.ui-slider-horizontal {
    height: 8px;
    width: 200px;
}
.ui-slider .ui-slider-handle {
    height: 15px;
    width: 5px;
    padding-left: 5px;
}

button.buyNowButton {
  width: 200px;
  height: 30px;
}
              
            
!

JS

              
                var slider = document.getElementById("dynamicPrice");

/* Defining the slider in JQuery. 

  The change event is being used to only trigger the price update function once the slider stops moving.
  Documentation: 
    - https://api.jqueryui.com/slider/#event-change
    
  When this update process is triggered, we're disabling the buy button until the new price is returned to the buyer.

*/
$("#slider").slider({
    value: 25,
    min: 0,
    max: 500,
    step: 50,
    change: function(event, data) {
        document.getElementById('buyNow').innerText = "Updating Price..."; 
        document.getElementById('buyNow').disabled = true;
        updatePrice(data.value);
    }
});

$("#slider-price").html($('#slider').slider('value'));


/* Using FastSpring's Secure Request feature to push the custom price as the USD price of the product for this order.
   USD is the base currency of the store, and any currency conversions will be based on this value.
*/

function updatePrice(newPrice) {
    fastspring.builder.secure({
        "items": [{
            "product": "demo-product-1",
            "quantity": 1,
            "pricing": {
                "price": {
                    "USD": newPrice
                }
            }
        }]
    });
}

function dataCallback(data) {

    /*  This function is called every time there is a data callback
        Documentation:
          - https://fastspringexamples.com/callback/data-data-callback/ 
    */


    // Loop over the JSON payload returned

    if (data && data.hasOwnProperty('groups')) {
        const {
            groups
        } = data;

        groups.forEach(group => {
            if (group.items && Array.isArray(group.items)) {
                group.items.forEach(item => {

                    // Display the pricing information for our product only
                    if (item.path == "demo-product-1") {

                        // Update the page with the price, fully localised to the buyer's location.
                        $("#slider-price").html(item.price);
                    }

                });
            }
        });
    }

    // Re-enable the buy now button
    document.getElementById('buyNow').disabled = false;
    document.getElementById('buyNow').innerText = "Buy It Now";

}

function switchCountry(country) {
  // Switch countries for demo purposes
  
  fastspring.builder.country(country);
  
  // Disable the appropriate button
  switch (country) {
    case "US":
      document.getElementById('country-us').disabled = true;
      document.getElementById('country-fr').disabled = false;
      document.getElementById('country-jp').disabled = false;
    break;
    case "FR":
      document.getElementById('country-us').disabled = false;
      document.getElementById('country-fr').disabled = true;
      document.getElementById('country-jp').disabled = false;
    break;
    case "JP":
      document.getElementById('country-us').disabled = false;
      document.getElementById('country-fr').disabled = false;
      document.getElementById('country-jp').disabled = true;
    break;
  }
}
              
            
!
999px

Console