Skip to content

Commit

Permalink
Merge pull request #4961 from nebulab/gsmendoza/4749-braintree-paymen…
Browse files Browse the repository at this point in the history
…t-method

Add Braintree to the installer as a payment method option
  • Loading branch information
kennyadsl committed Mar 30, 2023
2 parents 1c5adde + 3b9d196 commit b6f883e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 20 deletions.
8 changes: 8 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ jobs:
cd /tmp/my_app
bundle list | grep 'solidus_paypal_commerce_platform (1.'
- install_solidus: { flags: "--sample=false --frontend=starter --payment-method=braintree" }
- test_page: { expected_text: "The only eCommerce platform you’ll ever need." }
- run:
name: Ensure the correct Braintree is installed for SSF
command: |
cd /tmp/my_app
bundle list | grep 'solidus_braintree (3.'
- install_solidus: { flags: "--sample=false --frontend=none --authentication=none" }
- test_page: { expected_text: "<title>Ruby on Rails" }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
unless Bundler.locked_gems.dependencies['solidus_braintree']
bundle_command 'add solidus_braintree --version "~> 3.0"'
end

generate 'solidus_braintree:install'
54 changes: 36 additions & 18 deletions core/lib/generators/solidus/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,31 @@ class InstallGenerator < Rails::Generators::AppBase
none
]

PAYMENT_METHODS = %w[
paypal
bolt
none
PAYMENT_METHODS = [
{
name: 'paypal',
frontends: %w[none classic starter],
description: 'Install `solidus_paypal_commerce_platform`',
default: true,
},
{
name: 'bolt',
frontends: %w[classic],
description: 'Install `solidus_bolt`',
default: false,
},
{
name: 'braintree',
frontends: %w[none starter],
description: 'Install `solidus_braintree`',
default: false,
},
{
name: 'none',
frontends: %w[none classic starter],
description: 'Skip installing a payment method',
default: false,
},
]

class_option :migrate, type: :boolean, default: true, banner: 'Run Solidus migrations'
Expand All @@ -48,7 +69,7 @@ class InstallGenerator < Rails::Generators::AppBase

class_option :frontend, type: :string, enum: FRONTENDS + LEGACY_FRONTENDS, default: nil, desc: "Indicates which frontend to install."
class_option :authentication, type: :string, enum: AUTHENTICATIONS, default: nil, desc: "Indicates which authentication system to install."
class_option :payment_method, type: :string, enum: PAYMENT_METHODS, default: nil, desc: "Indicates which payment method to install."
class_option :payment_method, type: :string, enum: PAYMENT_METHODS.map { |payment_method| payment_method[:name] }, default: nil, desc: "Indicates which payment method to install."

# DEPRECATED
class_option :with_authentication, type: :boolean, hide: true, default: nil
Expand Down Expand Up @@ -323,29 +344,26 @@ def detect_payment_method_to_install
return 'paypal' if Bundler.locked_gems.dependencies['solidus_paypal_commerce_platform']
return 'bolt' if Bundler.locked_gems.dependencies['solidus_bolt']

descriptions = {
paypal: "- [#{set_color 'paypal', :bold}] Install `solidus_paypal_commerce_platform` (#{set_color :default, :bold}).",
bolt: "- [#{set_color 'bolt', :bold}] Install `solidus_bolt`.",
none: "- [#{set_color 'none', :bold}] Skip installing a payment method.",
}

payment_methods = PAYMENT_METHODS

if @selected_frontend != 'classic'
payment_methods -= ['bolt']
descriptions.delete(:bolt)
selected_frontend_payment_methods = PAYMENT_METHODS.select do |payment_method|
payment_method[:frontends].include?(@selected_frontend)
end

selected = options[:payment_method] || (options[:auto_accept] && 'paypal') ||
ask_with_description(
default: 'paypal',
limited_to: payment_methods,
limited_to: selected_frontend_payment_methods.map { |payment_method| payment_method[:name] },
desc: <<~TEXT
Which payment method would you like to use?
#{descriptions.values.join("\n")}
#{selected_frontend_payment_methods.map { |payment_method| formatted_payment_method_description(payment_method) }.join("\n")}
TEXT
)
end

def formatted_payment_method_description(payment_method)
default_label = " (#{set_color :default, :bold})" if payment_method[:default]

"- [#{set_color payment_method[:name], :bold}] #{payment_method[:description]}#{default_label}."
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,23 @@
expect(questions.first[:default]).to eq('paypal')
expect(strip_ansi questions.first[:desc]).to include('[paypal]')
expect(strip_ansi questions.first[:desc]).to include('[bolt]')
expect(strip_ansi questions.first[:desc]).to_not include('[braintree]')
expect(strip_ansi questions.first[:desc]).to include('[none]')
end

it 'presents different options for the "classic"' do
it 'presents different options for the "starter"' do
questions = []
generator = described_class.new([], ['--frontend=starter', '--authentication=devise'])
allow(generator).to receive(:ask_with_description) { |**args| questions << args }

generator.prepare_options

expect(questions.size).to eq(1)
expect(questions.first[:limited_to]).to eq(['paypal', 'none'])
expect(questions.first[:limited_to]).to eq(['paypal', 'braintree', 'none'])
expect(questions.first[:default]).to eq('paypal')
expect(strip_ansi questions.first[:desc]).to include('[paypal]')
expect(strip_ansi questions.first[:desc]).not_to include('[bolt]')
expect(strip_ansi questions.first[:desc]).to include('[braintree]')
expect(strip_ansi questions.first[:desc]).to include('[none]')
end
end
Expand Down

0 comments on commit b6f883e

Please sign in to comment.