Skip to content

Commit

Permalink
Merge pull request #240 from Crowdtilt/user_per_payment
Browse files Browse the repository at this point in the history
create CT API user per payment
  • Loading branch information
liuhenry committed Mar 10, 2014
2 parents 00bba83 + 7717b6b commit c826674
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 14 deletions.
31 changes: 22 additions & 9 deletions app/assets/javascripts/campaigns.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,31 @@ Crowdhoster.campaigns =

errors = crowdtilt.card.validate(cardData)
if !$.isEmptyObject(errors)
$('#refresh-msg').hide()
$.each errors, (index, value) ->
$('#errors').append('<p>' + value + '</p>')
$('#errors').show()
$('.loader').hide()
$button = $('button[type="submit"]')
$button.attr('disabled', false).html('Confirm payment of $' + $button.attr('data-total'))
$('#card_number').attr('name', 'card_number')
$('#security_code').attr('name', 'security_code')
Crowdhoster.campaigns.resetPaymentForm()
else
user_id = $form.find('#ct_user_id').val()
crowdtilt.card.create(user_id, cardData, this.cardResponseHandler)
$.post($form.attr('data-user-create-action'), { email: $(document.getElementById('email')).val() })
.done((user_id) ->
$('#ct_user_id').val(user_id)
crowdtilt.card.create(user_id, cardData, Crowdhoster.campaigns.cardResponseHandler)
)
.fail((jqXHR, textStatus) ->
Crowdhoster.campaigns.resetPaymentForm()
if jqXHR.status == 400
$('#errors').append('<p>Sorry, we weren\'t able to process your contribution. Please try again.</p><br><p>If you continue to experience issues, please <a href="mailto:[email protected]?subject=Support request for creating user payment">click here</a> to email support.</p>')
else
$('#errors').append('<p>Sorry, we weren\'t able to process your contribution. Please try again.</p><br><p>If you continue to experience issues, please <a href="mailto:[email protected]?subject=Unable to create user payment">click here</a> to email support.</p>')
)

resetPaymentForm: (with_errors = true) ->
$('#refresh-msg').hide()
$('#errors').show() if with_errors
$('.loader').hide()
$button = $('button[type="submit"]')
$button.attr('disabled', false).html('Confirm payment of $' + $button.attr('data-total'))
$('#card_number').attr('name', 'card_number')
$('#security_code').attr('name', 'security_code')

timeCheck: (element) ->
expiration = $(element).attr("date-element")
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/campaigns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ def checkout_error
render nothing: true
end

def ajax_create_payment_user
@campaign.production_flag? ? Crowdtilt.production(@settings) : Crowdtilt.sandbox
begin
render text: Crowdtilt.create_user(email: params[:email])['id']
rescue
render text: 'error', status: 400
end
end

private

def load_campaign
Expand Down
1 change: 1 addition & 0 deletions app/mixins/checkout_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def basic_payment_info(params)
}

info[:amount] = (params[:amount].to_f*100).ceil if params[:amount]
info[:ct_user_id] = params[:ct_user_id] if params[:ct_user_id]

info
end
Expand Down
3 changes: 2 additions & 1 deletion app/models/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ class Payment < ActiveRecord::Base
:address_one, :address_two, :city, :state, :postal_code, :country, :quantity,
:additional_info, :client_timestamp,
:ct_charge_request_id, :ct_charge_request_error_id,
:ct_tokenize_request_id, :ct_tokenize_request_error_id
:ct_tokenize_request_id, :ct_tokenize_request_error_id,
:ct_user_id

validates :fullname, :quantity, presence: true
validates :email, presence: true, email: true
Expand Down
8 changes: 5 additions & 3 deletions app/views/campaigns/checkout_payment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<div class="well checkout_block" style="padding-bottom: 16px">

<form accept-charset="UTF-8" action="<%= checkout_process_path(@campaign) %>" method="post" id="payment_form" data-error-action="<%= checkout_error_path(@campaign) %>">
<form accept-charset="UTF-8" action="<%= checkout_process_path(@campaign) %>" method="post" id="payment_form" data-error-action="<%= checkout_error_path(@campaign) %>" data-user-create-action="<%= ajax_create_payment_user_path(@campaign) %>">

<input name="authenticity_token" type="hidden" value="<%= form_authenticity_token %>">
<h4 class="contact">Contact Information</h4>
Expand Down Expand Up @@ -120,7 +120,7 @@
<label for="security_code" class="error" style="display: none"></label>
<label for="billing_postal_code" class="error" style="display: none"></label>

<input id="ct_user_id" type="hidden" name="ct_user_id" value="<%= @campaign.production_flag ? @settings.ct_production_guest_id : @settings.ct_sandbox_guest_id %>" >
<input id="ct_user_id" type="hidden" name="ct_user_id" value="">

<input id="quantity" type="hidden" name="quantity" value="<%= @quantity %>">
<input id="amount" type="hidden" name="amount" value="<%= @amount %>">
Expand All @@ -133,7 +133,9 @@
<% end%>

<div class="payment-submit">
<button class="btn btn-primary show_loader" type="submit" data-total="<%= number_with_precision(@total, precision: 2) %>" data-loader="payment_form">Confirm payment of $<%= number_with_precision(@total, :delimiter => ",", precision: 2) %></button>
<button class="btn btn-primary show_loader" type="submit" data-total="<%= number_with_precision(@total, precision: 2) %>" data-loader="payment_form">
Confirm payment of $<%= number_with_precision(@total, :delimiter => ",", precision: 2) %>
</button>
<span class="loader" data-loader="payment_form" style="display:none"></span>
<div id="refresh-msg" style="display: none; color: red; margin-top: 10px">Your payment is being processed! Please do not refresh this page.</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
match '/:id/checkout/confirmation', to: 'campaigns#checkout_confirmation', as: :checkout_confirmation
post '/:id/checkout/error', to: 'campaigns#checkout_error', as: :checkout_error
match '/:id', to: 'campaigns#home', as: :campaign_home
match '/:id/ajax/create_payment_user', to: 'campaigns#ajax_create_payment_user', as: :ajax_create_payment_user


namespace :api, defaults: {format: 'json'} do
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20140307205637_add_ct_user_id_to_payments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCtUserIdToPayments < ActiveRecord::Migration
def change
add_column :payments, :ct_user_id, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20140128001609) do
ActiveRecord::Schema.define(:version => 20140307205637) do

create_table "campaigns", :force => true do |t|
t.string "name"
Expand Down Expand Up @@ -130,6 +130,7 @@
t.string "ct_tokenize_request_error_id"
t.string "ct_charge_request_id"
t.string "ct_charge_request_error_id"
t.string "ct_user_id"
end

create_table "rewards", :force => true do |t|
Expand Down

0 comments on commit c826674

Please sign in to comment.