HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<form id='cart' method='post' name='dalenysForm' onsubmit='submitForm(event)'>
<div class='w1'>
<p>
<label>
Card number
</label>
<input class='cc-num' id='cardnum' name='cc-number' oninput='cancel(this);' onkeyup='dalenys.brandDetector.detectBrandsByBin(this.value, function (brands) { manageDefaultBrand(brands) })' placeholder='•••• •••• •••• ••••' required type='tel'>
</p>
</div>
<div class='w2'>
<p>
<label>
Expiry
</label>
<input id='cardexp' maxlength='7' name='cc-exp' oninput='cancel(this)' placeholder='MM/YY' required type='tel'>
</p>
</div>
<div class='w2'>
<p>
<label>
CVV
</label>
<input id='cardcvv' maxlength='4' name='cc-csc' oninput='cancel(this)' placeholder='•••' required type='tel'>
</p>
</div>
<div class='w1'>
<p>
<input type='submit' value='Pay'>
</p>
</div>
<!-- This hidden input will receive the selected brandtoken -->
<input id='selected-brand' name='selected-brand' type='hidden'>
</form>
// Dalenys colors
// Primary color
$yellow: #fff756
// Background
$white: #fff
$silver: #ebebfa
// Secondary colors
$coral: #ff7063
$teal: #37dcc8
$light-blue: #8ddaff
$pink: #ffc4d3
$orange: #ffc758
body,
body *
box-sizing: border-box
body *
font-family: "Open Sans", "Century Gothic", "Calibri", "Trebuchet MS", Arial, Helvetica, sans-serif
font-size: 1em
transition: all 250ms ease-in-out
body
min-width: 280px
padding: 20px
background-color: $silver
.w1
width: 100%
.w2
width: 48%
display: inline-block
form
width: 100%
max-width: 240px
margin: 0 auto
box-sizing: border-box
overflow: hidden
padding: 21px
background-color: white
box-shadow: 5px 5px 5px rgba(0,0,0,.2)
label
font-weight: 600
p,
p label
display: block
width: 100%
div:not(:last-of-type) p
margin-bottom: 15px
.w2
&:nth-of-type(3) p
margin-left: 20%
p
width: 80%
input[type=text],
input[type=tel]
display: inline-block
max-width: 100%
height: 2em
overflow: hidden
margin-top: 7px
padding: 5px 7px 3px 7px
border: 1px solid darkgrey
border-radius: 5px
box-shadow: 0 0 0 1px darkgrey
transition: all 250ms ease-in-out
&.valid
border: 2px solid $teal
&.invalid
border: 2px solid $coral
input[type=submit]
width: 100%
max-width: 300px
margin-top: 7px
padding: 3px 14px 7px 14px
border: 0
border-top: 1px solid rgba(255, 255, 255, 1)
border-radius: 5px
background: $teal
font-size: 1.5em
font-family: "Titillium Web", Arial, sans-serif
font-weight: 600
color: white
vertical-align: baseline
cursor: pointer
transition: all 500ms
&:hover
background-color: lighten($teal, 20%)
// Encryption and submition script
(function (window) {
function createXhrPost(serviceUrl, resolve) {
let xhr = new XMLHttpRequest();
xhr.open("POST", serviceUrl, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var json = JSON.parse(xhr.responseText);
resolve(json);
}
};
return xhr;
}
function fetchPublicKey(serviceUrl, apiKeyId) {
return new Promise((resolve) => {
let xhr = createXhrPost(serviceUrl, resolve);
var data = JSON.stringify({
apiKeyId: apiKeyId
});
xhr.send(data);
});
}
function encryptPlainText(plainText, publicKey) {
let cryptoSubtle;
let btoa = window.btoa;
let atob = window.atob;
// Fix Apple prefix if needed
if (window.crypto && !window.crypto.subtle && window.crypto.webkitSubtle) {
cryptoSubtle = window.crypto.subtle;
} else if (window.crypto && window.crypto.subtle) {
cryptoSubtle = window.crypto.subtle;
}
if (!cryptoSubtle) {
throw "No crypto api";
}
function getMessageEncoding(message) {
let enc = new TextEncoder();
return enc.encode(message);
}
function encryptMessage(publicKey, message) {
let encoded = getMessageEncoding(message);
return cryptoSubtle.encrypt(
{
name: "RSA-OAEP"
},
publicKey,
encoded
);
}
function arrayBufferToBase64(buffer) {
var binary = "";
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
function strToArrayBuffer(str) {
const buf = new ArrayBuffer(str.length);
const bufView = new Uint8Array(buf);
for (let i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function importRsaKey(pem) {
// base64 decode the string to get the binary data
const binaryDerString = atob(pem);
// convert from a binary string to an ArrayBuffer
const binaryDer = strToArrayBuffer(binaryDerString);
return cryptoSubtle.importKey(
"spki",
binaryDer,
{
name: "RSA-OAEP",
hash: "SHA-1"
},
true,
["encrypt"]
);
}
return importRsaKey(publicKey)
.then((binaryPublicKey) => {
return encryptMessage(binaryPublicKey, plainText);
})
.then((encryptedBytes) => {
return arrayBufferToBase64(encryptedBytes);
});
}
/**
* serviceUrl: form post url
* postPayload: expected content {
* "ENCRYPTEDDATA": base64Message,
* "ENCRYPTIONKEYID": encryptionKeyId,
* "APIKEYID": apiKeyId
* };
* */
function postEncryptedData(serviceUrl, postPayload) {
console.log(postPayload);
return fetch(serviceUrl, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify(postPayload)
}).then(async function (response) {
return await response.json();
}).catch(error => {
return {"error": error};
});
}
async function preparePayload(cseServiceUrl, apiKeyId, cardInfo) {
let plainText = JSON.stringify(cardInfo);
let rsaPublicKey = await cseLib.fetchPublicKey(cseServiceUrl, apiKeyId);
console.log(rsaPublicKey);
let encryptedMsd = await cseLib.encryptPlainText(
plainText,
rsaPublicKey.encryptionPublicKey
);
return {
ENCRYPTEDDATA: encryptedMsd,
ENCRYPTIONKEYID: rsaPublicKey.encryptionKeyId,
APIKEYID: apiKeyId
};
}
window.cseLib = {
fetchPublicKey: fetchPublicKey,
encryptPlainText: encryptPlainText,
preparePayload: preparePayload,
postEncryptedData: postEncryptedData
};
})(window);
const cseLib = window.cseLib;
// Form submission function
function submitForm(e) {
let apiKeyId = "62acc7fe-3496-434d-b24e-b14ebf296340";
let cseServiceUrl = "https://sandbox-payment.dlns.io/cseKeys";
let postFormUrl = "https://secure-test.dalenys.com/hosted-fields/service/tokenize";
e.preventDefault();
let jsonData = {
CARDCODE: document.getElementById('cardnum').value.replaceAll(' ', ''),
CARDVALIDITYDATE: document.getElementById('cardexp').value.replace('/', '-').replaceAll(' ', ''),
CARDCVV: document.getElementById('cardcvv').value,
SELECTEDBRAND: document.querySelector('#selected-brand').value
};
cseLib.preparePayload(cseServiceUrl, apiKeyId, jsonData).then(
(postPayLoad) => {
cseLib.postEncryptedData(postFormUrl, postPayLoad).then((response) => {
if (response.EXECCODE !== '0000') {
alert("Error : " + response.MESSAGE);
return;
}
prompt("Generated token:", response.HFTOKEN);
});
},
(reason) => {
console.error(reason);
}
);
}
// jQuery.payment
jQuery(function($) {
$('#cardnum').payment('formatCardNumber');
$('#cardexp').payment('formatCardExpiry');
$('#cardcvv').payment('formatCardCVC');
$('form').submit(function(e) {
e.preventDefault();
const cardNumField = document.querySelector('#cardnum');
const cardExpField = document.querySelector('#cardexp');
const cardCvvField = document.querySelector('#cardcvv');
const cardType = $.payment.cardType($('#cardnum').val());
let validCardNum = $.payment.validateCardNumber($('#cardnum').val());
!validCardNum ? cardNumField.classList.add('invalid') : cardNumField.classList.add('valid');
let validCardExp = $.payment.validateCardExpiry($('#cardexp').payment('cardExpiryVal'));
!validCardExp ? cardExpField.classList.add('invalid') : cardExpField.classList.add('valid');
let validCardCvv = $.payment.validateCardCVC($('#cardcvv').val(), cardType);
!validCardCvv ? cardCvvField.classList.add('invalid') : cardCvvField.classList.add('valid');
});
});
// Input fields style reset
function cancel(e)
{
e.classList.remove('valid');
e.classList.remove('invalid');
}
// Manages the selected default brand
function manageDefaultBrand(brands)
{
if(brands != '' && brands != 'null') {
const defaultBrand = brands[0]['brand']
document.querySelector('#selected-brand').value = defaultBrand;
}
}
Also see: Tab Triggers