From 7717b6bffd885d48507a8beb9116ebab0f0fb92c Mon Sep 17 00:00:00 2001 From: Sam Wyman Date: Fri, 7 Mar 2014 18:29:10 -0800 Subject: [PATCH] create CT API user per payment --- app/assets/javascripts/campaigns.js.coffee | 31 +++++++++++++------ app/controllers/campaigns_controller.rb | 9 ++++++ app/mixins/checkout_mixin.rb | 1 + app/models/payment.rb | 3 +- app/views/campaigns/checkout_payment.html.erb | 8 +++-- config/routes.rb | 1 + ...140307205637_add_ct_user_id_to_payments.rb | 5 +++ db/schema.rb | 3 +- 8 files changed, 47 insertions(+), 14 deletions(-) create mode 100644 db/migrate/20140307205637_add_ct_user_id_to_payments.rb diff --git a/app/assets/javascripts/campaigns.js.coffee b/app/assets/javascripts/campaigns.js.coffee index 2e62c254..6234909e 100644 --- a/app/assets/javascripts/campaigns.js.coffee +++ b/app/assets/javascripts/campaigns.js.coffee @@ -79,18 +79,31 @@ Crowdhoster.campaigns = errors = crowdtilt.card.validate(cardData) if !$.isEmptyObject(errors) - $('#refresh-msg').hide() $.each errors, (index, value) -> $('#errors').append('

' + value + '

') - $('#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('

Sorry, we weren\'t able to process your contribution. Please try again.


If you continue to experience issues, please click here to email support.

') + else + $('#errors').append('

Sorry, we weren\'t able to process your contribution. Please try again.


If you continue to experience issues, please click here to email support.

') + ) + + 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") diff --git a/app/controllers/campaigns_controller.rb b/app/controllers/campaigns_controller.rb index f1ad9728..3e8fd641 100644 --- a/app/controllers/campaigns_controller.rb +++ b/app/controllers/campaigns_controller.rb @@ -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 diff --git a/app/mixins/checkout_mixin.rb b/app/mixins/checkout_mixin.rb index 68589bac..f2e242cd 100644 --- a/app/mixins/checkout_mixin.rb +++ b/app/mixins/checkout_mixin.rb @@ -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 diff --git a/app/models/payment.rb b/app/models/payment.rb index 449b1798..0470200e 100644 --- a/app/models/payment.rb +++ b/app/models/payment.rb @@ -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 diff --git a/app/views/campaigns/checkout_payment.html.erb b/app/views/campaigns/checkout_payment.html.erb index 95a1038b..23c8919d 100644 --- a/app/views/campaigns/checkout_payment.html.erb +++ b/app/views/campaigns/checkout_payment.html.erb @@ -5,7 +5,7 @@
-
+

Contact Information

@@ -120,7 +120,7 @@ - + @@ -133,7 +133,9 @@ <% end%>
- +
diff --git a/config/routes.rb b/config/routes.rb index 48144a31..e1b04900 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20140307205637_add_ct_user_id_to_payments.rb b/db/migrate/20140307205637_add_ct_user_id_to_payments.rb new file mode 100644 index 00000000..46f17989 --- /dev/null +++ b/db/migrate/20140307205637_add_ct_user_id_to_payments.rb @@ -0,0 +1,5 @@ +class AddCtUserIdToPayments < ActiveRecord::Migration + def change + add_column :payments, :ct_user_id, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index b71192cb..026f96c5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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|