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

              
                <h1>Buy some cool widgets!</h1>

<form class="stripe-checkout">
	<div class="quantity-select">
		<label for="quantity">Select Quantity</label>
		<select name="quantity" class="form-control" id="quantity" onChange="calculatePrice()">
			<option value="1" selected>1</option>
			<option value="2">2</option>
			<option value="3">3</option>
			<option value="4">4</option>
			<option value="5">5</option>
			<option value="6">6</option>
			<option value="7">7</option>
			<option value="8">8</option>
			<option value="9">9</option>
			<option value="10">10</option>
			<option value="11">11</option>
			<option value="12">12</option>
			<option value="13">13</option>
			<option value="14">14</option>
			<option value="15">15</option>
			<option value="16">16</option>
			<option value="17">17</option>
			<option value="18">18</option>
			<option value="19">19</option>
			<option value="20">20</option>
		</select>
	</div><!-- .quantity-select -->

	<div class="text-content" id="pricingContent">
		<p class="total-price"><strong>Total: $89.99</strong></p>
	</div><!-- .text-content -->

	<button id="buyNow" data-price="8999">Pay <span class="price">$89.99</span></button>
</form>

<p><em>Select a quantity, and the click "Pay" to see this Pen in action!</em></p>
              
            
!

CSS

              
                body {
	font-family: sans-serif;
}

.stripe-checkout {
	background-color: gray;
	padding: 24px;
	width: 300px;
}

table {
	margin: 5px 0;
}

.total td {
	border-top: 2px solid black;
}

              
            
!

JS

              
                /**
 * Calculate price.
 */
function calculatePrice() {
	// Set defaults.
	const normalRetailPrice = 89.99;
	const extraDiscount = 0;
	let quantity = Number(document.querySelector("#quantity").value);
	let discount = 0;

	// First, calculate discount.
	if (quantity < 2) {
		discount = 0;
	} else if (quantity > 1 && quantity < 6) {
		discount = 30;
	} else if (quantity > 5 && quantity < 11) {
		discount = 33;
	} else if (quantity > 10 && quantity < 21) {
		discount = 35;
	} else if (quantity > 20 && quantity < 26) {
		discount = 40;
	}

	// Next, calculate the different prices.
	let price = +(
		quantity * normalRetailPrice -
		(quantity * normalRetailPrice * discount) / 100
	).toFixed(2);
	let pricePerSubscription = +(price / quantity).toFixed(2);
	let discountPrice = +(price - (price * extraDiscount) / 100).toFixed(2);
	let totalSavings = Math.abs(
		+(
			normalRetailPrice * quantity -
			(price - (price * extraDiscount) / 100)
		).toFixed(2)
	);
	let totalRetailPrice = +(normalRetailPrice * quantity).toFixed(2);
	let totalInCents = Math.round(discountPrice * 100);

	// Then, throw pricing into an object.
	let pricingData = {
		quantity: quantity,
		discount: discount,
		extraDiscount: extraDiscount,
		price: price,
		pricePerSubscription: pricePerSubscription,
		discountPrice: discountPrice,
		totalSavings: totalSavings,
		totalRetailPrice: totalRetailPrice,
		totalInCents: totalInCents
	};

	// Finally, update the markup!
	buyNowHandler(pricingData);
	pricingContentHandler(pricingData);
}

/**
 * Buy now button.
 */
function buyNowHandler(pricingData) {
	// Grab the buy now button.
	button = document.querySelector("#buyNow");

	// No button? Bail.
	if ("undefined" === button) {
		console.error("The buy now button is missing!");
	}

	// Update button content.
	button.setAttribute("data-price", pricingData.totalInCents);
	button.innerText = `Pay $${pricingData.discountPrice}`;
}

/**
 * Pricing content.
 */
function pricingContentHandler(pricingData) {
	// Grab the pricing content.
	content = document.querySelector("#pricingContent");

	// No content? Bail.
	if ("undefined" === content) {
		console.error("The pricing content is missing!");
	}

	// Update the markup.
	pricingContent.innerHTML = `
		<div class="text-content">
			<table class="pricing">
				<tr class="retail">
					<td>Retail Price</td>
					<td>$${pricingData.totalRetailPrice}</td>
				</tr>
				<tr class="discount">
					<td>${pricingData.discount}% Discount</td>
					<td>-$${pricingData.totalSavings}</td>
				</tr>
				<tr class="total">
					<td><strong>Total Price</strong></td>
					<td><strong>$${pricingData.discountPrice}</strong></td>
				</tr>
			</table>

			<p class="discount">You're saving ${pricingData.discount}% per subscription!</p>
		</div>
	`;
}

/**
 * Stripe checkout.
 *
 * @link https://stripe.com/docs/checkout#integration-custom
 */
function stripeHandler() {
	// Configure Stripe.
	const handler = StripeCheckout.configure({
		key: "pk_test_TYooMQauvdEDq54NiTphI7jx",
		image: "https://stripe.com/img/documentation/checkout/marketplace.png",
		locale: "auto",
		token: function (token) {
			// You can access the token ID with `token.id`.
			// Get the token ID to your server-side code for use.
		}
	});

	// Grab the button.
	let button = document.querySelector("#buyNow");

	// No button? Bail.
	if ("undefined" === button) {
		console.error("The buy now button is missing!");
	}

	// Open checkout on click.
	button.addEventListener("click", function (e) {
		handler.open({
			name: "Cool Widgets",
			description: "The best widgets money can buy!",
			zipCode: true,
			amount: button.dataset.price // Must be in cents!
		});
		e.preventDefault();
	});

	// Close Checkout on page navigation.
	window.addEventListener("popstate", () => handler.close());
}

// Fire checkout after everything has loaded.
document.addEventListener("DOMContentLoaded", stripeHandler());

              
            
!
999px

Console