Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new API v2 endpoint to support Braintree Token generation #272

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/controllers/spree/api/v2/braintree_client_token_controller.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion app/views/spree/shared/braintree_vzero/_dropin.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,3 @@ onError: function (error) {
SpreeBraintreeVzero.enableSubmitButton();
<%= render partial: 'spree/checkout/payment/braintree_vzero/dropin_on_error_callback', formats: [:js] %>
}

Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about putting that in api/v2/storefront/checkout/ namespace?
That would put it alongside other payment-related methods: https://api.spreecommerce.org/docs/api-v2/64334a793f6d3-create-new-payment

end
end
end