Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Adds a script that handles stripe checkout buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krienbühl committed Jun 1, 2017
1 parent f872e20 commit a01ea74
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
63 changes: 63 additions & 0 deletions onegov/pay/assets/js/stripe.js
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);
}
});
10 changes: 10 additions & 0 deletions onegov/pay/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ def configure_payment_providers(self, **cfg):
def default_payment_provider(self):
return self.session().query(PaymentProvider)\
.filter_by(default=True).first()


@PayApp.webasset_path()
def get_js_path():
return 'assets/js'


@PayApp.webasset('pay')
def get_pay_assets():
yield 'stripe.js'
1 change: 1 addition & 0 deletions onegov/pay/models/payment_providers/stripe.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def checkout_button(self, label, amount, currency, **extra):

extra['amount'] = round(amount * 100, 0)
extra['currency'] = currency
extra['key'] = self.publishable_key

attributes = (
(escape(str(key)), escape(str(value)))
Expand Down

0 comments on commit a01ea74

Please sign in to comment.