yum, we accept bitcoin!

This commit is contained in:
Emi Matchu 2014-12-23 23:22:15 -05:00
parent 6f92df17a4
commit a3c31f7042
3 changed files with 30 additions and 20 deletions

View file

@ -1,27 +1,28 @@
(function() { (function() {
var donationForm = document.getElementById('donation-form'); var donationForm = document.getElementById('donation-form');
var amountField = donationForm.querySelector(
'[name=donation\\[amount\\]]'); function field(name) {
var emailField = donationForm.querySelector( return donationForm.querySelector(
'[name=donation\\[donor_email\\]]'); 'input[name=donation\\[' + name + '\\]]');
var tokenField = donationForm.querySelector( }
'[name=donation\\[stripe_token\\]]');
var checkout = StripeCheckout.configure({ var checkout = StripeCheckout.configure({
key: donationForm.getAttribute('data-checkout-publishable-key'), key: donationForm.getAttribute('data-checkout-publishable-key'),
image: donationForm.getAttribute('data-checkout-image'), image: donationForm.getAttribute('data-checkout-image'),
token: function(token) { token: function(token) {
tokenField.value = token.id; field('stripe_token').value = token.id;
emailField.value = token.email; field('stripe_token_type').value = token.type;
field('donor_email').value = token.email;
donationForm.submit(); donationForm.submit();
} },
bitcoin: true
}); });
donationForm.addEventListener('submit', function(e) { donationForm.addEventListener('submit', function(e) {
if (!tokenField.value) { if (!field('stripe_token').value) {
e.preventDefault(); e.preventDefault();
var amount = Math.floor(parseFloat(amountField.value) * 100); var amount = Math.floor(parseFloat(field('amount').value) * 100);
if (!isNaN(amount)) { if (!isNaN(amount)) {
checkout.open({ checkout.open({

View file

@ -16,16 +16,24 @@ class Donation < ActiveRecord::Base
campaign.progress += amount campaign.progress += amount
customer = Stripe::Customer.create( charge_params = {
card: params[:stripe_token] amount: amount,
) description: 'Donation (thank you!)',
currency: 'usd'
}
charge = Stripe::Charge.create( if params[:stripe_token_type] == 'card'
:customer => customer.id, customer = Stripe::Customer.create(
:amount => amount, card: params[:stripe_token]
:description => 'Donation (thank you!)', )
:currency => 'usd' charge_params[:customer] = customer.id
) elsif params[:stripe_token_type] == 'bitcoin_receiver'
charge_params[:card] = params[:stripe_token]
else
raise ArgumentError, "unexpected stripe token type #{params[:stripe_token_type]}"
end
charge = Stripe::Charge.create(charge_params)
donation = campaign.donations.build donation = campaign.donations.build
donation.amount = amount donation.amount = amount

View file

@ -10,6 +10,7 @@
'data-checkout-image' => image_path('default_preview.png'), 'data-checkout-image' => image_path('default_preview.png'),
'data-checkout-publishable-key' => Rails.configuration.stripe[:publishable_key] do 'data-checkout-publishable-key' => Rails.configuration.stripe[:publishable_key] do
= hidden_field_tag 'donation[stripe_token]' = hidden_field_tag 'donation[stripe_token]'
= hidden_field_tag 'donation[stripe_token_type]'
= hidden_field_tag 'donation[donor_email]' = hidden_field_tag 'donation[donor_email]'
%header %header
%p#donation-form-title %p#donation-form-title