This repository has been archived by the owner on Sep 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a script that handles stripe checkout buttons
- Loading branch information
Denis Krienbühl
committed
Jun 1, 2017
1 parent
f872e20
commit a01ea74
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
var loadScript = function(url, callback, location) { | ||
var scriptTag = document.createElement('script'); | ||
scriptTag.onload = callback; | ||
scriptTag.onreadystatechange = callback; | ||
scriptTag.src = url; | ||
|
||
(location || document.body).appendChild(scriptTag); | ||
}; | ||
|
||
var getStripeAttributes = function(button) { | ||
var attributes = []; | ||
|
||
for (var i = 0; i < button.attributes.length; i++) { | ||
var attribute = button.attributes[i]; | ||
|
||
if (attribute.name.startsWith('data-stripe')) { | ||
attributes.push(attribute); | ||
} | ||
} | ||
|
||
return attributes; | ||
}; | ||
|
||
var setupCheckoutButton = function(button) { | ||
var config = { | ||
token: function(token) { | ||
button.setAttribute('value', token.id); | ||
} | ||
}; | ||
|
||
var attributes = getStripeAttributes(button); | ||
for (var i = 0; i < attributes.length; i++) { | ||
var attribute = attributes[i]; | ||
config[attribute.name.replace('data-stripe-', '')] = attribute.value; | ||
} | ||
|
||
var handler = StripeCheckout.configure(config); | ||
button.addEventListener('click', function(e) { | ||
handler.open(); | ||
e.preventDefault(); | ||
}); | ||
|
||
// close checkout on page navigation | ||
window.addEventListener('popstate', function() { | ||
handler.close(); | ||
}); | ||
}; | ||
|
||
var setupCheckout = function(buttons) { | ||
loadScript('https://checkout.stripe.com/checkout.js', function() { | ||
for (var i = 0; i < buttons.length; i++) { | ||
setupCheckoutButton(buttons[i]); | ||
} | ||
}); | ||
}; | ||
|
||
document.addEventListener("DOMContentLoaded", function() { | ||
var buttons = document.querySelectorAll('.stripe-connect'); | ||
|
||
if (buttons.length !== 0) { | ||
setupCheckout(buttons); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters