yum, we accept bitcoin!
This commit is contained in:
parent
6f92df17a4
commit
a3c31f7042
3 changed files with 30 additions and 20 deletions
|
@ -1,27 +1,28 @@
|
|||
(function() {
|
||||
var donationForm = document.getElementById('donation-form');
|
||||
var amountField = donationForm.querySelector(
|
||||
'[name=donation\\[amount\\]]');
|
||||
var emailField = donationForm.querySelector(
|
||||
'[name=donation\\[donor_email\\]]');
|
||||
var tokenField = donationForm.querySelector(
|
||||
'[name=donation\\[stripe_token\\]]');
|
||||
|
||||
function field(name) {
|
||||
return donationForm.querySelector(
|
||||
'input[name=donation\\[' + name + '\\]]');
|
||||
}
|
||||
|
||||
var checkout = StripeCheckout.configure({
|
||||
key: donationForm.getAttribute('data-checkout-publishable-key'),
|
||||
image: donationForm.getAttribute('data-checkout-image'),
|
||||
token: function(token) {
|
||||
tokenField.value = token.id;
|
||||
emailField.value = token.email;
|
||||
field('stripe_token').value = token.id;
|
||||
field('stripe_token_type').value = token.type;
|
||||
field('donor_email').value = token.email;
|
||||
donationForm.submit();
|
||||
}
|
||||
},
|
||||
bitcoin: true
|
||||
});
|
||||
|
||||
donationForm.addEventListener('submit', function(e) {
|
||||
if (!tokenField.value) {
|
||||
if (!field('stripe_token').value) {
|
||||
e.preventDefault();
|
||||
|
||||
var amount = Math.floor(parseFloat(amountField.value) * 100);
|
||||
var amount = Math.floor(parseFloat(field('amount').value) * 100);
|
||||
|
||||
if (!isNaN(amount)) {
|
||||
checkout.open({
|
||||
|
|
|
@ -16,16 +16,24 @@ class Donation < ActiveRecord::Base
|
|||
|
||||
campaign.progress += amount
|
||||
|
||||
charge_params = {
|
||||
amount: amount,
|
||||
description: 'Donation (thank you!)',
|
||||
currency: 'usd'
|
||||
}
|
||||
|
||||
if params[:stripe_token_type] == 'card'
|
||||
customer = Stripe::Customer.create(
|
||||
card: params[:stripe_token]
|
||||
)
|
||||
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(
|
||||
:customer => customer.id,
|
||||
:amount => amount,
|
||||
:description => 'Donation (thank you!)',
|
||||
:currency => 'usd'
|
||||
)
|
||||
charge = Stripe::Charge.create(charge_params)
|
||||
|
||||
donation = campaign.donations.build
|
||||
donation.amount = amount
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
'data-checkout-image' => image_path('default_preview.png'),
|
||||
'data-checkout-publishable-key' => Rails.configuration.stripe[:publishable_key] do
|
||||
= hidden_field_tag 'donation[stripe_token]'
|
||||
= hidden_field_tag 'donation[stripe_token_type]'
|
||||
= hidden_field_tag 'donation[donor_email]'
|
||||
%header
|
||||
%p#donation-form-title
|
||||
|
|
Loading…
Reference in a new issue