diff --git a/app/controllers/spree/api/v2/braintree_client_token_controller.rb b/app/controllers/spree/api/v2/braintree_client_token_controller.rb new file mode 100644 index 00000000..1fdfa8b1 --- /dev/null +++ b/app/controllers/spree/api/v2/braintree_client_token_controller.rb @@ -0,0 +1,24 @@ +module Spree + module Api + module V2 + class BraintreeClientTokenController < ::Spree::Api::V2::BaseController + include Spree::Api::V2::Storefront::OrderConcern + before_action :ensure_order + + def create + gateway = if params[:payment_method_id] + Spree::Gateway::BraintreeVzeroBase.find(params[:payment_method_id]) + else + Spree::Gateway::BraintreeVzeroBase.active.first + end + + render json: { + client_token: gateway.client_token(spree_current_order, @current_api_user), + payment_method_id: gateway.id + } + end + + end + end + end +end diff --git a/app/views/spree/shared/braintree_vzero/_dropin.js.erb b/app/views/spree/shared/braintree_vzero/_dropin.js.erb index af2ab798..bbedfbba 100644 --- a/app/views/spree/shared/braintree_vzero/_dropin.js.erb +++ b/app/views/spree/shared/braintree_vzero/_dropin.js.erb @@ -67,4 +67,3 @@ onError: function (error) { SpreeBraintreeVzero.enableSubmitButton(); <%= render partial: 'spree/checkout/payment/braintree_vzero/dropin_on_error_callback', formats: [:js] %> } - diff --git a/config/initializers/extend_spree_permitted_checkout_attributes.rb b/config/initializers/extend_spree_permitted_checkout_attributes.rb index 2a610cb3..75201d36 100644 --- a/config/initializers/extend_spree_permitted_checkout_attributes.rb +++ b/config/initializers/extend_spree_permitted_checkout_attributes.rb @@ -1,5 +1,12 @@ module Spree module PermittedAttributes @@payment_attributes = [:amount, :payment_method_id, :payment_method, :braintree_token, :braintree_nonce] + @@source_attributes = [ + :number, :month, :year, :expiry, :verification_value, + :first_name, :last_name, :cc_type, :gateway_customer_profile_id, + :gateway_payment_profile_id, :last_digits, :name, :encrypted_data, + # Add Braintree params to allow source to be created + :braintree_last_two, :braintree_card_type, :braintree_nonce + ] end end diff --git a/config/routes.rb b/config/routes.rb index 72ccda8d..41339b20 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,5 +3,8 @@ namespace :v1 do resource :braintree_client_token, only: :create, controller: 'braintree_client_token' end + namespace :v2 do + resource :braintree_client_token, only: :create, controller: 'braintree_client_token' + end end end